Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
));
}

// Traverse the parents to determine if this is a declaration.
static bool isDeclaration(Ast current)
{
Ast next = current.Parent;
while (true)
{
// Should come from an assignment statement.
if (next is AssignmentStatementAst assignment)
{
return assignment.Left == current;
}
// Or we might have type constraints or attributes to traverse first.
if (next is not ConvertExpressionAst or not AttributedExpressionAst)
{
return false;
}
current = next;
next = next.Parent;
}
}

// TODO: Consider tracking unscoped variable references only when they're declared within
// the same function definition.
return _action(new SymbolReference(
Expand All @@ -119,7 +140,7 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
variableExpressionAst.Extent,
variableExpressionAst.Extent, // TODO: Maybe parent?
_file,
isDeclaration: variableExpressionAst.Parent is AssignmentStatementAst or ParameterAst));
isDeclaration(variableExpressionAst)));
}

public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinitionAst)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerShell.EditorServices.Services.TextDocument;

namespace Microsoft.PowerShell.EditorServices.Test.Shared.Definition
{
public static class FindsTypedVariableDefinitionData
{
public static readonly ScriptRegion SourceDetails = new(
file: TestUtilities.NormalizePath("References/SimpleFile.ps1"),
text: string.Empty,
startLineNumber: 25,
startColumnNumber: 13,
startOffset: 0,
endLineNumber: 0,
endColumnNumber: 0,
endOffset: 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ Get-ChildItem
My-Alias

Invoke-Command -ScriptBlock ${Function:My-Function}

[string]$hello = "test"
Write-Host $hello
12 changes: 12 additions & 0 deletions test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ public async Task FindsVariableDefinition()
AssertIsRegion(symbol.NameRegion, 6, 1, 6, 8);
}

[Fact]
public async Task FindsTypedVariableDefinition()
{
IEnumerable<SymbolReference> definitions = await GetDefinitions(FindsTypedVariableDefinitionData.SourceDetails).ConfigureAwait(true);
SymbolReference symbol = Assert.Single(definitions);
Assert.Equal("var hello", symbol.Id);
Assert.Equal("$hello", symbol.Name);
Assert.Equal(SymbolType.Variable, symbol.Type);
Assert.True(symbol.IsDeclaration);
AssertIsRegion(symbol.NameRegion, 24, 9, 24, 15);
}

[Fact]
public async Task FindsReferencesOnVariable()
{
Expand Down