-
Notifications
You must be signed in to change notification settings - Fork 420
/
CompletionService.cs
533 lines (465 loc) · 29 KB
/
CompletionService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#nullable enable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Tags;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions;
using OmniSharp.Mef;
using OmniSharp.Models;
using OmniSharp.Models.v1.Completion;
using OmniSharp.Options;
using OmniSharp.Roslyn.CSharp.Helpers;
using OmniSharp.Roslyn.CSharp.Services.Intellisense;
using OmniSharp.Utilities;
using CompletionItem = OmniSharp.Models.v1.Completion.CompletionItem;
using CompletionTriggerKind = OmniSharp.Models.v1.Completion.CompletionTriggerKind;
using CSharpCompletionList = Microsoft.CodeAnalysis.Completion.CompletionList;
using CSharpCompletionService = Microsoft.CodeAnalysis.Completion.CompletionService;
namespace OmniSharp.Roslyn.CSharp.Services.Completion
{
[Shared]
[OmniSharpHandler(OmniSharpEndpoints.Completion, LanguageNames.CSharp)]
[OmniSharpHandler(OmniSharpEndpoints.CompletionResolve, LanguageNames.CSharp)]
public class CompletionService :
IRequestHandler<CompletionRequest, CompletionResponse>,
IRequestHandler<CompletionResolveRequest, CompletionResolveResponse>
{
private static readonly Dictionary<string, CompletionItemKind> s_roslynTagToCompletionItemKind = new Dictionary<string, CompletionItemKind>()
{
{ WellKnownTags.Public, CompletionItemKind.Keyword },
{ WellKnownTags.Protected, CompletionItemKind.Keyword },
{ WellKnownTags.Private, CompletionItemKind.Keyword },
{ WellKnownTags.Internal, CompletionItemKind.Keyword },
{ WellKnownTags.File, CompletionItemKind.File },
{ WellKnownTags.Project, CompletionItemKind.File },
{ WellKnownTags.Folder, CompletionItemKind.Folder },
{ WellKnownTags.Assembly, CompletionItemKind.File },
{ WellKnownTags.Class, CompletionItemKind.Class },
{ WellKnownTags.Constant, CompletionItemKind.Constant },
{ WellKnownTags.Delegate, CompletionItemKind.Function },
{ WellKnownTags.Enum, CompletionItemKind.Enum },
{ WellKnownTags.EnumMember, CompletionItemKind.EnumMember },
{ WellKnownTags.Event, CompletionItemKind.Event },
{ WellKnownTags.ExtensionMethod, CompletionItemKind.Method },
{ WellKnownTags.Field, CompletionItemKind.Field },
{ WellKnownTags.Interface, CompletionItemKind.Interface },
{ WellKnownTags.Intrinsic, CompletionItemKind.Text },
{ WellKnownTags.Keyword, CompletionItemKind.Keyword },
{ WellKnownTags.Label, CompletionItemKind.Text },
{ WellKnownTags.Local, CompletionItemKind.Variable },
{ WellKnownTags.Namespace, CompletionItemKind.Module },
{ WellKnownTags.Method, CompletionItemKind.Method },
{ WellKnownTags.Module, CompletionItemKind.Module },
{ WellKnownTags.Operator, CompletionItemKind.Operator },
{ WellKnownTags.Parameter, CompletionItemKind.Value },
{ WellKnownTags.Property, CompletionItemKind.Property },
{ WellKnownTags.RangeVariable, CompletionItemKind.Variable },
{ WellKnownTags.Reference, CompletionItemKind.Reference },
{ WellKnownTags.Structure, CompletionItemKind.Struct },
{ WellKnownTags.TypeParameter, CompletionItemKind.TypeParameter },
{ WellKnownTags.Snippet, CompletionItemKind.Snippet },
{ WellKnownTags.Error, CompletionItemKind.Text },
{ WellKnownTags.Warning, CompletionItemKind.Text },
};
private readonly OmniSharpWorkspace _workspace;
private readonly FormattingOptions _formattingOptions;
private readonly ILogger _logger;
private readonly object _lock = new object();
private (CSharpCompletionList Completions, string FileName, int position)? _lastCompletion = null;
[ImportingConstructor]
public CompletionService(OmniSharpWorkspace workspace, FormattingOptions formattingOptions, ILoggerFactory loggerFactory)
{
_workspace = workspace;
_formattingOptions = formattingOptions;
_logger = loggerFactory.CreateLogger<CompletionService>();
}
public async Task<CompletionResponse> Handle(CompletionRequest request)
{
_logger.LogTrace("Completions requested");
lock (_lock)
{
_lastCompletion = null;
}
var document = _workspace.GetDocument(request.FileName);
if (document is null)
{
_logger.LogInformation("Could not find document for file {0}", request.FileName);
return new CompletionResponse { Items = ImmutableArray<CompletionItem>.Empty };
}
var sourceText = await document.GetTextAsync();
var position = sourceText.GetTextPosition(request);
var completionService = CSharpCompletionService.GetService(document);
Debug.Assert(request.TriggerCharacter != null || request.CompletionTrigger != CompletionTriggerKind.TriggerCharacter);
if (request.CompletionTrigger == CompletionTriggerKind.TriggerCharacter &&
!completionService.ShouldTriggerCompletion(sourceText, position, getCompletionTrigger(includeTriggerCharacter: true)))
{
_logger.LogTrace("Should not insert completions here.");
return new CompletionResponse { Items = ImmutableArray<CompletionItem>.Empty };
}
var (completions, expandedItemsAvailable) = await completionService.GetCompletionsInternalAsync(
document,
position,
getCompletionTrigger(includeTriggerCharacter: false));
_logger.LogTrace("Found {0} completions for {1}:{2},{3}",
completions?.Items.IsDefaultOrEmpty != true ? 0 : completions.Items.Length,
request.FileName,
request.Line,
request.Column);
if (completions is null || completions.Items.Length == 0)
{
return new CompletionResponse { Items = ImmutableArray<CompletionItem>.Empty };
}
if (request.TriggerCharacter == ' ' && !completions.Items.Any(c => c.IsObjectCreationCompletionItem()))
{
// Only trigger on space if there is an object creation completion
return new CompletionResponse { Items = ImmutableArray<CompletionItem>.Empty };
}
var typedSpan = completionService.GetDefaultCompletionListSpan(sourceText, position);
string typedText = sourceText.GetSubText(typedSpan).ToString();
ImmutableArray<string> filteredItems = typedText != string.Empty
? completionService.FilterItems(document, completions.Items, typedText).SelectAsArray(i => i.DisplayText)
: ImmutableArray<string>.Empty;
_logger.LogTrace("Completions filled in");
lock (_lock)
{
_lastCompletion = (completions, request.FileName, position);
}
var triggerCharactersBuilder = ImmutableArray.CreateBuilder<char>(completions.Rules.DefaultCommitCharacters.Length);
var completionsBuilder = ImmutableArray.CreateBuilder<CompletionItem>();
// If we don't encounter any unimported types, and the completion context thinks that some would be available, then
// that completion provider is still creating the cache. We'll mark this completion list as not completed, and the
// editor will ask again when the user types more. By then, hopefully the cache will have populated and we can mark
// the completion as done.
bool seenUnimportedCompletions = false;
bool expectingImportedItems = expandedItemsAvailable && _workspace.Options.GetOption(CompletionItemExtensions.ShowItemsFromUnimportedNamespaces, LanguageNames.CSharp) == true;
var syntax = await document.GetSyntaxTreeAsync();
for (int i = 0; i < completions.Items.Length; i++)
{
var completion = completions.Items[i];
var insertTextFormat = InsertTextFormat.PlainText;
IReadOnlyList<LinePositionSpanTextChange>? additionalTextEdits = null;
char sortTextPrepend = '0';
if (!completion.TryGetInsertionText(out string insertText))
{
string providerName = completion.GetProviderName();
switch (providerName)
{
case CompletionItemExtensions.InternalsVisibleToCompletionProvider:
// The IVT completer doesn't add extra things before the completion
// span, only assembly keys at the end if they exist.
{
// if the completion is for the hidden Misc files project, skip it
if (completion.DisplayText == Configuration.OmniSharpMiscProjectName) continue;
CompletionChange change = await completionService.GetChangeAsync(document, completion);
Debug.Assert(typedSpan == change.TextChange.Span);
insertText = change.TextChange.NewText!;
}
break;
case CompletionItemExtensions.XmlDocCommentCompletionProvider:
{
// The doc comment completion might compensate for the < before
// the current word, if one exists. For these cases, if the token
// before the current location is a < and the text it's replacing starts
// with a <, erase the < from the given insertion text.
var change = await completionService.GetChangeAsync(document, completion);
bool trimFront = change.TextChange.NewText![0] == '<'
&& sourceText[change.TextChange.Span.Start] == '<';
Debug.Assert(!trimFront || change.TextChange.Span.Start + 1 == typedSpan.Start);
(insertText, insertTextFormat) = getAdjustedInsertTextWithPosition(change, position, newOffset: trimFront ? 1 : 0);
}
break;
case CompletionItemExtensions.OverrideCompletionProvider:
case CompletionItemExtensions.PartialMethodCompletionProvider:
{
// For these two, we potentially need to use additionalTextEdits. It's possible
// that override (or C# expanded partials) will cause the word or words before
// the cursor to be adjusted. For example:
//
// public class C {
// override $0
// }
//
// Invoking completion and selecting, say Equals, wants to cause the line to be
// rewritten as this:
//
// public class C {
// public override bool Equals(object other)
// {
// return base.Equals(other);$0
// }
// }
//
// In order to handle this, we need to chop off the section of the completion
// before the cursor and bundle that into an additionalTextEdit. Then, we adjust
// the remaining bit of the change to put the cursor in the expected spot via
// snippets. We could leave the additionalTextEdit bit for resolve, but we already
// have the data do the change and we basically have to compute the whole thing now
// anyway, so it doesn't really save us anything.
var change = await completionService.GetChangeAsync(document, completion);
// If the span we're using to key the completion off is the same as the replacement
// span, then we don't need to do anything special, just snippitize the text and
// exit
if (typedSpan == change.TextChange.Span)
{
(insertText, insertTextFormat) = getAdjustedInsertTextWithPosition(change, position, newOffset: 0);
break;
}
int additionalEditEndOffset;
(additionalTextEdits, additionalEditEndOffset) = await GetAdditionalTextEdits(change, sourceText, (CSharpParseOptions)syntax!.Options, typedSpan, completion.DisplayText, providerName);
// Now that we have the additional edit, adjust the rest of the new text
(insertText, insertTextFormat) = getAdjustedInsertTextWithPosition(change, position, additionalEditEndOffset);
}
break;
case CompletionItemExtensions.TypeImportCompletionProvider:
case CompletionItemExtensions.ExtensionMethodImportCompletionProvider:
// We did indeed find unimported types, the completion list can be considered complete.
// This is technically slightly incorrect: extension method completion can provide
// partial results. However, this should only affect the first completion session or
// two and isn't a big problem in practice.
seenUnimportedCompletions = true;
sortTextPrepend = '1';
goto default;
default:
insertText = completion.DisplayText;
break;
}
}
var commitCharacters = buildCommitCharacters(completions, completion.Rules.CommitCharacterRules, triggerCharactersBuilder);
completionsBuilder.Add(new CompletionItem
{
Label = completion.DisplayTextPrefix + completion.DisplayText + completion.DisplayTextSuffix,
InsertText = insertText,
InsertTextFormat = insertTextFormat,
AdditionalTextEdits = additionalTextEdits,
// Ensure that unimported items are sorted after things already imported.
SortText = expectingImportedItems ? sortTextPrepend + completion.SortText : completion.SortText,
FilterText = completion.FilterText,
Kind = getCompletionItemKind(completion.Tags),
Detail = completion.InlineDescription,
Data = i,
Preselect = completion.Rules.MatchPriority == MatchPriority.Preselect || filteredItems.Contains(completion.DisplayText),
CommitCharacters = commitCharacters,
});
}
return new CompletionResponse
{
IsIncomplete = !seenUnimportedCompletions && expectingImportedItems,
Items = completionsBuilder.ToImmutableArray()
};
CompletionTrigger getCompletionTrigger(bool includeTriggerCharacter)
=> request.CompletionTrigger switch
{
CompletionTriggerKind.Invoked => CompletionTrigger.Invoke,
// https://github.com/dotnet/roslyn/issues/42982: Passing a trigger character
// to GetCompletionsAsync causes a null ref currently.
CompletionTriggerKind.TriggerCharacter when includeTriggerCharacter => CompletionTrigger.CreateInsertionTrigger((char)request.TriggerCharacter!),
_ => CompletionTrigger.Invoke,
};
static CompletionItemKind getCompletionItemKind(ImmutableArray<string> tags)
{
foreach (var tag in tags)
{
if (s_roslynTagToCompletionItemKind.TryGetValue(tag, out var itemKind))
{
return itemKind;
}
}
return CompletionItemKind.Text;
}
static ImmutableArray<char> buildCommitCharacters(CSharpCompletionList completions, ImmutableArray<CharacterSetModificationRule> characterRules, ImmutableArray<char>.Builder triggerCharactersBuilder)
{
triggerCharactersBuilder.AddRange(completions.Rules.DefaultCommitCharacters);
foreach (var modifiedRule in characterRules)
{
switch (modifiedRule.Kind)
{
case CharacterSetModificationKind.Add:
triggerCharactersBuilder.AddRange(modifiedRule.Characters);
break;
case CharacterSetModificationKind.Remove:
for (int i = 0; i < triggerCharactersBuilder.Count; i++)
{
if (modifiedRule.Characters.Contains(triggerCharactersBuilder[i]))
{
triggerCharactersBuilder.RemoveAt(i);
i--;
}
}
break;
case CharacterSetModificationKind.Replace:
triggerCharactersBuilder.Clear();
triggerCharactersBuilder.AddRange(modifiedRule.Characters);
break;
}
}
// VS has a more complex concept of a commit mode vs suggestion mode for intellisense.
// LSP doesn't have this, so mock it as best we can by removing space ` ` from the list
// of commit characters if we're in suggestion mode.
if (completions.SuggestionModeItem is object)
{
triggerCharactersBuilder.Remove(' ');
}
return triggerCharactersBuilder.ToImmutableAndClear();
}
static (string, InsertTextFormat) getAdjustedInsertTextWithPosition(
CompletionChange change,
int originalPosition,
int newOffset)
{
// We often have to trim part of the given change off the front, but we
// still want to turn the resulting change into a snippet and control
// the cursor location in the insertion text. We therefore need to compensate
// by cutting off the requested portion of the text, finding the adjusted
// position in the requested string, and snippetizing it.
// NewText is annotated as nullable, but this is a misannotation that will be fixed.
string newText = change.TextChange.NewText!;
// Easy-out, either Roslyn doesn't have an opinion on adjustment, or the adjustment is after the
// end of the new text. Just return a substring from the requested offset to the end
if (!(change.NewPosition is int newPosition)
|| newPosition >= (change.TextChange.Span.Start + newText.Length))
{
return (newText.Substring(newOffset), InsertTextFormat.PlainText);
}
if (newPosition < (originalPosition + newOffset))
{
Debug.Fail($"Unknown case of attempting to move cursor before the text that needs to be cut off. Requested cutoff: {newOffset}. New Position: {newPosition}");
// Gracefully handle as best we can in release
return (newText.Substring(newOffset), InsertTextFormat.PlainText);
}
// Roslyn wants to move the cursor somewhere inside the result. Substring from the
// requested start to the new position, and from the new position to the end of the
// string.
int midpoint = newPosition - change.TextChange.Span.Start;
var beforeText = LspSnippetHelpers.Escape(newText.Substring(newOffset, midpoint - newOffset));
var afterText = LspSnippetHelpers.Escape(newText.Substring(midpoint));
return (beforeText + "$0" + afterText, InsertTextFormat.Snippet);
}
}
public async Task<CompletionResolveResponse> Handle(CompletionResolveRequest request)
{
if (_lastCompletion is null)
{
_logger.LogError("Cannot call completion/resolve before calling completion!");
return new CompletionResolveResponse { Item = request.Item };
}
var (completions, fileName, position) = _lastCompletion.Value;
if (request.Item is null
|| request.Item.Data >= completions.Items.Length
|| request.Item.Data < 0)
{
_logger.LogError("Received invalid completion resolve!");
return new CompletionResolveResponse { Item = request.Item };
}
var lastCompletionItem = completions.Items[request.Item.Data];
if (lastCompletionItem.DisplayTextPrefix + lastCompletionItem.DisplayText + lastCompletionItem.DisplayTextSuffix != request.Item.Label)
{
_logger.LogError($"Inconsistent completion data. Requested data on {request.Item.Label}, but found completion item {lastCompletionItem.DisplayText}");
return new CompletionResolveResponse { Item = request.Item };
}
var document = _workspace.GetDocument(fileName);
if (document is null)
{
_logger.LogInformation("Could not find document for file {0}", fileName);
return new CompletionResolveResponse { Item = request.Item };
}
var completionService = CSharpCompletionService.GetService(document);
var description = await completionService.GetDescriptionAsync(document, lastCompletionItem);
StringBuilder textBuilder = new StringBuilder();
MarkdownHelpers.TaggedTextToMarkdown(description.TaggedParts, textBuilder, _formattingOptions, MarkdownFormat.FirstLineAsCSharp, out _);
request.Item.Documentation = textBuilder.ToString();
string providerName = lastCompletionItem.GetProviderName();
switch (providerName)
{
case CompletionItemExtensions.ExtensionMethodImportCompletionProvider:
case CompletionItemExtensions.TypeImportCompletionProvider:
var syntax = await document.GetSyntaxTreeAsync();
var sourceText = await document.GetTextAsync();
var typedSpan = completionService.GetDefaultCompletionListSpan(sourceText, position);
var change = await completionService.GetChangeAsync(document, lastCompletionItem, typedSpan);
(request.Item.AdditionalTextEdits, _) = await GetAdditionalTextEdits(change, sourceText, (CSharpParseOptions)syntax!.Options, typedSpan, lastCompletionItem.DisplayText, providerName);
break;
}
return new CompletionResolveResponse
{
Item = request.Item
};
}
private async ValueTask<(IReadOnlyList<LinePositionSpanTextChange> edits, int endOffset)> GetAdditionalTextEdits(
CompletionChange change,
SourceText sourceText,
CSharpParseOptions parseOptions,
TextSpan typedSpan,
string completionDisplayText,
string providerName)
{
// We know the span starts before the text we're keying off of. So, break that
// out into a separate edit. We need to cut out the space before the current word,
// as the additional edit is not allowed to overlap with the insertion point.
var additionalEditStartPosition = sourceText.Lines.GetLinePosition(change.TextChange.Span.Start);
var additionalEditEndPosition = sourceText.Lines.GetLinePosition(typedSpan.Start - 1);
int additionalEditEndOffset = await getAdditionalTextEditEndOffset(change, sourceText, parseOptions, typedSpan, completionDisplayText, providerName);
if (additionalEditEndOffset < 1)
{
// The first index of this was either 0 and the edit span was wrong,
// or it wasn't found at all. In this case, just do the best we can:
// send the whole string wtih no additional edits and log a warning.
_logger.LogWarning("Could not find the first index of the display text.\nDisplay text: {0}.\nCompletion Text: {1}",
completionDisplayText, change.TextChange.NewText);
return default;
}
return (ImmutableArray.Create(new LinePositionSpanTextChange
{
// Again, we cut off the space at the end of the offset
NewText = change.TextChange.NewText!.Substring(0, additionalEditEndOffset - 1),
StartLine = additionalEditStartPosition.Line,
StartColumn = additionalEditStartPosition.Character,
EndLine = additionalEditEndPosition.Line,
EndColumn = additionalEditEndPosition.Character,
}), additionalEditEndOffset);
static async ValueTask<int> getAdditionalTextEditEndOffset(CompletionChange change, SourceText sourceText, CSharpParseOptions parseOptions, TextSpan typedSpan, string completionDisplayText, string providerName)
{
// For many simple cases, we can just find the first or last index of the completionDisplayText and that's good
// enough
int endOffset = (providerName == CompletionItemExtensions.ExtensionMethodImportCompletionProvider ||
providerName == CompletionItemExtensions.TypeImportCompletionProvider)
// Import completion will put the displaytext at the end of the line, override completion will
// put it at the front.
? change.TextChange.NewText!.LastIndexOf(completionDisplayText)
: change.TextChange.NewText!.IndexOf(completionDisplayText);
if (endOffset > -1)
{
return endOffset;
}
// The DisplayText wasn't in the final string. This can happen in a few cases:
// * The override or partial method completion is involving types that need
// to have a using added in the final version, and won't be fully qualified
// as they were in the DisplayText
// * Nullable context differences, such as if the thing you're overriding is
// annotated but the final context being generated into does not have
// annotations enabled.
// For these cases, we currently should only be seeing override or partial
// completions, as import completions don't have nullable annotations or
// fully-qualified types in their DisplayTexts. If that ever changes, we'll have
// to adjust the API here.
//
// In order to find the correct location here, we parse the change. The location
// of the name of the last method is the correct location in the string.
Debug.Assert(providerName == CompletionItemExtensions.OverrideCompletionProvider ||
providerName == CompletionItemExtensions.PartialMethodCompletionProvider);
var parsedTree = CSharpSyntaxTree.ParseText(change.TextChange.NewText, parseOptions);
var lastMethodDecl = (await parsedTree.GetRootAsync()).DescendantNodes().OfType<MethodDeclarationSyntax>().Last();
return lastMethodDecl.Identifier.SpanStart;
}
}
}
}