Skip to content

Commit 90d4506

Browse files
committed
feat: fig arg suggestCurrentToken
1 parent 6b9118f commit 90d4506

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

src/main/utils/completion.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import * as commas from '../../api/core-main'
88
import { flatAsync, normalizeArray } from '../../shared/helper'
99
import { extractCommand, extractCommandEntries } from './command'
1010
import type { FigContext } from './fig'
11-
import { aliasGenerator, commandGenerator, generateFigSpec, generateFigSuggestions, invalidateFigHistory } from './fig'
11+
import {
12+
createCommandWithFilepathsArg,
13+
createCurrentTokenGenerator,
14+
generateFigSpec,
15+
generateFigSuggestions,
16+
getFigSeparator,
17+
invalidateFigHistory,
18+
stepOnCommand,
19+
} from './fig'
1220
import { memoizeAsync } from './helper'
1321
import { BIN_PATH, loginExecute } from './shell'
1422

@@ -59,12 +67,6 @@ function stripFigCursor(insertion: string) {
5967
return insertion.slice(0, index) + suffix + '\u001b[D'.repeat(suffix.length)
6068
}
6169

62-
function getFigSeparator(spec: Fig.Option) {
63-
return spec.requiresSeparator
64-
? (typeof spec.requiresSeparator === 'string' ? spec.requiresSeparator : '=')
65-
: (spec.requiresEquals ? '=' : undefined)
66-
}
67-
6870
function getFigValues(spec: Fig.Subcommand | Fig.Suggestion | Fig.Option) {
6971
if (spec.insertValue) {
7072
return [stripFigCursor(spec.insertValue)]
@@ -100,21 +102,22 @@ function getFigSuggestionType(spec: Fig.Suggestion): CommandCompletion['type'] {
100102
case 'folder':
101103
return 'directory'
102104
default:
103-
if ('context' in spec) {
104-
const templateContext = (spec as Fig.TemplateSuggestion).context
105-
switch (templateContext.templateType) {
106-
case 'filepaths':
107-
return 'file'
108-
case 'folders':
109-
return 'directory'
110-
case 'history':
111-
return 'history'
112-
default:
113-
break
114-
}
115-
}
116-
return 'command'
105+
break
106+
}
107+
if ('context' in spec) {
108+
const templateContext = (spec as Fig.TemplateSuggestion).context
109+
switch (templateContext.templateType) {
110+
case 'filepaths':
111+
return 'file'
112+
case 'folders':
113+
return 'directory'
114+
case 'history':
115+
return 'history'
116+
default:
117+
break
118+
}
117119
}
120+
return 'command'
118121
}
119122

120123
function transformFigSuggestion(raw: string | Fig.Suggestion, query: string, strict?: boolean) {
@@ -203,7 +206,8 @@ function getFigArgCompletions(spec: Fig.Subcommand | Fig.Option, query: string,
203206
}
204207
}
205208
return flatAsync(specArgs.map(async arg => {
206-
const generators = [
209+
const generators: Fig.Generator[] = [
210+
...(arg.suggestCurrentToken ? [createCurrentTokenGenerator('arg')] : []),
207211
...(arg.template ? [{ template: arg.template }] : []),
208212
...normalizeArray(arg.generators),
209213
]
@@ -352,25 +356,13 @@ async function getCompletions(
352356
// Commands
353357
if (!command && !/^(.+|~)?[\\/]/.test(query)) {
354358
asyncCompletionLists.push(
355-
getFigCompletions({
356-
name: '',
357-
args: {
358-
name: 'command',
359-
generators: [commandGenerator, aliasGenerator],
360-
},
361-
}, query, args, figContext),
359+
getFigCompletions(stepOnCommand, query, args, figContext),
362360
)
363361
}
364362
// Files
365363
if (!isCommandLineArgument(query) && (operator && operator.op === '>' || /[\\/]/.test(query))) {
366364
asyncCompletionLists.push(
367-
getFigCompletions({
368-
name: command,
369-
args: {
370-
name: 'file',
371-
template: 'filepaths',
372-
},
373-
}, query, args, figContext),
365+
getFigCompletions(createCommandWithFilepathsArg(command), query, args, figContext),
374366
)
375367
}
376368
}

src/main/utils/fig.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,51 @@ const aliasGenerator: Fig.Generator = {
370370
},
371371
}
372372

373+
const stepOnCommand: Fig.Subcommand = {
374+
name: '',
375+
args: {
376+
name: 'command',
377+
generators: [commandGenerator, aliasGenerator],
378+
},
379+
}
380+
381+
function createCommandWithFilepathsArg(name: string): Fig.Subcommand {
382+
return {
383+
name,
384+
args: {
385+
name: 'file',
386+
template: 'filepaths',
387+
},
388+
}
389+
}
390+
391+
function createCurrentTokenGenerator(type: Fig.SuggestionType): Fig.Generator {
392+
return {
393+
custom: async tokens => {
394+
const token = tokens[tokens.length - 1]
395+
return [
396+
{
397+
type,
398+
name: token,
399+
},
400+
]
401+
},
402+
}
403+
}
404+
405+
function getFigSeparator(spec: Fig.Option) {
406+
return spec.requiresSeparator
407+
? (typeof spec.requiresSeparator === 'string' ? spec.requiresSeparator : '=')
408+
: (spec.requiresEquals ? '=' : undefined)
409+
}
410+
373411
export {
374412
normalizeArray,
375413
generateFigSpec,
376414
generateFigSuggestions,
377415
invalidateFigHistory,
378-
commandGenerator,
379-
aliasGenerator,
416+
stepOnCommand,
417+
createCommandWithFilepathsArg,
418+
createCurrentTokenGenerator,
419+
getFigSeparator,
380420
}

src/types/terminal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export interface CommandCompletion {
8989
state?: 'pending' | 'loading',
9090
key?: string,
9191
deprecated?: boolean,
92+
dangerous?: boolean,
9293
}
9394

9495
export interface TerminalTabPosition {

0 commit comments

Comments
 (0)