diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs index 868d3af76..1be623782 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs @@ -21,12 +21,17 @@ internal class PsesDocumentSymbolHandler : DocumentSymbolHandlerBase private static readonly SymbolInformationOrDocumentSymbolContainer s_emptySymbolInformationOrDocumentSymbolContainer = new(); private readonly ILogger _logger; private readonly WorkspaceService _workspaceService; + private readonly ConfigurationService _configurationService; private readonly IDocumentSymbolProvider[] _providers; - public PsesDocumentSymbolHandler(ILoggerFactory factory, WorkspaceService workspaceService) + public PsesDocumentSymbolHandler( + ILoggerFactory factory, + WorkspaceService workspaceService, + ConfigurationService configurationService) { _logger = factory.CreateLogger(); _workspaceService = workspaceService; + _configurationService = configurationService; _providers = new IDocumentSymbolProvider[] { new ScriptDocumentSymbolProvider(), @@ -166,7 +171,13 @@ public override async Task Handle(Do // // TODO: We should also include function invocations that are part of DSLs (like // Invoke-Build etc.). - if (!symbolReference.IsDeclaration || symbolReference.Type is SymbolType.Parameter) + if (!symbolReference.IsDeclaration) + { + continue; + } + + if (symbolReference.Type is SymbolType.Parameter && + !_configurationService.CurrentSettings.EnableParameterOutline) { continue; } diff --git a/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs b/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs index d310903ae..377f415cb 100644 --- a/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs +++ b/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs @@ -24,7 +24,7 @@ internal class LanguageServerSettings public string Cwd { get; set; } public bool EnableReferencesCodeLens { get; set; } = true; public bool AnalyzeOpenDocumentsOnly { get; set; } - + public bool EnableParameterOutline { get; set; } public LanguageServerSettings() { ScriptAnalysis = new ScriptAnalysisSettings(); @@ -50,6 +50,7 @@ public void Update( Cwd = settings.Cwd; EnableReferencesCodeLens = settings.EnableReferencesCodeLens; AnalyzeOpenDocumentsOnly = settings.AnalyzeOpenDocumentsOnly; + EnableParameterOutline = settings.EnableParameterOutline; } } } diff --git a/test/PowerShellEditorServices.Test.Shared/Symbols/DocumentSymbolParameters.ps1 b/test/PowerShellEditorServices.Test.Shared/Symbols/DocumentSymbolParameters.ps1 new file mode 100644 index 000000000..496d4a677 --- /dev/null +++ b/test/PowerShellEditorServices.Test.Shared/Symbols/DocumentSymbolParameters.ps1 @@ -0,0 +1,6 @@ +function New-MyFunc { + param( + [string]$MyString, + $MyLooselyTypedParam + ) +} diff --git a/test/PowerShellEditorServices.Test/Language/DocumentSymbolHandlerTests.cs b/test/PowerShellEditorServices.Test/Language/DocumentSymbolHandlerTests.cs new file mode 100644 index 000000000..b790b9c68 --- /dev/null +++ b/test/PowerShellEditorServices.Test/Language/DocumentSymbolHandlerTests.cs @@ -0,0 +1,94 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.PowerShell.EditorServices.Handlers; +using Microsoft.PowerShell.EditorServices.Services; +using Microsoft.PowerShell.EditorServices.Test.Shared; +using OmniSharp.Extensions.LanguageServer.Protocol; +using OmniSharp.Extensions.LanguageServer.Protocol.Models; +using Xunit; + +namespace PowerShellEditorServices.Test.Language +{ + [Trait("Category", "Symbols")] + public class DocumentSymbolHandlerTests + { + private readonly WorkspaceService workspace; + private readonly ConfigurationService configuration; + private readonly PsesDocumentSymbolHandler documentSymbolHandler; + + public DocumentSymbolHandlerTests() + { + workspace = new WorkspaceService(NullLoggerFactory.Instance); + configuration = new ConfigurationService(); + documentSymbolHandler = new PsesDocumentSymbolHandler( + NullLoggerFactory.Instance, + workspace, + configuration); + } + + [Fact] + public async Task ExcludesParametersWhenParameterOutlineIsDisabled() + { + configuration.CurrentSettings.EnableParameterOutline = false; + + string filePath = TestUtilities.GetSharedPath( + "Symbols/DocumentSymbolParameters.ps1"); + + SymbolInformationOrDocumentSymbolContainer result = + await documentSymbolHandler.Handle( + new DocumentSymbolParams + { + TextDocument = new TextDocumentIdentifier( + DocumentUri.FromFileSystemPath(filePath)) + }, + CancellationToken.None); + + DocumentSymbol function = result + .Select(symbol => symbol.DocumentSymbol) + .Single(symbol => symbol.Name.Contains("New-MyFunc")); + + Assert.DoesNotContain( + function.Children, + symbol => symbol.Name.Contains("$MyString")); + + Assert.DoesNotContain( + function.Children, + symbol => symbol.Name.Contains("$MyLooselyTypedParam")); + } + + [Fact] + public async Task IncludesParametersWhenParameterOutlineIsEnabled() + { + configuration.CurrentSettings.EnableParameterOutline = true; + + string filePath = TestUtilities.GetSharedPath( + "Symbols/DocumentSymbolParameters.ps1"); + + SymbolInformationOrDocumentSymbolContainer result = + await documentSymbolHandler.Handle( + new DocumentSymbolParams + { + TextDocument = new TextDocumentIdentifier( + DocumentUri.FromFileSystemPath(filePath)) + }, + CancellationToken.None); + + DocumentSymbol function = result + .Select(symbol => symbol.DocumentSymbol) + .Single(symbol => symbol.Name.Contains("New-MyFunc")); + + Assert.Contains( + function.Children, + symbol => symbol.Name.Contains("$MyString")); + + Assert.Contains( + function.Children, + symbol => symbol.Name.Contains("$MyLooselyTypedParam")); + } + } +} \ No newline at end of file