@@ -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