Work around issue with getting stale contents from openTextDocument (#3545)

* Work around issue with getting stale contents from openTextDocument

* Avoid calling openTextDocument when creating a new file, and use only writeFile.  Unsubscribe from window activation when settings UI is not visible.

* Switch to using editorTabSize for settings file
This commit is contained in:
Colen Garoutte-Carson
2019-04-29 15:17:01 -07:00
committed by GitHub
parent 6a88c9917b
commit 110e683790
2 changed files with 27 additions and 21 deletions
+24 -18
View File
@@ -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();
@@ -681,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);
}
@@ -700,17 +713,9 @@ 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));
let otherSettings: OtherSettings = new OtherSettings(this.rootUri);
await util.writeFileText(fullPathToFile, JSON.stringify(this.configurationJson, null, otherSettings.editorTabSize));
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"));
@@ -1118,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 {
@@ -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();