diff --git a/src/common/vscodeapi.ts b/src/common/vscodeapi.ts index 29de516..e988145 100644 --- a/src/common/vscodeapi.ts +++ b/src/common/vscodeapi.ts @@ -7,6 +7,8 @@ import { commands, ConfigurationScope, Disposable, + FileSystemWatcher, + GlobPattern, languages, LanguageStatusItem, LogOutputChannel, @@ -48,3 +50,12 @@ export function getWorkspaceFolder(uri: Uri): WorkspaceFolder | undefined { export function createLanguageStatusItem(id: string, selector: DocumentSelector): LanguageStatusItem { return languages.createLanguageStatusItem(id, selector); } + +export function createFileSystemWatcher( + globPattern: GlobPattern, + ignoreCreateEvents?: boolean, + ignoreChangeEvents?: boolean, + ignoreDeleteEvents?: boolean, +): FileSystemWatcher { + return workspace.createFileSystemWatcher(globPattern, ignoreCreateEvents, ignoreChangeEvents, ignoreDeleteEvents); +} diff --git a/src/extension.ts b/src/extension.ts index fde9aaf..046be38 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,12 +14,17 @@ import { } from './common/settings'; import { loadServerDefaults } from './common/setup'; import { getLSClientTraceLevel, getProjectRoot } from './common/utilities'; -import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi'; +import { + createFileSystemWatcher, + createOutputChannel, + getWorkspaceFolders, + onDidChangeConfiguration, + registerCommand, +} from './common/vscodeapi'; import { registerLanguageStatusItem, updateStatus } from './common/status'; import { PYTHON_VERSION } from './common/constants'; let lsClient: LanguageClient | undefined; -let watchers: vscode.FileSystemWatcher[] = []; export async function activate(context: vscode.ExtensionContext): Promise { // This is required to get server name and module. This should be // the first thing that we do in this extension. @@ -65,22 +70,17 @@ export async function activate(context: vscode.ExtensionContext): Promise } }; - const workspaceFolders = vscode.workspace.workspaceFolders; - - if (workspaceFolders) { - for (const workspaceFolder of workspaceFolders) { - const fullPath = `${workspaceFolder.uri.fsPath}/pyproject.toml`; - const watcher = vscode.workspace.createFileSystemWatcher(fullPath); - - watcher.onDidChange(async (uri) => { - console.log(`pyproject.toml changed in ${workspaceFolder.uri.fsPath}. Restarting ${serverName}`); + getWorkspaceFolders().forEach((workspaceFolder) => { + const watcher = createFileSystemWatcher( + new vscode.RelativePattern(workspaceFolder, '{pyproject.toml,mypy.ini}'), + ); + context.subscriptions.push( + watcher, + watcher.onDidChange(async () => { await runServer(); - }); - - watchers.push(watcher); - context.subscriptions.push(watcher); - } - } + }), + ); + }); context.subscriptions.push( onDidChangePythonInterpreter(async () => { @@ -121,8 +121,4 @@ export async function deactivate(): Promise { if (lsClient) { await lsClient.stop(); } - - for (const watcher of watchers) { - watcher.dispose(); - } }