From a9a235d469ee240ba2316b8f6da1cd15e87510d5 Mon Sep 17 00:00:00 2001 From: ayousuf23 <72010390+ayousuf23@users.noreply.github.com> Date: Wed, 9 Feb 2022 15:59:29 -0500 Subject: [PATCH] Prevent tab completion if the word to complete is a single dash (#16781) * added code to prevent tab completion if the word to complete is a single dash * added a test for testing tab completion if the word to complete is a single dash * Minor update and add comment Co-authored-by: Dongbo Wang --- .../engine/CommandCompletion/CompletionCompleters.cs | 3 ++- test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index d8360e714d..f0157ae8b1 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -1329,7 +1329,8 @@ namespace System.Management.Automation context.Options.Remove("LiteralPaths"); } - if (context.WordToComplete != string.Empty && context.WordToComplete.Contains('-')) + // The word to complete contains a dash and it's not the first character. We try command names in this case. + if (context.WordToComplete.IndexOf('-') > 0) { var commandResults = CompleteCommand(context); if (commandResults != null) diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index d2fa38dd2b..e05ba3352b 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1435,6 +1435,13 @@ dir -Recurse ` $res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length $res.CompletionMatches | Should -BeNullOrEmpty } + + It "A single dash should not complete to anything" { + function test-{} + $inputStr = 'git -' + $res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length + $res.CompletionMatches | Should -BeNullOrEmpty + } } Context "Tab completion error tests" {