From 8909eca0abd8b8d70685c29f2984f94882780702 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Fri, 26 Apr 2019 16:52:19 -0700 Subject: [PATCH 1/3] Work around issue with getting stale contents from openTextDocument --- .../src/LanguageServer/configurations.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index 97f9ab3f4c..4bf6683658 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -173,7 +173,8 @@ export class CppProperties { this.propertiesFile = null; } - this.configFileWatcher = vscode.workspace.createFileSystemWatcher(path.join(this.configFolder, this.configurationGlobPattern)); + let settingsPath: string = path.join(this.configFolder, this.configurationGlobPattern); + this.configFileWatcher = vscode.workspace.createFileSystemWatcher(settingsPath); this.disposables.push(this.configFileWatcher); this.configFileWatcher.onDidCreate((uri) => { this.propertiesFile = uri; @@ -187,7 +188,24 @@ export class CppProperties { }); this.configFileWatcher.onDidChange(() => { - this.handleConfigurationChange(); + // If the file is one of the textDocument's vscode is tracking, we need to wait for an + // onDidChangeTextDocument event, or we may get old/cached contents when we open it. + let alreadyTracking: boolean = false; + for (let i: number = 0; i < vscode.workspace.textDocuments.length; i++) { + if (vscode.workspace.textDocuments[i].uri.fsPath === settingsPath) { + alreadyTracking = true; + break; + } + } + if (!alreadyTracking) { + this.handleConfigurationChange(); + } + }); + + vscode.workspace.onDidChangeTextDocument((e: vscode.TextDocumentChangeEvent) => { + if (e.document.uri.fsPath === settingsPath) { + this.handleConfigurationChange(); + } }); this.handleConfigurationChange(); From a517eb6767717aafe6a41cce7aaee4003b38e26f Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Mon, 29 Apr 2019 14:12:47 -0700 Subject: [PATCH 2/3] Avoid calling openTextDocument when creating a new file, and use only writeFile. Unsubscribe from window activation when settings UI is not visible. --- Extension/src/LanguageServer/configurations.ts | 16 +--------------- Extension/src/LanguageServer/settingsPanel.ts | 6 +++--- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index 4bf6683658..ea67617fb4 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -699,11 +699,6 @@ export class CppProperties { } let fullPathToFile: string = path.join(this.configFolder, "c_cpp_properties.json"); - let filePath: vscode.Uri = vscode.Uri.file(fullPathToFile).with({ scheme: "untitled" }); - - let document: vscode.TextDocument = await vscode.workspace.openTextDocument(filePath); - - let edit: vscode.WorkspaceEdit = new vscode.WorkspaceEdit(); if (this.configurationJson) { this.resetToDefaultSettings(true); } @@ -718,17 +713,8 @@ export class CppProperties { let savedKnownCompilers: KnownCompiler[] = this.configurationJson.configurations[0].knownCompilers; delete this.configurationJson.configurations[0].knownCompilers; - edit.insert(document.uri, new vscode.Position(0, 0), JSON.stringify(this.configurationJson, null, 4)); - + await util.writeFileText(fullPathToFile, JSON.stringify(this.configurationJson, null, 4)); this.configurationJson.configurations[0].knownCompilers = savedKnownCompilers; - await vscode.workspace.applyEdit(edit); - - // Fix for issue 163 - // https://github.com/Microsoft/vscppsamples/issues/163 - // Save the file to disk so that when the user tries to re-open the file it exists. - // Before this fix the file existed but was unsaved, so we went through the same - // code path and reapplied the edit. - await document.save(); this.propertiesFile = vscode.Uri.file(path.join(this.configFolder, "c_cpp_properties.json")); diff --git a/Extension/src/LanguageServer/settingsPanel.ts b/Extension/src/LanguageServer/settingsPanel.ts index 5355b91779..0d91d98a0c 100644 --- a/Extension/src/LanguageServer/settingsPanel.ts +++ b/Extension/src/LanguageServer/settingsPanel.ts @@ -36,8 +36,7 @@ export class SettingsPanel { this.configValues = { name: undefined }; this.disposable = vscode.Disposable.from( this.settingsPanelActivated, - this.configValuesChanged, - vscode.window.onDidChangeWindowState(this.onWindowStateChanged, this) + this.configValuesChanged ); } @@ -75,7 +74,8 @@ export class SettingsPanel { this.panel, this.panel.onDidDispose(this.onPanelDisposed, this), this.panel.onDidChangeViewState(this.onViewStateChanged, this), - this.panel.webview.onDidReceiveMessage(this.onMessageReceived, this) + this.panel.webview.onDidReceiveMessage(this.onMessageReceived, this), + vscode.window.onDidChangeWindowState(this.onWindowStateChanged, this) ); this.panel.webview.html = this.getHtml(); From 7642ac2a78220b0d63b06b03564245e42cf9a83a Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Mon, 29 Apr 2019 14:32:19 -0700 Subject: [PATCH 3/3] Switch to using editorTabSize for settings file --- Extension/src/LanguageServer/configurations.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index ea67617fb4..1e7a5237e8 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -713,7 +713,8 @@ export class CppProperties { let savedKnownCompilers: KnownCompiler[] = this.configurationJson.configurations[0].knownCompilers; delete this.configurationJson.configurations[0].knownCompilers; - await util.writeFileText(fullPathToFile, JSON.stringify(this.configurationJson, null, 4)); + let otherSettings: OtherSettings = new OtherSettings(this.rootUri); + await util.writeFileText(fullPathToFile, JSON.stringify(this.configurationJson, null, otherSettings.editorTabSize)); this.configurationJson.configurations[0].knownCompilers = savedKnownCompilers; this.propertiesFile = vscode.Uri.file(path.join(this.configFolder, "c_cpp_properties.json")); @@ -1122,7 +1123,8 @@ export class CppProperties { private writeToJson(): void { console.assert(this.propertiesFile); - fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4)); + let otherSettings: OtherSettings = new OtherSettings(this.rootUri); + fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, otherSettings.editorTabSize)); } public checkCppProperties(): void {