|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System.Management.Automation.Language; |
| 7 | + |
| 8 | +namespace Microsoft.PowerShell.EditorServices.Symbols |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// The visitor used to find the the symbol at a specfic location in the AST |
| 12 | + /// </summary> |
| 13 | + internal class FindSymbolVisitor : AstVisitor |
| 14 | + { |
| 15 | + private int lineNumber; |
| 16 | + private int columnNumber; |
| 17 | + private bool includeFunctionDefinitions; |
| 18 | + |
| 19 | + public SymbolReference FoundSymbolReference { get; private set; } |
| 20 | + |
| 21 | + public FindSymbolVisitor( |
| 22 | + int lineNumber, |
| 23 | + int columnNumber, |
| 24 | + bool includeFunctionDefinitions) |
| 25 | + { |
| 26 | + this.lineNumber = lineNumber; |
| 27 | + this.columnNumber = columnNumber; |
| 28 | + this.includeFunctionDefinitions = includeFunctionDefinitions; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Checks to see if this command ast is the symbol we are looking for. |
| 33 | + /// </summary> |
| 34 | + /// <param name="commandAst">A CommandAst object in the script's AST</param> |
| 35 | + /// <returns>A decision to stop searching if the right symbol was found, |
| 36 | + /// or a decision to continue if it wasn't found</returns> |
| 37 | + public override AstVisitAction VisitCommand(CommandAst commandAst) |
| 38 | + { |
| 39 | + Ast commandNameAst = commandAst.CommandElements[0]; |
| 40 | + |
| 41 | + if (this.IsPositionInExtent(commandNameAst.Extent)) |
| 42 | + { |
| 43 | + this.FoundSymbolReference = |
| 44 | + new SymbolReference( |
| 45 | + SymbolType.Function, |
| 46 | + commandNameAst.Extent); |
| 47 | + |
| 48 | + return AstVisitAction.StopVisit; |
| 49 | + } |
| 50 | + |
| 51 | + return base.VisitCommand(commandAst); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Checks to see if this function definition is the symbol we are looking for. |
| 56 | + /// </summary> |
| 57 | + /// <param name="functionDefinitionAst">A functionDefinitionAst object in the script's AST</param> |
| 58 | + /// <returns>A decision to stop searching if the right symbol was found, |
| 59 | + /// or a decision to continue if it wasn't found</returns> |
| 60 | + public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) |
| 61 | + { |
| 62 | + int startColumnNumber = 1; |
| 63 | + |
| 64 | + if (!this.includeFunctionDefinitions) |
| 65 | + { |
| 66 | + startColumnNumber = |
| 67 | + functionDefinitionAst.Extent.Text.IndexOf( |
| 68 | + functionDefinitionAst.Name) + 1; |
| 69 | + } |
| 70 | + |
| 71 | + IScriptExtent nameExtent = new ScriptExtent() |
| 72 | + { |
| 73 | + Text = functionDefinitionAst.Name, |
| 74 | + StartLineNumber = functionDefinitionAst.Extent.StartLineNumber, |
| 75 | + EndLineNumber = functionDefinitionAst.Extent.EndLineNumber, |
| 76 | + StartColumnNumber = startColumnNumber, |
| 77 | + EndColumnNumber = startColumnNumber + functionDefinitionAst.Name.Length |
| 78 | + }; |
| 79 | + |
| 80 | + if (this.IsPositionInExtent(nameExtent)) |
| 81 | + { |
| 82 | + this.FoundSymbolReference = |
| 83 | + new SymbolReference( |
| 84 | + SymbolType.Function, |
| 85 | + nameExtent); |
| 86 | + |
| 87 | + return AstVisitAction.StopVisit; |
| 88 | + } |
| 89 | + |
| 90 | + return base.VisitFunctionDefinition(functionDefinitionAst); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Checks to see if this command parameter is the symbol we are looking for. |
| 95 | + /// </summary> |
| 96 | + /// <param name="commandParameterAst">A CommandParameterAst object in the script's AST</param> |
| 97 | + /// <returns>A decision to stop searching if the right symbol was found, |
| 98 | + /// or a decision to continue if it wasn't found</returns> |
| 99 | + public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst) |
| 100 | + { |
| 101 | + if (this.IsPositionInExtent(commandParameterAst.Extent)) |
| 102 | + { |
| 103 | + this.FoundSymbolReference = |
| 104 | + new SymbolReference( |
| 105 | + SymbolType.Parameter, |
| 106 | + commandParameterAst.Extent); |
| 107 | + return AstVisitAction.StopVisit; |
| 108 | + } |
| 109 | + return AstVisitAction.Continue; |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Checks to see if this variable expression is the symbol we are looking for. |
| 114 | + /// </summary> |
| 115 | + /// <param name="variableExpressionAst">A VariableExpressionAst object in the script's AST</param> |
| 116 | + /// <returns>A decision to stop searching if the right symbol was found, |
| 117 | + /// or a decision to continue if it wasn't found</returns> |
| 118 | + public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) |
| 119 | + { |
| 120 | + if (this.IsPositionInExtent(variableExpressionAst.Extent)) |
| 121 | + { |
| 122 | + this.FoundSymbolReference = |
| 123 | + new SymbolReference( |
| 124 | + SymbolType.Variable, |
| 125 | + variableExpressionAst.Extent); |
| 126 | + |
| 127 | + return AstVisitAction.StopVisit; |
| 128 | + } |
| 129 | + |
| 130 | + return AstVisitAction.Continue; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Is the position of the given location is in the ast's extent |
| 135 | + /// </summary> |
| 136 | + /// <param name="extent">The script extent of the element</param> |
| 137 | + /// <returns>True if the given position is in the range of the element's extent </returns> |
| 138 | + private bool IsPositionInExtent(IScriptExtent extent) |
| 139 | + { |
| 140 | + return (extent.StartLineNumber == lineNumber && |
| 141 | + extent.StartColumnNumber <= columnNumber && |
| 142 | + extent.EndColumnNumber >= columnNumber); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments