Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -82,3 +83,34 @@ export function getInterpreterFromSetting(namespace: string, scope?: Configurati
const config = getConfiguration(namespace, scope);
return config.get<string[]>('interpreter');
}

export async function createConfigFileWatcher(onConfigChanged: () => Promise<void>): Promise<Disposable> {
const disposables: Disposable[] = [];

const homeDir = process.env.HOME || process.env.USERPROFILE;
if (homeDir) {
watchConfigFile(path.join(homeDir, '.black'));
}

async function watchConfigFile(filePath: string): Promise<void> {
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()); }
};
}
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -70,6 +72,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
};

context.subscriptions.push(
await createConfigFileWatcher(runServer),
onDidChangePythonInterpreter(async () => {
await runServer();
}),
Expand Down