Skip to content

Commit ecdd1ac

Browse files
committed
Add IDocumentSymbols and IDocumentSymbolProvider interfaces and impls
This change adds the IDocumentSymbols and IDocumentSymbolProvider interfaces plus their implementations, moving the existing symbol provider classes over to this new model.
1 parent 19aee3b commit ecdd1ac

13 files changed

+319
-119
lines changed

src/PowerShellEditorServices.Host/EditorServicesHost.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
99
using Microsoft.PowerShell.EditorServices.Protocol.Server;
1010
using Microsoft.PowerShell.EditorServices.Session;
11+
using Microsoft.PowerShell.EditorServices.Symbols;
1112
using Microsoft.PowerShell.EditorServices.Utility;
1213
using System;
1314
using System.Collections.Generic;
@@ -346,6 +347,14 @@ private EditorSession CreateSession(
346347

347348
editorSession.StartSession(powerShellContext, hostUserInterface);
348349

350+
// TODO: Move component registrations elsewhere!
351+
editorSession.Components.Register(this.logger);
352+
editorSession.Components.Register(messageHandlers);
353+
editorSession.Components.Register(messageSender);
354+
editorSession.Components.Register(powerShellContext);
355+
356+
DocumentSymbolFeature.Create(editorSession.Components, editorSession);
357+
349358
return editorSession;
350359
}
351360

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Components;
7+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
8+
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
9+
using Microsoft.PowerShell.EditorServices.Utility;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.IO;
13+
using System.Linq;
14+
using System.Threading.Tasks;
15+
16+
using Servers = Microsoft.PowerShell.EditorServices.Protocol.Server;
17+
18+
namespace Microsoft.PowerShell.EditorServices.Symbols
19+
{
20+
internal class DocumentSymbolFeature : IDocumentSymbols
21+
{
22+
private EditorSession editorSession;
23+
24+
public IProviderCollection<IDocumentSymbolProvider> Providers { get; } =
25+
new ProviderCollection<IDocumentSymbolProvider>();
26+
27+
public DocumentSymbolFeature(
28+
EditorSession editorSession,
29+
IMessageHandlers messageHandlers,
30+
ILogger logger)
31+
{
32+
this.editorSession = editorSession;
33+
34+
messageHandlers.SetRequestHandler(
35+
DocumentSymbolRequest.Type,
36+
this.HandleDocumentSymbolRequest);
37+
}
38+
39+
public static DocumentSymbolFeature Create(
40+
IComponentRegistry components,
41+
EditorSession editorSession)
42+
{
43+
var documentSymbols =
44+
new DocumentSymbolFeature(
45+
editorSession,
46+
components.Get<IMessageHandlers>(),
47+
components.Get<ILogger>());
48+
49+
documentSymbols.Providers.Add(
50+
new ScriptDocumentSymbolProvider(
51+
editorSession.PowerShellContext.LocalPowerShellVersion.Version));
52+
53+
documentSymbols.Providers.Add(
54+
new PsdDocumentSymbolProvider());
55+
56+
documentSymbols.Providers.Add(
57+
new PesterDocumentSymbolProvider());
58+
59+
editorSession.Components.Register<IDocumentSymbols>(documentSymbols);
60+
61+
return documentSymbols;
62+
}
63+
64+
public IEnumerable<SymbolReference> ProvideDocumentSymbols(ScriptFile scriptFile)
65+
{
66+
throw new NotImplementedException();
67+
}
68+
69+
protected async Task HandleDocumentSymbolRequest(
70+
DocumentSymbolParams documentSymbolParams,
71+
RequestContext<SymbolInformation[]> requestContext)
72+
{
73+
ScriptFile scriptFile =
74+
editorSession.Workspace.GetFile(
75+
documentSymbolParams.TextDocument.Uri);
76+
77+
IEnumerable<SymbolReference> foundSymbols =
78+
this.Providers
79+
.SelectMany(
80+
provider => provider.ProvideDocumentSymbols(scriptFile));
81+
82+
SymbolInformation[] symbols = null;
83+
84+
string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);
85+
86+
if (foundSymbols != null)
87+
{
88+
symbols =
89+
foundSymbols
90+
.Select(r =>
91+
{
92+
return new SymbolInformation
93+
{
94+
ContainerName = containerName,
95+
Kind = Servers.LanguageServer.GetSymbolKind(r.SymbolType),
96+
Location = new Location
97+
{
98+
Uri = Servers.LanguageServer.GetFileUri(r.FilePath),
99+
Range = Servers.LanguageServer.GetRangeFromScriptRegion(r.ScriptRegion)
100+
},
101+
Name = Servers.LanguageServer.GetDecoratedSymbolName(r)
102+
};
103+
})
104+
.ToArray();
105+
}
106+
else
107+
{
108+
symbols = new SymbolInformation[0];
109+
}
110+
111+
await requestContext.SendResult(symbols);
112+
}
113+
}
114+
}

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public void Start()
100100
this.messageHandlers.SetRequestHandler(SignatureHelpRequest.Type, this.HandleSignatureHelpRequest);
101101
this.messageHandlers.SetRequestHandler(DocumentHighlightRequest.Type, this.HandleDocumentHighlightRequest);
102102
this.messageHandlers.SetRequestHandler(HoverRequest.Type, this.HandleHoverRequest);
103-
this.messageHandlers.SetRequestHandler(DocumentSymbolRequest.Type, this.HandleDocumentSymbolRequest);
104103
this.messageHandlers.SetRequestHandler(WorkspaceSymbolRequest.Type, this.HandleWorkspaceSymbolRequest);
105104
this.messageHandlers.SetRequestHandler(CodeActionRequest.Type, this.HandleCodeActionRequest);
106105

@@ -945,7 +944,7 @@ protected async Task HandleDocumentSymbolRequest(
945944
await requestContext.SendResult(symbols);
946945
}
947946

948-
private SymbolKind GetSymbolKind(SymbolType symbolType)
947+
public static SymbolKind GetSymbolKind(SymbolType symbolType)
949948
{
950949
switch (symbolType)
951950
{
@@ -959,7 +958,7 @@ private SymbolKind GetSymbolKind(SymbolType symbolType)
959958
}
960959
}
961960

962-
private string GetDecoratedSymbolName(SymbolReference symbolReference)
961+
public static string GetDecoratedSymbolName(SymbolReference symbolReference)
963962
{
964963
string name = symbolReference.SymbolName;
965964

@@ -1206,7 +1205,7 @@ await this.messageSender.SendEvent(
12061205

12071206
#region Helper Methods
12081207

1209-
private static string GetFileUri(string filePath)
1208+
public static string GetFileUri(string filePath)
12101209
{
12111210
// If the file isn't untitled, return a URI-style path
12121211
return
@@ -1215,7 +1214,7 @@ private static string GetFileUri(string filePath)
12151214
: filePath;
12161215
}
12171216

1218-
private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
1217+
public static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
12191218
{
12201219
return new Range
12211220
{

src/PowerShellEditorServices/Language/DocumentSymbolProvider.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices.Symbols;
67
using Microsoft.PowerShell.EditorServices.Utility;
78
using System;
89
using System.Collections;
@@ -33,7 +34,7 @@ public class LanguageService
3334
private string mostRecentRequestFile;
3435
private Dictionary<String, List<String>> CmdletToAliasDictionary;
3536
private Dictionary<String, String> AliasToCmdletDictionary;
36-
private DocumentSymbolProvider[] documentSymbolProviders;
37+
private IDocumentSymbolProvider[] documentSymbolProviders;
3738

3839
const int DefaultWaitTimeoutMilliseconds = 5000;
3940

@@ -60,10 +61,10 @@ public LanguageService(
6061

6162
this.CmdletToAliasDictionary = new Dictionary<String, List<String>>(StringComparer.OrdinalIgnoreCase);
6263
this.AliasToCmdletDictionary = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);
63-
this.documentSymbolProviders = new DocumentSymbolProvider[]
64+
this.documentSymbolProviders = new IDocumentSymbolProvider[]
6465
{
65-
new ScriptDocumentSymbolProvider(),
66-
new PSDDocumentSymbolProvider(),
66+
new ScriptDocumentSymbolProvider(powerShellContext.LocalPowerShellVersion.Version),
67+
new PsdDocumentSymbolProvider(),
6768
new PesterDocumentSymbolProvider()
6869
};
6970
}
@@ -242,7 +243,7 @@ public FindOccurrencesResult FindSymbolsInFile(ScriptFile scriptFile)
242243
return new FindOccurrencesResult
243244
{
244245
FoundOccurrences = documentSymbolProviders
245-
.SelectMany(p => p.GetSymbols(scriptFile, powerShellContext.LocalPowerShellVersion.Version))
246+
.SelectMany(p => p.ProvideDocumentSymbols(scriptFile))
246247
.Select(reference =>
247248
{
248249
reference.SourceLine =

src/PowerShellEditorServices/Language/PSDDocumentSymbolProvider.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/PowerShellEditorServices/Language/PesterDocumentSymbolProvider.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/PowerShellEditorServices/Language/ScriptDocumentSymbolProvider.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Collections.Generic;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Symbols
9+
{
10+
/// <summary>
11+
/// Specifies the contract for a document symbols provider.
12+
/// </summary>
13+
public interface IDocumentSymbolProvider : IProvider
14+
{
15+
/// <summary>
16+
/// Provides a list of symbols for the given document.
17+
/// </summary>
18+
/// <param name="scriptFile">
19+
/// The document for which SymbolReferences should be provided.
20+
/// </param>
21+
/// <returns>An IEnumerable collection of SymbolReferences.</returns>
22+
IEnumerable<SymbolReference> ProvideDocumentSymbols(ScriptFile scriptFile);
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Collections.Generic;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Symbols
9+
{
10+
/// <summary>
11+
/// Specifies the contract for an implementation of
12+
/// the IDocumentSymbols component.
13+
/// </summary>
14+
public interface IDocumentSymbols
15+
{
16+
/// <summary>
17+
/// Gets the collection of IDocumentSymbolsProvider implementations
18+
/// that are registered with this component.
19+
/// </summary>
20+
IProviderCollection<IDocumentSymbolProvider> Providers { get; }
21+
22+
/// <summary>
23+
/// Provides a list of symbols for the given document.
24+
/// </summary>
25+
/// <param name="scriptFile">
26+
/// The document for which SymbolReferences should be provided.
27+
/// </param>
28+
/// <returns>An IEnumerable collection of SymbolReferences.</returns>
29+
IEnumerable<SymbolReference> ProvideDocumentSymbols(ScriptFile scriptFile);
30+
}
31+
}

0 commit comments

Comments
 (0)