-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathFindDeclarationVisitor.cs
148 lines (130 loc) · 6.4 KB
/
FindDeclarationVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Language;
namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
/// The visitor used to find the definition of a symbol
/// </summary>
internal class FindDeclarationVisitor : AstVisitor
{
private SymbolReference symbolRef;
private string variableName;
public SymbolReference FoundDeclaration{ get; private set; }
public FindDeclarationVisitor(SymbolReference symbolRef)
{
this.symbolRef = symbolRef;
if (this.symbolRef.SymbolType == SymbolType.Variable)
{
// converts `$varName` to `varName` or of the form ${varName} to varName
variableName = symbolRef.SymbolName.TrimStart('$').Trim('{', '}');
}
}
/// <summary>
/// Decides if the current function definition is the right definition
/// for the symbol being searched for. The definition of the symbol will be a of type
/// SymbolType.Function and have the same name as the symbol
/// </summary>
/// <param name="functionDefinitionAst">A FunctionDefinitionAst in the script's AST</param>
/// <returns>A decision to stop searching if the right FunctionDefinitionAst was found,
/// or a decision to continue if it wasn't found</returns>
public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst)
{
// Get the start column number of the function name,
// instead of the the start column of 'function' and create new extent for the functionName
int startColumnNumber =
functionDefinitionAst.Extent.Text.IndexOf(
functionDefinitionAst.Name) + 1;
IScriptExtent nameExtent = new ScriptExtent()
{
Text = functionDefinitionAst.Name,
StartLineNumber = functionDefinitionAst.Extent.StartLineNumber,
StartColumnNumber = startColumnNumber,
EndLineNumber = functionDefinitionAst.Extent.StartLineNumber,
EndColumnNumber = startColumnNumber + functionDefinitionAst.Name.Length
};
if (symbolRef.SymbolType.Equals(SymbolType.Function) &&
nameExtent.Text.Equals(symbolRef.ScriptRegion.Text, StringComparison.CurrentCultureIgnoreCase))
{
this.FoundDeclaration =
new SymbolReference(
SymbolType.Function,
nameExtent);
return AstVisitAction.StopVisit;
}
return base.VisitFunctionDefinition(functionDefinitionAst);
}
/// <summary>
/// Check if the left hand side of an assignmentStatementAst is a VariableExpressionAst
/// with the same name as that of symbolRef.
/// </summary>
/// <param name="assignmentStatementAst">An AssignmentStatementAst</param>
/// <returns>A decision to stop searching if the right VariableExpressionAst was found,
/// or a decision to continue if it wasn't found</returns>
public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
{
if (variableName == null)
{
return AstVisitAction.Continue;
}
// We want to check VariableExpressionAsts from within this AssignmentStatementAst so we visit it.
FindDeclarationVariableExpressionVisitor visitor = new FindDeclarationVariableExpressionVisitor(symbolRef);
assignmentStatementAst.Left.Visit(visitor);
if (visitor.FoundDeclaration != null)
{
FoundDeclaration = visitor.FoundDeclaration;
return AstVisitAction.StopVisit;
}
return AstVisitAction.Continue;
}
/// <summary>
/// The private visitor used to find the variable expression that matches a symbol
/// </summary>
private class FindDeclarationVariableExpressionVisitor : AstVisitor
{
private SymbolReference symbolRef;
private string variableName;
public SymbolReference FoundDeclaration{ get; private set; }
public FindDeclarationVariableExpressionVisitor(SymbolReference symbolRef)
{
this.symbolRef = symbolRef;
if (this.symbolRef.SymbolType == SymbolType.Variable)
{
// converts `$varName` to `varName` or of the form ${varName} to varName
variableName = symbolRef.SymbolName.TrimStart('$').Trim('{', '}');
}
}
/// <summary>
/// Check if the VariableExpressionAst has the same name as that of symbolRef.
/// </summary>
/// <param name="variableExpressionAst">A VariableExpressionAst</param>
/// <returns>A decision to stop searching if the right VariableExpressionAst was found,
/// or a decision to continue if it wasn't found</returns>
public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst)
{
if (variableExpressionAst.VariablePath.UserPath.Equals(variableName, StringComparison.OrdinalIgnoreCase))
{
// TODO also find instances of set-variable
FoundDeclaration = new SymbolReference(SymbolType.Variable, variableExpressionAst.Extent);
return AstVisitAction.StopVisit;
}
return AstVisitAction.Continue;
}
public override AstVisitAction VisitMemberExpression(MemberExpressionAst functionDefinitionAst)
{
// We don't want to discover any variables in member expressisons (`$something.Foo`)
return AstVisitAction.SkipChildren;
}
public override AstVisitAction VisitIndexExpression(IndexExpressionAst functionDefinitionAst)
{
// We don't want to discover any variables in index expressions (`$something[0]`)
return AstVisitAction.SkipChildren;
}
}
}
}