-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The AST contains the most correct version of how a script is interpreted. This includes regions of text. Currently the code folder only uses the Tokens which requires the folder to re-implement some of the AST behaviour e.g. matching token pairs for arrays etc. The code folder should be implemented using as much of the AST as possible. This commit; * Moves most of the region detection to use the AST Extents and uses a new FindFoldsASTVisitor. * Modifies the tests and language server to use the new method fold detection class. * Moved the code to modify the end line of folding regions to the language server code. * The test fixture changes were needed due to how the tokeniser and ast see the beginning of some regions. Users will probably not notice. Note that this requires a modern PowerShell version due to use of ASTVisitor2 class.
- Loading branch information
1 parent
91f9b1a
commit f9c404a
Showing
6 changed files
with
219 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/PowerShellEditorServices/Language/FindFoldsVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// | ||
// 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.Management.Automation.Language; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
/// <summary> | ||
/// The visitor used to find the all folding regions in an AST | ||
/// </summary> | ||
internal class FindFoldsVisitor : AstVisitor2 | ||
{ | ||
private const string RegionKindNone = null; | ||
|
||
private FoldingReferenceList _refList; | ||
|
||
public FindFoldsVisitor(ref FoldingReferenceList refList) | ||
{ | ||
_refList = refList; | ||
} | ||
|
||
/// <summary> | ||
/// Returns whether an Extent could be used as a valid folding region | ||
/// </summary> | ||
private bool IsValidFoldingExtent( | ||
IScriptExtent extent) | ||
{ | ||
// The extent must span at least one line | ||
return extent.EndLineNumber > extent.StartLineNumber; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of a FoldingReference object from a script extent | ||
/// </summary> | ||
private FoldingReference CreateFoldingReference( | ||
IScriptExtent extent, | ||
string matchKind) | ||
{ | ||
// Extents are base 1, but LSP is base 0, so minus 1 off all lines and character positions | ||
return new FoldingReference | ||
{ | ||
StartLine = extent.StartLineNumber - 1, | ||
StartCharacter = extent.StartColumnNumber - 1, | ||
EndLine = extent.EndLineNumber - 1, | ||
EndCharacter = extent.EndColumnNumber - 1, | ||
Kind = matchKind | ||
}; | ||
} | ||
|
||
// AST object visitor methods | ||
public override AstVisitAction VisitArrayExpression(ArrayExpressionAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitHashtable(HashtableAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitParamBlock(ParamBlockAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) { _refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); } | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitStatementBlock(StatementBlockAst objAst) | ||
{ | ||
// These parent visitors will get this AST Object. No need to process it | ||
if (objAst.Parent == null) { return AstVisitAction.Continue; } | ||
if (objAst.Parent is ArrayExpressionAst) { return AstVisitAction.Continue; } | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitScriptBlock(ScriptBlockAst objAst) | ||
{ | ||
// If the Parent object is null then this represents the entire script. We don't want to fold that | ||
if (objAst.Parent == null) { return AstVisitAction.Continue; } | ||
// The ScriptBlockExpressionAst visitor will get this AST Object. No need to process it | ||
if (objAst.Parent is ScriptBlockExpressionAst) { return AstVisitAction.Continue; } | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) { | ||
FoldingReference foldRef = CreateFoldingReference(objAst.ScriptBlock.Extent, RegionKindNone); | ||
if (objAst.Parent == null) { return AstVisitAction.Continue; } | ||
if (objAst.Parent is InvokeMemberExpressionAst) { | ||
// This is a bit naive. The ScriptBlockExpressionAst Extent does not include the actual { and } | ||
// characters so the StartCharacter and EndCharacter indexes are out by one. This could be a bug in | ||
// PowerShell Parser. This is just a workaround | ||
foldRef.StartCharacter--; | ||
foldRef.EndCharacter++; | ||
} | ||
_refList.SafeAdd(foldRef); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitStringConstantExpression(StringConstantExpressionAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
|
||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitSubExpression(SubExpressionAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitVariableExpression(VariableExpressionAst objAst) | ||
{ | ||
if (IsValidFoldingExtent(objAst.Extent)) | ||
{ | ||
_refList.SafeAdd(CreateFoldingReference(objAst.Extent, RegionKindNone)); | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/PowerShellEditorServices/Language/FoldingOperations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// 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.Management.Automation.Language; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
/// <summary> | ||
/// Provides common operations for code folding in a script | ||
/// </summary> | ||
internal static class FoldingOperations | ||
{ | ||
/// <summary> | ||
/// Extracts all of the unique foldable regions in a script given a script AST and the list tokens | ||
/// used to generate the AST | ||
/// </summary> | ||
internal static FoldingReferenceList FoldableRegions( | ||
Token[] tokens, | ||
Ast scriptAst) | ||
{ | ||
var foldableRegions = new FoldingReferenceList(); | ||
|
||
// Add regions from AST | ||
AstOperations.FindFoldsInDocument(scriptAst, ref foldableRegions); | ||
|
||
// Add regions from Tokens | ||
TokenOperations.FoldableReferences(tokens, ref foldableRegions); | ||
|
||
return foldableRegions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters