110y / go-expr-completion

A tool to complete a left-hand side from given expression for Go.
16 stars 1 forks source link

go-expr-completion

A tool to complete a left-hand side from given expression for Go.
This tool is aimed to be integrated with text editors.

Usage

Example

Let's assume that your cursor is on the F on line:16 (fmt.Fprintln) with this file:

./internal/analysis/testdata/package1/file1.go

package package1

import (
    "context"
    "fmt"
    "os"
)

func f1() {
    context.Background()
}

func f2() {
    // some
    // comments
    fmt.Fprintln(os.Stdout, "message")
}

The F is 130 as byte offsets for the file.
So, let's specify the file path and -pos 130 argument to go-expr-completion like below:

> go-expr-completion -pos 130 -file ./internal/analysis/testdata/package1/file1.go | jq .

{
  "start_pos": 126,
  "end_pos": 160,
  "values": [
    {
      "name": "i",
      "type": "int"
    },
    {
      "name": "err",
      "type": "error"
    }
  ]
}

Finally, go-expr-completion returs type information about fmt.Fprintln which is specified by your cursor.
By using this type information in text editors, we can complete the left-hand side from the expression like below (Vim):

01d57234-441d-46ef-bb0d-7b8f336b84b4

Text Editor Plugins