From c53029eabf6e698ad23521b53e91f90c2a19c5fb Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 13 Sep 2014 13:05:50 +0800 Subject: [PATCH] Fix #21 --- editor/editors.go | 32 +++++++++++++++++++++----------- file/files.go | 1 + static/js/editor.js | 33 +++++++++++++++++++++++++++------ static/js/menu.js | 8 +++++++- static/js/tree.js | 10 ++++++++++ 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/editor/editors.go b/editor/editors.go index 28e2530..e6a2aa6 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -161,7 +161,6 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { 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) @@ -169,11 +168,11 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { return } - filePath := args["file"].(string) - curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] - filename := filePath[strings.LastIndex(filePath, string(os.PathSeparator))+1:] + 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(filePath) + fout, err := os.Create(path) if nil != err { glog.Error(err) @@ -196,6 +195,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { ch := int(args["cursorCh"].(float64)) offset := getCursorOffset(code, line, ch) + glog.Infof("offset [%d]", offset) // TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现 argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-def", "."} @@ -221,11 +221,11 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { part := found[:strings.LastIndex(found, ":")] cursorSep := strings.LastIndex(part, ":") - path := found[:cursorSep] + path = found[:cursorSep] cursorLine := found[cursorSep+1 : strings.LastIndex(found, ":")] cursorCh := found[strings.LastIndex(found, ":")+1:] - // glog.Infof("%s\n%s\n%s\n%s", found, path, cursorLine, cursorCh) + glog.Infof("Find Decl [path: %s, cursor(%s:%s)]", path, cursorLine, cursorCh) data["path"] = path data["cursorLine"] = cursorLine @@ -316,21 +316,31 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) { usages = append(usages, usage) } - // glog.Infof("%s\n%s\n%s\n%s", found, path, cursorLine, cursorCh) - data["usages"] = usages } +// 计算光标偏移位置. +// line 指定了行号(第一行为 0),ch 指定了列号(第一列为 0). func getCursorOffset(code string, line, ch int) (offset int) { lines := strings.Split(code, "\n") + // 计算前几行长度 for i := 0; i < line; i++ { offset += len(lines[i]) } - offset += line + ch + // 计算当前行、当前列长度 + curLine := lines[line] + var buffer bytes.Buffer + r := []rune(curLine) + for i := 0; i < ch; i++ { + buffer.WriteString(string(r[i])) + } - return + offset += line // 加换行符 + offset += len(buffer.String()) // 加当前行列偏移 + + return offset } func setCmdEnv(cmd *exec.Cmd, username string) { diff --git a/file/files.go b/file/files.go index 7e29262..5525446 100644 --- a/file/files.go +++ b/file/files.go @@ -105,6 +105,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) { } else { data["content"] = string(buf) data["mode"] = getEditorMode(extension) + data["path"] = path } } diff --git a/static/js/editor.js b/static/js/editor.js index c1fb342..ff63124 100644 --- a/static/js/editor.js +++ b/static/js/editor.js @@ -16,6 +16,8 @@ var editors = { break; } } + + wide.curEditor.focus(); }, removeAfter: function(id, nextId) { for (var i = 0, ii = editors.data.length; i < ii; i++) { @@ -130,7 +132,7 @@ var editors = { var cur = wide.curEditor.getCursor(); var request = { - file: $(".edit-header .current").data("index"), + path: $(".edit-header .current > span:eq(0)").attr("title"), code: wide.curEditor.getValue(), cursorLine: cur.line, cursorCh: cur.ch @@ -146,6 +148,9 @@ var editors = { return; } + var cursorLine = data.cursorLine; + var cursorCh = data.cursorCh; + var request = { path: data.path }; @@ -162,10 +167,12 @@ var editors = { return false; } - // FIXME: V, 这个可能不在文件树里,但是也需要打开一个编辑器 - // 打开一个新编辑器并定位到跳转的行列 - var line = data.cursorLine; - var ch = data.cursorCh; + var tId = tree.getTIdByPath(data.path); + wide.curNode = tree.fileTree.getNodeByTId(tId); + tree.fileTree.selectNode(wide.curNode); + + data.cursorLine = cursorLine; + data.cursorCh = cursorCh; editors.newEditor(data); } }); @@ -200,13 +207,24 @@ var editors = { }); }; }, + // 新建一个编辑器 Tab,如果已经存在 Tab 则切换到该 Tab. newEditor: function(data) { $(".ico-fullscreen").show(); var id = wide.curNode.tId; + + // 光标位置 + var cursor = CodeMirror.Pos(0, 0); + if (data.cursorLine && data.cursorCh) { + cursor = CodeMirror.Pos(data.cursorLine - 1, data.cursorCh - 1); + } + for (var i = 0, ii = editors.data.length; i < ii; i++) { if (editors.data[i].id === id) { editors.tabs.setCurrent(id); wide.curEditor = editors.data[i].editor; + wide.curEditor.setCursor(cursor); + wide.curEditor.focus(); + return false; } } @@ -218,11 +236,12 @@ var editors = { content: '' }); - rulers = []; + var rulers = []; rulers.push({color: "#ccc", column: 120, lineStyle: "dashed"}); var editor = CodeMirror.fromTextArea(document.getElementById("editor" + id), { lineNumbers: true, + autofocus: true, autoCloseBrackets: true, matchBrackets: true, highlightSelectionMatches: {showToken: /\w/}, @@ -262,6 +281,8 @@ var editors = { editor.setValue(data.content); editor.setOption("mode", data.mode); + editor.setCursor(cursor); + editor.setOption("gutters", ["CodeMirror-lint-markers", "CodeMirror-foldgutter"]); if ("text/x-go" === data.mode || "application/json" === data.mode) { diff --git a/static/js/menu.js b/static/js/menu.js index 22e3759..d043bd8 100644 --- a/static/js/menu.js +++ b/static/js/menu.js @@ -1,8 +1,14 @@ var menu = { init: function() { this.subMenu(); + + // 点击子菜单后消失 + $(".frame li").click(function() { + $(this).closest(".frame").hide(); + }); }, - subMenu: function () { + // 焦点不在菜单上时需点击展开子菜单,否则为鼠标移动展开 + subMenu: function() { $(".menu > ul > li > a, .menu > ul> li > span").click(function() { var $it = $(this); $it.next().show(); diff --git a/static/js/tree.js b/static/js/tree.js index 2d8a282..f1297c0 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -1,4 +1,14 @@ var tree = { + getTIdByPath: function (path) { + var nodes = tree.fileTree.transformToArray(tree.fileTree.getNodes()); + for (var i = 0, ii = nodes.length; i < ii; i++) { + if (nodes[i].path === path) { + return nodes[i].tId; + } + } + + return undefined; + }, fileTree: undefined, _isParents: function(tId, parentTId) { var node = tree.fileTree.getNodeByTId(tId);