Skip to content

Commit 697a696

Browse files
Copilotkelleyma49
andauthored
Quote array items in Invoke-FzfPsReadlineHandlerProvider to fix multi-select ambiguity (#354)
* Initial plan * Implement AlwaysQuote for array items in Invoke-FzfPsReadlineHandlerProvider Co-authored-by: kelleyma49 <2152684+kelleyma49@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kelleyma49 <2152684+kelleyma49@users.noreply.github.com>
1 parent e795521 commit 697a696

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

PSFzf.Base.ps1

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,27 @@ class FzfDefaultCmd {
103103
}
104104
}
105105

106-
function FixCompletionResult($str) {
106+
function FixCompletionResult($str, [switch]$AlwaysQuote) {
107107
if ([string]::IsNullOrEmpty($str)) {
108108
return ""
109109
}
110-
elseif ($str.Contains(" ") -or $str.Contains("`t")) {
111-
$str = $str.Replace("`r`n", "")
112-
# check if already quoted
113-
if (($str.StartsWith("'") -and $str.EndsWith("'")) -or `
114-
($str.StartsWith("""") -and $str.EndsWith(""""))) {
115-
return $str
116-
}
117-
else {
118-
return """{0}""" -f $str
119-
}
120-
110+
111+
$str = $str.Replace("`r`n", "")
112+
113+
# check if already quoted
114+
$isAlreadyQuoted = ($str.StartsWith("'") -and $str.EndsWith("'")) -or `
115+
($str.StartsWith("""") -and $str.EndsWith(""""))
116+
117+
if ($isAlreadyQuoted) {
118+
return $str
119+
}
120+
121+
# Quote if it contains spaces/tabs, or if AlwaysQuote is specified
122+
if ($AlwaysQuote -or $str.Contains(" ") -or $str.Contains("`t")) {
123+
return """{0}""" -f $str
121124
}
122-
else {
123-
return $str.Replace("`r`n", "")
125+
else {
126+
return $str
124127
}
125128
}
126129

@@ -807,7 +810,7 @@ function Invoke-FzfPsReadlineHandlerProvider {
807810
else {
808811
$resultFull = $result[$i]
809812
}
810-
$result[$i] = FixCompletionResult $resultFull
813+
$result[$i] = FixCompletionResult $resultFull -AlwaysQuote
811814
}
812815
}
813816
else {

PSFzf.tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,28 @@ Describe "Check FixCompletionResult" {
545545
FixCompletionResult("'with space and already quoted'") | Should -Be "'with space and already quoted'"
546546
}
547547
}
548+
549+
Context "AlwaysQuote Parameter Tests" {
550+
It "Should quote simple string when AlwaysQuote is used" {
551+
FixCompletionResult -str "file.txt" -AlwaysQuote | Should -Be """file.txt"""
552+
}
553+
554+
It "Should not double-quote already double-quoted string with AlwaysQuote" {
555+
FixCompletionResult -str """already_quoted.txt""" -AlwaysQuote | Should -Be """already_quoted.txt"""
556+
}
557+
558+
It "Should not double-quote already single-quoted string with AlwaysQuote" {
559+
FixCompletionResult -str "'already_quoted.txt'" -AlwaysQuote | Should -Be "'already_quoted.txt'"
560+
}
561+
562+
It "Should quote string with spaces when AlwaysQuote is used" {
563+
FixCompletionResult -str "file with spaces.txt" -AlwaysQuote | Should -Be """file with spaces.txt"""
564+
}
565+
566+
It "Should not double-quote already quoted string with spaces when AlwaysQuote is used" {
567+
FixCompletionResult -str """file with spaces.txt""" -AlwaysQuote | Should -Be """file with spaces.txt"""
568+
}
569+
}
548570
}
549571
}
550572

0 commit comments

Comments
 (0)