diff --git a/src/common/utilities.ts b/src/common/utilities.ts index 11461538..56c9560e 100644 --- a/src/common/utilities.ts +++ b/src/common/utilities.ts @@ -3,10 +3,11 @@ import * as fs from 'fs-extra'; import * as path from 'path'; -import { ConfigurationScope, env, LogLevel, Uri, WorkspaceFolder } from 'vscode'; +import { ConfigurationScope, env, LogLevel, Uri, WorkspaceFolder, Disposable, RelativePattern,FileSystemWatcher, workspace } from 'vscode'; import { Trace, TraceValues } from 'vscode-jsonrpc/node'; import { getConfiguration, getWorkspaceFolders, isVirtualWorkspace } from './vscodeapi'; import { DocumentSelector } from 'vscode-languageclient'; +import { traceLog, traceInfo } from './logging'; function logLevelToTrace(logLevel: LogLevel): Trace { switch (logLevel) { @@ -82,3 +83,34 @@ export function getInterpreterFromSetting(namespace: string, scope?: Configurati const config = getConfiguration(namespace, scope); return config.get('interpreter'); } + +export async function createConfigFileWatcher(onConfigChanged: () => Promise): Promise { + const disposables: Disposable[] = []; + + const homeDir = process.env.HOME || process.env.USERPROFILE; + if (homeDir) { + watchConfigFile(path.join(homeDir, '.black')); + } + + async function watchConfigFile(filePath: string): Promise { + if (await fs.pathExists(filePath)) { + const pattern = new RelativePattern( + path.dirname(filePath), + path.basename(filePath) + ); + + const watcher = workspace.createFileSystemWatcher(pattern); + disposables.push(watcher); + + const changeListener = watcher.onDidChange(async () => { + traceInfo(`Config file changed: ${filePath}`); + await onConfigChanged(); + }); + disposables.push(changeListener); + } + } + + return { + dispose: () => {disposables.forEach((d) => d.dispose()); } + }; +} diff --git a/src/extension.ts b/src/extension.ts index e813c922..f1b4b2c1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -15,13 +15,15 @@ import { logLegacySettings, } from './common/settings'; import { loadServerDefaults } from './common/setup'; -import { getInterpreterFromSetting, getProjectRoot } from './common/utilities'; +import { getInterpreterFromSetting, getProjectRoot, createConfigFileWatcher } from './common/utilities'; import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi'; import { registerEmptyFormatter } from './common/nullFormatter'; import { registerLanguageStatusItem, updateStatus } from './common/status'; import { LS_SERVER_RESTART_DELAY, PYTHON_VERSION } from './common/constants'; let lsClient: LanguageClient | undefined; +let configWatcherDisposable: vscode.Disposable | undefined; + 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. @@ -70,6 +72,7 @@ export async function activate(context: vscode.ExtensionContext): Promise }; context.subscriptions.push( + await createConfigFileWatcher(runServer), onDidChangePythonInterpreter(async () => { await runServer(); }),