From bd28a734f42ba2edeb25ac473a16d51d82cae413 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:18:08 +0000 Subject: [PATCH 01/10] add document symbols for #region start --- .../Services/Symbols/ReferenceTable.cs | 1 + .../Services/Symbols/SymbolType.cs | 6 +++ .../Symbols/Visitors/RegionVisitor.cs | 52 +++++++++++++++++++ .../Services/TextDocument/TokenOperations.cs | 2 +- .../Symbols/MultipleSymbols.ps1 | 8 +++ .../Language/SymbolsServiceTests.cs | 6 +++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs diff --git a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs index 6cb8e52c4..bc3dfa85c 100644 --- a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs +++ b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs @@ -79,6 +79,7 @@ internal void EnsureInitialized() } _parent.ScriptAst.Visit(new SymbolVisitor(_parent, AddReference)); + RegionVisitor.FindRegionsInDocument(_parent, AddReference); } private AstVisitAction AddReference(SymbolReference symbol) diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs index 6533e7726..b09a8097e 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs @@ -79,6 +79,11 @@ internal enum SymbolType /// The symbol is a type reference /// Type, + + /// + /// The symbol is a region + /// + Region } internal static class SymbolTypeUtils @@ -97,6 +102,7 @@ internal static SymbolKind GetSymbolKind(SymbolType symbolType) SymbolType.Variable or SymbolType.Parameter => SymbolKind.Variable, SymbolType.HashtableKey => SymbolKind.Key, SymbolType.Type => SymbolKind.TypeParameter, + SymbolType.Region => SymbolKind.String, SymbolType.Unknown or _ => SymbolKind.Object, }; } diff --git a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs new file mode 100644 index 000000000..c5a468b99 --- /dev/null +++ b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System; +using System.Management.Automation.Language; +using System.Text.RegularExpressions; +using Microsoft.PowerShell.EditorServices.Services.TextDocument; + +namespace Microsoft.PowerShell.EditorServices.Services.Symbols +{ + internal static class RegionVisitor + { + // This regular expression are used to match lines which mark the start of a region comment in a script. + // Based on the defaults in the VS Code Language Configuration at; + // https://github.com/Microsoft/vscode/blob/64186b0a26/extensions/powershell/language-configuration.json#L26-L31 + // https://github.com/Microsoft/vscode/issues/49070 + private static readonly Regex s_startRegionTextRegex = new( + @"^\s*#[rR]egion\b", RegexOptions.Compiled); + + internal static void FindRegionsInDocument(ScriptFile file, Func action) + { + 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) + { + continue; + } + + // Look for #region + // Document symbols only care about the symbol start and regex is expensive, + // so skip checking if region is actually closed with #endregion. + if (TokenOperations.IsBlockComment(i, tokens) && + s_startRegionTextRegex.IsMatch(token.Text)) + { + action(new SymbolReference( + SymbolType.Region, + token.Extent.Text.TrimStart().TrimStart('#'), + token.Extent.Text, + token.Extent, + token.Extent, + file, + isDeclaration: true)); + } + } + } + } +} diff --git a/src/PowerShellEditorServices/Services/TextDocument/TokenOperations.cs b/src/PowerShellEditorServices/Services/TextDocument/TokenOperations.cs index d1a3ca5e5..6798ea3ff 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/TokenOperations.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/TokenOperations.cs @@ -199,7 +199,7 @@ private static FoldingReference CreateFoldingReference( /// - Token text must start with a '#'.false This is because comment regions /// start with '<#' but have the same TokenKind /// - private static bool IsBlockComment(int index, Token[] tokens) + internal static bool IsBlockComment(int index, Token[] tokens) { Token thisToken = tokens[index]; if (thisToken.Kind != TokenKind.Comment) { return false; } diff --git a/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 b/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 index b4f54c329..d07522c6a 100644 --- a/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 +++ b/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 @@ -41,3 +41,11 @@ enum AEnum { AFunction 1..3 | AFilter AnAdvancedFunction + +<# +#region don't find me +abc +#endregion +#> +#region my region 123 +#endregion diff --git a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs index dbe0f8ca6..18662b87c 100644 --- a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs @@ -821,6 +821,12 @@ public void FindsSymbolsInFile() Assert.Equal("prop AValue", symbol.Id); Assert.Equal("AValue", symbol.Name); Assert.True(symbol.IsDeclaration); + + symbol = Assert.Single(symbols.Where(i => i.Type == SymbolType.Region)); + Assert.Equal("region my region 123", symbol.Id); + Assert.Equal("#region my region 123", symbol.Name); + AssertIsRegion(symbol.NameRegion, 50, 1, 50, 22); + Assert.True(symbol.IsDeclaration); } [Fact] From 27e80e0eb1fe7a91c256802e61f37f140cdb3412 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:25:42 +0000 Subject: [PATCH 02/10] Reuse regex-patterns for regions For consistent behavior and easier maintenance --- .../Services/Symbols/Visitors/RegionVisitor.cs | 10 +--------- .../Services/TextDocument/TokenOperations.cs | 4 ++-- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs index c5a468b99..6f9df6c24 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs @@ -3,20 +3,12 @@ using System; using System.Management.Automation.Language; -using System.Text.RegularExpressions; using Microsoft.PowerShell.EditorServices.Services.TextDocument; namespace Microsoft.PowerShell.EditorServices.Services.Symbols { internal static class RegionVisitor { - // This regular expression are used to match lines which mark the start of a region comment in a script. - // Based on the defaults in the VS Code Language Configuration at; - // https://github.com/Microsoft/vscode/blob/64186b0a26/extensions/powershell/language-configuration.json#L26-L31 - // https://github.com/Microsoft/vscode/issues/49070 - private static readonly Regex s_startRegionTextRegex = new( - @"^\s*#[rR]egion\b", RegexOptions.Compiled); - internal static void FindRegionsInDocument(ScriptFile file, Func action) { Token[] tokens = file.ScriptTokens; @@ -35,7 +27,7 @@ internal static void FindRegionsInDocument(ScriptFile file, Func From 851384f8bc8da9d844cb509f6ee92620f2740607 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:30:04 +0000 Subject: [PATCH 03/10] Track only regions with matching endregion Adds support for outline nesting and breadcrumbs --- .../Services/Symbols/ReferenceTable.cs | 2 +- .../Services/Symbols/SymbolType.cs | 2 +- .../Symbols/Visitors/RegionVisitor.cs | 54 ++++++++++++++----- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs index bc3dfa85c..bd7451b21 100644 --- a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs +++ b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs @@ -79,7 +79,7 @@ internal void EnsureInitialized() } _parent.ScriptAst.Visit(new SymbolVisitor(_parent, AddReference)); - RegionVisitor.FindRegionsInDocument(_parent, AddReference); + RegionVisitor.GetRegionsInDocument(_parent, AddReference); } private AstVisitAction AddReference(SymbolReference symbol) diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs index b09a8097e..02e34e6f8 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs @@ -81,7 +81,7 @@ internal enum SymbolType Type, /// - /// The symbol is a region + /// The symbol is a region. Only used for navigation-features. /// Region } diff --git a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs index 6f9df6c24..cfaa17cdd 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; using System.Management.Automation.Language; using Microsoft.PowerShell.EditorServices.Services.TextDocument; @@ -9,34 +10,63 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols { internal static class RegionVisitor { - internal static void FindRegionsInDocument(ScriptFile file, Func action) + internal static void GetRegionsInDocument(ScriptFile file, Func action) { + Stack 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) + token.Extent.StartLineNumber != token.Extent.EndLineNumber || + !TokenOperations.IsBlockComment(i, tokens)) { continue; } - // Look for #region - // Document symbols only care about the symbol start and regex is expensive, - // so skip checking if region is actually closed with #endregion. - if (TokenOperations.IsBlockComment(i, tokens) && - TokenOperations.s_startRegionTextRegex.IsMatch(token.Text)) + // Processing for #region -> #endregion + if (TokenOperations.s_startRegionTextRegex.IsMatch(token.Text)) { - action(new SymbolReference( + 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, - token.Extent.Text.TrimStart().TrimStart('#'), - token.Extent.Text, - token.Extent, - token.Extent, + regionStart.Extent.Text.TrimStart().TrimStart('#'), + regionStart.Extent.Text, + 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)); + } } } } From 6468b563dcd807cca6f906c7cdc98bcee6e083d9 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:33:19 +0000 Subject: [PATCH 04/10] Never scan for region symbols Regions aren't a reference. Only useful as cosmetic/navigation symbol in current document. --- .../Services/Symbols/SymbolsService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs index cf4040642..98449e521 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs @@ -160,7 +160,8 @@ public async Task> ScanForReferencesOfSymbolAsync( SymbolReference symbol, CancellationToken cancellationToken = default) { - if (symbol is null) + // Never scan for regions as they're only useful in current document + if (symbol is null || symbol.Type is SymbolType.Region) { return Enumerable.Empty(); } From 907d48125ab9002b9b595fdefe6bdd0afe4bbf67 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:33:33 +0000 Subject: [PATCH 05/10] Exclude hover for Region symbols --- .../Services/TextDocument/Handlers/HoverHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs index 567da0159..6db56ac09 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs @@ -52,7 +52,7 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync( request.Position.Line + 1, request.Position.Character + 1).ConfigureAwait(false); - if (symbolDetails is null) + if (symbolDetails is null || symbolDetails.SymbolReference.Type is SymbolType.Region) { return null; } From 4aed74c5526113ad620f6b640745d095f7cc2884 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:44:23 +0000 Subject: [PATCH 06/10] Exclude highlight for Region symbols --- .../TextDocument/Handlers/DocumentHighlightHandler.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs index 5758e59a1..df0feb881 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentHighlightHandler.cs @@ -53,6 +53,11 @@ public override Task Handle( List highlights = new(); foreach (SymbolReference occurrence in occurrences) { + if (occurrence.Type is SymbolType.Region) + { + continue; + } + highlights.Add(new DocumentHighlight { Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable? From 8e59ea8a15497e5a0b9a587a935ec63fcfff880d Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:57:27 +0000 Subject: [PATCH 07/10] cleanup symbol name and id --- .../Services/Symbols/Visitors/RegionVisitor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs index cfaa17cdd..1b76c9985 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs @@ -50,8 +50,8 @@ internal static void GetRegionsInDocument(ScriptFile file, Func Date: Sat, 18 Feb 2023 12:57:36 +0000 Subject: [PATCH 08/10] update tests --- .../Symbols/MultipleSymbols.ps1 | 2 ++ .../Language/SymbolsServiceTests.cs | 1 + 2 files changed, 3 insertions(+) diff --git a/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 b/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 index d07522c6a..d64b52491 100644 --- a/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 +++ b/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 @@ -48,4 +48,6 @@ abc #endregion #> #region my region 123 + #endregion +#region unclosed region diff --git a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs index 18662b87c..b3a7cbec8 100644 --- a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs @@ -826,6 +826,7 @@ public void FindsSymbolsInFile() Assert.Equal("region my region 123", symbol.Id); Assert.Equal("#region my region 123", symbol.Name); AssertIsRegion(symbol.NameRegion, 50, 1, 50, 22); + AssertIsRegion(symbol.ScriptRegion, 50, 1, 52, 11); Assert.True(symbol.IsDeclaration); } From bea72934535d69108edd596270dea3a2f5b68b7f Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Fri, 24 Feb 2023 20:39:04 +0000 Subject: [PATCH 09/10] Get region symbols only for DocumentSymbolHandler --- .../Services/Symbols/ReferenceTable.cs | 1 - .../Services/Symbols/SymbolsService.cs | 2 +- .../Symbols/Visitors/RegionVisitor.cs | 6 +-- .../Handlers/DocumentHighlightHandler.cs | 5 --- .../Handlers/DocumentSymbolHandler.cs | 5 +-- .../TextDocument/Handlers/HoverHandler.cs | 2 +- .../Symbols/MultipleSymbols.ps1 | 9 +++-- .../Language/SymbolsServiceTests.cs | 39 +++++++++++++++---- 8 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs index bd7451b21..6cb8e52c4 100644 --- a/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs +++ b/src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs @@ -79,7 +79,6 @@ internal void EnsureInitialized() } _parent.ScriptAst.Visit(new SymbolVisitor(_parent, AddReference)); - RegionVisitor.GetRegionsInDocument(_parent, AddReference); } private AstVisitAction AddReference(SymbolReference symbol) diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs index 98449e521..cfba67ff6 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs @@ -161,7 +161,7 @@ public async Task> ScanForReferencesOfSymbolAsync( CancellationToken cancellationToken = default) { // Never scan for regions as they're only useful in current document - if (symbol is null || symbol.Type is SymbolType.Region) + if (symbol is null) { return Enumerable.Empty(); } diff --git a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs index 1b76c9985..d85c06dd3 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Visitors/RegionVisitor.cs @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols { internal static class RegionVisitor { - internal static void GetRegionsInDocument(ScriptFile file, Func action) + internal static IEnumerable GetRegionsInDocument(ScriptFile file) { Stack tokenCommentRegionStack = new(); Token[] tokens = file.ScriptTokens; @@ -48,7 +48,7 @@ internal static void GetRegionsInDocument(ScriptFile file, Func Handle( List highlights = new(); foreach (SymbolReference occurrence in occurrences) { - if (occurrence.Type is SymbolType.Region) - { - continue; - } - highlights.Add(new DocumentHighlight { Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable? diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs index 823d01f26..435a4e585 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs @@ -50,10 +50,7 @@ public override async Task Handle(Do ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri); IEnumerable foundSymbols = ProvideDocumentSymbols(scriptFile); - if (foundSymbols is null) - { - return null; - } + foundSymbols = foundSymbols.Concat(RegionVisitor.GetRegionsInDocument(scriptFile)); string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath); diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs index 6db56ac09..567da0159 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs @@ -52,7 +52,7 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync( request.Position.Line + 1, request.Position.Character + 1).ConfigureAwait(false); - if (symbolDetails is null || symbolDetails.SymbolReference.Type is SymbolType.Region) + if (symbolDetails is null) { return null; } diff --git a/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 b/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 index d64b52491..2831ea332 100644 --- a/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 +++ b/test/PowerShellEditorServices.Test.Shared/Symbols/MultipleSymbols.ps1 @@ -43,11 +43,14 @@ AFunction AnAdvancedFunction <# -#region don't find me +#region don't find me inside comment block abc #endregion #> -#region my region 123 +#region find me outer +#region find me inner + +#endregion #endregion -#region unclosed region +#region ignore this unclosed region diff --git a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs index b3a7cbec8..7ef2b67c1 100644 --- a/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs @@ -145,6 +145,13 @@ private IEnumerable FindSymbolsInFile(ScriptRegion scriptRegion .OrderBy(symbol => symbol.ScriptRegion.ToRange().Start); } + private IEnumerable FindRegionsInFile(ScriptRegion scriptRegion) + { + return RegionVisitor + .GetRegionsInDocument(GetScriptFile(scriptRegion)) + .OrderBy(symbol => symbol.ScriptRegion.ToRange().Start); + } + [Fact] public async Task FindsParameterHintsOnCommand() { @@ -821,13 +828,6 @@ public void FindsSymbolsInFile() Assert.Equal("prop AValue", symbol.Id); Assert.Equal("AValue", symbol.Name); Assert.True(symbol.IsDeclaration); - - symbol = Assert.Single(symbols.Where(i => i.Type == SymbolType.Region)); - Assert.Equal("region my region 123", symbol.Id); - Assert.Equal("#region my region 123", symbol.Name); - AssertIsRegion(symbol.NameRegion, 50, 1, 50, 22); - AssertIsRegion(symbol.ScriptRegion, 50, 1, 52, 11); - Assert.True(symbol.IsDeclaration); } [Fact] @@ -959,5 +959,30 @@ public void FindsSymbolsInNoSymbolsFile() IEnumerable symbolsResult = FindSymbolsInFile(FindSymbolsInNoSymbolsFile.SourceDetails); Assert.Empty(symbolsResult); } + + [Fact] + public void FindsRegionSymbolsInFile() + { + IEnumerable 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); + }); + } } } From 196edbcaa6e649624a0c1002ada40207c9992a65 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Fri, 24 Feb 2023 20:40:29 +0000 Subject: [PATCH 10/10] cleanup comment --- src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs index cfba67ff6..cf4040642 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs @@ -160,7 +160,6 @@ public async Task> ScanForReferencesOfSymbolAsync( SymbolReference symbol, CancellationToken cancellationToken = default) { - // Never scan for regions as they're only useful in current document if (symbol is null) { return Enumerable.Empty();