0%

Fig Custom Completion

The most valuable aspect of Fig is the definition of completion standards. Here I mark down how to customize standards and put them into production use.

Examples of Completion Spec

https://fig.io/docAssets/autocomplete/handbook/cli_structure.png

  1. cd command:https://github.com/withfig/autocomplete/blob/master/src/cd.ts
  2. echo command:https://github.com/withfig/autocomplete/blob/master/src/echo.ts
  3. git command:https://github.com/withfig/autocomplete/blob/master/src/git.ts#L4024

Click here

Contributing to the Official Fig Completion Repository

The official readme has made it very clear.

That is, fork repo and then follow the instructions to generate completion standards => test => submit PR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Replace `YOUR_GITHUB_USERNAME` with your own GitHub username
git clone https://github.com/YOUR_GITHUB_USERNAME/autocomplete.git fig-autocomplete
cd fig-autocomplete

# Add withfig/autocomplete as a remote
git remote add upstream https://github.com/withfig/autocomplete.git

# Install packages
pnpm install

# Create an example spec (call it "abc")
pnpm create-spec abc

# Turn on "dev mode", local fig will load the specifications under this directory
pnpm dev

Bypassing the Official Repo

Besides submitting contributions to the official repo, you can also create your own project to write completions, steps as follows.

1
2
3
4
5
6
# Create spec
npx @withfig/autocomplete-tools create-spec

# Generate completion file, test, meanwhile in CodeWhisperer custom spec folder will be written with this specification path.
npx @withfig/autocomplete-tools dev

When the above dev is executed, it has already been compiled into TS and registered to index.js, but because it is a separate repo, the index.js only registered the single custom-created specification. In actual use, after confirming the specification is usable, it can be placed in the official specification folder together for packaging.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const completionSpec: Fig.Spec = {
name: "ak",
description: "",
subcommands: [{
name: "say",
description: "Example subcommand",
subcommands: [{
name: "haha",
description: "Nested subcommand, example usage: 'abc my_subcommand my_nested_subcommand'"
}],
}],
options: [{
name: ["--help", "-h"],
description: "Show help for abc",
}],
// Only uncomment if abc takes an argument
// args: {}
};
export default completionSpec;

https://static.1991421.cn/2024/2024-01-18-160706.jpeg

https://static.1991421.cn/2024/2024-01-18-215649.jpeg

The registration method in index.js is as follows.

1
var e=["ak"],diffVersionedCompletions=[];export{e as default,diffVersionedCompletions};

Toggling Development Mode

  • Direct GUI operation in CodeWhisperer is possible.

  • If it’s the Fig App, the following command can be used to toggle.

1
fig settings autocomplete.developerModeNPM false