-
Notifications
You must be signed in to change notification settings - Fork 237
Add document symbols for #region #1911
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
Changes from 8 commits
bd28a73
27e80e0
851384f
6468b56
907d481
4aed74c
8e59ea8
e892de3
bea7293
196edbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Management.Automation.Language; | ||
using Microsoft.PowerShell.EditorServices.Services.TextDocument; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Services.Symbols | ||
{ | ||
internal static class RegionVisitor | ||
{ | ||
internal static void GetRegionsInDocument(ScriptFile file, Func<SymbolReference, AstVisitAction> action) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this a lot, makes reworking it much easier! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andschwa: I assume you meant the separate class. |
||
{ | ||
Stack<Token> tokenCommentRegionStack = new(); | ||
Token[] tokens = file.ScriptTokens; | ||
|
||
for (int i = 0; i < tokens.Length; i++) | ||
fflaten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Token token = tokens[i]; | ||
|
||
// Exclude everything but single-line comments | ||
if (token.Kind != TokenKind.Comment || | ||
token.Extent.StartLineNumber != token.Extent.EndLineNumber || | ||
!TokenOperations.IsBlockComment(i, tokens)) | ||
{ | ||
continue; | ||
} | ||
|
||
// Processing for #region -> #endregion | ||
if (TokenOperations.s_startRegionTextRegex.IsMatch(token.Text)) | ||
{ | ||
tokenCommentRegionStack.Push(token); | ||
continue; | ||
} | ||
|
||
if (TokenOperations.s_endRegionTextRegex.IsMatch(token.Text)) | ||
{ | ||
// Mismatched regions in the script can cause bad stacks. | ||
if (tokenCommentRegionStack.Count > 0) | ||
{ | ||
Token regionStart = tokenCommentRegionStack.Pop(); | ||
Token regionEnd = token; | ||
|
||
BufferRange regionRange = new( | ||
regionStart.Extent.StartLineNumber, | ||
regionStart.Extent.StartColumnNumber, | ||
regionEnd.Extent.EndLineNumber, | ||
regionEnd.Extent.EndColumnNumber); | ||
|
||
action(new SymbolReference( | ||
SymbolType.Region, | ||
regionStart.Extent.Text.Trim().TrimStart('#'), | ||
regionStart.Extent.Text.Trim(), | ||
regionStart.Extent, | ||
new ScriptExtent() | ||
{ | ||
Text = string.Join(Environment.NewLine, file.GetLinesInRange(regionRange)), | ||
StartLineNumber = regionStart.Extent.StartLineNumber, | ||
StartColumnNumber = regionStart.Extent.StartColumnNumber, | ||
StartOffset = regionStart.Extent.StartOffset, | ||
EndLineNumber = regionEnd.Extent.EndLineNumber, | ||
EndColumnNumber = regionEnd.Extent.EndColumnNumber, | ||
EndOffset = regionEnd.Extent.EndOffset, | ||
File = regionStart.Extent.File | ||
}, | ||
file, | ||
isDeclaration: true)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.