|
| 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.Collections.Generic; |
| 7 | +using System.Management.Automation.Language; |
| 8 | + |
| 9 | +using System; |
| 10 | + |
| 11 | +namespace Microsoft.PowerShell.EditorServices |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// The visitor used to find the the symbol at a specfic location in the AST |
| 15 | + /// </summary> |
| 16 | + internal class FindFoldsVisitor : AstVisitor |
| 17 | + { |
| 18 | + private const Boolean DEBUGMODE = false; //true; |
| 19 | + |
| 20 | + public List<FoldingReference> FoldableRegions { get; private set; } |
| 21 | + |
| 22 | + // private int lineNumber; |
| 23 | + // private int columnNumber; |
| 24 | + // private bool includeFunctionDefinitions; |
| 25 | + |
| 26 | + // public SymbolReference FoundSymbolReference { get; private set; } |
| 27 | + |
| 28 | + public FindFoldsVisitor() |
| 29 | + { |
| 30 | + this.FoldableRegions = new List<FoldingReference>(); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Creates an instance of a FoldingReference object from a start and end langauge Token |
| 35 | + /// Returns null if the line range is invalid |
| 36 | + /// </summary> |
| 37 | + private bool IsValidFoldingExtent( |
| 38 | + IScriptExtent extent) |
| 39 | + { |
| 40 | + // if (DEBUGMODE) { return true; } |
| 41 | + if (extent.EndLineNumber == extent.StartLineNumber) { return false; } |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Creates an instance of a FoldingReference object from a start and end langauge Token |
| 47 | + /// Returns null if the line range is invalid |
| 48 | + /// </summary> |
| 49 | + private FoldingReference CreateFoldingReference( |
| 50 | + IScriptExtent extent, |
| 51 | + string matchKind) |
| 52 | + { |
| 53 | + // Extents are base 1, but LSP is base 0, so minus 1 off all lines and character positions |
| 54 | + return new FoldingReference { |
| 55 | + StartLine = extent.StartLineNumber - 1, |
| 56 | + StartCharacter = extent.StartColumnNumber - 1, |
| 57 | + EndLine = extent.EndLineNumber - 1, |
| 58 | + EndCharacter = extent.EndColumnNumber - 1, |
| 59 | + Kind = matchKind |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + static private string debugKindName(object obj) { |
| 64 | + if (DEBUGMODE) { return obj.GetType().ToString(); } |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + public override AstVisitAction VisitArrayExpression(ArrayExpressionAst objAst) |
| 69 | + { |
| 70 | + if (IsValidFoldingExtent(objAst.Extent)) { this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, debugKindName(objAst))); } |
| 71 | + return AstVisitAction.Continue; |
| 72 | + } |
| 73 | + public override AstVisitAction VisitStringConstantExpression(StringConstantExpressionAst objAst) |
| 74 | + { |
| 75 | + if (IsValidFoldingExtent(objAst.Extent)) { this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, debugKindName(objAst))); } |
| 76 | + return AstVisitAction.Continue; |
| 77 | + } |
| 78 | + |
| 79 | + public override AstVisitAction VisitHashtable(HashtableAst objAst) |
| 80 | + { |
| 81 | + if (IsValidFoldingExtent(objAst.Extent)) { this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, debugKindName(objAst))); } |
| 82 | + return AstVisitAction.Continue; |
| 83 | + } |
| 84 | + |
| 85 | + public override AstVisitAction VisitStatementBlock(StatementBlockAst objAst) |
| 86 | + { |
| 87 | + // These parent visitors will get this AST Object. No need to process it |
| 88 | + if (objAst.Parent == null) { return AstVisitAction.Continue; } |
| 89 | + if (objAst.Parent is ArrayExpressionAst) { return AstVisitAction.Continue; } |
| 90 | + if (IsValidFoldingExtent(objAst.Extent)) { this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, debugKindName(objAst))); } |
| 91 | + return AstVisitAction.Continue; |
| 92 | + } |
| 93 | + |
| 94 | + public override AstVisitAction VisitSubExpression(SubExpressionAst objAst) |
| 95 | + { |
| 96 | + if (IsValidFoldingExtent(objAst.Extent)) { this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, debugKindName(objAst))); } |
| 97 | + return AstVisitAction.Continue; |
| 98 | + } |
| 99 | + |
| 100 | + public override AstVisitAction VisitScriptBlock(ScriptBlockAst objAst) |
| 101 | + { |
| 102 | + // If the Parent object is null then this represents the entire script. We don't want to fold that |
| 103 | + if (objAst.Parent == null) { return AstVisitAction.Continue; } |
| 104 | + // The ScriptBlockExpressionAst visitor will get this AST Object. No need to process it |
| 105 | + if (objAst.Parent is ScriptBlockExpressionAst) { return AstVisitAction.Continue; } |
| 106 | + if (IsValidFoldingExtent(objAst.Extent)) { this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, debugKindName(objAst))); } |
| 107 | + return AstVisitAction.Continue; |
| 108 | + } |
| 109 | + |
| 110 | + public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionAst objAst) |
| 111 | + { |
| 112 | + if (IsValidFoldingExtent(objAst.Extent)) { |
| 113 | + FoldingReference foldRef = CreateFoldingReference(objAst.ScriptBlock.Extent, debugKindName(objAst)); |
| 114 | + if (objAst.Parent == null) { return AstVisitAction.Continue; } |
| 115 | + if (objAst.Parent is InvokeMemberExpressionAst) { |
| 116 | + // This is a bit naive. The ScriptBlockExpressionAst Extent does not include the actual { and } |
| 117 | + // characters so the StartCharacter and EndCharacter indexes are out by one. This could be a bug in |
| 118 | + // PowerShell Parser. This is just a workaround |
| 119 | + foldRef.StartCharacter--; |
| 120 | + foldRef.EndCharacter++; |
| 121 | + } |
| 122 | + this.FoldableRegions.Add(foldRef); |
| 123 | + } |
| 124 | + return AstVisitAction.Continue; |
| 125 | + } |
| 126 | + |
| 127 | + // /// <summary> |
| 128 | + // /// Checks to see if this command ast is the symbol we are looking for. |
| 129 | + // /// </summary> |
| 130 | + // /// <param name="commandAst">A CommandAst object in the script's AST</param> |
| 131 | + // /// <returns>A decision to stop searching if the right symbol was found, |
| 132 | + // /// or a decision to continue if it wasn't found</returns> |
| 133 | + // public override AstVisitAction VisitCommand(CommandAst commandAst) |
| 134 | + // { |
| 135 | + // Ast commandNameAst = commandAst.CommandElements[0]; |
| 136 | + |
| 137 | + // if (this.IsPositionInExtent(commandNameAst.Extent)) |
| 138 | + // { |
| 139 | + // this.FoundSymbolReference = |
| 140 | + // new SymbolReference( |
| 141 | + // SymbolType.Function, |
| 142 | + // commandNameAst.Extent); |
| 143 | + |
| 144 | + // return AstVisitAction.StopVisit; |
| 145 | + // } |
| 146 | + |
| 147 | + // return base.VisitCommand(commandAst); |
| 148 | + // } |
| 149 | + |
| 150 | + // /// <summary> |
| 151 | + // /// Checks to see if this function definition is the symbol we are looking for. |
| 152 | + // /// </summary> |
| 153 | + // /// <param name="functionDefinitionAst">A functionDefinitionAst object in the script's AST</param> |
| 154 | + // /// <returns>A decision to stop searching if the right symbol was found, |
| 155 | + // /// or a decision to continue if it wasn't found</returns> |
| 156 | + // public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) |
| 157 | + // { |
| 158 | + // int startColumnNumber = 1; |
| 159 | + |
| 160 | + // if (!this.includeFunctionDefinitions) |
| 161 | + // { |
| 162 | + // startColumnNumber = |
| 163 | + // functionDefinitionAst.Extent.Text.IndexOf( |
| 164 | + // functionDefinitionAst.Name) + 1; |
| 165 | + // } |
| 166 | + |
| 167 | + // IScriptExtent nameExtent = new ScriptExtent() |
| 168 | + // { |
| 169 | + // Text = functionDefinitionAst.Name, |
| 170 | + // StartLineNumber = functionDefinitionAst.Extent.StartLineNumber, |
| 171 | + // EndLineNumber = functionDefinitionAst.Extent.EndLineNumber, |
| 172 | + // StartColumnNumber = startColumnNumber, |
| 173 | + // EndColumnNumber = startColumnNumber + functionDefinitionAst.Name.Length |
| 174 | + // }; |
| 175 | + |
| 176 | + // if (this.IsPositionInExtent(nameExtent)) |
| 177 | + // { |
| 178 | + // this.FoundSymbolReference = |
| 179 | + // new SymbolReference( |
| 180 | + // SymbolType.Function, |
| 181 | + // nameExtent); |
| 182 | + |
| 183 | + // return AstVisitAction.StopVisit; |
| 184 | + // } |
| 185 | + |
| 186 | + // return base.VisitFunctionDefinition(functionDefinitionAst); |
| 187 | + // } |
| 188 | + |
| 189 | + // /// <summary> |
| 190 | + // /// Checks to see if this command parameter is the symbol we are looking for. |
| 191 | + // /// </summary> |
| 192 | + // /// <param name="commandParameterAst">A CommandParameterAst object in the script's AST</param> |
| 193 | + // /// <returns>A decision to stop searching if the right symbol was found, |
| 194 | + // /// or a decision to continue if it wasn't found</returns> |
| 195 | + // public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst) |
| 196 | + // { |
| 197 | + // if (this.IsPositionInExtent(commandParameterAst.Extent)) |
| 198 | + // { |
| 199 | + // this.FoundSymbolReference = |
| 200 | + // new SymbolReference( |
| 201 | + // SymbolType.Parameter, |
| 202 | + // commandParameterAst.Extent); |
| 203 | + // return AstVisitAction.StopVisit; |
| 204 | + // } |
| 205 | + // return AstVisitAction.Continue; |
| 206 | + // } |
| 207 | + |
| 208 | + // /// <summary> |
| 209 | + // /// Checks to see if this variable expression is the symbol we are looking for. |
| 210 | + // /// </summary> |
| 211 | + // /// <param name="variableExpressionAst">A VariableExpressionAst object in the script's AST</param> |
| 212 | + // /// <returns>A decision to stop searching if the right symbol was found, |
| 213 | + // /// or a decision to continue if it wasn't found</returns> |
| 214 | + // public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) |
| 215 | + // { |
| 216 | + // if (this.IsPositionInExtent(variableExpressionAst.Extent)) |
| 217 | + // { |
| 218 | + // this.FoundSymbolReference = |
| 219 | + // new SymbolReference( |
| 220 | + // SymbolType.Variable, |
| 221 | + // variableExpressionAst.Extent); |
| 222 | + |
| 223 | + // return AstVisitAction.StopVisit; |
| 224 | + // } |
| 225 | + |
| 226 | + // return AstVisitAction.Continue; |
| 227 | + // } |
| 228 | + |
| 229 | + // /// <summary> |
| 230 | + // /// Is the position of the given location is in the ast's extent |
| 231 | + // /// </summary> |
| 232 | + // /// <param name="extent">The script extent of the element</param> |
| 233 | + // /// <returns>True if the given position is in the range of the element's extent </returns> |
| 234 | + // private bool IsPositionInExtent(IScriptExtent extent) |
| 235 | + // { |
| 236 | + // return (extent.StartLineNumber == lineNumber && |
| 237 | + // extent.StartColumnNumber <= columnNumber && |
| 238 | + // extent.EndColumnNumber >= columnNumber); |
| 239 | + // } |
| 240 | + } |
| 241 | +} |
0 commit comments