Skip to content

Commit

Permalink
refactored code regarding adding FileSystemWatcher for pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
xytian315 authored and karthiknadig committed Dec 14, 2023
1 parent 525d1b0 commit 4aa6049
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { registerLanguageStatusItem, updateStatus } from './common/status';
import { PYTHON_VERSION } from './common/constants';

let lsClient: LanguageClient | undefined;
let watcher: vscode.FileSystemWatcher | undefined;
let watchers: vscode.FileSystemWatcher[] = [];
export async function activate(context: vscode.ExtensionContext): Promise<void> {
// This is required to get server name and module. This should be
// the first thing that we do in this extension.
Expand Down Expand Up @@ -65,16 +65,21 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}
};

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(
Expand Down Expand Up @@ -116,4 +121,8 @@ export async function deactivate(): Promise<void> {
if (lsClient) {
await lsClient.stop();
}

for (const watcher of watchers) {
watcher.dispose();
}
}

0 comments on commit 4aa6049

Please sign in to comment.