查看表达式信息

This commit is contained in:
Liang Ding 2014-10-10 11:18:52 +08:00
parent 0b5216a3bf
commit 0bcd418dda
3 changed files with 126 additions and 12 deletions

View File

@ -163,6 +163,79 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
w.Write(output) 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) { func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true} data := map[string]interface{}{"succ": true}
@ -208,7 +281,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) {
ch := int(args["cursorCh"].(float64)) ch := int(args["cursorCh"].(float64))
offset := getCursorOffset(code, line, ch) offset := getCursorOffset(code, line, ch)
// glog.Infof("offset [%d]", offset) glog.Infof("offset [%d]", offset)
// TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现 // TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现
ide_stub := conf.Wide.GetIDEStub() ide_stub := conf.Wide.GetIDEStub()
@ -263,7 +336,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
filePath := args["file"].(string) filePath := args["path"].(string)
curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))]
filename := filePath[strings.LastIndex(filePath, string(os.PathSeparator))+1:] filename := filePath[strings.LastIndex(filePath, string(os.PathSeparator))+1:]

View File

@ -191,6 +191,7 @@ func main() {
http.HandleFunc("/editor/ws", handlerWrapper(editor.WSHandler)) http.HandleFunc("/editor/ws", handlerWrapper(editor.WSHandler))
http.HandleFunc("/go/fmt", handlerWrapper(editor.GoFmtHandler)) http.HandleFunc("/go/fmt", handlerWrapper(editor.GoFmtHandler))
http.HandleFunc("/autocomplete", handlerWrapper(editor.AutocompleteHandler)) http.HandleFunc("/autocomplete", handlerWrapper(editor.AutocompleteHandler))
http.HandleFunc("/exprinfo", handlerWrapper(editor.GetExprInfoHandler))
http.HandleFunc("/find/decl", handlerWrapper(editor.FindDeclarationHandler)) http.HandleFunc("/find/decl", handlerWrapper(editor.FindDeclarationHandler))
http.HandleFunc("/find/usages", handlerWrapper(editor.FindUsagesHandler)) http.HandleFunc("/find/usages", handlerWrapper(editor.FindUsagesHandler))
http.HandleFunc("/html/fmt", handlerWrapper(editor.HTMLFmtHandler)) http.HandleFunc("/html/fmt", handlerWrapper(editor.HTMLFmtHandler))

View File

@ -106,15 +106,29 @@ var editors = {
if (autocompleteArray) { if (autocompleteArray) {
for (var i = 0; i < autocompleteArray.length; i++) { for (var i = 0; i < autocompleteArray.length; i++) {
var displayText = ''; var displayText = '';
if (autocompleteArray[i].class === 'type') {
switch (autocompleteArray[i].class) {
case "type":
case "const":
case "var":
case "package":
displayText = '<span class="fn-clear">'// + autocompleteArray[i].class displayText = '<span class="fn-clear">'// + autocompleteArray[i].class
+ '<b class="fn-left">' + autocompleteArray[i].name + '</b> ' + '<b class="fn-left">' + autocompleteArray[i].name + '</b> '
+ autocompleteArray[i].type + '</span>'; + autocompleteArray[i].type + '</span>';
} else {
break;
case "func":
displayText = '<span>'// + autocompleteArray[i].class displayText = '<span>'// + autocompleteArray[i].class
+ '<b>' + autocompleteArray[i].name + '</b>' + '<b>' + autocompleteArray[i].name + '</b>'
+ autocompleteArray[i].type.substring(4) + '</span>'; + autocompleteArray[i].type.substring(4) + '</span>';
break;
default:
console.warn("Can't handle autocomplete [" + autocompleteArray[i].class + "]");
break;
} }
autocompleteHints[i] = { autocompleteHints[i] = {
// TODO: 添加类型、图标 // TODO: 添加类型、图标
@ -150,6 +164,31 @@ var editors = {
CodeMirror.commands.doNothing = function (cm) { 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) { CodeMirror.commands.jumpToDecl = function (cm) {
var cur = wide.curEditor.getCursor(); var cur = wide.curEditor.getCursor();
@ -204,7 +243,7 @@ var editors = {
var cur = wide.curEditor.getCursor(); var cur = wide.curEditor.getCursor();
var request = newWideRequest(); 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.code = wide.curEditor.getValue();
request.cursorLine = cur.line; request.cursorLine = cur.line;
request.cursorCh = cur.ch; request.cursorCh = cur.ch;
@ -216,7 +255,7 @@ var editors = {
dataType: "json", dataType: "json",
success: function (data) { success: function (data) {
console.log(data); console.log(data);
// TODO: V
if (!data.succ) { if (!data.succ) {
return; return;
} }
@ -272,6 +311,7 @@ var editors = {
extraKeys: { extraKeys: {
"Ctrl-\\": "autocompleteAnyWord", "Ctrl-\\": "autocompleteAnyWord",
".": "autocompleteAfterDot", ".": "autocompleteAfterDot",
"Ctrl-I": "exprInfo",
"Ctrl-G": "gotoLine", "Ctrl-G": "gotoLine",
"Ctrl-E": "deleteLine", "Ctrl-E": "deleteLine",
"Ctrl-D": "doNothing", // 取消默认的 deleteLine "Ctrl-D": "doNothing", // 取消默认的 deleteLine