Skip to content

Commit

Permalink
fix: add more configuration filepaths (#243)
Browse files Browse the repository at this point in the history
* fix: add more configuration filepaths

Signed-off-by: Chapman Pendery <[email protected]>

* fix: missing folder at config level

Signed-off-by: Chapman Pendery <[email protected]>

---------

Signed-off-by: Chapman Pendery <[email protected]>
  • Loading branch information
cpendery committed Apr 15, 2024
1 parent 1f599d6 commit a7219b1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ inshellisense supports the following shells:

## Configuration

All configuration is done through a [toml](https://toml.io/) file located at `~/.inshellisenserc`. The [JSON schema](https://json-schema.org/) for the configuration file can be found [here](https://github.com/microsoft/inshellisense/blob/main/src/utils/config.ts).
All configuration is done through a [toml](https://toml.io/) file. You can create this file at `~/.inshellisenserc` or, for XDG compliance, at `~/.config/inshellisense/rc.toml`. The [JSON schema](https://json-schema.org/) for the configuration file can be found [here](https://github.com/microsoft/inshellisense/blob/main/src/utils/config.ts).

### Keybindings

Expand Down
71 changes: 39 additions & 32 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ const configSchema = {
additionalProperties: false,
};

const configFile = ".inshellisenserc";
const rcFile = ".inshellisenserc";
const xdgFile = "rc.toml";
const cachePath = path.join(os.homedir(), ".inshellisense");
const configPath = path.join(os.homedir(), configFile);
const rcPath = path.join(os.homedir(), rcFile);
const xdgPath = path.join(os.homedir(), ".config", "inshellisense", xdgFile);

const configPaths = [rcPath, xdgPath];

let globalConfig: Config = {
bindings: {
Expand All @@ -123,37 +127,40 @@ let globalConfig: Config = {

export const getConfig = (): Config => globalConfig;
export const loadConfig = async (program: Command) => {
if (fs.existsSync(configPath)) {
let config: Config;
try {
config = toml.parse((await fsAsync.readFile(configPath)).toString());
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
program.error(`${configFile} is invalid toml. Parsing error on line ${e.line}, column ${e.column}: ${e.message}`);
}
const isValid = ajv.validate(configSchema, config);
if (!isValid) {
program.error(`${configFile} is invalid: ${ajv.errorsText()}`);
configPaths.forEach(async (configPath) => {
if (fs.existsSync(configPath)) {
let config: Config;
try {
config = toml.parse((await fsAsync.readFile(configPath)).toString());
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
program.error(`${configPath} is invalid toml. Parsing error on line ${e.line}, column ${e.column}: ${e.message}`);
}
const isValid = ajv.validate(configSchema, config);
if (!isValid) {
program.error(`${configPath} is invalid: ${ajv.errorsText()}`);
}
globalConfig = {
bindings: {
nextSuggestion: config?.bindings?.nextSuggestion ?? globalConfig.bindings.nextSuggestion,
previousSuggestion: config?.bindings?.previousSuggestion ?? globalConfig.bindings.previousSuggestion,
acceptSuggestion: config?.bindings?.acceptSuggestion ?? globalConfig.bindings.acceptSuggestion,
dismissSuggestions: config?.bindings?.dismissSuggestions ?? globalConfig.bindings.dismissSuggestions,
},
prompt: {
bash: config.prompt?.bash ?? globalConfig?.prompt?.bash,
powershell: config.prompt?.powershell ?? globalConfig?.prompt?.powershell,
xonsh: config.prompt?.xonsh ?? globalConfig?.prompt?.xonsh,
pwsh: config.prompt?.pwsh ?? globalConfig?.prompt?.pwsh,
nu: config.prompt?.nu ?? globalConfig?.prompt?.nu,
},
specs: {
path: [...(config?.specs?.path ?? []), ...(config?.specs?.path ?? [])],
},
};
}
globalConfig = {
bindings: {
nextSuggestion: config?.bindings?.nextSuggestion ?? globalConfig.bindings.nextSuggestion,
previousSuggestion: config?.bindings?.previousSuggestion ?? globalConfig.bindings.previousSuggestion,
acceptSuggestion: config?.bindings?.acceptSuggestion ?? globalConfig.bindings.acceptSuggestion,
dismissSuggestions: config?.bindings?.dismissSuggestions ?? globalConfig.bindings.dismissSuggestions,
},
prompt: {
bash: config.prompt?.bash,
powershell: config.prompt?.powershell,
xonsh: config.prompt?.xonsh,
pwsh: config.prompt?.pwsh,
nu: config.prompt?.nu,
},
specs: {
path: [`${os.homedir()}/.fig/autocomplete/build`, ...(config?.specs?.path ?? [])],
},
};
}
});
globalConfig.specs = { path: [`${os.homedir()}/.fig/autocomplete/build`, ...(globalConfig.specs?.path ?? [])] };
};

export const deleteCacheFolder = async (): Promise<void> => {
Expand Down

0 comments on commit a7219b1

Please sign in to comment.