@@ -14,25 +14,31 @@ namespace Microsoft.PowerShell.EditorServices
14
14
internal class FindDeclartionVisitor : AstVisitor
15
15
{
16
16
private SymbolReference symbolRef ;
17
+ private string variableName ;
17
18
18
19
public SymbolReference FoundDeclartion { get ; private set ; }
19
20
20
21
public FindDeclartionVisitor ( SymbolReference symbolRef )
21
22
{
22
23
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
+ }
23
29
}
24
30
25
31
/// <summary>
26
32
/// 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
28
34
/// SymbolType.Function and have the same name as the symbol
29
35
/// </summary>
30
36
/// <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,
32
38
/// or a decision to continue if it wasn't found</returns>
33
39
public override AstVisitAction VisitFunctionDefinition ( FunctionDefinitionAst functionDefinitionAst )
34
40
{
35
- // Get the start column number of the function name,
41
+ // Get the start column number of the function name,
36
42
// instead of the the start column of 'function' and create new extent for the functionName
37
43
int startColumnNumber =
38
44
functionDefinitionAst . Extent . Text . IndexOf (
@@ -62,27 +68,27 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
62
68
}
63
69
64
70
/// <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.
68
73
/// </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,
71
76
/// 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 )
73
78
{
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 ) )
76
85
{
77
- this . FoundDeclartion =
78
- new SymbolReference (
79
- SymbolType . Variable ,
80
- variableExpressionAst . Extent ) ;
81
-
82
- return AstVisitAction . StopVisit ;
86
+ return AstVisitAction . Continue ;
83
87
}
84
88
85
- return AstVisitAction . Continue ;
89
+ // TODO also find instances of set-variable
90
+ FoundDeclartion = new SymbolReference ( SymbolType . Variable , variableExprAst . Extent ) ;
91
+ return AstVisitAction . StopVisit ;
86
92
}
87
93
}
88
94
}
0 commit comments