1
0
mirror of https://github.com/golang/go synced 2024-10-01 16:08:33 -06:00
go/internal/lsp/testdata/snippets/snippets.go
Muir Manders f217c98fae internal/lsp: fix completion insertion
The insertion range for completion items was not right. The range's
end was 1 before the start. Fix by taking into account the length of
the prefix when generating the range start and end.

Now instead of a "prefix", we track the completion's
"surrounding". This is basically the start and end of the abutting
identifier along with the cursor position. When we insert the
completion text, we overwrite the entire identifier, not just the
prefix. This fixes postfix completion like completing "foo.<>Bar" to
"foo.BarBaz".

Fixes golang/go#32078
Fixes golang/go#32057

Change-Id: I9d065a413ff9a6e20ae662ff93ad0092c2007c1d
GitHub-Last-Rev: af5ab4d60566bf0589d9a712c80d75280178cba9
GitHub-Pull-Request: golang/tools#103
Reviewed-on: https://go-review.googlesource.com/c/tools/+/177757
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-05-17 18:32:58 +00:00

40 lines
1.3 KiB
Go

package snippets
func foo(i int, b bool) {} //@item(snipFoo, "foo(i int, b bool)", "", "func")
func bar(fn func()) func() {} //@item(snipBar, "bar(fn func())", "", "func")
type Foo struct {
Bar int //@item(snipFieldBar, "Bar", "int", "field")
}
func (Foo) Baz() func() {} //@item(snipMethodBaz, "Baz()", "func()", "method")
func (Foo) BazBar() func() {} //@item(snipMethodBazBar, "BazBar()", "func()", "method")
func _() {
f //@snippet(" //", snipFoo, "foo(${1})", "foo(${1:i int}, ${2:b bool})")
bar //@snippet(" //", snipBar, "bar(${1})", "bar(${1:fn func()})")
bar(nil) //@snippet("(", snipBar, "bar", "bar")
bar(ba) //@snippet(")", snipBar, "bar(${1})", "bar(${1:fn func()})")
var f Foo
bar(f.Ba) //@snippet(")", snipMethodBaz, "Baz()", "Baz()")
(bar)(nil) //@snippet(")", snipBar, "bar(${1})", "bar(${1:fn func()})")
(f.Ba)() //@snippet(")", snipMethodBaz, "Baz()", "Baz()")
Foo{
B //@snippet(" //", snipFieldBar, "Bar: ${1},", "Bar: ${1:int},")
}
Foo{B} //@snippet("}", snipFieldBar, "Bar: ${1}", "Bar: ${1:int}")
Foo{} //@snippet("}", snipFieldBar, "Bar: ${1}", "Bar: ${1:int}")
Foo{Foo{}.B} //@snippet("} ", snipFieldBar, "Bar", "Bar")
var err error
err.Error() //@snippet("E", Error, "Error", "Error")
f.Baz() //@snippet("B", snipMethodBaz, "Baz", "Baz")
f.Baz() //@snippet("(", snipMethodBazBar, "BazBar", "BazBar")
}