🎨 go1.13+ 自动完成失效问题 Fix #6
This commit is contained in:
parent
e51b78ea85
commit
3256f60990
|
@ -25,85 +25,19 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
"github.com/88250/wide/conf"
|
"github.com/88250/wide/conf"
|
||||||
"github.com/88250/wide/file"
|
"github.com/88250/wide/file"
|
||||||
"github.com/88250/wide/session"
|
"github.com/88250/wide/session"
|
||||||
"github.com/88250/wide/util"
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger.
|
// Logger.
|
||||||
var logger = gulu.Log.NewLogger(os.Stdout)
|
var logger = gulu.Log.NewLogger(os.Stdout)
|
||||||
|
|
||||||
// WSHandler handles request of creating editor channel.
|
|
||||||
// XXX: NOT used at present
|
|
||||||
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
|
|
||||||
if httpSession.IsNew {
|
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sid := httpSession.Values["id"].(string)
|
|
||||||
|
|
||||||
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
|
|
||||||
editorChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
|
|
||||||
|
|
||||||
ret := map[string]interface{}{"output": "Editor initialized", "cmd": "init-editor"}
|
|
||||||
err := editorChan.WriteJSON(&ret)
|
|
||||||
if nil != err {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
session.EditorWS[sid] = &editorChan
|
|
||||||
|
|
||||||
logger.Tracef("Open a new [Editor] with session [%s], %d", sid, len(session.EditorWS))
|
|
||||||
|
|
||||||
args := map[string]interface{}{}
|
|
||||||
for {
|
|
||||||
if err := session.EditorWS[sid].ReadJSON(&args); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
code := args["code"].(string)
|
|
||||||
line := int(args["cursorLine"].(float64))
|
|
||||||
ch := int(args["cursorCh"].(float64))
|
|
||||||
|
|
||||||
offset := getCursorOffset(code, line, ch)
|
|
||||||
|
|
||||||
logger.Tracef("offset: %d", offset)
|
|
||||||
|
|
||||||
gocode := gulu.Go.GetExecutableInGOBIN("gocode")
|
|
||||||
argv := []string{"-f=json", "autocomplete", strconv.Itoa(offset)}
|
|
||||||
|
|
||||||
var output bytes.Buffer
|
|
||||||
|
|
||||||
cmd := exec.Command(gocode, argv...)
|
|
||||||
cmd.Stdout = &output
|
|
||||||
|
|
||||||
stdin, _ := cmd.StdinPipe()
|
|
||||||
cmd.Start()
|
|
||||||
stdin.Write([]byte(code))
|
|
||||||
stdin.Close()
|
|
||||||
cmd.Wait()
|
|
||||||
|
|
||||||
ret = map[string]interface{}{"output": string(output.Bytes()), "cmd": "autocomplete"}
|
|
||||||
|
|
||||||
if err := session.EditorWS[sid].WriteJSON(&ret); err != nil {
|
|
||||||
logger.Error("Editor WS ERROR: " + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AutocompleteHandler handles request of code autocompletion.
|
// AutocompleteHandler handles request of code autocompletion.
|
||||||
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var args map[string]interface{}
|
var args map[string]interface{}
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -120,9 +54,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
uid := session.Values["uid"].(string)
|
uid := session.Values["uid"].(string)
|
||||||
|
|
||||||
path := args["path"].(string)
|
path := args["path"].(string)
|
||||||
|
|
||||||
fout, err := os.Create(path)
|
fout, err := os.Create(path)
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -132,7 +64,6 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
code := args["code"].(string)
|
code := args["code"].(string)
|
||||||
fout.WriteString(code)
|
fout.WriteString(code)
|
||||||
|
|
||||||
if err := fout.Close(); nil != err {
|
if err := fout.Close(); nil != err {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -142,9 +73,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
line := int(args["cursorLine"].(float64))
|
line := int(args["cursorLine"].(float64))
|
||||||
ch := int(args["cursorCh"].(float64))
|
ch := int(args["cursorCh"].(float64))
|
||||||
|
|
||||||
offset := getCursorOffset(code, line, ch)
|
offset := getCursorOffset(code, line, ch)
|
||||||
|
|
||||||
logger.Tracef("offset: %d", offset)
|
logger.Tracef("offset: %d", offset)
|
||||||
|
|
||||||
userWorkspace := conf.GetUserWorkspace(uid)
|
userWorkspace := conf.GetUserWorkspace(uid)
|
||||||
|
@ -158,8 +87,8 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
logger.Tracef("gocode set lib-path [%s]", libPath)
|
logger.Tracef("gocode set lib-path [%s]", libPath)
|
||||||
|
|
||||||
// FIXME: using gocode set lib-path has some issues while accrossing workspaces
|
|
||||||
gocode := gulu.Go.GetExecutableInGOBIN("gocode")
|
gocode := gulu.Go.GetExecutableInGOBIN("gocode")
|
||||||
|
// FIXME: using gocode set lib-path has some issues while accrossing workspaces
|
||||||
exec.Command(gocode, []string{"set", "lib-path", libPath}...).Run()
|
exec.Command(gocode, []string{"set", "lib-path", libPath}...).Run()
|
||||||
|
|
||||||
argv := []string{"-f=json", "--in=" + path, "autocomplete", strconv.Itoa(offset)}
|
argv := []string{"-f=json", "--in=" + path, "autocomplete", strconv.Itoa(offset)}
|
||||||
|
|
1
main.go
1
main.go
|
@ -131,7 +131,6 @@ func main() {
|
||||||
http.HandleFunc("/file/zip", handlerWrapper(file.GetZipHandler))
|
http.HandleFunc("/file/zip", handlerWrapper(file.GetZipHandler))
|
||||||
|
|
||||||
// editor
|
// editor
|
||||||
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("/exprinfo", handlerWrapper(editor.GetExprInfoHandler))
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -29,7 +30,6 @@ import (
|
||||||
// AutocompleteHandler handles request of code autocompletion.
|
// AutocompleteHandler handles request of code autocompletion.
|
||||||
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var args map[string]interface{}
|
var args map[string]interface{}
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -48,16 +48,24 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
line := int(args["cursorLine"].(float64))
|
line := int(args["cursorLine"].(float64))
|
||||||
ch := int(args["cursorCh"].(float64))
|
ch := int(args["cursorCh"].(float64))
|
||||||
|
|
||||||
offset := getCursorOffset(code, line, ch)
|
file, err := os.Create("wide_autocomplete_" + gulu.Rand.String(16) + ".go")
|
||||||
|
if nil != err {
|
||||||
|
logger.Error(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
|
||||||
argv := []string{"-f=json", "autocomplete", strconv.Itoa(offset)}
|
return
|
||||||
|
}
|
||||||
|
file.WriteString(code)
|
||||||
|
file.Close()
|
||||||
|
|
||||||
|
path := file.Name()
|
||||||
|
defer os.Remove(path)
|
||||||
|
|
||||||
|
offset := getCursorOffset(code, line, ch)
|
||||||
|
argv := []string{"-f=json", "--in=" + path, "autocomplete", strconv.Itoa(offset)}
|
||||||
gocode := gulu.Go.GetExecutableInGOBIN("gocode")
|
gocode := gulu.Go.GetExecutableInGOBIN("gocode")
|
||||||
cmd := exec.Command(gocode, argv...)
|
cmd := exec.Command(gocode, argv...)
|
||||||
|
|
||||||
stdin, _ := cmd.StdinPipe()
|
|
||||||
stdin.Write([]byte(code))
|
|
||||||
stdin.Close()
|
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
|
|
Loading…
Reference in New Issue