Skip to content

Commit 5cee5c3

Browse files
authored
Merge pull request #4143 from 333fred/master
Additional incremental tweaks
2 parents bc072c5 + d89fb12 commit 5cee5c3

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
## 1.23.5 (Not yet released)
1515
* Set meaning of UseGlobalMono "auto" to "never" since Mono 6.12.0 still ships with MSBuild 16.7 (PR: [#4130](https://github.com/OmniSharp/omnisharp-vscode/pull/4130))
1616
* Ensure that the rename identifier and run code action providers do not apply changes twice (PR: [#4133](https://github.com/OmniSharp/omnisharp-vscode/pull/4133))
17-
* Do not send file changed events for .cs files (PR: [#4141](https://github.com/OmniSharp/omnisharp-vscode/pull/4141))
17+
* Do not send file changed events for .cs files (PR: [#4141](https://github.com/OmniSharp/omnisharp-vscode/pull/4141), [#4143](https://github.com/OmniSharp/omnisharp-vscode/pull/4143))
1818
* Update Razor to 6.0.0-alpha1.20521.3:
1919
* Improvements to HTML colorization for non-C# portions of the document.
2020
* Bug fix - the `razor.format.enable` option is honored again

src/features/changeForwarding.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ function forwardFileChanges(server: OmniSharpServer): IDisposable {
4949
return;
5050
}
5151

52-
if (changeType === FileChangeType.Change && uri.fsPath.endsWith(".cs")) {
53-
// When a file changes on disk a FileSystemEvent is generated as well as
54-
// a DidChangeTextDocumentEvent. The OmniSharp server listens for Change events
55-
// for ".cs" files and reloads their text from disk. This creates a situation where the server
56-
// may have updated the document to reflect disk and also recieves a set of TextChanges
57-
// to apply to the document. In order to avoid that situation, we will not send Change events
58-
// for ".cs" files and instead allow them to be updated via the DidChangeTextDocumentEvent.
52+
const docs = workspace.textDocuments.filter(doc => doc.uri.fsPath === uri.fsPath);
53+
if (Array.isArray(docs) && docs.some(doc => !doc.isClosed)) {
54+
// When a file changes on disk a FileSystemEvent is generated as well as a
55+
// DidChangeTextDocumentEvent.The ordering of these is:
56+
// 1. This method is called back. vscode's TextDocument has not yet been reloaded, so it has
57+
// the version from before the changes are applied.
58+
// 2. vscode reloads the file, and fires onDidChangeTextDocument. The document has been updated,
59+
// and the changes have the delta.
60+
// If we send this change to the server, then it will reload from the disk, which means it will
61+
// be synchronized to the version after the changes. Then, onDidChangeTextDocument will fire and
62+
// send the delta changes, which will cause the server to apply those exact changes. The results
63+
// being that the file is now in an inconsistent state.
64+
// If the document is closed, however, it will no longer be synchronized, so the text change will
65+
// not be triggered and we should tell the server to reread from the disk.
5966
return;
6067
}
6168

62-
let req = { FileName: uri.fsPath, changeType };
69+
const req = { FileName: uri.fsPath, changeType };
6370

6471
serverUtils.filesChanged(server, [req]).catch(err => {
6572
console.warn(`[o] failed to forward file change event for ${uri.fsPath}`, err);
@@ -75,7 +82,7 @@ function forwardFileChanges(server: OmniSharpServer): IDisposable {
7582
}
7683

7784
if (changeType === FileChangeType.Delete) {
78-
let requests = [{ FileName: uri.fsPath, changeType: FileChangeType.DirectoryDelete }];
85+
const requests = [{ FileName: uri.fsPath, changeType: FileChangeType.DirectoryDelete }];
7986

8087
serverUtils.filesChanged(server, requests).catch(err => {
8188
console.warn(`[o] failed to forward file change event for ${uri.fsPath}`, err);

0 commit comments

Comments
 (0)