mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
Now when you accept a struct literal field name completion, you will get a snippet that includes the colon, a tab stop, and a comma if the literal is multi-line. If you have "gopls.usePlaceholders" enabled, you will get a placeholder with the field's type as well. I pushed snippet generation into the "source" package so ast and type info is available. This allows for smarter, more context aware snippet generation. For example, this let me fix an issue with the function snippets where "foo<>()" was completing to "foo(<>)()". Now we don't add the function call snippet if the position is already in a CallExpr. I also added a new "Insert" field to CompletionItem to store the plain object name. This way, we don't have to undo label decorations when generating the insert text for the completion response. I also changed "filterText" to use this "Insert" field since you don't want the filter text to include the extra label decorations. Fixes golang/go#31556 Change-Id: I75266b2a4c0fe4036c44b315582f51738e464a39 GitHub-Last-Rev: 1ec28b2395c7bbe748940befe8c38579f5d75f61 GitHub-Pull-Request: golang/tools#89 Reviewed-on: https://go-review.googlesource.com/c/tools/+/173577 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package source
|
|
|
|
import (
|
|
"go/ast"
|
|
|
|
"golang.org/x/tools/internal/lsp/snippet"
|
|
)
|
|
|
|
// structField calculates the plain and placeholder snippets for struct literal
|
|
// field names as in "Foo{Ba<>".
|
|
func (c *completer) structFieldSnippet(label, detail string) (*snippet.Builder, *snippet.Builder) {
|
|
if !c.inCompositeLiteralField {
|
|
return nil, nil
|
|
}
|
|
|
|
cl := c.enclosingCompositeLiteral
|
|
kv := c.enclosingKeyValue
|
|
|
|
// If we aren't in a composite literal or are already in a key/value
|
|
// expression, we don't want a snippet.
|
|
if cl == nil || kv != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
if len(cl.Elts) > 0 {
|
|
i := indexExprAtPos(c.pos, cl.Elts)
|
|
if i >= len(cl.Elts) {
|
|
return nil, nil
|
|
}
|
|
|
|
// If our expression is not an identifer, we know it isn't a
|
|
// struct field name.
|
|
if _, ok := cl.Elts[i].(*ast.Ident); !ok {
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// It is a multi-line literal if pos is not on the same line as the literal's
|
|
// opening brace.
|
|
multiLine := c.fset.Position(c.pos).Line != c.fset.Position(cl.Lbrace).Line
|
|
|
|
// Plain snippet will turn "Foo{Ba<>" into "Foo{Bar: <>"
|
|
plain := &snippet.Builder{}
|
|
plain.WriteText(label + ": ")
|
|
plain.WritePlaceholder(nil)
|
|
if multiLine {
|
|
plain.WriteText(",")
|
|
}
|
|
|
|
// Placeholder snippet will turn "Foo{Ba<>" into "Foo{Bar: *int*"
|
|
placeholder := &snippet.Builder{}
|
|
placeholder.WriteText(label + ": ")
|
|
placeholder.WritePlaceholder(func(b *snippet.Builder) {
|
|
b.WriteText(detail)
|
|
})
|
|
if multiLine {
|
|
placeholder.WriteText(",")
|
|
}
|
|
|
|
return plain, placeholder
|
|
}
|
|
|
|
// funcCall calculates the plain and placeholder snippets for function calls.
|
|
func (c *completer) funcCallSnippet(funcName string, params []string) (*snippet.Builder, *snippet.Builder) {
|
|
for i := 1; i <= 2 && i < len(c.path); i++ {
|
|
call, ok := c.path[i].(*ast.CallExpr)
|
|
// If we are the left side (i.e. "Fun") part of a call expression,
|
|
// we don't want a snippet since there are already parens present.
|
|
if ok && call.Fun == c.path[i-1] {
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// Plain snippet turns "someFun<>" into "someFunc(<>)"
|
|
plain := &snippet.Builder{}
|
|
plain.WriteText(funcName + "(")
|
|
if len(params) > 0 {
|
|
plain.WritePlaceholder(nil)
|
|
}
|
|
plain.WriteText(")")
|
|
|
|
// Placeholder snippet turns "someFun<>" into "someFunc(*i int*, s string)"
|
|
placeholder := &snippet.Builder{}
|
|
placeholder.WriteText(funcName + "(")
|
|
for i, p := range params {
|
|
if i > 0 {
|
|
placeholder.WriteText(", ")
|
|
}
|
|
placeholder.WritePlaceholder(func(b *snippet.Builder) {
|
|
b.WriteText(p)
|
|
})
|
|
}
|
|
placeholder.WriteText(")")
|
|
|
|
return plain, placeholder
|
|
}
|