This commit is contained in:
Liang Ding 2014-09-13 13:05:50 +08:00
parent a94d5c90a2
commit c53029eabf
5 changed files with 66 additions and 18 deletions

View File

@ -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 指定了行号(第一行为 0ch 指定了列号(第一列为 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) {

View File

@ -105,6 +105,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
} else {
data["content"] = string(buf)
data["mode"] = getEditorMode(extension)
data["path"] = path
}
}

View File

@ -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: '<textarea id="editor' + id + '"></textarea>'
});
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) {

View File

@ -1,7 +1,13 @@
var menu = {
init: function() {
this.subMenu();
// 点击子菜单后消失
$(".frame li").click(function() {
$(this).closest(".frame").hide();
});
},
// 焦点不在菜单上时需点击展开子菜单,否则为鼠标移动展开
subMenu: function() {
$(".menu > ul > li > a, .menu > ul> li > span").click(function() {
var $it = $(this);

View File

@ -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);