Skip to content

Commit

Permalink
Tweak how watcher is created.
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Jul 29, 2024
1 parent 610fbfc commit c39f0d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/common/vscodeapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
commands,
ConfigurationScope,
Disposable,
FileSystemWatcher,
GlobPattern,
languages,
LanguageStatusItem,
LogOutputChannel,
Expand Down Expand Up @@ -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);
}
38 changes: 17 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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,22 +70,17 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}
};

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

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

0 comments on commit c39f0d9

Please sign in to comment.