Skip to content

Commit 7419af8

Browse files
author
Kapil Borle
committed
Fix finding variable definitions
1 parent c01145c commit 7419af8

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/PowerShellEditorServices/Language/FindDeclartionVisitor.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@ namespace Microsoft.PowerShell.EditorServices
1414
internal class FindDeclartionVisitor : AstVisitor
1515
{
1616
private SymbolReference symbolRef;
17+
private string variableName;
1718

1819
public SymbolReference FoundDeclartion{ get; private set; }
1920

2021
public FindDeclartionVisitor(SymbolReference symbolRef)
2122
{
2223
this.symbolRef = symbolRef;
24+
if (this.symbolRef.SymbolType == SymbolType.Variable)
25+
{
26+
// converts `$varName` to `varName` or of the form ${varName} to varName
27+
variableName = symbolRef.SymbolName.TrimStart('$').Trim('{', '}');
28+
}
2329
}
2430

2531
/// <summary>
2632
/// Decides if the current function defintion is the right defition
27-
/// for the symbol being searched for. The defintion of the symbol will be a of type
33+
/// for the symbol being searched for. The defintion of the symbol will be a of type
2834
/// SymbolType.Function and have the same name as the symbol
2935
/// </summary>
3036
/// <param name="functionDefinitionAst">A FunctionDefinitionAst in the script's AST</param>
31-
/// <returns>A descion to stop searching if the right FunctionDefinitionAst was found,
37+
/// <returns>A descion to stop searching if the right FunctionDefinitionAst was found,
3238
/// or a decision to continue if it wasn't found</returns>
3339
public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst)
3440
{
35-
// Get the start column number of the function name,
41+
// Get the start column number of the function name,
3642
// instead of the the start column of 'function' and create new extent for the functionName
3743
int startColumnNumber =
3844
functionDefinitionAst.Extent.Text.IndexOf(
@@ -62,27 +68,27 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
6268
}
6369

6470
/// <summary>
65-
/// Decides if the current variable expression is the right defition for
66-
/// the symbol being searched for. The defintion of the symbol will be a of type
67-
/// SymbolType.Variable and have the same name as the symbol
71+
/// Check if the left hand side of an assignmentStatementAst is a VariableExpressionAst
72+
/// with the same name as that of symbolRef.
6873
/// </summary>
69-
/// <param name="variableExpressionAst">A FunctionDefinitionAst in the script's AST</param>
70-
/// <returns>A descion to stop searching if the right VariableExpressionAst was found,
74+
/// <param name="assignmentStatementAst">An AssignmentStatementAst/param>
75+
/// <returns>A descion to stop searching if the right VariableExpressionAst was found,
7176
/// or a decision to continue if it wasn't found</returns>
72-
public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst)
77+
public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
7378
{
74-
if(symbolRef.SymbolType.Equals(SymbolType.Variable) &&
75-
variableExpressionAst.Extent.Text.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
79+
var variableExprAst = assignmentStatementAst.Left as VariableExpressionAst;
80+
if (variableExprAst == null ||
81+
variableName == null ||
82+
!variableExprAst.VariablePath.UserPath.Equals(
83+
variableName,
84+
StringComparison.OrdinalIgnoreCase))
7685
{
77-
this.FoundDeclartion =
78-
new SymbolReference(
79-
SymbolType.Variable,
80-
variableExpressionAst.Extent);
81-
82-
return AstVisitAction.StopVisit;
86+
return AstVisitAction.Continue;
8387
}
8488

85-
return AstVisitAction.Continue;
89+
// TODO also find instances of set-variable
90+
FoundDeclartion = new SymbolReference(SymbolType.Variable, variableExprAst.Extent);
91+
return AstVisitAction.StopVisit;
8692
}
8793
}
8894
}

src/PowerShellEditorServices/Language/SymbolReference.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using System;
67
using System.Diagnostics;
78
using System.Management.Automation.Language;
89

0 commit comments

Comments
 (0)