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