This commit is contained in:
Liang Ding 2014-09-12 00:22:24 +08:00
parent d7079781dd
commit dc65f78c2e
5 changed files with 118 additions and 6 deletions

View File

@ -8,6 +8,7 @@
<body>
<h1>键盘快捷键</h1>
Done:
<ul>
<li>Ctrl+G: 跳转到行</li>
<li>Ctrl+\\: 自动补全</li>
@ -17,5 +18,17 @@
<li>Ctrl+]: 缩进</li>
<li>Ctrl+[: 反缩进</li>
</ul>
In Process:
<ul>
<li>Ctrl+B跳转到声明</li>
</ul>
TODO:
<ul>
<li>Ctrl+1焦点切换到文件树</li>
<li>Ctrl+0焦点切换到编辑器</li>
<li>Ctrl+4焦点切换到 Output</li>
</ul>
</body>
</html>

View File

@ -12,6 +12,7 @@ import (
"github.com/b3log/wide/conf"
"github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog"
"github.com/gorilla/websocket"
)
@ -97,7 +98,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset: %d", offset)
userWorkspace := conf.Wide.UserWorkspaces + string(os.PathSeparator) + username
userWorkspace := conf.Wide.GetUserWorkspace(username)
//glog.Infof("User [%s] workspace [%s]", username, userWorkspace)
userLib := userWorkspace + string(os.PathSeparator) + "pkg" + string(os.PathSeparator) +
@ -116,6 +117,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("gocode", argv...)
cmd.Start()
//gocode 试验性质特性:自动构建
//argv = []string{"set", "autobuild", "true"}
//cmd := exec.Command("gocode", argv...)
//cmd.Start()
@ -139,6 +141,73 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
w.Write(output)
}
func FindDeclarationHandler(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)
// liteide_stub type -cursor main.go:318 -def .
glog.Info(filename, offset)
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-def", "."}
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
}
// TODO: 解析返回
glog.Info(string(output))
}
func getCursorOffset(code string, line, ch int) (offset int) {
lines := strings.Split(code, "\n")
@ -150,3 +219,14 @@ func getCursorOffset(code string, line, ch int) (offset int) {
return
}
func setCmdEnv(cmd *exec.Cmd, username string) {
userWorkspace := conf.Wide.GetUserWorkspace(username)
cmd.Env = append(cmd.Env,
"GOPATH="+userWorkspace,
"GOOS="+runtime.GOOS,
"GOARCH="+runtime.GOARCH,
"GOROOT="+runtime.GOROOT(),
"PATH="+os.Getenv("PATH"))
}

View File

@ -93,6 +93,7 @@ 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("/html/fmt", editor.HTMLFmtHandler)
http.HandleFunc("/json/fmt", editor.JSONFmtHandler)

View File

@ -462,7 +462,4 @@ func setCmdEnv(cmd *exec.Cmd, username string) {
"GOARCH="+runtime.GOARCH,
"GOROOT="+runtime.GOROOT(),
"PATH="+os.Getenv("PATH"))
//"TERM="+os.Getenv("COMSPEC"),
//"ComSpec="+os.Getenv("ComSpec")
}

View File

@ -125,6 +125,26 @@ var editors = {
CodeMirror.commands.doNothing = function(cm) {
};
CodeMirror.commands.jumpToDecl = 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: '/finddecl',
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
}
});
};
},
newEditor: function(data) {
$(".ico-fullscreen").show();
@ -169,7 +189,8 @@ var editors = {
},
"Ctrl-G": "gotoLine",
"Ctrl-E": "deleteLine",
"Ctrl-D": "doNothing" // 取消默认的 deleteLine
"Ctrl-D": "doNothing", // 取消默认的 deleteLine
"Ctrl-B": "jumpToDecl"
}
});