-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathDocumentSymbolFeature.cs
115 lines (97 loc) · 4 KB
/
DocumentSymbolFeature.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.PowerShell.EditorServices.Components;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
using Microsoft.PowerShell.EditorServices.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Servers = Microsoft.PowerShell.EditorServices.Protocol.Server;
namespace Microsoft.PowerShell.EditorServices.Symbols
{
internal class DocumentSymbolFeature :
FeatureComponentBase<IDocumentSymbolProvider>,
IDocumentSymbols
{
private EditorSession editorSession;
public DocumentSymbolFeature(
EditorSession editorSession,
IMessageHandlers messageHandlers,
ILogger logger)
: base(logger)
{
this.editorSession = editorSession;
messageHandlers.SetRequestHandler(
DocumentSymbolRequest.Type,
this.HandleDocumentSymbolRequestAsync);
}
public static DocumentSymbolFeature Create(
IComponentRegistry components,
EditorSession editorSession)
{
var documentSymbols =
new DocumentSymbolFeature(
editorSession,
components.Get<IMessageHandlers>(),
components.Get<ILogger>());
documentSymbols.Providers.Add(
new ScriptDocumentSymbolProvider(
editorSession.PowerShellContext.LocalPowerShellVersion.Version));
documentSymbols.Providers.Add(
new PsdDocumentSymbolProvider());
documentSymbols.Providers.Add(
new PesterDocumentSymbolProvider());
editorSession.Components.Register<IDocumentSymbols>(documentSymbols);
return documentSymbols;
}
public IEnumerable<SymbolReference> ProvideDocumentSymbols(
ScriptFile scriptFile)
{
return
this.InvokeProviders(p => p.ProvideDocumentSymbols(scriptFile))
.SelectMany(r => r);
}
protected async Task HandleDocumentSymbolRequestAsync(
DocumentSymbolParams documentSymbolParams,
RequestContext<SymbolInformation[]> requestContext)
{
ScriptFile scriptFile =
editorSession.Workspace.GetFile(
documentSymbolParams.TextDocument.Uri);
IEnumerable<SymbolReference> foundSymbols =
this.ProvideDocumentSymbols(scriptFile);
SymbolInformation[] symbols = null;
string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);
if (foundSymbols != null)
{
symbols =
foundSymbols
.Select(r =>
{
return new SymbolInformation
{
ContainerName = containerName,
Kind = Servers.LanguageServer.GetSymbolKind(r.SymbolType),
Location = new Location
{
Uri = Servers.LanguageServer.GetFileUri(r.FilePath),
Range = Servers.LanguageServer.GetRangeFromScriptRegion(r.ScriptRegion)
},
Name = Servers.LanguageServer.GetDecoratedSymbolName(r)
};
})
.ToArray();
}
else
{
symbols = new SymbolInformation[0];
}
await requestContext.SendResultAsync(symbols);
}
}
}