@drfed/drfed 0.1.0 → 0.2.0-dev.605

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/completions/bash/drfed-account +529 -30
  2. package/completions/bash/drfed-migrate +529 -30
  3. package/completions/bash/drfed-server +529 -30
  4. package/completions/bash/drfed-web +529 -30
  5. package/completions/fish/drfed-account.fish +137 -49
  6. package/completions/fish/drfed-migrate.fish +137 -49
  7. package/completions/fish/drfed-server.fish +137 -49
  8. package/completions/fish/drfed-web.fish +137 -49
  9. package/completions/nu/drfed-account.nu +303 -73
  10. package/completions/nu/drfed-migrate.nu +303 -73
  11. package/completions/nu/drfed-server.nu +303 -73
  12. package/completions/nu/drfed-web.nu +303 -73
  13. package/completions/pwsh/drfed-account.ps1 +153 -34
  14. package/completions/pwsh/drfed-migrate.ps1 +153 -34
  15. package/completions/pwsh/drfed-server.ps1 +153 -34
  16. package/completions/pwsh/drfed-web.ps1 +153 -34
  17. package/completions/zsh/_drfed-account +532 -34
  18. package/completions/zsh/_drfed-migrate +532 -34
  19. package/completions/zsh/_drfed-server +532 -34
  20. package/completions/zsh/_drfed-web +532 -34
  21. package/dist/api-mail-parser.d.mts.map +1 -1
  22. package/dist/commands/account-program.d.mts.map +1 -1
  23. package/dist/commands/account.mjs +6 -4
  24. package/dist/commands/account.mjs.map +1 -1
  25. package/dist/commands/migrate-program.d.mts.map +1 -1
  26. package/dist/commands/migrate.mjs +6 -4
  27. package/dist/commands/migrate.mjs.map +1 -1
  28. package/dist/commands/web-program.d.mts.map +1 -1
  29. package/dist/commands/web.mjs +7 -5
  30. package/dist/commands/web.mjs.map +1 -1
  31. package/dist/index.mjs +4 -3
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/jmap-parser.d.mts.map +1 -1
  34. package/dist/logging.d.mts.map +1 -1
  35. package/dist/mail-parser-DyI4wuQp.d.mts.map +1 -1
  36. package/dist/mail-runtime.mjs +1 -2
  37. package/dist/mail-settings-DjWMAUaE.d.mts.map +1 -1
  38. package/dist/mail-settings.mjs +1 -2
  39. package/dist/operations.mjs.map +1 -1
  40. package/dist/package-BRpsF5SF.mjs +6 -0
  41. package/dist/package-BRpsF5SF.mjs.map +1 -0
  42. package/dist/parser-TANGHRjL.d.mts.map +1 -1
  43. package/dist/resources.mjs.map +1 -1
  44. package/package.json +18 -18
  45. package/dist/package-7FOUqcPO.mjs +0 -6
  46. package/dist/package-7FOUqcPO.mjs.map +0 -1
@@ -2,27 +2,48 @@
2
2
  Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
3
3
  param($wordToComplete, $commandAst, $cursorPosition)
4
4
 
5
+ # CompletionText is shell syntax; display labels remain unquoted.
6
+ $quoteArgument = {
7
+ param([string]$value)
8
+ if ($value -cmatch '\A[a-zA-Z0-9_./:\\-]+\z' -and $value -cnotmatch '\A-(?!-).*[.:]' -and $value -cnotmatch '\A-?(?:[0-9]|\.[0-9])') {
9
+ return $value
10
+ }
11
+ return "'" + [System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent($value) + "'"
12
+ }
13
+
5
14
  # Extract arguments from AST (handles quoted strings properly)
6
15
  $arguments = @()
7
16
  $commandElements = $commandAst.CommandElements
17
+ $valuePrefix = $wordToComplete
18
+ $quoteType = $null
8
19
 
9
- # Determine the range of elements to extract
10
- # Exclude the last element if it matches wordToComplete (partial input case)
11
- $maxIndex = $commandElements.Count - 1
12
- if ($commandElements.Count -gt 1) {
13
- $lastElement = $commandElements[$commandElements.Count - 1]
14
- $lastText = if ($lastElement -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
15
- $lastElement.Value
16
- } else {
17
- $lastElement.Extent.Text
18
- }
19
- if ($lastText -eq $wordToComplete) {
20
- $maxIndex = $commandElements.Count - 2
21
- }
22
- }
23
-
24
- for ($i = 1; $i -le $maxIndex; $i++) {
20
+ # Keep only complete arguments before the cursor. The current word
21
+ # (including a token ending exactly at the cursor) is appended below.
22
+ for ($i = 1; $i -lt $commandElements.Count; $i++) {
25
23
  $element = $commandElements[$i]
24
+ if ($element.Extent.EndOffset -ge $cursorPosition) {
25
+ # wordToComplete has synthesized delimiters and decoded escapes.
26
+ # Only the AST retains the literal value and original quote type.
27
+ # PSReadLine leaves the cursor before a directory's closing quote.
28
+ if ($element -is [System.Management.Automation.Language.StringConstantExpressionAst] -and
29
+ ($element.Extent.EndOffset -eq $cursorPosition -or
30
+ ($element.Extent.EndOffset -eq ($cursorPosition + 1) -and
31
+ $element.StringConstantType -in @('SingleQuoted', 'DoubleQuoted')))) {
32
+ if ($element.StringConstantType -in @('SingleQuoted', 'DoubleQuoted')) {
33
+ $valuePrefix = $element.Value
34
+ $quoteType = $element.StringConstantType
35
+ } elseif ($element.Extent.Text -match "^[^=]+=['\u2018-\u201b]") {
36
+ # Joined values are BareWord constants despite their quotes.
37
+ # Quote the entire replacement to keep it one argument.
38
+ $valuePrefix = $element.Value
39
+ $quoteType = 'SingleQuoted'
40
+ } elseif ($element.Extent.Text -match '^[^=]+=["\u201c-\u201e]') {
41
+ $valuePrefix = $element.Value
42
+ $quoteType = 'DoubleQuoted'
43
+ }
44
+ }
45
+ break
46
+ }
26
47
 
27
48
  if ($element -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
28
49
  $arguments += $element.Value
@@ -31,30 +52,54 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
31
52
  }
32
53
  }
33
54
 
55
+ # CompletionText replaces the whole token, including its opening quote.
56
+ # Escape literals without evaluating variables or expressions.
57
+ $quoteCompletion = {
58
+ param($text)
59
+ if ($quoteType -eq 'SingleQuoted') {
60
+ "'" + ($text -replace "['\u2018-\u201b]", '$0$0') + "'"
61
+ } elseif ($quoteType -eq 'DoubleQuoted') {
62
+ '"' + ($text -replace '[\x60$"\u201c-\u201e]', '`$0') + '"'
63
+ } else {
64
+ & $quoteArgument $text
65
+ }
66
+ }
67
+
34
68
  # Build arguments array for completion command
35
69
  $completionArgs = @()
36
70
  $completionArgs += @('completion', 'pwsh')
37
71
  $completionArgs += $arguments
38
- $completionArgs += $wordToComplete
39
72
 
40
73
  # Call completion command and capture output
41
74
  try {
42
- $output = & drfed-account $completionArgs 2>$null
75
+ # Quote the prefix separately: array arguments expand a literal tilde
76
+ # when passed to native programs on Unix.
77
+ $output = & drfed-account $completionArgs "$valuePrefix" 2>$null
43
78
  if (-not $output) { return }
44
79
 
45
80
  # Parse tab-separated output and create CompletionResult objects
46
81
  $output -split "`n" | ForEach-Object {
47
- $line = $_.Trim()
82
+ $literal = $_.StartsWith('__LITERAL__:', [System.StringComparison]::Ordinal)
83
+ # Quoted values preserve whitespace; only remove the CR of CRLF.
84
+ # Keep the existing trimming for unquoted completions.
85
+ $line = if ($null -ne $quoteType) { $_.TrimEnd([char]13) } else { $_.Trim() }
48
86
  if (-not $line) { return }
87
+ if ($literal) { $line = $line.Substring(12) }
49
88
 
50
- if ($line -match '^__FILE__:') {
89
+ if (-not $literal -and $_.StartsWith('__FILE__:', [System.StringComparison]::Ordinal)) {
51
90
  # Parse file completion directive: __FILE__:type:extensions:pattern:hidden
52
91
  $directive = ($line -split "`t")[0]
53
- $parts = $directive -split ':', 5
92
+ $parts = $directive -split ':', 6
54
93
  $type = $parts[1]
55
94
  $extensions = $parts[2]
56
95
  $pattern = $parts[3] -replace '%3A', ':' -replace '%25', '%'
57
96
  $hidden = $parts[4] -eq '1'
97
+ $insertionPrefix = if ($parts.Length -gt 5) { $parts[5] -replace '%3A', ':' -replace '%25', '%' } else { '' }
98
+ $matchingWord = $valuePrefix
99
+ if ($insertionPrefix -and $pattern.StartsWith($insertionPrefix, [System.StringComparison]::Ordinal) -and $matchingWord.StartsWith($insertionPrefix, [System.StringComparison]::Ordinal)) {
100
+ $pattern = $pattern.Substring($insertionPrefix.Length)
101
+ $matchingWord = $matchingWord.Substring($insertionPrefix.Length)
102
+ } else { $insertionPrefix = '' }
58
103
 
59
104
  # When a pattern is specified, use it as the file matching
60
105
  # prefix instead of the current word. If the user has
@@ -63,7 +108,7 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
63
108
  # comparing so that Windows backslashes match forward slashes
64
109
  # in the transported pattern.
65
110
  $normalizedPattern = if ($pattern) { $pattern.Replace('\', '/') -replace '^\./','' } else { '' }
66
- $normalizedWord = if ($wordToComplete) { $wordToComplete.Replace('\', '/') -replace '^\./','' } else { '' }
111
+ $normalizedWord = if ($matchingWord) { $matchingWord.Replace('\', '/') -replace '^\./','' } else { '' }
67
112
  $comparison = if ($IsWindows) { [System.StringComparison]::OrdinalIgnoreCase } else { [System.StringComparison]::Ordinal }
68
113
  # For wildcard patterns, compare only the directory prefix
69
114
  $comparePattern = $normalizedPattern
@@ -75,12 +120,25 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
75
120
  $comparePattern = ''
76
121
  }
77
122
  }
78
- $prefix = if (($comparePattern -or $normalizedWord) -and $normalizedWord -and $normalizedWord.StartsWith($comparePattern, $comparison) -and $normalizedWord.Length -ge $comparePattern.Length -and $wordToComplete -ne $pattern) {
79
- $wordToComplete
123
+ # A quoted home-pattern directory inserts an absolute path.
124
+ # Compare subsequent input against the same expanded prefix.
125
+ # Detect home syntax before removing ./, which can target a
126
+ # literal local tilde directory rather than the home directory.
127
+ $homePattern = $pattern -and ($pattern -eq '~' -or $pattern.Replace('\', '/').StartsWith('~/'))
128
+ if ($homePattern) {
129
+ # Preserve the home root when the pattern is exactly ~.
130
+ $expandedPattern = if ($comparePattern -eq '~') { $HOME } else { $HOME.Replace('\', '/').TrimEnd('/') + $comparePattern.Substring(1) }
131
+ $expandedPattern = [IO.Path]::GetFullPath($expandedPattern).Replace('\', '/')
132
+ if ($normalizedWord.StartsWith($expandedPattern, $comparison)) {
133
+ $comparePattern = $expandedPattern
134
+ }
135
+ }
136
+ $prefix = if (($comparePattern -or $normalizedWord) -and $normalizedWord -and $normalizedWord.StartsWith($comparePattern, $comparison) -and $normalizedWord.Length -ge $comparePattern.Length -and $matchingWord -ne $pattern) {
137
+ $matchingWord
80
138
  } elseif ($pattern) {
81
139
  $pattern
82
- } elseif ($wordToComplete) {
83
- $wordToComplete
140
+ } elseif ($matchingWord) {
141
+ $matchingWord
84
142
  } else { '' }
85
143
 
86
144
  # Use -Force to include hidden files when requested, or when
@@ -88,15 +146,35 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
88
146
  $prefixBasename = if ($prefix) { Split-Path -Leaf $prefix } else { '' }
89
147
  $forceParam = if ($hidden -or ($prefixBasename -and $prefixBasename.StartsWith('.'))) { @{Force = $true} } else { @{} }
90
148
 
149
+ # A quoted tilde is literal when passed to a native command.
150
+ # Prevent PowerShell's file provider from expanding it to home.
151
+ # Parser-provided home patterns still need home expansion.
152
+ $prefixFromInput = $matchingWord -and $prefix -ceq $matchingWord
153
+ $lookupPrefix = if ($prefixFromInput -and $null -ne $quoteType -and $prefix.StartsWith('~')) { './' + $prefix } else { $prefix }
154
+
155
+ # Detect narrowing before directory normalization changes prefix.
156
+ $narrowFixedGlob = $type -ne 'directory' -and $pattern -match '[*?]' -and $prefix -ceq $matchingWord -and $pattern -cne $matchingWord
157
+
91
158
  # If prefix is a directory without trailing slash, append
92
159
  # one so Get-ChildItem lists its contents
93
- if ($prefix -and (Test-Path -Path $prefix -PathType Container) -and -not $prefix.EndsWith('/') -and -not $prefix.EndsWith('\')) {
160
+ if ($prefix -and (Test-Path -Path $lookupPrefix -PathType Container) -and -not $prefix.EndsWith('/') -and -not $prefix.EndsWith('\')) {
94
161
  $prefix = $prefix + '/'
162
+ $lookupPrefix = $lookupPrefix + '/'
95
163
  }
96
164
 
97
165
  # Build the glob path—when the prefix contains wildcard
98
166
  # characters, use it as-is; otherwise append *
99
- $globPath = if ($prefix -match '[*?]') { $prefix } else { "${prefix}*" }
167
+ $globPath = if ($lookupPrefix -match '[*?]') { $lookupPrefix } else { "${lookupPrefix}*" }
168
+
169
+ # Keep the original native glob as an independent file filter
170
+ # when the current word narrows its candidates.
171
+ $fixedMatches = @()
172
+ if ($narrowFixedGlob) {
173
+ # Include hidden parents here; candidate enumeration still
174
+ # controls which hidden children can be suggested.
175
+ $fixedMatches = @(Get-Item -Force -Path $pattern -ErrorAction SilentlyContinue |
176
+ Select-Object -ExpandProperty FullName)
177
+ }
100
178
 
101
179
  # Get file system items based on type
102
180
  $items = @()
@@ -107,7 +185,10 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
107
185
  $extList = ($extensions -split ',') -replace '^\.',''
108
186
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue |
109
187
  Where-Object {
110
- $_.PSIsContainer -or (($_.Extension -replace '^\.','' ) -in $extList)
188
+ $fileName = $_.Name
189
+ $_.PSIsContainer -or (@($extList | Where-Object {
190
+ $fileName.EndsWith('.' + $_, [System.StringComparison]::Ordinal)
191
+ }).Count -gt 0)
111
192
  }
112
193
  } else {
113
194
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue
@@ -122,7 +203,10 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
122
203
  $extList = ($extensions -split ',') -replace '^\.',''
123
204
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue |
124
205
  Where-Object {
125
- $_.PSIsContainer -or (($_.Extension -replace '^\.','' ) -in $extList)
206
+ $fileName = $_.Name
207
+ $_.PSIsContainer -or (@($extList | Where-Object {
208
+ $fileName.EndsWith('.' + $_, [System.StringComparison]::Ordinal)
209
+ }).Count -gt 0)
126
210
  }
127
211
  } else {
128
212
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue
@@ -133,7 +217,7 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
133
217
  # For file/any modes with glob patterns, also add directories
134
218
  # from the base directory for navigation
135
219
  if ($prefix -match '[*?]' -and $type -ne 'directory') {
136
- $globDir = Split-Path -Parent $prefix
220
+ $globDir = Split-Path -Parent $lookupPrefix
137
221
  if (-not $globDir) { $globDir = '.' }
138
222
  $extraDirs = Get-ChildItem @forceParam -Directory -Path "$globDir/*" -ErrorAction SilentlyContinue
139
223
  if ($extraDirs) { $items = @($items) + @($extraDirs) | Select-Object -Unique }
@@ -144,6 +228,14 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
144
228
  $items = $items | Where-Object { -not $_.Attributes.HasFlag([System.IO.FileAttributes]::Hidden) }
145
229
  }
146
230
 
231
+ if ($narrowFixedGlob) {
232
+ $items = $items | Where-Object {
233
+ if ($_.PSIsContainer) { $true }
234
+ elseif ($IsWindows) { $fixedMatches -contains $_.FullName }
235
+ else { $fixedMatches -ccontains $_.FullName }
236
+ }
237
+ }
238
+
147
239
  # Extract directory prefix to preserve in completion text
148
240
  $dirPrefix = if ($prefix -and ($prefix.Contains('/') -or $prefix.Contains('\'))) {
149
241
  $slashIdx = [Math]::Max($prefix.LastIndexOf('/'), $prefix.LastIndexOf('\'))
@@ -154,10 +246,34 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
154
246
  $items | ForEach-Object {
155
247
  $completionText = if ($_.PSIsContainer) { "$dirPrefix$($_.Name)/" } else { "$dirPrefix$($_.Name)" }
156
248
  $itemType = if ($_.PSIsContainer) { 'Directory' } else { 'File' }
249
+ $resultType = if ($_.PSIsContainer) { 'ProviderContainer' } else { 'ParameterValue' }
250
+ # PSReadLine recognizes the platform separator for containers.
251
+ # Keep the display label's existing slash convention.
252
+ $directorySuffix = [IO.Path]::DirectorySeparatorChar
253
+ $pathText = if ($_.PSIsContainer) { "$dirPrefix$($_.Name)$directorySuffix" } else { $completionText }
254
+ # Quoting disables native tilde expansion. A home pattern
255
+ # supplied by the parser must insert the discovered path.
256
+ $replacementText = if ($null -ne $quoteType -and -not $prefixFromInput -and $homePattern) {
257
+ if ($_.PSIsContainer) { $_.FullName + $directorySuffix } else { $_.FullName }
258
+ } else { $pathText }
259
+ # Quoting suppresses native tilde expansion. Keep safe
260
+ # ~/ paths bare; otherwise use the enumerated full path.
261
+ # Keyed paths keep the typed spelling, including ~/, and
262
+ # always take the quoted argument form.
263
+ $insertionText = if ($null -ne $quoteType) {
264
+ & $quoteCompletion ($insertionPrefix + $replacementText)
265
+ } elseif (-not $insertionPrefix -and $completionText -cmatch '\A~/[a-zA-Z0-9_./:\\-]*\z') {
266
+ $pathText
267
+ } else {
268
+ $argumentText = if (-not $insertionPrefix -and ($completionText.StartsWith('~/') -or $completionText.StartsWith('~\'))) {
269
+ if ($_.PSIsContainer) { $_.FullName + $directorySuffix } else { $_.FullName }
270
+ } else { $pathText }
271
+ & $quoteArgument ($insertionPrefix + $argumentText)
272
+ }
157
273
  [System.Management.Automation.CompletionResult]::new(
274
+ $insertionText,
158
275
  $completionText,
159
- $completionText,
160
- 'ParameterValue',
276
+ $resultType,
161
277
  $itemType
162
278
  )
163
279
  }
@@ -165,11 +281,14 @@ Register-ArgumentCompleter -Native -CommandName drfed-account -ScriptBlock {
165
281
  # Parse literal completion: text\tlistItemText\tdescription
166
282
  $parts = $line -split "`t", 3
167
283
  $completionText = $parts[0]
284
+ # Empty display text is invalid for CompletionResult. Keep
285
+ # ignoring empty suggestions without dropping later records.
286
+ if (-not $completionText) { return }
168
287
  $listItemText = if ($parts.Length -gt 1 -and $parts[1]) { $parts[1] } else { $completionText }
169
288
  $toolTip = if ($parts.Length -gt 2 -and $parts[2]) { $parts[2] } else { $completionText }
170
289
 
171
290
  [System.Management.Automation.CompletionResult]::new(
172
- $completionText,
291
+ (& $quoteCompletion $completionText),
173
292
  $listItemText,
174
293
  'ParameterValue',
175
294
  $toolTip
@@ -2,27 +2,48 @@
2
2
  Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
3
3
  param($wordToComplete, $commandAst, $cursorPosition)
4
4
 
5
+ # CompletionText is shell syntax; display labels remain unquoted.
6
+ $quoteArgument = {
7
+ param([string]$value)
8
+ if ($value -cmatch '\A[a-zA-Z0-9_./:\\-]+\z' -and $value -cnotmatch '\A-(?!-).*[.:]' -and $value -cnotmatch '\A-?(?:[0-9]|\.[0-9])') {
9
+ return $value
10
+ }
11
+ return "'" + [System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent($value) + "'"
12
+ }
13
+
5
14
  # Extract arguments from AST (handles quoted strings properly)
6
15
  $arguments = @()
7
16
  $commandElements = $commandAst.CommandElements
17
+ $valuePrefix = $wordToComplete
18
+ $quoteType = $null
8
19
 
9
- # Determine the range of elements to extract
10
- # Exclude the last element if it matches wordToComplete (partial input case)
11
- $maxIndex = $commandElements.Count - 1
12
- if ($commandElements.Count -gt 1) {
13
- $lastElement = $commandElements[$commandElements.Count - 1]
14
- $lastText = if ($lastElement -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
15
- $lastElement.Value
16
- } else {
17
- $lastElement.Extent.Text
18
- }
19
- if ($lastText -eq $wordToComplete) {
20
- $maxIndex = $commandElements.Count - 2
21
- }
22
- }
23
-
24
- for ($i = 1; $i -le $maxIndex; $i++) {
20
+ # Keep only complete arguments before the cursor. The current word
21
+ # (including a token ending exactly at the cursor) is appended below.
22
+ for ($i = 1; $i -lt $commandElements.Count; $i++) {
25
23
  $element = $commandElements[$i]
24
+ if ($element.Extent.EndOffset -ge $cursorPosition) {
25
+ # wordToComplete has synthesized delimiters and decoded escapes.
26
+ # Only the AST retains the literal value and original quote type.
27
+ # PSReadLine leaves the cursor before a directory's closing quote.
28
+ if ($element -is [System.Management.Automation.Language.StringConstantExpressionAst] -and
29
+ ($element.Extent.EndOffset -eq $cursorPosition -or
30
+ ($element.Extent.EndOffset -eq ($cursorPosition + 1) -and
31
+ $element.StringConstantType -in @('SingleQuoted', 'DoubleQuoted')))) {
32
+ if ($element.StringConstantType -in @('SingleQuoted', 'DoubleQuoted')) {
33
+ $valuePrefix = $element.Value
34
+ $quoteType = $element.StringConstantType
35
+ } elseif ($element.Extent.Text -match "^[^=]+=['\u2018-\u201b]") {
36
+ # Joined values are BareWord constants despite their quotes.
37
+ # Quote the entire replacement to keep it one argument.
38
+ $valuePrefix = $element.Value
39
+ $quoteType = 'SingleQuoted'
40
+ } elseif ($element.Extent.Text -match '^[^=]+=["\u201c-\u201e]') {
41
+ $valuePrefix = $element.Value
42
+ $quoteType = 'DoubleQuoted'
43
+ }
44
+ }
45
+ break
46
+ }
26
47
 
27
48
  if ($element -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
28
49
  $arguments += $element.Value
@@ -31,30 +52,54 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
31
52
  }
32
53
  }
33
54
 
55
+ # CompletionText replaces the whole token, including its opening quote.
56
+ # Escape literals without evaluating variables or expressions.
57
+ $quoteCompletion = {
58
+ param($text)
59
+ if ($quoteType -eq 'SingleQuoted') {
60
+ "'" + ($text -replace "['\u2018-\u201b]", '$0$0') + "'"
61
+ } elseif ($quoteType -eq 'DoubleQuoted') {
62
+ '"' + ($text -replace '[\x60$"\u201c-\u201e]', '`$0') + '"'
63
+ } else {
64
+ & $quoteArgument $text
65
+ }
66
+ }
67
+
34
68
  # Build arguments array for completion command
35
69
  $completionArgs = @()
36
70
  $completionArgs += @('completion', 'pwsh')
37
71
  $completionArgs += $arguments
38
- $completionArgs += $wordToComplete
39
72
 
40
73
  # Call completion command and capture output
41
74
  try {
42
- $output = & drfed-migrate $completionArgs 2>$null
75
+ # Quote the prefix separately: array arguments expand a literal tilde
76
+ # when passed to native programs on Unix.
77
+ $output = & drfed-migrate $completionArgs "$valuePrefix" 2>$null
43
78
  if (-not $output) { return }
44
79
 
45
80
  # Parse tab-separated output and create CompletionResult objects
46
81
  $output -split "`n" | ForEach-Object {
47
- $line = $_.Trim()
82
+ $literal = $_.StartsWith('__LITERAL__:', [System.StringComparison]::Ordinal)
83
+ # Quoted values preserve whitespace; only remove the CR of CRLF.
84
+ # Keep the existing trimming for unquoted completions.
85
+ $line = if ($null -ne $quoteType) { $_.TrimEnd([char]13) } else { $_.Trim() }
48
86
  if (-not $line) { return }
87
+ if ($literal) { $line = $line.Substring(12) }
49
88
 
50
- if ($line -match '^__FILE__:') {
89
+ if (-not $literal -and $_.StartsWith('__FILE__:', [System.StringComparison]::Ordinal)) {
51
90
  # Parse file completion directive: __FILE__:type:extensions:pattern:hidden
52
91
  $directive = ($line -split "`t")[0]
53
- $parts = $directive -split ':', 5
92
+ $parts = $directive -split ':', 6
54
93
  $type = $parts[1]
55
94
  $extensions = $parts[2]
56
95
  $pattern = $parts[3] -replace '%3A', ':' -replace '%25', '%'
57
96
  $hidden = $parts[4] -eq '1'
97
+ $insertionPrefix = if ($parts.Length -gt 5) { $parts[5] -replace '%3A', ':' -replace '%25', '%' } else { '' }
98
+ $matchingWord = $valuePrefix
99
+ if ($insertionPrefix -and $pattern.StartsWith($insertionPrefix, [System.StringComparison]::Ordinal) -and $matchingWord.StartsWith($insertionPrefix, [System.StringComparison]::Ordinal)) {
100
+ $pattern = $pattern.Substring($insertionPrefix.Length)
101
+ $matchingWord = $matchingWord.Substring($insertionPrefix.Length)
102
+ } else { $insertionPrefix = '' }
58
103
 
59
104
  # When a pattern is specified, use it as the file matching
60
105
  # prefix instead of the current word. If the user has
@@ -63,7 +108,7 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
63
108
  # comparing so that Windows backslashes match forward slashes
64
109
  # in the transported pattern.
65
110
  $normalizedPattern = if ($pattern) { $pattern.Replace('\', '/') -replace '^\./','' } else { '' }
66
- $normalizedWord = if ($wordToComplete) { $wordToComplete.Replace('\', '/') -replace '^\./','' } else { '' }
111
+ $normalizedWord = if ($matchingWord) { $matchingWord.Replace('\', '/') -replace '^\./','' } else { '' }
67
112
  $comparison = if ($IsWindows) { [System.StringComparison]::OrdinalIgnoreCase } else { [System.StringComparison]::Ordinal }
68
113
  # For wildcard patterns, compare only the directory prefix
69
114
  $comparePattern = $normalizedPattern
@@ -75,12 +120,25 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
75
120
  $comparePattern = ''
76
121
  }
77
122
  }
78
- $prefix = if (($comparePattern -or $normalizedWord) -and $normalizedWord -and $normalizedWord.StartsWith($comparePattern, $comparison) -and $normalizedWord.Length -ge $comparePattern.Length -and $wordToComplete -ne $pattern) {
79
- $wordToComplete
123
+ # A quoted home-pattern directory inserts an absolute path.
124
+ # Compare subsequent input against the same expanded prefix.
125
+ # Detect home syntax before removing ./, which can target a
126
+ # literal local tilde directory rather than the home directory.
127
+ $homePattern = $pattern -and ($pattern -eq '~' -or $pattern.Replace('\', '/').StartsWith('~/'))
128
+ if ($homePattern) {
129
+ # Preserve the home root when the pattern is exactly ~.
130
+ $expandedPattern = if ($comparePattern -eq '~') { $HOME } else { $HOME.Replace('\', '/').TrimEnd('/') + $comparePattern.Substring(1) }
131
+ $expandedPattern = [IO.Path]::GetFullPath($expandedPattern).Replace('\', '/')
132
+ if ($normalizedWord.StartsWith($expandedPattern, $comparison)) {
133
+ $comparePattern = $expandedPattern
134
+ }
135
+ }
136
+ $prefix = if (($comparePattern -or $normalizedWord) -and $normalizedWord -and $normalizedWord.StartsWith($comparePattern, $comparison) -and $normalizedWord.Length -ge $comparePattern.Length -and $matchingWord -ne $pattern) {
137
+ $matchingWord
80
138
  } elseif ($pattern) {
81
139
  $pattern
82
- } elseif ($wordToComplete) {
83
- $wordToComplete
140
+ } elseif ($matchingWord) {
141
+ $matchingWord
84
142
  } else { '' }
85
143
 
86
144
  # Use -Force to include hidden files when requested, or when
@@ -88,15 +146,35 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
88
146
  $prefixBasename = if ($prefix) { Split-Path -Leaf $prefix } else { '' }
89
147
  $forceParam = if ($hidden -or ($prefixBasename -and $prefixBasename.StartsWith('.'))) { @{Force = $true} } else { @{} }
90
148
 
149
+ # A quoted tilde is literal when passed to a native command.
150
+ # Prevent PowerShell's file provider from expanding it to home.
151
+ # Parser-provided home patterns still need home expansion.
152
+ $prefixFromInput = $matchingWord -and $prefix -ceq $matchingWord
153
+ $lookupPrefix = if ($prefixFromInput -and $null -ne $quoteType -and $prefix.StartsWith('~')) { './' + $prefix } else { $prefix }
154
+
155
+ # Detect narrowing before directory normalization changes prefix.
156
+ $narrowFixedGlob = $type -ne 'directory' -and $pattern -match '[*?]' -and $prefix -ceq $matchingWord -and $pattern -cne $matchingWord
157
+
91
158
  # If prefix is a directory without trailing slash, append
92
159
  # one so Get-ChildItem lists its contents
93
- if ($prefix -and (Test-Path -Path $prefix -PathType Container) -and -not $prefix.EndsWith('/') -and -not $prefix.EndsWith('\')) {
160
+ if ($prefix -and (Test-Path -Path $lookupPrefix -PathType Container) -and -not $prefix.EndsWith('/') -and -not $prefix.EndsWith('\')) {
94
161
  $prefix = $prefix + '/'
162
+ $lookupPrefix = $lookupPrefix + '/'
95
163
  }
96
164
 
97
165
  # Build the glob path—when the prefix contains wildcard
98
166
  # characters, use it as-is; otherwise append *
99
- $globPath = if ($prefix -match '[*?]') { $prefix } else { "${prefix}*" }
167
+ $globPath = if ($lookupPrefix -match '[*?]') { $lookupPrefix } else { "${lookupPrefix}*" }
168
+
169
+ # Keep the original native glob as an independent file filter
170
+ # when the current word narrows its candidates.
171
+ $fixedMatches = @()
172
+ if ($narrowFixedGlob) {
173
+ # Include hidden parents here; candidate enumeration still
174
+ # controls which hidden children can be suggested.
175
+ $fixedMatches = @(Get-Item -Force -Path $pattern -ErrorAction SilentlyContinue |
176
+ Select-Object -ExpandProperty FullName)
177
+ }
100
178
 
101
179
  # Get file system items based on type
102
180
  $items = @()
@@ -107,7 +185,10 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
107
185
  $extList = ($extensions -split ',') -replace '^\.',''
108
186
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue |
109
187
  Where-Object {
110
- $_.PSIsContainer -or (($_.Extension -replace '^\.','' ) -in $extList)
188
+ $fileName = $_.Name
189
+ $_.PSIsContainer -or (@($extList | Where-Object {
190
+ $fileName.EndsWith('.' + $_, [System.StringComparison]::Ordinal)
191
+ }).Count -gt 0)
111
192
  }
112
193
  } else {
113
194
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue
@@ -122,7 +203,10 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
122
203
  $extList = ($extensions -split ',') -replace '^\.',''
123
204
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue |
124
205
  Where-Object {
125
- $_.PSIsContainer -or (($_.Extension -replace '^\.','' ) -in $extList)
206
+ $fileName = $_.Name
207
+ $_.PSIsContainer -or (@($extList | Where-Object {
208
+ $fileName.EndsWith('.' + $_, [System.StringComparison]::Ordinal)
209
+ }).Count -gt 0)
126
210
  }
127
211
  } else {
128
212
  $items = Get-ChildItem @forceParam -Path $globPath -ErrorAction SilentlyContinue
@@ -133,7 +217,7 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
133
217
  # For file/any modes with glob patterns, also add directories
134
218
  # from the base directory for navigation
135
219
  if ($prefix -match '[*?]' -and $type -ne 'directory') {
136
- $globDir = Split-Path -Parent $prefix
220
+ $globDir = Split-Path -Parent $lookupPrefix
137
221
  if (-not $globDir) { $globDir = '.' }
138
222
  $extraDirs = Get-ChildItem @forceParam -Directory -Path "$globDir/*" -ErrorAction SilentlyContinue
139
223
  if ($extraDirs) { $items = @($items) + @($extraDirs) | Select-Object -Unique }
@@ -144,6 +228,14 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
144
228
  $items = $items | Where-Object { -not $_.Attributes.HasFlag([System.IO.FileAttributes]::Hidden) }
145
229
  }
146
230
 
231
+ if ($narrowFixedGlob) {
232
+ $items = $items | Where-Object {
233
+ if ($_.PSIsContainer) { $true }
234
+ elseif ($IsWindows) { $fixedMatches -contains $_.FullName }
235
+ else { $fixedMatches -ccontains $_.FullName }
236
+ }
237
+ }
238
+
147
239
  # Extract directory prefix to preserve in completion text
148
240
  $dirPrefix = if ($prefix -and ($prefix.Contains('/') -or $prefix.Contains('\'))) {
149
241
  $slashIdx = [Math]::Max($prefix.LastIndexOf('/'), $prefix.LastIndexOf('\'))
@@ -154,10 +246,34 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
154
246
  $items | ForEach-Object {
155
247
  $completionText = if ($_.PSIsContainer) { "$dirPrefix$($_.Name)/" } else { "$dirPrefix$($_.Name)" }
156
248
  $itemType = if ($_.PSIsContainer) { 'Directory' } else { 'File' }
249
+ $resultType = if ($_.PSIsContainer) { 'ProviderContainer' } else { 'ParameterValue' }
250
+ # PSReadLine recognizes the platform separator for containers.
251
+ # Keep the display label's existing slash convention.
252
+ $directorySuffix = [IO.Path]::DirectorySeparatorChar
253
+ $pathText = if ($_.PSIsContainer) { "$dirPrefix$($_.Name)$directorySuffix" } else { $completionText }
254
+ # Quoting disables native tilde expansion. A home pattern
255
+ # supplied by the parser must insert the discovered path.
256
+ $replacementText = if ($null -ne $quoteType -and -not $prefixFromInput -and $homePattern) {
257
+ if ($_.PSIsContainer) { $_.FullName + $directorySuffix } else { $_.FullName }
258
+ } else { $pathText }
259
+ # Quoting suppresses native tilde expansion. Keep safe
260
+ # ~/ paths bare; otherwise use the enumerated full path.
261
+ # Keyed paths keep the typed spelling, including ~/, and
262
+ # always take the quoted argument form.
263
+ $insertionText = if ($null -ne $quoteType) {
264
+ & $quoteCompletion ($insertionPrefix + $replacementText)
265
+ } elseif (-not $insertionPrefix -and $completionText -cmatch '\A~/[a-zA-Z0-9_./:\\-]*\z') {
266
+ $pathText
267
+ } else {
268
+ $argumentText = if (-not $insertionPrefix -and ($completionText.StartsWith('~/') -or $completionText.StartsWith('~\'))) {
269
+ if ($_.PSIsContainer) { $_.FullName + $directorySuffix } else { $_.FullName }
270
+ } else { $pathText }
271
+ & $quoteArgument ($insertionPrefix + $argumentText)
272
+ }
157
273
  [System.Management.Automation.CompletionResult]::new(
274
+ $insertionText,
158
275
  $completionText,
159
- $completionText,
160
- 'ParameterValue',
276
+ $resultType,
161
277
  $itemType
162
278
  )
163
279
  }
@@ -165,11 +281,14 @@ Register-ArgumentCompleter -Native -CommandName drfed-migrate -ScriptBlock {
165
281
  # Parse literal completion: text\tlistItemText\tdescription
166
282
  $parts = $line -split "`t", 3
167
283
  $completionText = $parts[0]
284
+ # Empty display text is invalid for CompletionResult. Keep
285
+ # ignoring empty suggestions without dropping later records.
286
+ if (-not $completionText) { return }
168
287
  $listItemText = if ($parts.Length -gt 1 -and $parts[1]) { $parts[1] } else { $completionText }
169
288
  $toolTip = if ($parts.Length -gt 2 -and $parts[2]) { $parts[2] } else { $completionText }
170
289
 
171
290
  [System.Management.Automation.CompletionResult]::new(
172
- $completionText,
291
+ (& $quoteCompletion $completionText),
173
292
  $listItemText,
174
293
  'ParameterValue',
175
294
  $toolTip