Skip to content

Commit 5b1d1d5

Browse files
Feature Request: Expose parameters to the Outline View #5153
1 parent ba8b420 commit 5b1d1d5

4 files changed

Lines changed: 115 additions & 3 deletions

File tree

‎src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ internal class PsesDocumentSymbolHandler : DocumentSymbolHandlerBase
2121
private static readonly SymbolInformationOrDocumentSymbolContainer s_emptySymbolInformationOrDocumentSymbolContainer = new();
2222
private readonly ILogger _logger;
2323
private readonly WorkspaceService _workspaceService;
24+
private readonly ConfigurationService _configurationService;
2425
private readonly IDocumentSymbolProvider[] _providers;
2526

26-
public PsesDocumentSymbolHandler(ILoggerFactory factory, WorkspaceService workspaceService)
27+
public PsesDocumentSymbolHandler(
28+
ILoggerFactory factory,
29+
WorkspaceService workspaceService,
30+
ConfigurationService configurationService)
2731
{
2832
_logger = factory.CreateLogger<PsesDocumentSymbolHandler>();
2933
_workspaceService = workspaceService;
34+
_configurationService = configurationService;
3035
_providers = new IDocumentSymbolProvider[]
3136
{
3237
new ScriptDocumentSymbolProvider(),
@@ -166,7 +171,13 @@ public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(Do
166171
//
167172
// TODO: We should also include function invocations that are part of DSLs (like
168173
// Invoke-Build etc.).
169-
if (!symbolReference.IsDeclaration || symbolReference.Type is SymbolType.Parameter)
174+
if (!symbolReference.IsDeclaration)
175+
{
176+
continue;
177+
}
178+
179+
if (symbolReference.Type is SymbolType.Parameter &&
180+
!_configurationService.CurrentSettings.EnableParameterOutline)
170181
{
171182
continue;
172183
}

‎src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class LanguageServerSettings
2424
public string Cwd { get; set; }
2525
public bool EnableReferencesCodeLens { get; set; } = true;
2626
public bool AnalyzeOpenDocumentsOnly { get; set; }
27-
27+
public bool EnableParameterOutline { get; set; }
2828
public LanguageServerSettings()
2929
{
3030
ScriptAnalysis = new ScriptAnalysisSettings();
@@ -50,6 +50,7 @@ public void Update(
5050
Cwd = settings.Cwd;
5151
EnableReferencesCodeLens = settings.EnableReferencesCodeLens;
5252
AnalyzeOpenDocumentsOnly = settings.AnalyzeOpenDocumentsOnly;
53+
EnableParameterOutline = settings.EnableParameterOutline;
5354
}
5455
}
5556
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function New-MyFunc {
2+
param(
3+
[string]$MyString,
4+
$MyLooselyTypedParam
5+
)
6+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.Extensions.Logging.Abstractions;
8+
using Microsoft.PowerShell.EditorServices.Handlers;
9+
using Microsoft.PowerShell.EditorServices.Services;
10+
using Microsoft.PowerShell.EditorServices.Test.Shared;
11+
using OmniSharp.Extensions.LanguageServer.Protocol;
12+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
13+
using Xunit;
14+
15+
namespace PowerShellEditorServices.Test.Language
16+
{
17+
[Trait("Category", "Symbols")]
18+
public class DocumentSymbolHandlerTests
19+
{
20+
private readonly WorkspaceService workspace;
21+
private readonly ConfigurationService configuration;
22+
private readonly PsesDocumentSymbolHandler documentSymbolHandler;
23+
24+
public DocumentSymbolHandlerTests()
25+
{
26+
workspace = new WorkspaceService(NullLoggerFactory.Instance);
27+
configuration = new ConfigurationService();
28+
documentSymbolHandler = new PsesDocumentSymbolHandler(
29+
NullLoggerFactory.Instance,
30+
workspace,
31+
configuration);
32+
}
33+
34+
[Fact]
35+
public async Task ExcludesParametersWhenParameterOutlineIsDisabled()
36+
{
37+
configuration.CurrentSettings.EnableParameterOutline = false;
38+
39+
string filePath = TestUtilities.GetSharedPath(
40+
"Symbols/DocumentSymbolParameters.ps1");
41+
42+
SymbolInformationOrDocumentSymbolContainer result =
43+
await documentSymbolHandler.Handle(
44+
new DocumentSymbolParams
45+
{
46+
TextDocument = new TextDocumentIdentifier(
47+
DocumentUri.FromFileSystemPath(filePath))
48+
},
49+
CancellationToken.None);
50+
51+
DocumentSymbol function = result
52+
.Select(symbol => symbol.DocumentSymbol)
53+
.Single(symbol => symbol.Name.Contains("New-MyFunc"));
54+
55+
Assert.DoesNotContain(
56+
function.Children,
57+
symbol => symbol.Name.Contains("$MyString"));
58+
59+
Assert.DoesNotContain(
60+
function.Children,
61+
symbol => symbol.Name.Contains("$MyLooselyTypedParam"));
62+
}
63+
64+
[Fact]
65+
public async Task IncludesParametersWhenParameterOutlineIsEnabled()
66+
{
67+
configuration.CurrentSettings.EnableParameterOutline = true;
68+
69+
string filePath = TestUtilities.GetSharedPath(
70+
"Symbols/DocumentSymbolParameters.ps1");
71+
72+
SymbolInformationOrDocumentSymbolContainer result =
73+
await documentSymbolHandler.Handle(
74+
new DocumentSymbolParams
75+
{
76+
TextDocument = new TextDocumentIdentifier(
77+
DocumentUri.FromFileSystemPath(filePath))
78+
},
79+
CancellationToken.None);
80+
81+
DocumentSymbol function = result
82+
.Select(symbol => symbol.DocumentSymbol)
83+
.Single(symbol => symbol.Name.Contains("New-MyFunc"));
84+
85+
Assert.Contains(
86+
function.Children,
87+
symbol => symbol.Name.Contains("$MyString"));
88+
89+
Assert.Contains(
90+
function.Children,
91+
symbol => symbol.Name.Contains("$MyLooselyTypedParam"));
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)