33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { workspace , TextDocument , Uri } from 'vscode' ;
7- import { OmniSharpServer } from '../omnisharp/server' ;
6+ import { workspace , TextDocument , Uri } from 'vscode' ;
7+ import { OmniSharpServer } from '../omnisharp/server' ;
88import * as serverUtils from '../omnisharp/utils' ;
99import { FileChangeType } from '../omnisharp/protocol' ;
1010import { IDisposable } from '../Disposable' ;
1111import CompositeDisposable from '../CompositeDisposable' ;
1212import { EventStream } from '../EventStream' ;
1313import { DocumentSynchronizationFailure } from '../omnisharp/loggingEvents' ;
1414
15- function trackCurrentVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) {
16- let registration = server . onProjectAdded ( async ( ) => {
17- registration . dispose ( ) ;
15+ async function trackCurrentVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) {
16+ for ( let i = 0 ; i < workspace . textDocuments . length ; i ++ ) {
17+ let document = workspace . textDocuments [ i ] ;
1818
19- for ( let i = 0 ; i < workspace . textDocuments . length ; i ++ ) {
20- let document = workspace . textDocuments [ i ] ;
21-
22- if ( ! shouldIgnoreDocument ( document , server ) ) {
23- await openVirtualDocument ( document , server , eventStream ) ;
24- }
19+ if ( ! shouldIgnoreDocument ( document , server ) ) {
20+ await openVirtualDocument ( document , server , eventStream ) ;
2521 }
26- } ) ;
22+ }
23+ }
24+
25+ export function isVirtualCSharpDocument ( document : TextDocument ) {
26+ if ( document . languageId !== 'csharp' ) {
27+ return false ;
28+ }
29+
30+ if ( document . uri . scheme === 'virtualCSharp-' ) {
31+ return false ;
32+ }
33+
34+ if ( ! document . uri . scheme . startsWith ( 'virtualCSharp-' ) ) {
35+ return false ;
36+ }
37+
38+ return true ;
2739}
2840
2941function trackFutureVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) : IDisposable {
@@ -35,22 +47,33 @@ function trackFutureVirtualDocuments(server: OmniSharpServer, eventStream: Event
3547 await openVirtualDocument ( document , server , eventStream ) ;
3648 } ) ;
3749
50+ let onTextDocumentChange = workspace . onDidChangeTextDocument ( async changeEvent => {
51+ const document = changeEvent . document ;
52+
53+ if ( shouldIgnoreDocument ( document , server ) ) {
54+ return ;
55+ }
56+
57+ await changeVirtualDocument ( document , server , eventStream ) ;
58+ } ) ;
59+
3860 let onTextDocumentClose = workspace . onDidCloseTextDocument ( async document => {
3961 if ( shouldIgnoreDocument ( document , server ) ) {
4062 return ;
4163 }
4264
4365 await closeVirtualDocument ( document , server , eventStream ) ;
4466 } ) ;
45-
67+
4668 // We already track text document changes for virtual documents in our change forwarder.
4769 return new CompositeDisposable (
4870 onTextDocumentOpen ,
49- onTextDocumentClose ) ;
71+ onTextDocumentClose ,
72+ onTextDocumentChange ) ;
5073}
5174
5275function shouldIgnoreDocument ( document : TextDocument , server : OmniSharpServer ) : boolean {
53- if ( document . uri . scheme === 'file' || document . languageId !== 'csharp' ) {
76+ if ( ! isVirtualCSharpDocument ( document ) ) {
5477 // We're only interested in non-physical CSharp documents.
5578 return true ;
5679 }
@@ -68,10 +91,27 @@ async function openVirtualDocument(document: TextDocument, server: OmniSharpServ
6891 if ( ! path ) {
6992 path = document . uri . path ;
7093 }
71-
94+
7295 let req = { FileName : path , changeType : FileChangeType . Create } ;
7396 try {
7497 await serverUtils . filesChanged ( server , [ req ] ) ;
98+
99+ // Trigger a change for the opening so we can get content refreshed.
100+ await changeVirtualDocument ( document , server , eventStream ) ;
101+ }
102+ catch ( error ) {
103+ logSynchronizationFailure ( document . uri , error , server , eventStream ) ;
104+ }
105+ }
106+
107+ async function changeVirtualDocument ( document : TextDocument , server : OmniSharpServer , eventStream : EventStream ) {
108+ let path = document . uri . fsPath ;
109+
110+ if ( ! path ) {
111+ path = document . uri . path ;
112+ }
113+
114+ try {
75115 await serverUtils . updateBuffer ( server , { Buffer : document . getText ( ) , FileName : document . fileName } ) ;
76116 }
77117 catch ( error ) {
@@ -85,7 +125,7 @@ async function closeVirtualDocument(document: TextDocument, server: OmniSharpSer
85125 if ( ! path ) {
86126 path = document . uri . path ;
87127 }
88-
128+
89129 let req = { FileName : path , changeType : FileChangeType . Delete } ;
90130 try {
91131 await serverUtils . filesChanged ( server , [ req ] ) ;
@@ -103,7 +143,7 @@ function logSynchronizationFailure(uri: Uri, error: any, server: OmniSharpServer
103143
104144export default function trackVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) : IDisposable {
105145 trackCurrentVirtualDocuments ( server , eventStream ) ;
106- let disposable = trackFutureVirtualDocuments ( server , eventStream ) ;
107-
146+ const disposable = trackFutureVirtualDocuments ( server , eventStream ) ;
147+
108148 return disposable ;
109149}
0 commit comments