|
| 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; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Management.Automation.Language; |
| 9 | + |
| 10 | +namespace Microsoft.PowerShell.EditorServices |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// The visitor used to find the all folding regions in an AST |
| 14 | + /// </summary> |
| 15 | + internal class FindFoldsVisitor : AstVisitor |
| 16 | + { |
| 17 | + private const string RegionKindNone = null; |
| 18 | + |
| 19 | + public List<FoldingReference> FoldableRegions { get; } |
| 20 | + |
| 21 | + public FindFoldsVisitor() |
| 22 | + { |
| 23 | + this.FoldableRegions = new List<FoldingReference>(); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Returns whether an Extent could be used as a valid folding region |
| 28 | + /// </summary> |
| 29 | + private bool IsValidFoldingExtent( |
| 30 | + IScriptExtent extent) |
| 31 | + { |
| 32 | + // The extent must span at least one line |
| 33 | + return extent.EndLineNumber > extent.StartLineNumber; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates an instance of a FoldingReference object from a script extent |
| 38 | + /// </summary> |
| 39 | + private FoldingReference CreateFoldingReference( |
| 40 | + IScriptExtent extent, |
| 41 | + string matchKind) |
| 42 | + { |
| 43 | + // Extents are base 1, but LSP is base 0, so minus 1 off all lines and character positions |
| 44 | + return new FoldingReference { |
| 45 | + StartLine = extent.StartLineNumber - 1, |
| 46 | + StartCharacter = extent.StartColumnNumber - 1, |
| 47 | + EndLine = extent.EndLineNumber - 1, |
| 48 | + EndCharacter = extent.EndColumnNumber - 1, |
| 49 | + Kind = matchKind |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + // AST object visitor methods |
| 54 | + public override AstVisitAction VisitArrayExpression(ArrayExpressionAst objAst) |
| 55 | + { |
| 56 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 57 | + { |
| 58 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 59 | + } |
| 60 | + return AstVisitAction.Continue; |
| 61 | + } |
| 62 | + |
| 63 | + public override AstVisitAction VisitHashtable(HashtableAst objAst) |
| 64 | + { |
| 65 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 66 | + { |
| 67 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 68 | + } |
| 69 | + return AstVisitAction.Continue; |
| 70 | + } |
| 71 | + |
| 72 | + public override AstVisitAction VisitStatementBlock(StatementBlockAst objAst) |
| 73 | + { |
| 74 | + // These parent visitors will get this AST Object. No need to process it |
| 75 | + if (objAst.Parent == null) { return AstVisitAction.Continue; } |
| 76 | + if (objAst.Parent is ArrayExpressionAst) { return AstVisitAction.Continue; } |
| 77 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 78 | + { |
| 79 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 80 | + } |
| 81 | + return AstVisitAction.Continue; |
| 82 | + } |
| 83 | + |
| 84 | + public override AstVisitAction VisitScriptBlock(ScriptBlockAst objAst) |
| 85 | + { |
| 86 | + // If the Parent object is null then this represents the entire script. We don't want to fold that |
| 87 | + if (objAst.Parent == null) { return AstVisitAction.Continue; } |
| 88 | + // The ScriptBlockExpressionAst visitor will get this AST Object. No need to process it |
| 89 | + if (objAst.Parent is ScriptBlockExpressionAst) { return AstVisitAction.Continue; } |
| 90 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 91 | + { |
| 92 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 93 | + } |
| 94 | + return AstVisitAction.Continue; |
| 95 | + } |
| 96 | + |
| 97 | + public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionAst objAst) |
| 98 | + { |
| 99 | + if (IsValidFoldingExtent(objAst.Extent)) { |
| 100 | + FoldingReference foldRef = CreateFoldingReference(objAst.ScriptBlock.Extent, RegionKindNone); |
| 101 | + if (objAst.Parent == null) { return AstVisitAction.Continue; } |
| 102 | + if (objAst.Parent is InvokeMemberExpressionAst) { |
| 103 | + // This is a bit naive. The ScriptBlockExpressionAst Extent does not include the actual { and } |
| 104 | + // characters so the StartCharacter and EndCharacter indexes are out by one. This could be a bug in |
| 105 | + // PowerShell Parser. This is just a workaround |
| 106 | + foldRef.StartCharacter--; |
| 107 | + foldRef.EndCharacter++; |
| 108 | + } |
| 109 | + this.FoldableRegions.Add(foldRef); |
| 110 | + } |
| 111 | + return AstVisitAction.Continue; |
| 112 | + } |
| 113 | + |
| 114 | + public override AstVisitAction VisitStringConstantExpression(StringConstantExpressionAst objAst) |
| 115 | + { |
| 116 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 117 | + { |
| 118 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 119 | + } |
| 120 | + |
| 121 | + return AstVisitAction.Continue; |
| 122 | + } |
| 123 | + |
| 124 | + public override AstVisitAction VisitSubExpression(SubExpressionAst objAst) |
| 125 | + { |
| 126 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 127 | + { |
| 128 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 129 | + } |
| 130 | + return AstVisitAction.Continue; |
| 131 | + } |
| 132 | + |
| 133 | + public override AstVisitAction VisitVariableExpression(VariableExpressionAst objAst) |
| 134 | + { |
| 135 | + if (IsValidFoldingExtent(objAst.Extent)) |
| 136 | + { |
| 137 | + this.FoldableRegions.Add(CreateFoldingReference(objAst.Extent, RegionKindNone)); |
| 138 | + } |
| 139 | + return AstVisitAction.Continue; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments