-
Notifications
You must be signed in to change notification settings - Fork 419
/
CodeStructureService.cs
370 lines (315 loc) · 14.6 KB
/
CodeStructureService.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
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Extensions;
using OmniSharp.Mef;
using OmniSharp.Models.V2;
using OmniSharp.Models.V2.CodeStructure;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Services;
namespace OmniSharp.Roslyn.CSharp.Services.Structure
{
[OmniSharpHandler(OmniSharpEndpoints.V2.CodeStructure, LanguageNames.CSharp)]
public class CodeStructureService : IRequestHandler<CodeStructureRequest, CodeStructureResponse>
{
private readonly OmniSharpWorkspace _workspace;
private readonly IEnumerable<ICodeElementPropertyProvider> _propertyProviders;
[ImportingConstructor]
public CodeStructureService(
OmniSharpWorkspace workspace,
[ImportMany] IEnumerable<ICodeElementPropertyProvider> propertyProviders)
{
_workspace = workspace;
_propertyProviders = propertyProviders;
}
public async Task<CodeStructureResponse> Handle(CodeStructureRequest request)
{
var document = _workspace.GetDocument(request.FileName);
if (document == null)
{
return null;
}
var elements = await GetCodeElementsAsync(document);
var response = new CodeStructureResponse
{
Elements = elements
};
return response;
}
private async Task<IReadOnlyList<CodeElement>> GetCodeElementsAsync(Document document)
{
var text = await document.GetTextAsync();
var syntaxRoot = await document.GetSyntaxRootAsync();
var semanticModel = await document.GetSemanticModelAsync();
var results = ImmutableList.CreateBuilder<CodeElement>();
foreach (var node in ((CompilationUnitSyntax)syntaxRoot).Members)
{
foreach (var element in CreateCodeElements(node, text, semanticModel))
{
if (element != null)
{
results.Add(element);
}
}
}
return results.ToImmutable();
}
private IEnumerable<CodeElement> CreateCodeElements(SyntaxNode node, SourceText text, SemanticModel semanticModel)
{
switch (node)
{
case TypeDeclarationSyntax typeDeclaration:
yield return CreateCodeElement(typeDeclaration, text, semanticModel);
break;
case DelegateDeclarationSyntax delegateDeclaration:
yield return CreateCodeElement(delegateDeclaration, text, semanticModel);
break;
case EnumDeclarationSyntax enumDeclaration:
yield return CreateCodeElement(enumDeclaration, text, semanticModel);
break;
case NamespaceDeclarationSyntax namespaceDeclaration:
yield return CreateCodeElement(namespaceDeclaration, text, semanticModel);
break;
case BaseMethodDeclarationSyntax baseMethodDeclaration:
yield return CreateCodeElement(baseMethodDeclaration, text, semanticModel);
break;
case BasePropertyDeclarationSyntax basePropertyDeclaration:
yield return CreateCodeElement(basePropertyDeclaration, text, semanticModel);
break;
case BaseFieldDeclarationSyntax baseFieldDeclaration:
foreach (var variableDeclarator in baseFieldDeclaration.Declaration.Variables)
{
yield return CreateCodeElement(variableDeclarator, baseFieldDeclaration, text, semanticModel);
}
break;
case EnumMemberDeclarationSyntax enumMemberDeclarationSyntax:
yield return CreateCodeElement(enumMemberDeclarationSyntax, text, semanticModel);
break;
}
}
private CodeElement CreateCodeElement(TypeDeclarationSyntax typeDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(typeDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortTypeFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.TypeFormat)
};
AddRanges(builder, typeDeclaration.AttributeLists.Span, typeDeclaration.Span, typeDeclaration.Identifier.Span, text);
AddSymbolProperties(symbol, builder);
foreach (var member in typeDeclaration.Members)
{
foreach (var childElement in CreateCodeElements(member, text, semanticModel))
{
builder.AddChild(childElement);
}
}
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(DelegateDeclarationSyntax delegateDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(delegateDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortTypeFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.TypeFormat),
};
AddRanges(builder, delegateDeclaration.AttributeLists.Span, delegateDeclaration.Span, delegateDeclaration.Identifier.Span, text);
AddSymbolProperties(symbol, builder);
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(EnumDeclarationSyntax enumDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(enumDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortTypeFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.TypeFormat),
};
AddRanges(builder, enumDeclaration.AttributeLists.Span, enumDeclaration.Span, enumDeclaration.Identifier.Span, text);
AddSymbolProperties(symbol, builder);
foreach (var member in enumDeclaration.Members)
{
foreach (var childElement in CreateCodeElements(member, text, semanticModel))
{
builder.AddChild(childElement);
}
}
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(NamespaceDeclarationSyntax namespaceDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(namespaceDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortTypeFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.TypeFormat),
};
AddRanges(builder, attributesSpan: default, namespaceDeclaration.Span, namespaceDeclaration.Name.Span, text);
foreach (var member in namespaceDeclaration.Members)
{
foreach (var childElement in CreateCodeElements(member, text, semanticModel))
{
builder.AddChild(childElement);
}
}
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(BaseMethodDeclarationSyntax baseMethodDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(baseMethodDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortMemberFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.MemberFormat),
};
AddRanges(builder, baseMethodDeclaration.AttributeLists.Span, baseMethodDeclaration.Span, GetNameSpan(baseMethodDeclaration), text);
AddSymbolProperties(symbol, builder);
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(BasePropertyDeclarationSyntax basePropertyDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(basePropertyDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortMemberFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.MemberFormat),
};
AddRanges(builder, basePropertyDeclaration.AttributeLists.Span, basePropertyDeclaration.Span, GetNameSpan(basePropertyDeclaration), text);
AddSymbolProperties(symbol, builder);
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(VariableDeclaratorSyntax variableDeclarator, BaseFieldDeclarationSyntax baseFieldDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(variableDeclarator);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortMemberFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.MemberFormat),
};
AddRanges(builder, baseFieldDeclaration.AttributeLists.Span, variableDeclarator.Span, variableDeclarator.Identifier.Span, text);
AddSymbolProperties(symbol, builder);
return builder.ToCodeElement();
}
private CodeElement CreateCodeElement(EnumMemberDeclarationSyntax enumMemberDeclaration, SourceText text, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(enumMemberDeclaration);
if (symbol == null)
{
return null;
}
var builder = new CodeElement.Builder
{
Kind = symbol.GetKindString(),
Name = symbol.ToDisplayString(SymbolDisplayFormats.ShortMemberFormat),
DisplayName = symbol.ToDisplayString(SymbolDisplayFormats.MemberFormat),
};
AddRanges(builder, enumMemberDeclaration.AttributeLists.Span, enumMemberDeclaration.Span, enumMemberDeclaration.Identifier.Span, text);
AddSymbolProperties(symbol, builder);
return builder.ToCodeElement();
}
private static TextSpan GetNameSpan(BaseMethodDeclarationSyntax baseMethodDeclaration)
{
switch (baseMethodDeclaration)
{
case MethodDeclarationSyntax methodDeclaration:
return methodDeclaration.Identifier.Span;
case ConstructorDeclarationSyntax constructorDeclaration:
return constructorDeclaration.Identifier.Span;
case DestructorDeclarationSyntax destructorDeclaration:
return destructorDeclaration.Identifier.Span;
case OperatorDeclarationSyntax operatorDeclaration:
return operatorDeclaration.OperatorToken.Span;
case ConversionOperatorDeclarationSyntax conversionOperatorDeclaration:
return conversionOperatorDeclaration.Type.Span;
default:
return default;
}
}
private static TextSpan GetNameSpan(BasePropertyDeclarationSyntax basePropertyDeclaration)
{
switch (basePropertyDeclaration)
{
case PropertyDeclarationSyntax propertyDeclaration:
return propertyDeclaration.Identifier.Span;
case EventDeclarationSyntax eventDeclaration:
return eventDeclaration.Identifier.Span;
case IndexerDeclarationSyntax indexerDeclaration:
return indexerDeclaration.ThisKeyword.Span;
default:
return default;
}
}
private static void AddRanges(CodeElement.Builder builder, TextSpan attributesSpan, TextSpan fullSpan, TextSpan nameSpan, SourceText text)
{
if (attributesSpan != default)
{
builder.AddRange(SymbolRangeNames.Attributes, text.GetRangeFromSpan(attributesSpan));
}
if (fullSpan != default)
{
builder.AddRange(SymbolRangeNames.Full, text.GetRangeFromSpan(fullSpan));
}
if (nameSpan != default)
{
builder.AddRange(SymbolRangeNames.Name, text.GetRangeFromSpan(nameSpan));
}
}
private void AddSymbolProperties(ISymbol symbol, CodeElement.Builder builder)
{
var accessibility = symbol.GetAccessibilityString();
if (accessibility != null)
{
builder.AddProperty(SymbolPropertyNames.Accessibility, accessibility);
}
builder.AddProperty(SymbolPropertyNames.Static, symbol.IsStatic);
foreach (var propertyProvider in _propertyProviders)
{
foreach (var (name, value) in propertyProvider.ProvideProperties(symbol))
{
builder.AddProperty(name, value);
}
}
}
}
}