-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
{WIP} (GH-813) Use AST for Syntax Folder #806
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
152 changes: 152 additions & 0 deletions
152
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,152 @@ | ||
// | ||
// 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 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)) { | ||
glennsarti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FoldingReference foldRef = CreateFoldingReference(objAst.ScriptBlock.Extent, RegionKindNone); | ||
if (objAst.Parent == null) { return AstVisitAction.Continue; } | ||
if (objAst.Parent is InvokeMemberExpressionAst) { | ||
glennsarti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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.FoldableRegions(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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a line break in the
foreach
, you can also save it to a local variable first. That'll be optimized out by the compiler in a release build.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure what you mean here.... do you mean;
If so what is that actually getting us?