|  | 
|  | 1 | +import { EventEmitter } from 'events'; | 
|  | 2 | +import { ConfigurationTarget, Disposable, Uri, workspace, WorkspaceFolder } from 'vscode'; | 
|  | 3 | +import { PythonSettings } from '../common/configSettings'; | 
|  | 4 | + | 
|  | 5 | +type settingsToMonitor = 'linting'; | 
|  | 6 | + | 
|  | 7 | +export class ConfigSettingMonitor extends EventEmitter implements Disposable { | 
|  | 8 | + private oldSettings = new Map<string, string>(); | 
|  | 9 | + // tslint:disable-next-line:no-any | 
|  | 10 | + private timeout?: any; | 
|  | 11 | + constructor(private settingToMonitor: settingsToMonitor) { | 
|  | 12 | + super(); | 
|  | 13 | + this.initializeSettings(); | 
|  | 14 | + // tslint:disable-next-line:no-void-expression | 
|  | 15 | + PythonSettings.getInstance().on('change', () => this.onConfigChange()); | 
|  | 16 | + } | 
|  | 17 | + public dispose() { | 
|  | 18 | + if (this.timeout) { | 
|  | 19 | + // tslint:disable-next-line:no-unsafe-any | 
|  | 20 | + clearTimeout(this.timeout); | 
|  | 21 | + } | 
|  | 22 | + } | 
|  | 23 | + private onConfigChange() { | 
|  | 24 | + if (this.timeout) { | 
|  | 25 | + // tslint:disable-next-line:no-unsafe-any | 
|  | 26 | + clearTimeout(this.timeout); | 
|  | 27 | + } | 
|  | 28 | + this.timeout = setTimeout(() => { | 
|  | 29 | + this.timeout = undefined; | 
|  | 30 | + this.checkChangesToSettingsInWorkspace(); | 
|  | 31 | + this.checkChangesToSettingsInWorkspaceFolders(); | 
|  | 32 | + }, 1000); | 
|  | 33 | + } | 
|  | 34 | + private initializeSettings() { | 
|  | 35 | + if (!Array.isArray(workspace.workspaceFolders)) { | 
|  | 36 | + return; | 
|  | 37 | + } | 
|  | 38 | + if (workspace.workspaceFolders.length === 1) { | 
|  | 39 | + const key = this.getWorkspaceKey(); | 
|  | 40 | + const currentValue = JSON.stringify(PythonSettings.getInstance()[this.settingToMonitor]); | 
|  | 41 | + this.oldSettings.set(key, currentValue); | 
|  | 42 | + } else { | 
|  | 43 | + workspace.workspaceFolders.forEach(wkspaceFolder => { | 
|  | 44 | + const key = this.getWorkspaceFolderKey(wkspaceFolder.uri); | 
|  | 45 | + const currentValue = JSON.stringify(PythonSettings.getInstance(wkspaceFolder.uri)[this.settingToMonitor]); | 
|  | 46 | + this.oldSettings.set(key, currentValue); | 
|  | 47 | + }); | 
|  | 48 | + } | 
|  | 49 | + } | 
|  | 50 | + private checkChangesToSettingsInWorkspace() { | 
|  | 51 | + if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) { | 
|  | 52 | + return; | 
|  | 53 | + } | 
|  | 54 | + const newValue = JSON.stringify(PythonSettings.getInstance()[this.settingToMonitor]); | 
|  | 55 | + this.checkChangesAndNotifiy(ConfigurationTarget.Workspace, workspace.workspaceFolders[0].uri, newValue); | 
|  | 56 | + } | 
|  | 57 | + private checkChangesToSettingsInWorkspaceFolders() { | 
|  | 58 | + if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length <= 1) { | 
|  | 59 | + return; | 
|  | 60 | + } | 
|  | 61 | + // tslint:disable-next-line:no-void-expression | 
|  | 62 | + workspace.workspaceFolders.forEach(folder => this.checkChangesToSettingsInWorkspaceFolder(folder)); | 
|  | 63 | + } | 
|  | 64 | + private checkChangesToSettingsInWorkspaceFolder(workspaceFolder: WorkspaceFolder) { | 
|  | 65 | + const newValue = JSON.stringify(PythonSettings.getInstance(workspaceFolder.uri)[this.settingToMonitor]); | 
|  | 66 | + this.checkChangesAndNotifiy(ConfigurationTarget.WorkspaceFolder, workspaceFolder.uri, newValue); | 
|  | 67 | + } | 
|  | 68 | + private checkChangesAndNotifiy(configTarget: ConfigurationTarget, uri: Uri, newValue: string) { | 
|  | 69 | + const key = configTarget === ConfigurationTarget.Workspace ? this.getWorkspaceKey() : this.getWorkspaceFolderKey(uri); | 
|  | 70 | + if (this.oldSettings.has(key)) { | 
|  | 71 | + const oldValue = this.oldSettings.get(key); | 
|  | 72 | + if (oldValue !== newValue) { | 
|  | 73 | + this.oldSettings.set(key, newValue); | 
|  | 74 | + this.emit('change', configTarget, uri); | 
|  | 75 | + } | 
|  | 76 | + } else { | 
|  | 77 | + this.oldSettings.set(key, newValue); | 
|  | 78 | + } | 
|  | 79 | + } | 
|  | 80 | + private getWorkspaceKey() { | 
|  | 81 | + // tslint:disable-next-line:no-non-null-assertion | 
|  | 82 | + return workspace.workspaceFolders[0]!.uri.fsPath; | 
|  | 83 | + } | 
|  | 84 | + private getWorkspaceFolderKey(wkspaceFolder: Uri) { | 
|  | 85 | + return `${ConfigurationTarget.WorkspaceFolder}:${wkspaceFolder.fsPath}`; | 
|  | 86 | + } | 
|  | 87 | +} | 
0 commit comments