xref: /llvm-project/mlir/utils/vscode/src/configWatcher.ts (revision fb5a59f6e1b9ec3192ecdc14b69ab83dafcf90f2)
1import * as chokidar from 'chokidar';
2import * as vscode from 'vscode';
3
4import * as config from './config';
5import {MLIRContext} from './mlirContext';
6
7/**
8 *  Prompt the user to see if we should restart the server.
9 */
10async function promptRestart(settingName: string, promptMessage: string) {
11  switch (config.get<string>(settingName)) {
12  case 'restart':
13    vscode.commands.executeCommand('mlir.restart');
14    break;
15  case 'ignore':
16    break;
17  case 'prompt':
18  default:
19    switch (await vscode.window.showInformationMessage(
20        promptMessage, 'Yes', 'Yes, always', 'No, never')) {
21    case 'Yes':
22      vscode.commands.executeCommand('mlir.restart');
23      break;
24    case 'Yes, always':
25      vscode.commands.executeCommand('mlir.restart');
26      config.update<string>(settingName, 'restart',
27                            vscode.ConfigurationTarget.Global);
28      break;
29    case 'No, never':
30      config.update<string>(settingName, 'ignore',
31                            vscode.ConfigurationTarget.Global);
32      break;
33    default:
34      break;
35    }
36    break;
37  }
38}
39
40/**
41 *  Activate watchers that track configuration changes for the given workspace
42 *  folder, or null if the workspace is top-level.
43 */
44export async function activate(
45    mlirContext: MLIRContext, workspaceFolder: vscode.WorkspaceFolder,
46    serverSettings: string[], serverPaths: string[]) {
47  // When a configuration change happens, check to see if we should restart the
48  // server.
49  mlirContext.subscriptions.push(vscode.workspace.onDidChangeConfiguration(event => {
50    for (const serverSetting of serverSettings) {
51      const expandedSetting = `mlir.${serverSetting}`;
52      if (event.affectsConfiguration(expandedSetting, workspaceFolder)) {
53        promptRestart(
54            'onSettingsChanged',
55            `setting '${
56                expandedSetting}' has changed. Do you want to reload the server?`);
57      }
58    }
59  }));
60
61  // Setup watchers for the provided server paths.
62  const fileWatcherConfig = {
63    disableGlobbing : true,
64    followSymlinks : true,
65    ignoreInitial : true,
66    awaitWriteFinish : true,
67  };
68  for (const serverPath of serverPaths) {
69    if (serverPath === '') {
70      return;
71    }
72
73    // If the server path actually exists, track it in case it changes.
74    const fileWatcher = chokidar.watch(serverPath, fileWatcherConfig);
75    fileWatcher.on('all', (event, _filename, _details) => {
76      if (event != 'unlink') {
77        promptRestart(
78            'onSettingsChanged',
79            'MLIR language server file has changed. Do you want to reload the server?');
80      }
81    });
82    mlirContext.subscriptions.push(
83        new vscode.Disposable(() => { fileWatcher.close(); }));
84  }
85}
86