From f1ad910a336ad65b9882eb60574a5f1b8729e12c Mon Sep 17 00:00:00 2001 From: Kristy Tian Date: Tue, 26 Sep 2023 17:59:23 -0700 Subject: [PATCH] refactored code regarding adding FileSystemWatcher for pyproject.toml --- src/extension.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 0afa66b..7f3413e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -13,7 +13,7 @@ import { createOutputChannel, onDidChangeConfiguration, registerCommand } from ' import { registerLanguageStatusItem, updateStatus } from './common/status'; let lsClient: LanguageClient | undefined; -let watcher: vscode.FileSystemWatcher | 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. @@ -45,16 +45,21 @@ export async function activate(context: vscode.ExtensionContext): Promise } }; - const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; + const workspaceFolders = vscode.workspace.workspaceFolders; - if (workspaceFolder) { - const fullPath = `${workspaceFolder.uri.fsPath}/pyproject.toml`; - watcher = vscode.workspace.createFileSystemWatcher(fullPath); + if (workspaceFolders) { + for (const workspaceFolder of workspaceFolders) { + const fullPath = `${workspaceFolder.uri.fsPath}/pyproject.toml`; + const watcher = vscode.workspace.createFileSystemWatcher(fullPath); - watcher.onDidChange((uri) => { - console.log('pyproject.toml changed'); - vscode.commands.executeCommand('mypy-type-checker.restart', uri.fsPath); - }); + watcher.onDidChange(async (uri) => { + console.log(`pyproject.toml changed in ${workspaceFolder.uri.fsPath}. Restarting ${serverName}`); + await runServer(); + }); + + watchers.push(watcher); + context.subscriptions.push(watcher); + } } context.subscriptions.push( @@ -91,4 +96,8 @@ export async function deactivate(): Promise { if (lsClient) { await lsClient.stop(); } + + for (const watcher of watchers) { + watcher.dispose(); + } }