@optique/core 1.0.15-dev.2792 → 1.0.15-dev.2812

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.
@@ -913,6 +913,16 @@ complete -c ${programName} -f -a '(${functionName})'
913
913
  }
914
914
  };
915
915
  /**
916
+ * Quotes a string as a Nushell string literal. Nushell single-quoted strings
917
+ * have no escape sequences, so a value containing an apostrophe is written as
918
+ * a double-quoted string, in which only backslashes and double quotes need
919
+ * escaping.
920
+ */
921
+ function quoteNuString(value) {
922
+ if (!value.includes("'")) return `'${value}'`;
923
+ return `"${value.replace(/["\\]/g, "\\$&")}"`;
924
+ }
925
+ /**
916
926
  * The Nushell completion generator.
917
927
  * @since 0.6.0
918
928
  */
@@ -920,7 +930,7 @@ const nu = {
920
930
  name: "nu",
921
931
  generateScript(programName, args = []) {
922
932
  validateProgramName(programName);
923
- const escapedArgs = args.map((arg) => `'${arg.replace(/'/g, "''")}'`).join(" ");
933
+ const escapedArgs = args.map(quoteNuString).join(" ");
924
934
  const safeName = programName.replace(/[^a-zA-Z0-9]+/g, "-");
925
935
  const functionName = `nu-complete-${safeName}`;
926
936
  return `
@@ -1269,23 +1279,11 @@ Register-ArgumentCompleter -Native -CommandName ${programName} -ScriptBlock {
1269
1279
  \$arguments = @()
1270
1280
  \$commandElements = \$commandAst.CommandElements
1271
1281
 
1272
- # Determine the range of elements to extract
1273
- # Exclude the last element if it matches wordToComplete (partial input case)
1274
- \$maxIndex = \$commandElements.Count - 1
1275
- if (\$commandElements.Count -gt 1) {
1276
- \$lastElement = \$commandElements[\$commandElements.Count - 1]
1277
- \$lastText = if (\$lastElement -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
1278
- \$lastElement.Value
1279
- } else {
1280
- \$lastElement.Extent.Text
1281
- }
1282
- if (\$lastText -eq \$wordToComplete) {
1283
- \$maxIndex = \$commandElements.Count - 2
1284
- }
1285
- }
1286
-
1287
- for (\$i = 1; \$i -le \$maxIndex; \$i++) {
1282
+ # Keep only complete arguments before the cursor. The current word
1283
+ # (including a token ending exactly at the cursor) is appended below.
1284
+ for (\$i = 1; \$i -lt \$commandElements.Count; \$i++) {
1288
1285
  \$element = \$commandElements[\$i]
1286
+ if (\$element.Extent.EndOffset -ge \$cursorPosition) { break }
1289
1287
 
1290
1288
  if (\$element -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
1291
1289
  \$arguments += \$element.Value
@@ -913,6 +913,16 @@ complete -c ${programName} -f -a '(${functionName})'
913
913
  }
914
914
  };
915
915
  /**
916
+ * Quotes a string as a Nushell string literal. Nushell single-quoted strings
917
+ * have no escape sequences, so a value containing an apostrophe is written as
918
+ * a double-quoted string, in which only backslashes and double quotes need
919
+ * escaping.
920
+ */
921
+ function quoteNuString(value) {
922
+ if (!value.includes("'")) return `'${value}'`;
923
+ return `"${value.replace(/["\\]/g, "\\$&")}"`;
924
+ }
925
+ /**
916
926
  * The Nushell completion generator.
917
927
  * @since 0.6.0
918
928
  */
@@ -920,7 +930,7 @@ const nu = {
920
930
  name: "nu",
921
931
  generateScript(programName, args = []) {
922
932
  validateProgramName(programName);
923
- const escapedArgs = args.map((arg) => `'${arg.replace(/'/g, "''")}'`).join(" ");
933
+ const escapedArgs = args.map(quoteNuString).join(" ");
924
934
  const safeName = programName.replace(/[^a-zA-Z0-9]+/g, "-");
925
935
  const functionName = `nu-complete-${safeName}`;
926
936
  return `
@@ -1269,23 +1279,11 @@ Register-ArgumentCompleter -Native -CommandName ${programName} -ScriptBlock {
1269
1279
  \$arguments = @()
1270
1280
  \$commandElements = \$commandAst.CommandElements
1271
1281
 
1272
- # Determine the range of elements to extract
1273
- # Exclude the last element if it matches wordToComplete (partial input case)
1274
- \$maxIndex = \$commandElements.Count - 1
1275
- if (\$commandElements.Count -gt 1) {
1276
- \$lastElement = \$commandElements[\$commandElements.Count - 1]
1277
- \$lastText = if (\$lastElement -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
1278
- \$lastElement.Value
1279
- } else {
1280
- \$lastElement.Extent.Text
1281
- }
1282
- if (\$lastText -eq \$wordToComplete) {
1283
- \$maxIndex = \$commandElements.Count - 2
1284
- }
1285
- }
1286
-
1287
- for (\$i = 1; \$i -le \$maxIndex; \$i++) {
1282
+ # Keep only complete arguments before the cursor. The current word
1283
+ # (including a token ending exactly at the cursor) is appended below.
1284
+ for (\$i = 1; \$i -lt \$commandElements.Count; \$i++) {
1288
1285
  \$element = \$commandElements[\$i]
1286
+ if (\$element.Extent.EndOffset -ge \$cursorPosition) { break }
1289
1287
 
1290
1288
  if (\$element -is [System.Management.Automation.Language.StringConstantExpressionAst]) {
1291
1289
  \$arguments += \$element.Value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.15-dev.2792",
3
+ "version": "1.0.15-dev.2812",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",