-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathDocumentSymbolHandler.cs
178 lines (159 loc) · 7.36 KB
/
DocumentSymbolHandler.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Symbols;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace Microsoft.PowerShell.EditorServices.Handlers
{
internal class PsesDocumentSymbolHandler : DocumentSymbolHandlerBase
{
private static readonly SymbolInformationOrDocumentSymbolContainer s_emptySymbolInformationOrDocumentSymbolContainer = new();
private readonly ILogger _logger;
private readonly WorkspaceService _workspaceService;
private readonly IDocumentSymbolProvider[] _providers;
public PsesDocumentSymbolHandler(ILoggerFactory factory, WorkspaceService workspaceService)
{
_logger = factory.CreateLogger<PsesDocumentSymbolHandler>();
_workspaceService = workspaceService;
_providers = new IDocumentSymbolProvider[]
{
new ScriptDocumentSymbolProvider(),
new PsdDocumentSymbolProvider(),
new PesterDocumentSymbolProvider(),
new RegionDocumentSymbolProvider()
};
}
protected override DocumentSymbolRegistrationOptions CreateRegistrationOptions(DocumentSymbolCapability capability, ClientCapabilities clientCapabilities) => new()
{
DocumentSelector = LspUtils.PowerShellDocumentSelector
};
// This turns a flat list of symbols into a hierarchical list. It's ugly because we're
// dealing with records and so sadly must slowly copy and replace things whenever need to do
// a modification, but it seems to work.
private static async Task<List<DocumentSymbol>> SortDocumentSymbols(List<DocumentSymbol> symbols, CancellationToken cancellationToken)
{
// Sort by the start of the symbol definition.
symbols.Sort((x1, x2) => x1.Range.Start.CompareTo(x2.Range.Start));
List<DocumentSymbol> parents = new();
foreach (DocumentSymbol symbol in symbols)
{
// This async method is pretty dense with synchronous code
// so it's helpful to add some yields.
await Task.Yield();
if (cancellationToken.IsCancellationRequested)
{
return symbols;
}
// Base case.
if (parents.Count == 0)
{
parents.Add(symbol);
}
// Symbol starts after end of last symbol parsed.
else if (symbol.Range.Start > parents[parents.Count - 1].Range.End)
{
parents.Add(symbol);
}
// Find where it fits.
else
{
for (int i = 0; i < parents.Count; i++)
{
DocumentSymbol parent = parents[i];
if (parent.Range.Start <= symbol.Range.Start && symbol.Range.End <= parent.Range.End)
{
List<DocumentSymbol> children = new();
if (parent.Children is not null)
{
children.AddRange(parent.Children);
}
children.Add(symbol);
parents[i] = parent with { Children = children };
break;
}
}
}
}
// Recursively sort the children.
for (int i = 0; i < parents.Count; i++)
{
DocumentSymbol parent = parents[i];
if (parent.Children is not null)
{
List<DocumentSymbol> children = new(parent.Children);
children = await SortDocumentSymbols(children, cancellationToken).ConfigureAwait(false);
parents[i] = parent with { Children = children };
}
}
return parents;
}
// AKA the outline feature
public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSymbolParams request, CancellationToken cancellationToken)
{
_logger.LogDebug($"Handling document symbols for {request.TextDocument.Uri}");
ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
List<DocumentSymbol> symbols = new();
foreach (SymbolReference r in ProvideDocumentSymbols(scriptFile))
{
// This async method is pretty dense with synchronous code
// so it's helpful to add some yields.
await Task.Yield();
if (cancellationToken.IsCancellationRequested)
{
break;
}
// Outline view should only include declarations.
//
// TODO: We should also include function invocations that are part of DSLs (like
// Invoke-Build etc.).
if (!r.IsDeclaration || r.Type is SymbolType.Parameter)
{
continue;
}
// TODO: This now needs the Children property filled out to support hierarchical
// symbols, and we don't have the information nor algorithm to do that currently.
// OmniSharp was previously doing this for us based on the range, perhaps we can
// find that logic and reuse it.
symbols.Add(new DocumentSymbol
{
Kind = SymbolTypeUtils.GetSymbolKind(r.Type),
Range = r.ScriptRegion.ToRange(),
SelectionRange = r.NameRegion.ToRange(),
Name = r.Name
});
}
// Short-circuit if we have no symbols.
if (symbols.Count == 0)
{
return s_emptySymbolInformationOrDocumentSymbolContainer;
}
// Otherwise slowly sort them into a hierarchy.
symbols = await SortDocumentSymbols(symbols, cancellationToken).ConfigureAwait(false);
// And finally convert them to the silly SymbolInformationOrDocumentSymbol wrapper.
List<SymbolInformationOrDocumentSymbol> container = new();
foreach (DocumentSymbol symbol in symbols)
{
container.Add(new SymbolInformationOrDocumentSymbol(symbol));
}
return container;
}
private IEnumerable<SymbolReference> ProvideDocumentSymbols(ScriptFile scriptFile)
{
foreach (IDocumentSymbolProvider provider in _providers)
{
foreach (SymbolReference symbol in provider.ProvideDocumentSymbols(scriptFile))
{
yield return symbol;
}
}
}
}
}