| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
|
| 10 |
"golang.org/x/tools/gopls/internal/golang/completion/snippet" |
| 11 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
func (c *completer) structFieldSnippet(cand candidate, detail string, snip *snippet.Builder) { |
| 16 |
if !wantStructFieldCompletions(c.enclosingCompositeLiteral) { |
| 17 |
return |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
if len(cand.path) > 0 { |
| 24 |
return |
| 25 |
} |
| 26 |
|
| 27 |
clInfo := c.enclosingCompositeLiteral |
| 28 |
|
| 29 |
|
| 30 |
if clInfo.kv != nil { |
| 31 |
return |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
snip.WriteText(": ") |
| 36 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 37 |
|
| 38 |
if c.opts.placeholders { |
| 39 |
b.WriteText(detail) |
| 40 |
} |
| 41 |
}) |
| 42 |
|
| 43 |
fset := c.pkg.FileSet() |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
if safetoken.StartPosition(fset, c.pos).Line != safetoken.StartPosition(fset, clInfo.cl.Lbrace).Line { |
| 48 |
snip.WriteText(",") |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func (c *completer) functionCallSnippet(name string, tparams, params []string, snip *snippet.Builder) { |
| 59 |
if !c.opts.completeFunctionCalls { |
| 60 |
snip.WriteText(name) |
| 61 |
return |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if c.surrounding != nil && c.surrounding.Suffix() == "" && len(c.path) > 1 { |
| 68 |
|
| 69 |
|
| 70 |
switch n := c.path[1].(type) { |
| 71 |
case *ast.CallExpr: |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if n.Fun == c.path[0] && n.Lparen != n.Rparen { |
| 76 |
return |
| 77 |
} |
| 78 |
case *ast.SelectorExpr: |
| 79 |
if len(c.path) > 2 { |
| 80 |
if call, ok := c.path[2].(*ast.CallExpr); ok && call.Fun == c.path[1] && call.Lparen != call.Rparen { |
| 81 |
return |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
snip.WriteText(name) |
| 88 |
|
| 89 |
if len(tparams) > 0 { |
| 90 |
snip.WriteText("[") |
| 91 |
if c.opts.placeholders { |
| 92 |
for i, tp := range tparams { |
| 93 |
if i > 0 { |
| 94 |
snip.WriteText(", ") |
| 95 |
} |
| 96 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 97 |
b.WriteText(tp) |
| 98 |
}) |
| 99 |
} |
| 100 |
} else { |
| 101 |
snip.WritePlaceholder(nil) |
| 102 |
} |
| 103 |
snip.WriteText("]") |
| 104 |
} |
| 105 |
|
| 106 |
snip.WriteText("(") |
| 107 |
|
| 108 |
if c.opts.placeholders { |
| 109 |
|
| 110 |
for i, p := range params { |
| 111 |
if i > 0 { |
| 112 |
snip.WriteText(", ") |
| 113 |
} |
| 114 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 115 |
b.WriteText(p) |
| 116 |
}) |
| 117 |
} |
| 118 |
} else { |
| 119 |
|
| 120 |
if len(params) > 0 { |
| 121 |
snip.WritePlaceholder(nil) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
snip.WriteText(")") |
| 126 |
} |