From bb8a0e3b1410ca297ba3343d8ca72c4b7889adbc Mon Sep 17 00:00:00 2001 From: Andy Jordan Date: Tue, 7 Feb 2023 12:54:43 -0800 Subject: [PATCH] Keep only first assignment as declaration --- .../Services/Symbols/ReferenceTable.cs | 9 +++++++++ .../References/SimpleFile.ps1 | 2 +- .../Language/SymbolsServiceTests.cs | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs index 620fe2ea1..6cb8e52c4 100644 --- a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs +++ b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs @@ -94,6 +94,15 @@ private AstVisitAction AddReference(SymbolReference symbol) _ => new ConcurrentBag { symbol }, (_, existing) => { + // Keep only the first variable encountered as a declaration marked as such. This + // keeps the first assignment without also counting every reassignment as a + // declaration (cleaning up e.g. Code's outline view). + if (symbol.Type is SymbolType.Variable && symbol.IsDeclaration + && existing.Any(i => i.IsDeclaration)) + { + symbol = symbol with { IsDeclaration = false }; + } + existing.Add(symbol); return existing; }); diff --git a/test/PowerShellEditorServices.Test.Shared/References/SimpleFile.ps1 b/test/PowerShellEditorServices.Test.Shared/References/SimpleFile.ps1 index b60389c63..64f3d0f43 100644 --- a/test/PowerShellEditorServices.Test.Shared/References/SimpleFile.ps1 +++ b/test/PowerShellEditorServices.Test.Shared/References/SimpleFile.ps1 @@ -5,7 +5,7 @@ function My-Function ($myInput) $things = 4 -$things +$things = 3 My-Function $things diff --git a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs index 53993a484..6fe20e407 100644 --- a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs @@ -263,7 +263,8 @@ public async Task FindsFunctionDefinitionInWorkspace() [Fact] public async Task FindsVariableDefinition() { - SymbolReference symbol = await GetDefinition(FindsVariableDefinitionData.SourceDetails).ConfigureAwait(true); + IEnumerable definitions = await GetDefinitions(FindsVariableDefinitionData.SourceDetails).ConfigureAwait(true); + SymbolReference symbol = Assert.Single(definitions); // Even though it's re-assigned Assert.Equal("var things", symbol.Id); Assert.Equal("$things", symbol.Name); Assert.Equal(SymbolType.Variable, symbol.Type);