-
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 all 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 IEnumerable<SymbolReference> GetRegionsInDocument(ScriptFile file) | ||
{ | ||
Stack<Token> tokenCommentRegionStack = new(); | ||
Token[] tokens = file.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(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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,10 +50,7 @@ public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(Do | |
ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri); | ||
|
||
IEnumerable<SymbolReference> foundSymbols = ProvideDocumentSymbols(scriptFile); | ||
if (foundSymbols is null) | ||
{ | ||
return null; | ||
} | ||
foundSymbols = foundSymbols.Concat(RegionVisitor.GetRegionsInDocument(scriptFile)); | ||
|
||
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 didn't see a reason to keep the null-check. The local method |
||
string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,13 @@ private IEnumerable<SymbolReference> FindSymbolsInFile(ScriptRegion scriptRegion | |
.OrderBy(symbol => symbol.ScriptRegion.ToRange().Start); | ||
} | ||
|
||
private IEnumerable<SymbolReference> FindRegionsInFile(ScriptRegion scriptRegion) | ||
{ | ||
return RegionVisitor | ||
.GetRegionsInDocument(GetScriptFile(scriptRegion)) | ||
.OrderBy(symbol => symbol.ScriptRegion.ToRange().Start); | ||
} | ||
|
||
[Fact] | ||
public async Task FindsParameterHintsOnCommand() | ||
{ | ||
|
@@ -952,5 +959,30 @@ public void FindsSymbolsInNoSymbolsFile() | |
IEnumerable<SymbolReference> symbolsResult = FindSymbolsInFile(FindSymbolsInNoSymbolsFile.SourceDetails); | ||
Assert.Empty(symbolsResult); | ||
} | ||
|
||
[Fact] | ||
public void FindsRegionSymbolsInFile() | ||
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. This technically doesn't belong here as SymbolsService isn't used for DocumentSymbolHandler. Move to own test-file? Suggestion? |
||
{ | ||
IEnumerable<SymbolReference> symbols = FindRegionsInFile(FindSymbolsInMultiSymbolFile.SourceDetails); | ||
Assert.Collection(symbols, | ||
(i) => | ||
{ | ||
Assert.Equal("region find me outer", i.Id); | ||
Assert.Equal("#region find me outer", i.Name); | ||
Assert.Equal(SymbolType.Region, i.Type); | ||
Assert.True(i.IsDeclaration); | ||
AssertIsRegion(i.NameRegion, 51, 1, 51, 22); | ||
AssertIsRegion(i.ScriptRegion, 51, 1, 55, 11); | ||
}, | ||
(i) => | ||
{ | ||
Assert.Equal("region find me inner", i.Id); | ||
Assert.Equal("#region find me inner", i.Name); | ||
Assert.Equal(SymbolType.Region, i.Type); | ||
Assert.True(i.IsDeclaration); | ||
AssertIsRegion(i.NameRegion, 52, 1, 52, 22); | ||
AssertIsRegion(i.ScriptRegion, 52, 1, 54, 11); | ||
}); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.