From 0bcd418dda36657d8d48cd07e312a165c71b1aa2 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 10 Oct 2014 11:18:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- editor/editors.go | 77 ++++++++++++++++++++++++++++++++++++++++++-- main.go | 1 + static/js/editors.js | 60 ++++++++++++++++++++++++++++------ 3 files changed, 126 insertions(+), 12 deletions(-) diff --git a/editor/editors.go b/editor/editors.go index 9c67aec..3d5acdf 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -163,6 +163,79 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) { w.Write(output) } +// 查看表达式信息. +func GetExprInfoHandler(w http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{"succ": true} + defer util.RetJSON(w, r, data) + + session, _ := session.HTTPSession.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 + } + + path := args["path"].(string) + curDir := path[:strings.LastIndex(path, string(os.PathSeparator))] + filename := path[strings.LastIndex(path, string(os.PathSeparator))+1:] + + fout, err := os.Create(path) + + 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) + glog.Infof("offset [%d]", offset) + + // TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现 + ide_stub := conf.Wide.GetIDEStub() + argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-info", "."} + cmd := exec.Command(ide_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 + } + + exprInfo := strings.TrimSpace(string(output)) + if "" == exprInfo { + data["succ"] = false + + return + } + + data["info"] = exprInfo +} + // 查找声明. func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} @@ -208,7 +281,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { ch := int(args["cursorCh"].(float64)) offset := getCursorOffset(code, line, ch) - // glog.Infof("offset [%d]", offset) + glog.Infof("offset [%d]", offset) // TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现 ide_stub := conf.Wide.GetIDEStub() @@ -263,7 +336,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) { return } - filePath := args["file"].(string) + filePath := args["path"].(string) curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] filename := filePath[strings.LastIndex(filePath, string(os.PathSeparator))+1:] diff --git a/main.go b/main.go index 6e9c0b5..f93a325 100644 --- a/main.go +++ b/main.go @@ -191,6 +191,7 @@ func main() { http.HandleFunc("/editor/ws", handlerWrapper(editor.WSHandler)) http.HandleFunc("/go/fmt", handlerWrapper(editor.GoFmtHandler)) http.HandleFunc("/autocomplete", handlerWrapper(editor.AutocompleteHandler)) + http.HandleFunc("/exprinfo", handlerWrapper(editor.GetExprInfoHandler)) http.HandleFunc("/find/decl", handlerWrapper(editor.FindDeclarationHandler)) http.HandleFunc("/find/usages", handlerWrapper(editor.FindUsagesHandler)) http.HandleFunc("/html/fmt", handlerWrapper(editor.HTMLFmtHandler)) diff --git a/static/js/editors.js b/static/js/editors.js index 3599b46..9b172f1 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -106,15 +106,29 @@ var editors = { if (autocompleteArray) { for (var i = 0; i < autocompleteArray.length; i++) { var displayText = ''; - if (autocompleteArray[i].class === 'type') { - displayText = ''// + autocompleteArray[i].class - + '' + autocompleteArray[i].name + '' - + autocompleteArray[i].type + ''; - } else { - displayText = ''// + autocompleteArray[i].class - + '' + autocompleteArray[i].name + ' ' - + autocompleteArray[i].type.substring(4) + ''; + + switch (autocompleteArray[i].class) { + case "type": + case "const": + case "var": + case "package": + displayText = ''// + autocompleteArray[i].class + + '' + autocompleteArray[i].name + ' ' + + autocompleteArray[i].type + ''; + + break; + case "func": + displayText = ''// + autocompleteArray[i].class + + '' + autocompleteArray[i].name + '' + + autocompleteArray[i].type.substring(4) + ''; + + break; + default: + console.warn("Can't handle autocomplete [" + autocompleteArray[i].class + "]"); + + break; } + autocompleteHints[i] = { // TODO: 添加类型、图标 @@ -149,6 +163,31 @@ var editors = { CodeMirror.commands.doNothing = function (cm) { }; + + CodeMirror.commands.exprInfo = function (cm) { + var cur = wide.curEditor.getCursor(); + + var request = newWideRequest(); + request.path = $(".edit-panel .tabs .current > span:eq(0)").attr("title"); + request.code = wide.curEditor.getValue(); + request.cursorLine = cur.line; + request.cursorCh = cur.ch; + + $.ajax({ + type: 'POST', + url: '/exprinfo', + data: JSON.stringify(request), + dataType: "json", + success: function (data) { + // TODO: V + console.log(data); + + if (!data.succ) { + return; + } + } + }); + }; CodeMirror.commands.jumpToDecl = function (cm) { var cur = wide.curEditor.getCursor(); @@ -204,7 +243,7 @@ var editors = { var cur = wide.curEditor.getCursor(); var request = newWideRequest(); - request.file = wide.curNode.path; + request.path = $(".edit-panel .tabs .current > span:eq(0)").attr("title"); request.code = wide.curEditor.getValue(); request.cursorLine = cur.line; request.cursorCh = cur.ch; @@ -216,7 +255,7 @@ var editors = { dataType: "json", success: function (data) { console.log(data); - + // TODO: V if (!data.succ) { return; } @@ -272,6 +311,7 @@ var editors = { extraKeys: { "Ctrl-\\": "autocompleteAnyWord", ".": "autocompleteAfterDot", + "Ctrl-I": "exprInfo", "Ctrl-G": "gotoLine", "Ctrl-E": "deleteLine", "Ctrl-D": "doNothing", // 取消默认的 deleteLine