示例#1
0
文件: explore.go 项目: garyburd/vigor
func (e *explorer) onDef(args []string, eval *struct {
	Env   context.Env
	Cwd   string `eval:"getcwd()"`
	Bufnr int    `eval:"bufnr('%')"`
}) error {
	if len(args) < 1 || len(args) > 2 {
		return errors.New("one or two arguments required")
	}

	spec, err := e.expandSpec(args[0])
	if err != nil {
		return err
	}

	ctx := context.Get(&eval.Env)
	path := resolvePackageSpec(&ctx.Build, eval.Cwd, nvim.NewBufferReader(e.nvim, nvim.Buffer(eval.Bufnr)), spec)

	var sym string
	if len(args) >= 2 {
		sym = strings.Trim(args[1], ".")
	}

	file, line, col, err := findDef(&ctx.Build, eval.Cwd, path, sym)
	if err != nil {
		return errors.New("definition not found")
	}

	return e.nvim.Command(fmt.Sprintf("edit %s | call cursor(%d, %d)", file, line, col))
}
示例#2
0
文件: format.go 项目: garyburd/vigor
func format(v *nvim.Nvim, r [2]int, eval *struct {
	Env   context.Env
	Bufnr int `eval:"bufnr('%')"`
}) error {
	var (
		in    [][]byte
		fname string
	)
	buf := nvim.Buffer(eval.Bufnr)

	b := v.NewBatch()
	b.BufferLines(buf, 0, -1, true, &in)
	b.BufferName(buf, &fname)
	if err := b.Execute(); err != nil {
		return nil
	}

	var stdout, stderr bytes.Buffer
	c := exec.Command("goimports", "-srcdir", filepath.Dir(fname))
	c.Stdin = bytes.NewReader(bytes.Join(in, []byte{'\n'}))
	c.Stdout = &stdout
	c.Stderr = &stderr
	c.Env = context.Get(&eval.Env).Environ
	err := c.Run()
	if err == nil {
		out := bytes.Split(bytes.TrimSuffix(stdout.Bytes(), []byte{'\n'}), []byte{'\n'})
		return minUpdate(v, buf, in, out)
	}
	if _, ok := err.(*exec.ExitError); ok {
		var qfl []*nvim.QuickfixError
		for _, m := range errorPat.FindAllSubmatch(stderr.Bytes(), -1) {
			qfe := nvim.QuickfixError{}
			qfe.LNum, _ = strconv.Atoi(string(m[2]))
			qfe.Col, _ = strconv.Atoi(string(m[3]))
			qfe.Text = string(bytes.TrimSpace(m[4]))
			qfe.Bufnr = eval.Bufnr
			qfl = append(qfl, &qfe)
		}
		if len(qfl) > 0 {
			b := v.NewBatch()
			b.Call("setqflist", nil, qfl)
			b.Command("cc")
			return b.Execute()
		}
	}
	return err
}
示例#3
0
文件: explore.go 项目: garyburd/vigor
func (e *explorer) onBufReadCmd(eval *struct {
	Env   context.Env
	Cwd   string `eval:"getcwd()"`
	Name  string `eval:"expand('%')"`
	Bufnr int    `eval:"bufnr('%')"`
}) error {

	ctx := context.Get(&eval.Env)
	d, err := printDoc(&ctx.Build, eval.Name, eval.Cwd)
	if err != nil {
		d := doc.NewDoc()
		d.WriteString(err.Error())
	}
	return e.docm.Display(d, nvim.Buffer(eval.Bufnr))
	/*
		p.Command("nnoremap <buffer> <silent> g? :<C-U>help :Godoc<CR>")
		p.Command(`nnoremap <buffer> <silent> ]] :<C-U>call search('\C\v^[^ \t)}]', 'W')<CR>`)
		p.Command(`nnoremap <buffer> <silent> [[ :<C-U>call search('\C\v^[^ \t)}]', 'Wb')<CR>`)
	*/
}
示例#4
0
文件: explore.go 项目: garyburd/vigor
func (e *explorer) onDoc(args []string, eval *struct {
	Env   context.Env
	Cwd   string `eval:"getcwd()"`
	Name  string `eval:"expand('%')"`
	Bufnr int    `eval:"bufnr('%')"`
}) error {

	if len(args) < 1 || len(args) > 2 {
		return errors.New("one or two arguments required")
	}

	spec, err := e.expandSpec(args[0])
	if err != nil {
		return err
	}

	ctx := context.Get(&eval.Env)
	name := bufNamePrefix + resolvePackageSpec(&ctx.Build, eval.Cwd, nvim.NewBufferReader(e.nvim, nvim.Buffer(eval.Bufnr)), spec)

	var cmds []string
	if name != eval.Name {
		cmds = append(cmds, "edit "+name)
	}

	if len(args) >= 2 {
		cmds = append(cmds, fmt.Sprintf("call cursor(get(b:anchors, %q, [0, 0]))", strings.Trim(args[1], ".")))
	}
	if len(cmds) == 0 {
		return nil
	}
	return e.nvim.Command(strings.Join(cmds, " | "))
}
示例#5
0
文件: explore.go 项目: garyburd/vigor
func (e *explorer) onComplete(a *nvim.CommandCompletionArgs, eval *struct {
	Env   context.Env
	Cwd   string `eval:"getcwd()"`
	Bufnr int    `eval:"bufnr('%')"`
}) ([]string, error) {

	ctx := context.Get(&eval.Env)

	f := strings.Fields(a.CmdLine)
	var completions []string
	if len(f) >= 3 || (len(f) == 2 && a.ArgLead == "") {
		spec, err := e.expandSpec(f[1])
		if err != nil {
			return nil, err
		}
		completions = completeSymMethodArg(&ctx.Build, resolvePackageSpec(&ctx.Build, eval.Cwd, nvim.NewBufferReader(e.nvim, nvim.Buffer(eval.Bufnr)), spec), a.ArgLead)
	} else {
		completions = completePackageArg(&ctx.Build, eval.Cwd, nvim.NewBufferReader(e.nvim, nvim.Buffer(eval.Bufnr)), a.ArgLead)
	}
	return completions, nil
}