prefs select option enhance

This commit is contained in:
Liang Ding 2014-11-26 11:54:01 +08:00
parent 5ea21eb25f
commit 61ef48147b
6 changed files with 82 additions and 51 deletions

View File

@ -21,7 +21,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
@ -126,7 +125,7 @@ func checkEnv() {
os.Exit(-1) os.Exit(-1)
} }
gocode := Wide.GetExecutableInGOBIN("gocode") gocode := util.Go.GetExecutableInGOBIN("gocode")
cmd = exec.Command(gocode, "close") cmd = exec.Command(gocode, "close")
_, err = cmd.Output() _, err = cmd.Output()
if nil != err { if nil != err {
@ -135,7 +134,7 @@ func checkEnv() {
glog.Warningf("Not found gocode [%s]", gocode) glog.Warningf("Not found gocode [%s]", gocode)
} }
ide_stub := Wide.GetExecutableInGOBIN("ide_stub") ide_stub := util.Go.GetExecutableInGOBIN("ide_stub")
cmd = exec.Command(ide_stub, "version") cmd = exec.Command(ide_stub, "version")
_, err = cmd.Output() _, err = cmd.Output()
if nil != err { if nil != err {
@ -167,7 +166,7 @@ func (c *conf) GetUserWorkspace(username string) string {
return "" return ""
} }
// GetGoFmt gets the path of Go format tool, returns "gofmt" if not found. // GetGoFmt gets the path of Go format tool, returns "gofmt" if not found "goimports".
func (c *conf) GetGoFmt(username string) string { func (c *conf) GetGoFmt(username string) string {
for _, user := range c.Users { for _, user := range c.Users {
if user.Name == username { if user.Name == username {
@ -175,7 +174,7 @@ func (c *conf) GetGoFmt(username string) string {
case "gofmt": case "gofmt":
return "gofmt" return "gofmt"
case "goimports": case "goimports":
return c.GetExecutableInGOBIN("goimports") return util.Go.GetExecutableInGOBIN("goimports")
default: default:
glog.Errorf("Unsupported Go Format tool [%s]", user.GoFormat) glog.Errorf("Unsupported Go Format tool [%s]", user.GoFormat)
return "gofmt" return "gofmt"
@ -210,42 +209,6 @@ func (*conf) GetUser(username string) *User {
return nil return nil
} }
// GetExecutableInGOBIN gets executable file under GOBIN path.
//
// The specified executable should not with extension, this function will append .exe if on Windows.
func (*conf) GetExecutableInGOBIN(executable string) string {
if util.OS.IsWindows() {
executable += ".exe"
}
gopaths := filepath.SplitList(os.Getenv("GOPATH"))
for _, gopath := range gopaths {
// $GOPATH/bin/$GOOS_$GOARCH/executable
ret := gopath + PathSeparator + "bin" + PathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable
if util.File.IsExist(ret) {
return ret
}
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
ret = gopath + PathSeparator + "bin" + PathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable
if util.File.IsExist(ret) {
return ret
}
// $GOPATH/bin/executable
ret = gopath + PathSeparator + "bin" + PathSeparator + executable
if util.File.IsExist(ret) {
return ret
}
}
// $GOBIN/executable
return os.Getenv("GOBIN") + PathSeparator + executable
}
// Save saves Wide configurations. // Save saves Wide configurations.
func Save() bool { func Save() bool {
// just the Users field are volatile // just the Users field are volatile

View File

@ -67,7 +67,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset: %d", offset) // glog.Infof("offset: %d", offset)
gocode := conf.Wide.GetExecutableInGOBIN("gocode") gocode := util.Go.GetExecutableInGOBIN("gocode")
argv := []string{"-f=json", "autocomplete", strconv.Itoa(offset)} argv := []string{"-f=json", "autocomplete", strconv.Itoa(offset)}
var output bytes.Buffer var output bytes.Buffer
@ -144,7 +144,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
glog.V(5).Infof("gocode set lib-path %s", libPath) glog.V(5).Infof("gocode set lib-path %s", libPath)
// FIXME: using gocode set lib-path has some issues while accrossing workspaces // FIXME: using gocode set lib-path has some issues while accrossing workspaces
gocode := conf.Wide.GetExecutableInGOBIN("gocode") gocode := util.Go.GetExecutableInGOBIN("gocode")
argv := []string{"set", "lib-path", libPath} argv := []string{"set", "lib-path", libPath}
exec.Command(gocode, argv...).Run() exec.Command(gocode, argv...).Run()
@ -213,7 +213,7 @@ func GetExprInfoHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset [%d]", offset) // glog.Infof("offset [%d]", offset)
ide_stub := conf.Wide.GetExecutableInGOBIN("ide_stub") ide_stub := util.Go.GetExecutableInGOBIN("ide_stub")
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-info", "."} argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-info", "."}
cmd := exec.Command(ide_stub, argv...) cmd := exec.Command(ide_stub, argv...)
cmd.Dir = curDir cmd.Dir = curDir
@ -284,7 +284,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset [%d]", offset) // glog.Infof("offset [%d]", offset)
ide_stub := conf.Wide.GetExecutableInGOBIN("ide_stub") ide_stub := util.Go.GetExecutableInGOBIN("ide_stub")
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-def", "."} argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-def", "."}
cmd := exec.Command(ide_stub, argv...) cmd := exec.Command(ide_stub, argv...)
cmd.Dir = curDir cmd.Dir = curDir
@ -363,7 +363,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {
offset := getCursorOffset(code, line, ch) offset := getCursorOffset(code, line, ch)
// glog.Infof("offset [%d]", offset) // glog.Infof("offset [%d]", offset)
ide_stub := conf.Wide.GetExecutableInGOBIN("ide_stub") ide_stub := util.Go.GetExecutableInGOBIN("ide_stub")
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-use", "."} argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-use", "."}
cmd := exec.Command(ide_stub, argv...) cmd := exec.Command(ide_stub, argv...)
cmd.Dir = curDir cmd.Dir = curDir

View File

@ -59,7 +59,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
if "GET" == r.Method { if "GET" == r.Method {
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user, model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user,
"ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version(), "ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version(),
"locales": i18n.GetLocalesNames()} "locales": i18n.GetLocalesNames(), "gofmts": util.Go.GetGoFormats()}
t, err := template.ParseFiles("views/preference.html") t, err := template.ParseFiles("views/preference.html")

View File

@ -287,9 +287,16 @@ var menu = {
$("#dialogPreference").load('/preference', function () { $("#dialogPreference").load('/preference', function () {
$("#localeSelect").on('change', function () { $("#localeSelect").on('change', function () {
var $dialogPreference = $("#dialogPreference"), var $dialogPreference = $("#dialogPreference"),
$locale = $dialogPreference.find("input[name=locale]") $input = $dialogPreference.find("input[name=locale]")
$locale.val(this.value); $input.val(this.value);
});
$("#goFmtSelect").on('change', function () {
var $dialogPreference = $("#dialogPreference"),
$input = $dialogPreference.find("input[name=goFmt]")
$input.val(this.value);
}); });
$("#dialogPreference input").keyup(function () { $("#dialogPreference input").keyup(function () {

View File

@ -19,6 +19,12 @@ import (
"path" "path"
"runtime" "runtime"
"strings" "strings"
"os"
)
const (
PathSeparator = string(os.PathSeparator) // OS-specific path separator
PathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
) )
type mygo struct{} type mygo struct{}
@ -45,3 +51,51 @@ func (*mygo) IsAPI(path string) bool {
return strings.HasPrefix(path, apiPath) return strings.HasPrefix(path, apiPath)
} }
// GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
func (*mygo) GetGoFormats() []string {
ret := []string {"gofmt"}
p := Go.GetExecutableInGOBIN("goimports")
if File.IsExist(p) {
ret = append(ret, "goimports")
}
return ret
}
// GetExecutableInGOBIN gets executable file under GOBIN path.
//
// The specified executable should not with extension, this function will append .exe if on Windows.
func (*mygo) GetExecutableInGOBIN(executable string) string {
if OS.IsWindows() {
executable += ".exe"
}
gopaths := filepath.SplitList(os.Getenv("GOPATH"))
for _, gopath := range gopaths {
// $GOPATH/bin/$GOOS_$GOARCH/executable
ret := gopath + PathSeparator + "bin" + PathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
ret = gopath + PathSeparator + "bin" + PathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/executable
ret = gopath + PathSeparator + "bin" + PathSeparator + executable
if File.IsExist(ret) {
return ret
}
}
// $GOBIN/executable
return os.Getenv("GOBIN") + PathSeparator + executable
}

View File

@ -41,8 +41,15 @@
<div class="fn-none" data-index="gotool"> <div class="fn-none" data-index="gotool">
<label> <label>
{{.i18n.go_format}}{{.i18n.colon}} {{.i18n.go_format}}{{.i18n.colon}}
<input data-value="{{.user.GoFormat}}" value="{{.user.GoFormat}}" name="goFmt"/> <br/>
<select class="select" id="goFmtSelect">
{{range $index, $gofmt := .gofmts }}
<option name="{{$gofmt}}" value="{{$gofmt}}" {{if eq $.user.GoFormat $gofmt}}selected="selected"{{end}}>{{$gofmt}}</option>
{{end}}
</select>
<br/>
</label> </label>
<input data-value="{{.user.GoFormat}}" value="{{.user.GoFormat}}" name="goFmt" hidden="hidden" />
</div> </div>
<div class="fn-none" data-index="user"> <div class="fn-none" data-index="user">
<label> <label>
@ -50,7 +57,7 @@
<br/> <br/>
<select class="select" id="localeSelect"> <select class="select" id="localeSelect">
{{range $index, $localeName := .locales }} {{range $index, $localeName := .locales }}
<option name="locale" value="{{$localeName}}" {{if eq $.user.Locale $localeName}}selected="selected"{{end}}>{{$localeName}}</option> <option name="{{$localeName}}" value="{{$localeName}}" {{if eq $.user.Locale $localeName}}selected="selected"{{end}}>{{$localeName}}</option>
{{end}} {{end}}
</select> </select>
<br/> <br/>