From f5b0b105b758262fd8a5c02aedc0f9ded80dc625 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 12 Sep 2014 15:55:52 +0800 Subject: [PATCH] #42 --- doc/zh_CN/keyboard_shortcuts.html | 1 + editor/editors.go | 92 +++++++++++++++++++++++++++++++ file/files.go | 2 +- main.go | 3 +- static/js/editor.js | 34 +++++++++++- 5 files changed, 127 insertions(+), 5 deletions(-) diff --git a/doc/zh_CN/keyboard_shortcuts.html b/doc/zh_CN/keyboard_shortcuts.html index 1388353..134fc42 100644 --- a/doc/zh_CN/keyboard_shortcuts.html +++ b/doc/zh_CN/keyboard_shortcuts.html @@ -22,6 +22,7 @@ In Process: TODO: diff --git a/editor/editors.go b/editor/editors.go index 5213103..918b565 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -222,6 +222,98 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { data["cursorCh"] = cursorCh } +func FindUsagesHandler(w http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{"succ": true} + defer util.RetJSON(w, r, data) + + session, _ := user.Session.Get(r, "wide-session") + username := session.Values["username"].(string) + + decoder := json.NewDecoder(r.Body) + + var args map[string]interface{} + + if err := decoder.Decode(&args); err != nil { + glog.Error(err) + http.Error(w, err.Error(), 500) + + return + } + + filePath := args["file"].(string) + curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] + filename := filePath[strings.LastIndex(filePath, string(os.PathSeparator))+1:] + + fout, err := os.Create(filePath) + + if nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + code := args["code"].(string) + fout.WriteString(code) + + if err := fout.Close(); nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + line := int(args["cursorLine"].(float64)) + ch := int(args["cursorCh"].(float64)) + + offset := getCursorOffset(code, line, ch) + + // TODO: 目前是调用 liteide_stub 工具来查找使用,后续需要重新实现 + argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-use", "."} + cmd := exec.Command("liteide_stub", argv...) + cmd.Dir = curDir + + setCmdEnv(cmd, username) + + output, err := cmd.CombinedOutput() + if nil != err { + glog.Error(err) + http.Error(w, err.Error(), 500) + + return + } + + result := strings.TrimSpace(string(output)) + if "" == result { + data["succ"] = false + + return + } + + founds := strings.Split(result, "\n") + usages := []interface{}{} + for _, found := range founds { + found = strings.TrimSpace(found) + + part := found[:strings.LastIndex(found, ":")] + cursorSep := strings.LastIndex(part, ":") + path := found[:cursorSep] + cursorLine := found[cursorSep+1 : strings.LastIndex(found, ":")] + cursorCh := found[strings.LastIndex(found, ":")+1:] + + usage := map[string]string{} + usage["path"] = path + usage["cursorLine"] = cursorLine + usage["cursorCh"] = cursorCh + + usages = append(usages, usage) + } + + // glog.Infof("%s\n%s\n%s\n%s", found, path, cursorLine, cursorCh) + + data["usages"] = usages +} + func getCursorOffset(code string, line, ch int) (offset int) { lines := strings.Split(code, "\n") diff --git a/file/files.go b/file/files.go index 18fbeee..f9833cd 100644 --- a/file/files.go +++ b/file/files.go @@ -193,7 +193,7 @@ type FileNode struct { Name string `json:"name"` Path string `json:"path"` IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格 - Type string `json:"type"` + Type string `json:"type"` // "f":文件,"d":文件夹 Mode string `json:"mode"` FileNodes []*FileNode `json:"children"` } diff --git a/main.go b/main.go index 4d6e8bb..7c1cb30 100644 --- a/main.go +++ b/main.go @@ -93,7 +93,8 @@ func main() { http.HandleFunc("/editor/ws", editor.WSHandler) http.HandleFunc("/go/fmt", editor.GoFmtHandler) http.HandleFunc("/autocomplete", editor.AutocompleteHandler) - http.HandleFunc("/finddecl", editor.FindDeclarationHandler) + http.HandleFunc("/find/decl", editor.FindDeclarationHandler) + http.HandleFunc("/find/usages", editor.FindUsagesHandler) http.HandleFunc("/html/fmt", editor.HTMLFmtHandler) http.HandleFunc("/json/fmt", editor.JSONFmtHandler) diff --git a/static/js/editor.js b/static/js/editor.js index 0b99a96..3df60a7 100644 --- a/static/js/editor.js +++ b/static/js/editor.js @@ -138,7 +138,7 @@ var editors = { $.ajax({ type: 'POST', - url: '/finddecl', + url: '/find/decl', data: JSON.stringify(request), dataType: "json", success: function(data) { @@ -172,6 +172,33 @@ var editors = { } }); }; + + CodeMirror.commands.findUsages = function(cm) { + var cur = wide.curEditor.getCursor(); + + var request = { + file: wide.curNode.path, + code: wide.curEditor.getValue(), + cursorLine: cur.line, + cursorCh: cur.ch + }; + + $.ajax({ + type: 'POST', + url: '/find/usages', + data: JSON.stringify(request), + dataType: "json", + success: function(data) { + console.log(data); + + if (!data.succ) { + return; + } + + + } + }); + }; }, newEditor: function(data) { $(".ico-fullscreen").show(); @@ -186,7 +213,7 @@ var editors = { editors.tabs.add({ id: id, - title: '' + wide.curNode.name + '', content: '' }); @@ -218,7 +245,8 @@ var editors = { "Ctrl-G": "gotoLine", "Ctrl-E": "deleteLine", "Ctrl-D": "doNothing", // 取消默认的 deleteLine - "Ctrl-B": "jumpToDecl" + "Ctrl-B": "jumpToDecl", + "Alt-F7": "findUsages" } });