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);