| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/file" |
| 13 |
"golang.org/x/tools/gopls/internal/golang" |
| 14 |
"golang.org/x/tools/gopls/internal/golang/completion" |
| 15 |
"golang.org/x/tools/gopls/internal/label" |
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
"golang.org/x/tools/gopls/internal/settings" |
| 18 |
"golang.org/x/tools/gopls/internal/telemetry" |
| 19 |
"golang.org/x/tools/gopls/internal/template" |
| 20 |
"golang.org/x/tools/gopls/internal/work" |
| 21 |
"golang.org/x/tools/internal/event" |
| 22 |
) |
| 23 |
|
| 24 |
func (s *server) Completion(ctx context.Context, params *protocol.CompletionParams) (_ *protocol.CompletionList, rerr error) { |
| 25 |
recordLatency := telemetry.StartLatencyTimer("completion") |
| 26 |
defer func() { |
| 27 |
recordLatency(ctx, rerr) |
| 28 |
}() |
| 29 |
|
| 30 |
ctx, done := event.Start(ctx, "server.Completion", label.URI.Of(params.TextDocument.URI)) |
| 31 |
defer done() |
| 32 |
|
| 33 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
defer release() |
| 38 |
|
| 39 |
if params.Range.Start != params.Range.End { |
| 40 |
return nil, fmt.Errorf("textDocument/completion request only applicable for position") |
| 41 |
} |
| 42 |
pos := params.Range.Start |
| 43 |
|
| 44 |
var candidates []completion.CompletionItem |
| 45 |
var surrounding *completion.Selection |
| 46 |
switch snapshot.FileKind(fh) { |
| 47 |
case file.Go: |
| 48 |
candidates, surrounding, err = completion.Completion(ctx, snapshot, fh, pos, params.Context) |
| 49 |
case file.Mod: |
| 50 |
candidates, surrounding = nil, nil |
| 51 |
case file.Work: |
| 52 |
cl, err := work.Completion(ctx, snapshot, fh, pos) |
| 53 |
if err != nil { |
| 54 |
break |
| 55 |
} |
| 56 |
return cl, nil |
| 57 |
case file.Tmpl: |
| 58 |
var cl *protocol.CompletionList |
| 59 |
cl, err = template.Completion(ctx, snapshot, fh, pos, params.Context) |
| 60 |
if err != nil { |
| 61 |
break |
| 62 |
} |
| 63 |
return cl, nil |
| 64 |
} |
| 65 |
if err != nil { |
| 66 |
event.Error(ctx, "no completions found", err, label.Position.Of(pos)) |
| 67 |
} |
| 68 |
if candidates == nil || surrounding == nil { |
| 69 |
complEmpty.Inc() |
| 70 |
return &protocol.CompletionList{ |
| 71 |
IsIncomplete: true, |
| 72 |
Items: []protocol.CompletionItem{}, |
| 73 |
}, nil |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
options := snapshot.Options() |
| 79 |
incompleteResults := options.DeepCompletion || options.Matcher == settings.Fuzzy |
| 80 |
|
| 81 |
items, err := toProtocolCompletionItems(candidates, surrounding, options) |
| 82 |
if err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
if snapshot.FileKind(fh) == file.Go { |
| 86 |
s.saveLastCompletion(fh.URI(), fh.Version(), items, params.Position) |
| 87 |
} |
| 88 |
|
| 89 |
if len(items) > 10 { |
| 90 |
|
| 91 |
complLong.Inc() |
| 92 |
} else { |
| 93 |
complShort.Inc() |
| 94 |
} |
| 95 |
return &protocol.CompletionList{ |
| 96 |
IsIncomplete: incompleteResults, |
| 97 |
Items: items, |
| 98 |
}, nil |
| 99 |
} |
| 100 |
|
| 101 |
func (s *server) saveLastCompletion(uri protocol.DocumentURI, version int32, items []protocol.CompletionItem, pos protocol.Position) { |
| 102 |
s.efficacyMu.Lock() |
| 103 |
defer s.efficacyMu.Unlock() |
| 104 |
s.efficacyVersion = version |
| 105 |
s.efficacyURI = uri |
| 106 |
s.efficacyPos = pos |
| 107 |
s.efficacyItems = items |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func toProtocolCompletionItems(candidates []completion.CompletionItem, surrounding *completion.Selection, options *settings.Options) ([]protocol.CompletionItem, error) { |
| 113 |
replaceRng, err := surrounding.Range() |
| 114 |
if err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
insertRng0, err := surrounding.PrefixRange() |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
suffix := surrounding.Suffix() |
| 122 |
|
| 123 |
var ( |
| 124 |
items = make([]protocol.CompletionItem, 0, len(candidates)) |
| 125 |
numDeepCompletionsSeen int |
| 126 |
) |
| 127 |
for i, candidate := range candidates { |
| 128 |
|
| 129 |
|
| 130 |
if candidate.Depth > 0 { |
| 131 |
if !options.DeepCompletion { |
| 132 |
continue |
| 133 |
} |
| 134 |
if numDeepCompletionsSeen >= completion.MaxDeepCompletions { |
| 135 |
continue |
| 136 |
} |
| 137 |
numDeepCompletionsSeen++ |
| 138 |
} |
| 139 |
insertText := candidate.InsertText |
| 140 |
if options.InsertTextFormat == protocol.SnippetTextFormat { |
| 141 |
insertText = candidate.Snippet() |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if insertText == "" { |
| 147 |
continue |
| 148 |
} |
| 149 |
|
| 150 |
var doc *protocol.Or_CompletionItem_documentation |
| 151 |
if candidate.Documentation != "" { |
| 152 |
var value any |
| 153 |
if options.PreferredContentFormat == protocol.Markdown { |
| 154 |
value = protocol.MarkupContent{ |
| 155 |
Kind: protocol.Markdown, |
| 156 |
Value: golang.DocCommentToMarkdown(candidate.Documentation, options), |
| 157 |
} |
| 158 |
} else { |
| 159 |
value = candidate.Documentation |
| 160 |
} |
| 161 |
doc = &protocol.Or_CompletionItem_documentation{Value: value} |
| 162 |
} |
| 163 |
var edits *protocol.Or_CompletionItem_textEdit |
| 164 |
if options.InsertReplaceSupported { |
| 165 |
insertRng := insertRng0 |
| 166 |
if suffix == "" || strings.Contains(insertText, suffix) { |
| 167 |
insertRng = replaceRng |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
edits = &protocol.Or_CompletionItem_textEdit{ |
| 174 |
Value: protocol.InsertReplaceEdit{ |
| 175 |
NewText: insertText, |
| 176 |
Insert: insertRng, |
| 177 |
Replace: replaceRng, |
| 178 |
}, |
| 179 |
} |
| 180 |
} else { |
| 181 |
edits = &protocol.Or_CompletionItem_textEdit{ |
| 182 |
Value: protocol.TextEdit{ |
| 183 |
NewText: insertText, |
| 184 |
Range: replaceRng, |
| 185 |
}, |
| 186 |
} |
| 187 |
} |
| 188 |
item := protocol.CompletionItem{ |
| 189 |
Label: candidate.Label, |
| 190 |
Detail: candidate.Detail, |
| 191 |
Kind: candidate.Kind, |
| 192 |
TextEdit: edits, |
| 193 |
InsertTextFormat: &options.InsertTextFormat, |
| 194 |
AdditionalTextEdits: candidate.AdditionalTextEdits, |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
SortText: fmt.Sprintf("%05d", i), |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
FilterText: strings.TrimLeft(candidate.InsertText, "&*"), |
| 203 |
|
| 204 |
Preselect: i == 0, |
| 205 |
Documentation: doc, |
| 206 |
Tags: protocol.NonNilSlice(candidate.Tags), |
| 207 |
Deprecated: candidate.Deprecated, |
| 208 |
} |
| 209 |
items = append(items, item) |
| 210 |
} |
| 211 |
return items, nil |
| 212 |
} |