func (gi *GotoDef) Exec(on interface{}) (executed, consume bool) { opener, ok := on.(LineOpener) if !ok { return false, false } editor := opener.CurrentEditor() if editor == nil { return true, true } proj := opener.Project() lastCaret := editor.Controller().LastCaret() cmd := exec.Command("godef", "-f", editor.Filepath(), "-o", strconv.Itoa(lastCaret), "-i") cmd.Stdin = bytes.NewBufferString(editor.Text()) errBuffer := &bytes.Buffer{} cmd.Stderr = errBuffer cmd.Env = []string{"PATH=" + os.Getenv("PATH")} if proj.Gopath != "" { cmd.Env[0] += string(os.PathListSeparator) + filepath.Join(proj.Gopath, "bin") cmd.Env = append(cmd.Env, "GOPATH="+proj.Gopath) } output, err := cmd.Output() if err != nil { gi.err = fmt.Sprintf("godef error: %s", string(output)) return true, true } path, line, column, err := parseGodef(output) if err != nil { gi.err = err.Error() return true, true } opener.OpenLine(path, line, column) return true, true }
func (s *SaveCurrent) Exec(target interface{}) (executed, consume bool) { finder, ok := target.(CurrentFileFinder) if !ok { return false, false } editor := finder.CurrentEditor() if editor == nil { return true, true } s.filepath = finder.CurrentFile() if !editor.LastKnownMTime().IsZero() { finfo, err := os.Stat(s.filepath) if err != nil { s.err = fmt.Sprintf("Could not stat file %s: %s", s.filepath, err) return true, false } if finfo.ModTime().After(editor.LastKnownMTime()) { // TODO: prompt for override s.err = fmt.Sprintf("File %s changed on disk. Cowardly refusing to overwrite.", s.filepath) return true, false } } f, err := os.Create(s.filepath) if err != nil { s.err = fmt.Sprintf("Could not open %s for writing: %s", s.filepath, err) return true, false } defer f.Close() if !strings.HasSuffix(editor.Text(), "\n") { editor.SetText(editor.Text() + "\n") } if _, err := f.WriteString(editor.Text()); err != nil { s.err = fmt.Sprintf("Could not write to file %s: %s", s.filepath, err) return true, false } s.info = fmt.Sprintf("Successfully saved %s", s.filepath) editor.FlushedChanges() return true, true }