From 8d0ae1d7d140c998195cbd19575e925215b11d90 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 30 Dec 2014 12:04:16 +0800 Subject: [PATCH] #199 --- file/outline.go | 70 +++++++++++++++++++++++++++++++++++++++++++- static/js/hotkeys.js | 48 +++++++++++++++++++++++------- 2 files changed, 107 insertions(+), 11 deletions(-) diff --git a/file/outline.go b/file/outline.go index 2f1cfea..7a3830e 100755 --- a/file/outline.go +++ b/file/outline.go @@ -24,6 +24,12 @@ import ( "github.com/b3log/wide/util" ) +type element struct { + Name string + Pos token.Pos + End token.Pos +} + // GetOutline gets outfile of a go file. func GetOutline(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} @@ -46,5 +52,67 @@ func GetOutline(w http.ResponseWriter, r *http.Request) { panic(err) } - ast.Print(fset, f) + //ast.Print(fset, f) + + data["package"] = &element{Name: f.Name.Name, Pos: f.Name.Pos(), End: f.Name.End()} + + imports := []*element{} + for _, astImport := range f.Imports { + + impt := &element{Name: astImport.Path.Value, Pos: astImport.Path.Pos(), End: astImport.Path.End()} + + imports = append(imports, impt) + } + data["imports"] = imports + + funcDecls := []*element{} + varDecls := []*element{} + constDecls := []*element{} + structDecls := []*element{} + interfaceDecls := []*element{} + for _, decl := range f.Decls { + switch decl.(type) { + case *ast.FuncDecl: + funcDecl := decl.(*ast.FuncDecl) + + decl := &element{Name: funcDecl.Name.Name, Pos: funcDecl.Name.Pos(), End: funcDecl.Name.End()} + + funcDecls = append(funcDecls, decl) + case *ast.GenDecl: + genDecl := decl.(*ast.GenDecl) + + for _, spec := range genDecl.Specs { + + switch genDecl.Tok { + case token.VAR: + variableSpec := spec.(*ast.ValueSpec) + decl := &element{Name: variableSpec.Names[0].Name, Pos: variableSpec.Pos(), End: variableSpec.End()} + + varDecls = append(varDecls, decl) + case token.TYPE: + typeSpec := spec.(*ast.TypeSpec) + decl := &element{Name: typeSpec.Name.Name, Pos: typeSpec.Name.Pos(), End: typeSpec.Name.End()} + + switch typeSpec.Type.(type) { + case *ast.StructType: + structDecls = append(structDecls, decl) + case *ast.InterfaceType: + interfaceDecls = append(interfaceDecls, decl) + } + case token.CONST: + constSpec := spec.(*ast.ValueSpec) + decl := &element{Name: constSpec.Names[0].Name, Pos: constSpec.Pos(), End: constSpec.End()} + + constDecls = append(constDecls, decl) + } + } + + } + } + + data["funcDecls"] = funcDecls + data["varDecls"] = varDecls + data["constDecls"] = constDecls + data["structDecls"] = structDecls + data["interfaceDecls"] = interfaceDecls } diff --git a/static/js/hotkeys.js b/static/js/hotkeys.js index fb3171b..2f7c333 100644 --- a/static/js/hotkeys.js +++ b/static/js/hotkeys.js @@ -16,7 +16,7 @@ var hotkeys = { defaultKeyMap: { - // Ctrl-0 焦点切换到当前编辑器 + // Ctrl-0 goEditor: { ctrlKey: true, altKey: false, @@ -28,7 +28,7 @@ var hotkeys = { } } }, - // Ctrl-1 焦点切换到文件树 + // Ctrl-1 goFileTree: { ctrlKey: true, altKey: false, @@ -51,7 +51,27 @@ var hotkeys = { $("#files").focus(); } }, - // Ctrl-4 焦点切换到输出窗口 + goOutline: { + ctrlKey: true, + altKey: false, + shiftKey: false, + which: 50, + fun: function () { + var request = newWideRequest(); + request.code = wide.curEditor.getValue(); + + $.ajax({ + type: 'POST', + url: config.context + '/outline', + data: JSON.stringify(request), + dataType: "json", + success: function (data) { + console.log(data); + } + }); + } + }, + // Ctrl-4 goOutput: { ctrlKey: true, altKey: false, @@ -63,7 +83,7 @@ var hotkeys = { $(".bottom-window-group .output").focus(); } }, - // Ctrl-5 焦点切换到搜索窗口 + // Ctrl-5 goSearch: { ctrlKey: true, altKey: false, @@ -75,7 +95,7 @@ var hotkeys = { $(".bottom-window-group .search").focus(); } }, - // Ctrl-6 焦点切换到通知窗口 + // Ctrl-6 goNotification: { ctrlKey: true, altKey: false, @@ -87,7 +107,7 @@ var hotkeys = { $(".bottom-window-group .notification").focus(); } }, - // Ctrl-C 清空窗口内容 + // Ctrl-C clearWindow: { ctrlKey: true, altKey: false, @@ -101,21 +121,21 @@ var hotkeys = { shiftKey: false, which: 68 }, - // Ctrl-F 搜索 + // Ctrl-F search search: { ctrlKey: true, altKey: false, shiftKey: false, which: 70 }, - // Ctrl-Q 关闭当前编辑器 + // Ctrl-Q close current editor closeCurEditor: { ctrlKey: true, altKey: false, shiftKey: false, which: 81 }, - // Ctrl-R 重命名 + // Ctrl-R rename: { ctrlKey: true, altKey: false, @@ -245,7 +265,7 @@ var hotkeys = { tree.fileTree.expandNode(wide.curNode, true, false, true); $("#files").focus(); - + break; } @@ -365,6 +385,14 @@ var hotkeys = { return; } + + if (event.ctrlKey === hotKeys.goOutline.ctrlKey + && event.which === hotKeys.goOutline.which) { // Ctrl-2 焦点切换到大纲 + hotKeys.goOutline.fun(); + event.preventDefault(); + + return; + } if (event.ctrlKey === hotKeys.goOutput.ctrlKey && event.which === hotKeys.goOutput.which) { // Ctrl-4 焦点切换到输出窗口