@clerc/plugin-completions 1.2.0 → 1.3.0-beta.1

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.
package/dist/index.d.mts CHANGED
@@ -1,50 +1,81 @@
1
1
  import { Plugin } from "@clerc/core";
2
- import { ArgumentHandler, Command, OptionHandler } from "@bomb.sh/tab";
3
2
 
3
+ //#region ../../node_modules/.pnpm/@bomb.sh+tab@0.0.11_cac@6.7.14_citty@0.1.6/node_modules/@bomb.sh/tab/dist/t-Cao2EVMz.d.ts
4
+ type OptionsMap = Map<string, Option>;
5
+ type Complete = (value: string, description: string) => void;
6
+ type OptionHandler = (this: Option, complete: Complete, options: OptionsMap) => void;
7
+ type ArgumentHandler = (this: Argument, complete: Complete, options: OptionsMap) => void;
8
+ declare class Argument {
9
+ name: string;
10
+ variadic: boolean;
11
+ command: Command;
12
+ handler?: ArgumentHandler;
13
+ constructor(command: Command, name: string, handler?: ArgumentHandler, variadic?: boolean);
14
+ }
15
+ declare class Option {
16
+ value: string;
17
+ description: string;
18
+ command: Command;
19
+ handler?: OptionHandler;
20
+ alias?: string;
21
+ isBoolean?: boolean;
22
+ constructor(command: Command, value: string, description: string, handler?: OptionHandler, alias?: string, isBoolean?: boolean);
23
+ }
24
+ declare class Command {
25
+ value: string;
26
+ description: string;
27
+ options: Map<string, Option>;
28
+ arguments: Map<string, Argument>;
29
+ parent?: Command;
30
+ constructor(value: string, description: string);
31
+ option(value: string, description: string, handlerOrAlias?: OptionHandler | string, alias?: string): Command;
32
+ argument(name: string, handler?: ArgumentHandler, variadic?: boolean): this;
33
+ }
34
+ //#endregion
4
35
  //#region src/index.d.ts
5
36
  declare module "@clerc/core" {
6
37
  interface CommandCustomOptions {
7
38
  /**
8
- * Completions options for the command.
9
- */
39
+ * Completions options for the command.
40
+ */
10
41
  completions?: {
11
42
  /**
12
- * Whether to show the command in completions output.
13
- *
14
- * @default true
15
- */
43
+ * Whether to show the command in completions output.
44
+ *
45
+ * @default true
46
+ */
16
47
  show?: boolean;
17
48
  /**
18
- * Handler to provide custom completions for the command.
19
- */
49
+ * Handler to provide custom completions for the command.
50
+ */
20
51
  handler?: (command: Command) => void;
21
52
  };
22
53
  }
23
54
  interface FlagCustomOptions {
24
55
  /**
25
- * Completions options for the flag.
26
- */
56
+ * Completions options for the flag.
57
+ */
27
58
  completions?: {
28
59
  /**
29
- * Whether to show the flag in completions output.
30
- *
31
- * @default true
32
- */
60
+ * Whether to show the flag in completions output.
61
+ *
62
+ * @default true
63
+ */
33
64
  show?: boolean;
34
65
  /**
35
- * Handler to provide custom completions for the flag.
36
- */
66
+ * Handler to provide custom completions for the flag.
67
+ */
37
68
  handler?: OptionHandler;
38
69
  };
39
70
  }
40
71
  interface ParameterCustomOptions {
41
72
  /**
42
- * Completions options for the parameter.
43
- */
73
+ * Completions options for the parameter.
74
+ */
44
75
  completions?: {
45
76
  /**
46
- * Handler to provide custom completions for the parameter.
47
- */
77
+ * Handler to provide custom completions for the parameter.
78
+ */
48
79
  handler?: ArgumentHandler;
49
80
  };
50
81
  }
package/dist/index.mjs CHANGED
@@ -511,7 +511,10 @@ complete -c ${e} -f -a "(eval __${n}_perform_completion)"
511
511
  # Split the command at the first space to separate the program and arguments.
512
512
  $Program, $Arguments = $Command.Split(" ", 2)
513
513
 
514
- $RequestComp = "& ${t} complete -- $Arguments"
514
+ $QuotedArgs = ($Arguments -split ' ' | ForEach-Object { "'" + ($_ -replace "'", "''") + "'" }) -join ' '
515
+ __${e}_debug "QuotedArgs: $QuotedArgs"
516
+
517
+ $RequestComp = "& ${t} complete '--' $QuotedArgs"
515
518
  __${e}_debug "RequestComp: $RequestComp"
516
519
 
517
520
  # we cannot use $WordToComplete because it
@@ -707,4 +710,4 @@ complete -c ${e} -f -a "(eval __${n}_perform_completion)"
707
710
  }
708
711
 
709
712
  Register-ArgumentCompleter -CommandName '${e}' -ScriptBlock $__${n}CompleterBlock
710
- `}const u={ShellCompDirectiveError:1,ShellCompDirectiveNoSpace:2,ShellCompDirectiveNoFileComp:4,ShellCompDirectiveFilterFileExt:8,ShellCompDirectiveFilterDirs:16,ShellCompDirectiveKeepOrder:32,shellCompDirectiveMaxValue:64,ShellCompDirectiveDefault:0};var d=class{name;variadic;command;handler;constructor(e,t,n,r=!1){this.command=e,this.name=t,this.handler=n,this.variadic=r}},f=class{value;description;command;handler;alias;isBoolean;constructor(e,t,n,r,i,a){this.command=e,this.value=t,this.description=n,this.handler=r,this.alias=i,this.isBoolean=a}},p=class{value;description;options=new Map;arguments=new Map;parent;constructor(e,t){this.value=e,this.description=t}option(e,t,n,r){let i,a,o;typeof n==`function`?(i=n,a=r,o=!1):typeof n==`string`?(i=void 0,a=n,o=!0):(i=void 0,a=void 0,o=!0);let s=new f(this,e,t,i,a,o);return this.options.set(e,s),this}argument(e,t,n=!1){let r=new d(this,e,t,n);return this.arguments.set(e,r),this}},m=class extends p{commands=new Map;completions=[];directive=u.ShellCompDirectiveDefault;constructor(){super(``,``)}command(e,t){let n=new p(e,t);return this.commands.set(e,n),n}stripOptions(e){let t=[],n=0;for(;n<e.length;){let r=e[n];if(r.startsWith(`-`)){n++;let t=!1,i=this.findOption(this,r);if(i)t=i.isBoolean??!1;else for(let[,e]of this.commands){let n=this.findOption(e,r);if(n){t=n.isBoolean??!1;break}}!t&&n<e.length&&!e[n].startsWith(`-`)&&n++}else t.push(r),n++}return t}matchCommand(e){e=this.stripOptions(e);let t=[],n=[],r=null;for(let i=0;i<e.length;i++){let a=e[i];t.push(a);let o=this.commands.get(t.join(` `));if(o)r=o;else{n=e.slice(i,e.length);break}}return[r||this,n]}shouldCompleteFlags(e,t){if(t.startsWith(`-`))return!0;if(e?.startsWith(`-`)){let t=this.findOption(this,e);if(!t){for(let[,n]of this.commands)if(t=this.findOption(n,e),t)break}return!(t&&t.isBoolean)}return!1}shouldCompleteCommands(e){return!e.startsWith(`-`)}handleFlagCompletion(e,t,n,r){let i;if(n.includes(`=`)){let[e]=n.split(`=`);i=e}else r?.startsWith(`-`)&&(i=r);if(i){let t=this.findOption(e,i);if(t?.handler){let n=[];t.handler.call(t,(e,t)=>n.push({value:e,description:t}),e.options),this.completions=n}return}if(n.startsWith(`-`)){let t=n.startsWith(`-`)&&!n.startsWith(`--`),r=n.replace(/^-+/,``);for(let[i,a]of e.options)t&&a.alias&&`-${a.alias}`.startsWith(n)?this.completions.push({value:`-${a.alias}`,description:a.description}):!t&&i.startsWith(r)&&this.completions.push({value:`--${i}`,description:a.description})}}findOption(e,t){let n=e.options.get(t);if(n||(n=e.options.get(t.replace(/^-+/,``)),n))return n;for(let[n,r]of e.options)if(r.alias&&`-${r.alias}`===t)return r}handleCommandCompletion(e,t){let n=this.stripOptions(e);for(let[e,r]of this.commands){if(e===``)continue;let i=e.split(` `);i.slice(0,n.length).every((e,t)=>e===n[t])&&i[n.length]?.startsWith(t)&&this.completions.push({value:i[n.length],description:r.description})}}handlePositionalCompletion(e,t){let n=e.value.split(` `).length,r=Math.max(0,t.length-n),i=Array.from(e.arguments.entries());if(i.length>0){let t;if(r<i.length){let[e,n]=i[r];t=n}else{let e=i[i.length-1][1];e.variadic&&(t=e)}if(t&&t.handler&&typeof t.handler==`function`){let n=[];t.handler.call(t,(e,t)=>n.push({value:e,description:t}),e.options),this.completions.push(...n)}}}complete(e){this.directive=u.ShellCompDirectiveNoFileComp;let t=new Set;this.completions.filter(e=>t.has(e.value)?!1:(t.add(e.value),!0)).filter(t=>{if(e.includes(`=`)){let[,n]=e.split(`=`);return t.value.startsWith(n)}return t.value.startsWith(e)}).forEach(e=>console.log(`${e.value}\t${e.description??``}`)),console.log(`:${this.directive}`)}parse(e){this.completions=[];let t=e[e.length-1]===``;t&&e.pop();let n=e[e.length-1]||``,r=e.slice(0,-1);t&&(n!==``&&r.push(n),n=``);let[i]=this.matchCommand(r),a=r[r.length-1];if(this.shouldCompleteFlags(a,n))this.handleFlagCompletion(i,r,n,a);else{if(a?.startsWith(`-`)&&n===``&&t){let e=this.findOption(this,a);if(!e){for(let[,t]of this.commands)if(e=this.findOption(t,a),e)break}if(e&&e.isBoolean){this.complete(n);return}}this.shouldCompleteCommands(n)&&this.handleCommandCompletion(r,n),i&&i.arguments.size>0&&this.handlePositionalCompletion(i,r)}this.complete(n)}setup(t,n,r){switch(e(r===`zsh`||r===`bash`||r===`fish`||r===`powershell`,`Unsupported shell`),r){case`zsh`:{let e=o(t,n);console.log(e);break}case`bash`:{let e=s(t,n);console.log(e);break}case`fish`:{let e=c(t,n);console.log(e);break}case`powershell`:{let e=l(t,n);console.log(e);break}}}};new m;function h(e,t,n){let r=i(n),a=r.description??``;if(r.type===Boolean)e.option(t,a,r.short);else{let n=r.completions?.handler??(async()=>{});e.option(t,a,n,r.short)}}function g(e,t){for(let[n,r]of Object.entries(e))h(t,n,r)}function _(e,t,n){g(t,e);for(let i of n.values()){if(i.completions?.show===!1)continue;let n=e;i.name!==``&&(n=e.command(i.name,i.description??``),g(t,n)),i.completions?.handler?.(n);for(let e of i.parameters??[]){let t=a(e),{name:i,isVariadic:o}=r(t.key);n.argument(i,t.completions?.handler,o)}for(let[e,t]of Object.entries(i.flags??{}))h(n,e,t)}}const v=()=>n({setup:e=>{let n=new m,r=t.Enum(`zsh`,`bash`,`fish`,`powershell`);return e.command(`completions`,`Generate shell completion scripts`,{flags:{shell:{description:`Shell type`,type:r}},parameters:[{key:`[shell]`,description:`Shell type`,type:r}]}).on(`completions`,async t=>{let r=t.parameters.shell??t.flags.shell;if(!r)throw Error(`Shell type is required. Please provide it via --shell flag or [shell] parameter.`);_(n,e._globalFlags,e._commands),n.setup(e._scriptName,e._scriptName,r)}),e.command(`complete`,{help:{show:!1},completions:{show:!1},parameters:[`--`,`[input...]`]}).on(`complete`,async t=>{_(n,e._globalFlags,e._commands);let{input:r}=t.parameters;n.parse(r)}),e}});export{v as completionsPlugin};
713
+ `}const u={ShellCompDirectiveError:1,ShellCompDirectiveNoSpace:2,ShellCompDirectiveNoFileComp:4,ShellCompDirectiveFilterFileExt:8,ShellCompDirectiveFilterDirs:16,ShellCompDirectiveKeepOrder:32,shellCompDirectiveMaxValue:64,ShellCompDirectiveDefault:0};var d=class{name;variadic;command;handler;constructor(e,t,n,r=!1){this.command=e,this.name=t,this.handler=n,this.variadic=r}},f=class{value;description;command;handler;alias;isBoolean;constructor(e,t,n,r,i,a){this.command=e,this.value=t,this.description=n,this.handler=r,this.alias=i,this.isBoolean=a}},p=class{value;description;options=new Map;arguments=new Map;parent;constructor(e,t){this.value=e,this.description=t}option(e,t,n,r){let i,a,o;typeof n==`function`?(i=n,a=r,o=!1):typeof n==`string`?(i=void 0,a=n,o=!0):(i=void 0,a=void 0,o=!0);let s=new f(this,e,t,i,a,o);return this.options.set(e,s),this}argument(e,t,n=!1){let r=new d(this,e,t,n);return this.arguments.set(e,r),this}},m=class extends p{commands=new Map;completions=[];directive=u.ShellCompDirectiveDefault;constructor(){super(``,``)}command(e,t){let n=new p(e,t);return this.commands.set(e,n),n}stripOptions(e){let t=[],n=0;for(;n<e.length;){let r=e[n];if(r.startsWith(`-`)){n++;let t=!1,i=this.findOption(this,r);if(i)t=i.isBoolean??!1;else for(let[,e]of this.commands){let n=this.findOption(e,r);if(n){t=n.isBoolean??!1;break}}!t&&n<e.length&&!e[n].startsWith(`-`)&&n++}else t.push(r),n++}return t}matchCommand(e){e=this.stripOptions(e);let t=[],n=[],r=null;for(let i=0;i<e.length;i++){let a=e[i];t.push(a);let o=this.commands.get(t.join(` `));if(o)r=o;else{n=e.slice(i,e.length);break}}return[r||this,n]}shouldCompleteFlags(e,t){if(t.startsWith(`-`))return!0;if(e?.startsWith(`-`)){let t=this.findOption(this,e);if(!t){for(let[,n]of this.commands)if(t=this.findOption(n,e),t)break}return!(t&&t.isBoolean)}return!1}shouldCompleteCommands(e){return!e.startsWith(`-`)}handleFlagCompletion(e,t,n,r){let i;if(n.includes(`=`)){let[e]=n.split(`=`);i=e}else if(r?.startsWith(`-`)){let t=this.findOption(e,r);t&&!t.isBoolean&&(i=r)}if(i){let t=this.findOption(e,i);if(t?.handler){let n=[];t.handler.call(t,(e,t)=>n.push({value:e,description:t}),e.options),this.completions=n}return}if(n.startsWith(`-`)){let t=n.startsWith(`-`)&&!n.startsWith(`--`),r=n.replace(/^-+/,``);for(let[i,a]of e.options)t&&a.alias&&`-${a.alias}`.startsWith(n)?this.completions.push({value:`-${a.alias}`,description:a.description}):!t&&i.startsWith(r)&&this.completions.push({value:`--${i}`,description:a.description})}}findOption(e,t){let n=e.options.get(t);if(n||(n=e.options.get(t.replace(/^-+/,``)),n))return n;for(let[n,r]of e.options)if(r.alias&&`-${r.alias}`===t)return r}handleCommandCompletion(e,t){let n=this.stripOptions(e);for(let[e,r]of this.commands){if(e===``)continue;let i=e.split(` `);i.slice(0,n.length).every((e,t)=>e===n[t])&&i[n.length]?.startsWith(t)&&this.completions.push({value:i[n.length],description:r.description})}}handlePositionalCompletion(e,t){let n=e.value.split(` `).length,r=Math.max(0,t.length-n),i=Array.from(e.arguments.entries());if(i.length>0){let t;if(r<i.length){let[e,n]=i[r];t=n}else{let e=i[i.length-1][1];e.variadic&&(t=e)}if(t&&t.handler&&typeof t.handler==`function`){let n=[];t.handler.call(t,(e,t)=>n.push({value:e,description:t}),e.options),this.completions.push(...n)}}}complete(e){this.directive=u.ShellCompDirectiveNoFileComp;let t=new Set;this.completions.filter(e=>t.has(e.value)?!1:(t.add(e.value),!0)).filter(t=>{if(e.includes(`=`)){let[,n]=e.split(`=`);return t.value.startsWith(n)}return t.value.startsWith(e)}).forEach(e=>console.log(`${e.value}\t${e.description??``}`)),console.log(`:${this.directive}`)}parse(e){this.completions=[];let t=e[e.length-1]===``;t&&e.pop();let n=e[e.length-1]||``,r=e.slice(0,-1);t&&(n!==``&&r.push(n),n=``);let[i]=this.matchCommand(r),a=r[r.length-1];if(this.shouldCompleteFlags(a,n))this.handleFlagCompletion(i,r,n,a);else{if(a?.startsWith(`-`)&&n===``&&t){let e=this.findOption(this,a);if(!e){for(let[,t]of this.commands)if(e=this.findOption(t,a),e)break}if(e&&e.isBoolean){this.complete(n);return}}this.shouldCompleteCommands(n)&&this.handleCommandCompletion(r,n),i&&i.arguments.size>0&&this.handlePositionalCompletion(i,r)}this.complete(n)}setup(t,n,r){switch(e(r===`zsh`||r===`bash`||r===`fish`||r===`powershell`,`Unsupported shell`),r){case`zsh`:{let e=o(t,n);console.log(e);break}case`bash`:{let e=s(t,n);console.log(e);break}case`fish`:{let e=c(t,n);console.log(e);break}case`powershell`:{let e=l(t,n);console.log(e);break}}}};new m;function h(e,t,n){let r=i(n),a=r.description??``;if(r.type===Boolean)e.option(t,a,r.short);else{let n=r.completions?.handler??(async()=>{});e.option(t,a,n,r.short)}}function g(e,t){for(let[n,r]of Object.entries(e))h(t,n,r)}function _(e,t,n){g(t,e);for(let i of n.values()){if(i.completions?.show===!1)continue;let n=e;i.name!==``&&(n=e.command(i.name,i.description??``),g(t,n)),i.completions?.handler?.(n);for(let e of i.parameters??[]){let t=a(e),{name:i,isVariadic:o}=r(t.key);n.argument(i,t.completions?.handler,o)}for(let[e,t]of Object.entries(i.flags??{}))h(n,e,t)}}const v=()=>n({setup:e=>{let n=new m,r=t.Enum(`zsh`,`bash`,`fish`,`powershell`);return e.command(`completions`,`Generate shell completion scripts`,{flags:{shell:{description:`Shell type`,type:r}},parameters:[{key:`[shell]`,description:`Shell type`,type:r}]}).on(`completions`,async t=>{let r=t.parameters.shell??t.flags.shell;if(!r)throw Error(`Shell type is required. Please provide it via --shell flag or [shell] parameter.`);_(n,e._globalFlags,e._commands),n.setup(e._scriptName,e._scriptName,r)}),e.command(`complete`,{help:{show:!1},completions:{show:!1},parameters:[`--`,`[input...]`]}).on(`complete`,async t=>{_(n,e._globalFlags,e._commands);let{input:r}=t.parameters;n.parse(r)}),e}});export{v as completionsPlugin};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "1.2.0",
3
+ "version": "1.3.0-beta.1",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc plugin completions",
@@ -28,8 +28,6 @@
28
28
  ".": "./dist/index.mjs",
29
29
  "./package.json": "./package.json"
30
30
  },
31
- "main": "./dist/index.mjs",
32
- "module": "./dist/index.mjs",
33
31
  "types": "./dist/index.d.mts",
34
32
  "files": [
35
33
  "dist"
@@ -38,8 +36,8 @@
38
36
  "access": "public"
39
37
  },
40
38
  "devDependencies": {
41
- "@bomb.sh/tab": "^0.0.10",
42
- "@clerc/core": "1.2.0"
39
+ "@bomb.sh/tab": "^0.0.11",
40
+ "@clerc/core": "1.3.0-beta.1"
43
41
  },
44
42
  "peerDependencies": {
45
43
  "@clerc/core": "*"