Skip to content

Commit

Permalink
Work around issue with getting stale contents from openTextDocument (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Colengms authored Apr 29, 2019
1 parent 6a88c99 commit 110e683
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
42 changes: 24 additions & 18 deletions Extension/src/LanguageServer/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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"));

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions Extension/src/LanguageServer/settingsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 110e683

Please sign in to comment.