From dc65f78c2e3fcd26ef7727dbf1484552d47ba718 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 12 Sep 2014 00:22:24 +0800 Subject: [PATCH] #21 --- doc/zh_CN/keyboard_shortcuts.html | 15 +++++- editor/editors.go | 82 ++++++++++++++++++++++++++++++- main.go | 1 + output/outputs.go | 3 -- static/js/editor.js | 23 ++++++++- 5 files changed, 118 insertions(+), 6 deletions(-) diff --git a/doc/zh_CN/keyboard_shortcuts.html b/doc/zh_CN/keyboard_shortcuts.html index c942992..17dab56 100644 --- a/doc/zh_CN/keyboard_shortcuts.html +++ b/doc/zh_CN/keyboard_shortcuts.html @@ -8,6 +8,7 @@

键盘快捷键

+ Done: + + + In Process: + + + TODO: + diff --git a/editor/editors.go b/editor/editors.go index ab02a0f..61f9725 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -12,6 +12,7 @@ import ( "github.com/b3log/wide/conf" "github.com/b3log/wide/user" + "github.com/b3log/wide/util" "github.com/golang/glog" "github.com/gorilla/websocket" ) @@ -97,7 +98,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) { // glog.Infof("offset: %d", offset) - userWorkspace := conf.Wide.UserWorkspaces + string(os.PathSeparator) + username + userWorkspace := conf.Wide.GetUserWorkspace(username) //glog.Infof("User [%s] workspace [%s]", username, userWorkspace) userLib := userWorkspace + string(os.PathSeparator) + "pkg" + string(os.PathSeparator) + @@ -116,6 +117,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) { cmd := exec.Command("gocode", argv...) cmd.Start() + //gocode 试验性质特性:自动构建 //argv = []string{"set", "autobuild", "true"} //cmd := exec.Command("gocode", argv...) //cmd.Start() @@ -139,6 +141,73 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) { w.Write(output) } +func FindDeclarationHandler(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) + + // liteide_stub type -cursor main.go:318 -def . + glog.Info(filename, offset) + + argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-def", "."} + 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 + } + + // TODO: 解析返回 + glog.Info(string(output)) +} + func getCursorOffset(code string, line, ch int) (offset int) { lines := strings.Split(code, "\n") @@ -150,3 +219,14 @@ func getCursorOffset(code string, line, ch int) (offset int) { return } + +func setCmdEnv(cmd *exec.Cmd, username string) { + userWorkspace := conf.Wide.GetUserWorkspace(username) + + cmd.Env = append(cmd.Env, + "GOPATH="+userWorkspace, + "GOOS="+runtime.GOOS, + "GOARCH="+runtime.GOARCH, + "GOROOT="+runtime.GOROOT(), + "PATH="+os.Getenv("PATH")) +} diff --git a/main.go b/main.go index 64cd70e..4d6e8bb 100644 --- a/main.go +++ b/main.go @@ -93,6 +93,7 @@ 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("/html/fmt", editor.HTMLFmtHandler) http.HandleFunc("/json/fmt", editor.JSONFmtHandler) diff --git a/output/outputs.go b/output/outputs.go index 61af03d..f89571a 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -462,7 +462,4 @@ func setCmdEnv(cmd *exec.Cmd, username string) { "GOARCH="+runtime.GOARCH, "GOROOT="+runtime.GOROOT(), "PATH="+os.Getenv("PATH")) - - //"TERM="+os.Getenv("COMSPEC"), - //"ComSpec="+os.Getenv("ComSpec") } diff --git a/static/js/editor.js b/static/js/editor.js index 9b5ab29..6dc20e9 100644 --- a/static/js/editor.js +++ b/static/js/editor.js @@ -125,6 +125,26 @@ var editors = { CodeMirror.commands.doNothing = function(cm) { }; + + CodeMirror.commands.jumpToDecl = 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: '/finddecl', + data: JSON.stringify(request), + dataType: "json", + success: function(data) { + } + }); + }; }, newEditor: function(data) { $(".ico-fullscreen").show(); @@ -169,7 +189,8 @@ var editors = { }, "Ctrl-G": "gotoLine", "Ctrl-E": "deleteLine", - "Ctrl-D": "doNothing" // 取消默认的 deleteLine + "Ctrl-D": "doNothing", // 取消默认的 deleteLine + "Ctrl-B": "jumpToDecl" } });