2014-08-18 17:45:43 +04:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-08-31 13:39:39 +04:00
|
|
|
"github.com/b3log/wide/user"
|
2014-08-18 17:51:03 +04:00
|
|
|
"github.com/b3log/wide/util"
|
2014-08-18 17:45:43 +04:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var editorWS = map[string]*websocket.Conn{}
|
|
|
|
|
|
|
|
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
2014-08-31 13:39:39 +04:00
|
|
|
session, _ := user.Session.Get(r, "wide-session")
|
2014-08-18 17:45:43 +04:00
|
|
|
sid := session.Values["id"].(string)
|
|
|
|
|
|
|
|
editorWS[sid], _ = websocket.Upgrade(w, r, nil, 1024, 1024)
|
|
|
|
|
|
|
|
ret := map[string]interface{}{"output": "Editor initialized", "cmd": "init-editor"}
|
|
|
|
editorWS[sid].WriteJSON(&ret)
|
|
|
|
|
|
|
|
glog.Infof("Open a new [Editor] with session [%s], %d", sid, len(editorWS))
|
|
|
|
|
|
|
|
args := map[string]interface{}{}
|
|
|
|
for {
|
|
|
|
if err := editorWS[sid].ReadJSON(&args); err != nil {
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() == "unexpected EOF" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Error("Editor WS ERROR: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code := args["code"].(string)
|
|
|
|
line := int(args["cursorLine"].(float64))
|
|
|
|
ch := int(args["cursorCh"].(float64))
|
|
|
|
|
|
|
|
offset := util.Editor.GetCursorOffset(code, line, ch)
|
|
|
|
|
|
|
|
// glog.Infof("offset: %d", offset)
|
|
|
|
|
|
|
|
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 := editorWS[sid].WriteJSON(&ret); err != nil {
|
|
|
|
glog.Error("Editor WS ERROR: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FmtHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
fout, err := os.Create(filePath)
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
glog.Error(err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code := args["code"].(string)
|
|
|
|
|
|
|
|
fout.WriteString(code)
|
|
|
|
|
|
|
|
if err := fout.Close(); nil != err {
|
|
|
|
glog.Error(err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
argv := []string{filePath}
|
|
|
|
|
|
|
|
cmd := exec.Command("gofmt", argv...)
|
|
|
|
|
|
|
|
bytes, _ := cmd.Output()
|
|
|
|
output := string(bytes)
|
|
|
|
|
|
|
|
succ := true
|
|
|
|
if "" == output {
|
|
|
|
succ = false
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, _ := json.Marshal(map[string]interface{}{"succ": succ, "code": string(output)})
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
code := args["code"].(string)
|
|
|
|
line := int(args["cursorLine"].(float64))
|
|
|
|
ch := int(args["cursorCh"].(float64))
|
|
|
|
|
|
|
|
offset := util.Editor.GetCursorOffset(code, line, ch)
|
|
|
|
|
|
|
|
// glog.Infof("offset: %d", offset)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(output.Bytes())
|
|
|
|
}
|