This commit is contained in:
Liang Ding 2014-09-12 15:55:52 +08:00
parent 07ece43f51
commit f5b0b105b7
5 changed files with 127 additions and 5 deletions

View File

@ -22,6 +22,7 @@
In Process:
<ul>
<li>Ctrl+B跳转到声明</li>
<li>Alt+F7查找使用</li>
</ul>
TODO:

View File

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

View File

@ -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"`
}

View File

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

View File

@ -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: '<span title="' + wide.curNode.path + '"><span class="'
title: '<span title="' + wide.curNode.path + '"><span class="'
+ wide.curNode.iconSkin + 'ico"></span>' + wide.curNode.name + '</span>',
content: '<textarea id="editor' + id + '"></textarea>'
});
@ -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"
}
});