From 3296e35ca9be1abda20bd6a90d1af6e5394af54f Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Mon, 20 Feb 2023 21:48:38 +0000 Subject: [PATCH 1/4] Support module-qualified calls for Pester keywords --- .../Symbols/PesterDocumentSymbolProvider.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs index 7a77d7607..46f571ff2 100644 --- a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs +++ b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Management.Automation.Language; using Microsoft.PowerShell.EditorServices.Services.TextDocument; +using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility; namespace Microsoft.PowerShell.EditorServices.Services.Symbols { @@ -42,8 +43,7 @@ IEnumerable IDocumentSymbolProvider.ProvideDocumentSymbols( private static bool IsNamedCommandWithArguments(Ast ast) { return ast is CommandAst commandAst && - commandAst.InvocationOperator != TokenKind.Dot && - PesterSymbolReference.GetCommandType(commandAst.GetCommandName()).HasValue && + commandAst.InvocationOperator is not (TokenKind.Dot or TokenKind.Ampersand) && commandAst.CommandElements.Count >= 2; } @@ -59,8 +59,10 @@ private static bool IsPesterCommand(CommandAst commandAst) return false; } - // Ensure the first word is a Pester keyword - if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandAst.GetCommandName())) + // Ensure the first word is a Pester keyword and in Pester-module if using module-qualified call + string commandName = CommandHelpers.StripModuleQualification(commandAst.GetCommandName(), out ReadOnlyMemory module); + if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandName) || + (!module.IsEmpty && !string.Equals(module.ToString(), "pester", StringComparison.OrdinalIgnoreCase))) { return false; } @@ -90,14 +92,16 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil .TrimStart() .TrimEnd(DefinitionTrimChars); - PesterCommandType? commandName = PesterSymbolReference.GetCommandType(pesterCommandAst.GetCommandName()); - if (commandName == null) + string commandName = CommandHelpers.StripModuleQualification(pesterCommandAst.GetCommandName(), out _); + PesterCommandType? commandType = PesterSymbolReference.GetCommandType(commandName); + if (commandType == null) { return null; } string testName = null; - if (PesterSymbolReference.IsPesterTestCommand(commandName.Value)) { + if (PesterSymbolReference.IsPesterTestCommand(commandType.Value)) + { // Search for a name for the test // If the test has more than one argument for names, we set it to null bool alreadySawName = false; @@ -130,7 +134,7 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil return new PesterSymbolReference( scriptFile, - commandName.Value, + commandType.Value, symbolName, testName, pesterCommandAst.Extent From 3d1191eac2b4e496fad2423fe77b4bd855f589b2 Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andschwa@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:44:35 -0800 Subject: [PATCH 2/4] Update src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs Co-authored-by: Patrick Meinecke --- .../Services/Symbols/PesterDocumentSymbolProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs index 46f571ff2..187ba9f06 100644 --- a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs +++ b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs @@ -62,7 +62,7 @@ private static bool IsPesterCommand(CommandAst commandAst) // Ensure the first word is a Pester keyword and in Pester-module if using module-qualified call string commandName = CommandHelpers.StripModuleQualification(commandAst.GetCommandName(), out ReadOnlyMemory module); if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandName) || - (!module.IsEmpty && !string.Equals(module.ToString(), "pester", StringComparison.OrdinalIgnoreCase))) + !module.Span.Equals("pester".AsSpan(), StringComparison.OrdinalIgnoreCase) { return false; } From fff7b9485ff8174cf1449fea934f2af15fd2d8e3 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Wed, 22 Feb 2023 19:56:05 +0100 Subject: [PATCH 3/4] Update src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs --- .../Services/Symbols/PesterDocumentSymbolProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs index 187ba9f06..c666641a5 100644 --- a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs +++ b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs @@ -62,7 +62,7 @@ private static bool IsPesterCommand(CommandAst commandAst) // Ensure the first word is a Pester keyword and in Pester-module if using module-qualified call string commandName = CommandHelpers.StripModuleQualification(commandAst.GetCommandName(), out ReadOnlyMemory module); if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandName) || - !module.Span.Equals("pester".AsSpan(), StringComparison.OrdinalIgnoreCase) + !module.Span.Equals("pester".AsSpan(), StringComparison.OrdinalIgnoreCase)) { return false; } From 95a1cc08096cc290c81fbf8960921e37fd04c8ec Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andschwa@users.noreply.github.com> Date: Wed, 22 Feb 2023 11:08:00 -0800 Subject: [PATCH 4/4] Update src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs --- .../Services/Symbols/PesterDocumentSymbolProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs index c666641a5..dcf675975 100644 --- a/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs +++ b/src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs @@ -62,7 +62,7 @@ private static bool IsPesterCommand(CommandAst commandAst) // Ensure the first word is a Pester keyword and in Pester-module if using module-qualified call string commandName = CommandHelpers.StripModuleQualification(commandAst.GetCommandName(), out ReadOnlyMemory module); if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandName) || - !module.Span.Equals("pester".AsSpan(), StringComparison.OrdinalIgnoreCase)) + (!module.IsEmpty && !module.Span.Equals("pester".AsSpan(), StringComparison.OrdinalIgnoreCase))) { return false; }