-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2003 from PowerShell/andschwa/region-symbols
Add document symbols for `#region`
- Loading branch information
Showing
24 changed files
with
309 additions
and
179 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
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
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
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
79 changes: 79 additions & 0 deletions
79
src/PowerShellEditorServices/Services/Symbols/RegionDocumentSymbolProvider.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,79 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Management.Automation.Language; | ||
using Microsoft.PowerShell.EditorServices.Services.TextDocument; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Services.Symbols | ||
{ | ||
/// <summary> | ||
/// Provides an IDocumentSymbolProvider implementation for | ||
/// enumerating regions as symbols in script (.psd1, .psm1) files. | ||
/// </summary> | ||
internal class RegionDocumentSymbolProvider : IDocumentSymbolProvider | ||
{ | ||
string IDocumentSymbolProvider.ProviderId => nameof(RegionDocumentSymbolProvider); | ||
|
||
IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(ScriptFile scriptFile) | ||
{ | ||
Stack<Token> tokenCommentRegionStack = new(); | ||
Token[] tokens = scriptFile.ScriptTokens; | ||
|
||
for (int i = 0; i < tokens.Length; i++) | ||
{ | ||
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); | ||
|
||
yield return new SymbolReference( | ||
SymbolType.Region, | ||
regionStart.Extent.Text.Trim().TrimStart('#'), | ||
regionStart.Extent.Text.Trim(), | ||
regionStart.Extent, | ||
new ScriptExtent() | ||
{ | ||
Text = string.Join(System.Environment.NewLine, scriptFile.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 | ||
}, | ||
scriptFile, | ||
isDeclaration: true); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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.