Skip to content

Commit

Permalink
fix: Use VS Code file watcher for config file changes. (#1579)
Browse files Browse the repository at this point in the history
Related to #1572
  • Loading branch information
Jason3S authored Dec 6, 2021
1 parent e4dc8af commit add7ae9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 116 deletions.
1 change: 0 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"jest-when": "^3.4.2",
"kefir": "^3.8.8",
"lorem-ipsum": "^2.0.4",
"node-watch": "^0.7.2",
"rfdc": "^1.3.0",
"rimraf": "^3.0.2",
"server": "^2.0.0-alpha.1",
Expand Down
11 changes: 10 additions & 1 deletion packages/client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ export async function activate(context: ExtensionContext): Promise<ExtensionApi>
setTimeout(() => silenceErrors(client.triggerSettingsRefresh(), 'triggerGetSettings'), delayInMs);
}

function triggerConfigChange() {
triggerGetSettings();
}

initStatusBar(context, client);

const configWatcher = vscode.workspace.createFileSystemWatcher(settings.configFileLocationGlob);

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(
settings.watchSettingsFiles(triggerGetSettings),
configWatcher,
configWatcher.onDidChange(triggerConfigChange),
configWatcher.onDidCreate(triggerConfigChange),
configWatcher.onDidDelete(triggerConfigChange),
vscode.workspace.onDidSaveTextDocument(handleOnDidSaveTextDocument),
vscode.workspace.onDidRenameFiles(handleRenameFile),
vscode.workspace.onDidDeleteFiles(handleDeleteFile),
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/settings/CSpellSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const configFileLocations = [
'cspell.config.cjs',
] as const;

export const configFileLocationGlob = `**/{${configFileLocations.join(',')}}`;

type ConfigFileNames = typeof configFileLocations[number];

export const nestedConfigLocations = ['package.json'];
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { ConfigFields } from './configFields';
export { configFilesToWatch, CSpellSettings } from './CSpellSettings';
export { configFileLocationGlob, configFilesToWatch, CSpellSettings } from './CSpellSettings';
export {
addIgnoreWordsToSettings,
ConfigTargetLegacy,
Expand All @@ -11,7 +11,6 @@ export {
setEnableSpellChecking,
TargetsAndScopes,
toggleEnableSpellChecker,
watchSettingsFiles,
} from './settings';
export {
ConfigurationTarget,
Expand Down
61 changes: 3 additions & 58 deletions packages/client/src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
import { CSpellSettings } from '@cspell/cspell-types';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import { Uri, workspace } from 'vscode';
import { CSpellUserSettings } from '../client';
import { isDefined, unique } from '../util';
import * as watcher from '../util/watcher';
import { unique } from '../util';
import { ClientConfigTarget } from './clientConfigTarget';
import { readConfigFile, writeConfigFile } from './configFileReadWrite';
import { configFileLocations, isUpdateSupportedForConfigFileFormat, normalizeWords, preferredConfigFiles } from './CSpellSettings';
import { normalizeWords, preferredConfigFiles } from './CSpellSettings';
import { setConfigFieldQuickPick } from './settings.base';

export { setEnableSpellChecking, toggleEnableSpellChecker } from './settings.enable';
export { enableLocaleForTarget } from './settings.locale';
export type { TargetsAndScopes } from './settings.types';
export { ConfigTargetLegacy, InspectScope, Scope } from './vsConfig';
export interface SettingsInfo {
path: Uri;
settings: CSpellUserSettings;
}
export type { TargetsAndScopes } from './settings.types';

export function watchSettingsFiles(callback: () => void): vscode.Disposable {
// Every 10 seconds see if we have new files to watch.
let busy = false;
const intervalObj = setInterval(async () => {
if (busy) {
return;
}
busy = true;
const settingsFiles = await findSettingsFiles();
settingsFiles
.map((uri) => uri.fsPath)
.filter((file) => !watcher.isWatching(file))
.forEach((file) => watcher.add(file, callback));
busy = false;
}, 10000);

return vscode.Disposable.from({
dispose: () => {
watcher.dispose();
clearInterval(intervalObj);
},
});
}

function getDefaultWorkspaceConfigLocation(docUri?: Uri): vscode.WorkspaceFolder | undefined {
const defaultFolderUri = docUri && vscode.workspace.getWorkspaceFolder(docUri);
Expand All @@ -53,34 +26,6 @@ export function hasWorkspaceLocation(): boolean {
return !!workspace.workspaceFile || !!workspace.workspaceFolders?.[0];
}

/**
* Returns a list of files in the order of Best to Worst Match.
* @param docUri
*/
function findSettingsFiles(docUri?: Uri, isUpdatable?: boolean): Promise<Uri[]> {
const { workspaceFolders } = workspace;
if (!workspaceFolders || !hasWorkspaceLocation()) {
return Promise.resolve([]);
}

const folders = docUri ? [workspace.getWorkspaceFolder(docUri)].filter(isDefined).concat(workspaceFolders) : workspaceFolders;

const possibleLocations = folders
.map((folder) => folder.uri.fsPath)
.map((root) => configFileLocations.map((rel) => path.join(root, rel)))
.reduce((a, b) => a.concat(b), []);

const found = possibleLocations.map(async (filename) => ({ filename, exists: await fs.pathExists(filename) }));

return Promise.all(found).then((found) =>
found
.filter((found) => found.exists)
.map((found) => found.filename)
.map((filename) => Uri.file(filename))
.filter((uri) => !isUpdatable || isUpdateSupportedForConfigFileFormat(uri))
);
}

/**
* @description Enable a programming language
* @param target - which level of setting to set
Expand Down
54 changes: 0 additions & 54 deletions packages/client/src/util/watcher.ts

This file was deleted.

0 comments on commit add7ae9

Please sign in to comment.