-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathIntellisenseService.cs
220 lines (188 loc) · 10.4 KB
/
IntellisenseService.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
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Completion;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Recommendations;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Extensions;
using OmniSharp.Mef;
using OmniSharp.Models.AutoComplete;
using OmniSharp.Options;
using OmniSharp.Roslyn.CSharp.Services.Documentation;
using OmniSharp.Roslyn.CSharp.Services.Completion;
using CompletionService = Microsoft.CodeAnalysis.Completion.CompletionService;
using System.Threading;
namespace OmniSharp.Roslyn.CSharp.Services.Intellisense
{
[Obsolete("Please use CompletionService.")]
[OmniSharpHandler(OmniSharpEndpoints.AutoComplete, LanguageNames.CSharp)]
public class IntellisenseService : IRequestHandler<AutoCompleteRequest, IEnumerable<AutoCompleteResponse>>
{
private readonly OmniSharpWorkspace _workspace;
private readonly FormattingOptions _formattingOptions;
private readonly OmniSharpOptions _omniSharpOptions;
[ImportingConstructor]
public IntellisenseService(OmniSharpWorkspace workspace, FormattingOptions formattingOptions, OmniSharpOptions omniSharpOptions)
{
_workspace = workspace;
_formattingOptions = formattingOptions;
_omniSharpOptions = omniSharpOptions;
}
public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest request)
{
var documents = _workspace.GetDocuments(request.FileName);
var wordToComplete = request.WordToComplete;
var completions = new HashSet<AutoCompleteResponse>();
foreach (var document in documents)
{
var sourceText = await document.GetTextAsync();
var position = sourceText.GetTextPosition(request);
var service = CompletionService.GetService(document);
var options = new OmniSharpCompletionOptions(
ShowItemsFromUnimportedNamespaces: _omniSharpOptions.RoslynExtensionsOptions.EnableImportCompletion,
ForceExpandedCompletionIndexCreation: false);
var completionList = await OmniSharpCompletionService.GetCompletionsAsync(service, document, position, trigger: default, roles: null, options, CancellationToken.None);
if (completionList != null)
{
// Only trigger on space if Roslyn has object creation items
if (request.TriggerCharacter == " " && !completionList.Items.Any(i => i.GetProviderName() is CompletionListBuilder.ObjectCreationCompletionProvider))
{
return completions;
}
// get recommended symbols to match them up later with SymbolCompletionProvider
var semanticModel = await document.GetSemanticModelAsync();
var recommendedSymbols = (await Recommender.GetRecommendedSymbolsAtPositionAsync(semanticModel, position, _workspace)).ToArray();
var isSuggestionMode = completionList.SuggestionModeItem != null;
foreach (var item in completionList.Items)
{
var completionText = item.DisplayText;
var preselect = item.Rules.MatchPriority == MatchPriority.Preselect;
if (completionText.IsValidCompletionFor(wordToComplete))
{
var symbols = await item.GetCompletionSymbolsAsync(recommendedSymbols, document);
if (symbols.Any())
{
foreach (var symbol in symbols)
{
if (item.UseDisplayTextAsCompletionText())
{
completionText = item.DisplayText;
}
else if (item.TryGetInsertionText(out var insertionText))
{
completionText = insertionText;
}
else
{
completionText = symbol.Name;
}
if (symbol != null)
{
if (request.WantSnippet)
{
foreach (var completion in MakeSnippetedResponses(request, symbol, completionText, preselect, isSuggestionMode))
{
completions.Add(completion);
}
}
else
{
completions.Add(MakeAutoCompleteResponse(request, symbol, completionText, preselect, isSuggestionMode));
}
}
}
// if we had any symbols from the completion, we can continue, otherwise it means
// the completion didn't have an associated symbol so we'll add it manually
continue;
}
// for other completions, i.e. keywords or em, create a simple AutoCompleteResponse
var response = item.ToAutoCompleteResponse(request.WantKind, isSuggestionMode, preselect);
completions.Add(response);
}
}
}
}
return completions
.OrderByDescending(c => c.CompletionText.IsValidCompletionStartsWithExactCase(wordToComplete))
.ThenByDescending(c => c.CompletionText.IsValidCompletionStartsWithIgnoreCase(wordToComplete))
.ThenByDescending(c => c.CompletionText.IsCamelCaseMatch(wordToComplete))
.ThenByDescending(c => c.CompletionText.IsSubsequenceMatch(wordToComplete))
.ThenBy(c => c.DisplayText, StringComparer.OrdinalIgnoreCase)
.ThenBy(c => c.CompletionText, StringComparer.OrdinalIgnoreCase);
}
private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, ISymbol symbol, string completionText, bool preselect, bool isSuggestionMode)
{
return symbol switch
{
IMethodSymbol methodSymbol => MakeSnippetedResponses(request, methodSymbol, completionText, preselect, isSuggestionMode),
INamedTypeSymbol typeSymbol => MakeSnippetedResponses(request, typeSymbol, completionText, preselect, isSuggestionMode),
_ => new[] { MakeAutoCompleteResponse(request, symbol, completionText, preselect, isSuggestionMode) },
};
}
private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, IMethodSymbol methodSymbol, string completionText, bool preselect, bool isSuggestionMode)
{
var completions = new List<AutoCompleteResponse>();
if (methodSymbol.Parameters.Any(p => p.IsOptional))
{
completions.Add(MakeAutoCompleteResponse(request, methodSymbol, completionText, preselect, isSuggestionMode, includeOptionalParams: false));
}
completions.Add(MakeAutoCompleteResponse(request, methodSymbol, completionText, preselect, isSuggestionMode));
return completions;
}
private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, INamedTypeSymbol typeSymbol, string completionText, bool preselect, bool isSuggestionMode)
{
var completions = new List<AutoCompleteResponse>
{
MakeAutoCompleteResponse(request, typeSymbol, completionText, preselect, isSuggestionMode)
};
if (typeSymbol.TypeKind != TypeKind.Enum)
{
foreach (var ctor in typeSymbol.InstanceConstructors)
{
completions.Add(MakeAutoCompleteResponse(request, ctor, completionText, preselect, isSuggestionMode));
}
}
return completions;
}
private AutoCompleteResponse MakeAutoCompleteResponse(AutoCompleteRequest request, ISymbol symbol, string completionText, bool preselect, bool isSuggestionMode, bool includeOptionalParams = true)
{
var displayNameGenerator = new SnippetGenerator();
displayNameGenerator.IncludeMarkers = false;
displayNameGenerator.IncludeOptionalParameters = includeOptionalParams;
var response = new AutoCompleteResponse();
response.CompletionText = completionText;
// TODO: Do something more intelligent here
response.DisplayText = displayNameGenerator.Generate(symbol);
response.IsSuggestionMode = isSuggestionMode;
if (request.WantDocumentationForEveryCompletionResult)
{
response.Description = DocumentationConverter.ConvertDocumentation(symbol.GetDocumentationCommentXml(), _formattingOptions.NewLine);
}
if (request.WantReturnType)
{
response.ReturnType = ReturnTypeFormatter.GetReturnType(symbol);
}
if (request.WantKind)
{
response.Kind = symbol.GetKind();
}
if (request.WantSnippet)
{
var snippetGenerator = new SnippetGenerator();
snippetGenerator.IncludeMarkers = true;
snippetGenerator.IncludeOptionalParameters = includeOptionalParams;
response.Snippet = snippetGenerator.Generate(symbol);
}
if (request.WantMethodHeader)
{
response.MethodHeader = displayNameGenerator.Generate(symbol);
}
response.Preselect = preselect;
return response;
}
}
}