@@ -50,21 +50,12 @@ export class RazorDocumentManager implements IRazorDocumentManager {
5050 return Object . values ( this . razorDocuments ) ;
5151 }
5252
53- public async getDocument ( uri : vscode . Uri ) {
53+ public async getDocument ( uri : vscode . Uri ) : Promise < IRazorDocument > {
5454 const document = this . _getDocument ( uri ) ;
55-
56- // VS Code closes virtual documents after some timeout if they are not open in the IDE. Since our generated C# and Html
57- // documents are never open in the IDE, we need to ensure that VS Code considers them open so that requests against them
58- // succeed. Without this, even a simple diagnostics request will fail in Roslyn if the user just opens a .razor document
59- // and leaves it open past the timeout.
60- if ( this . razorDocumentGenerationInitialized ) {
61- await this . ensureDocumentAndProjectedDocumentsOpen ( document ) ;
62- }
63-
6455 return document ;
6556 }
6657
67- public async getActiveDocument ( ) {
58+ public async getActiveDocument ( ) : Promise < IRazorDocument | null > {
6859 if ( ! vscode . window . activeTextEditor ) {
6960 return null ;
7061 }
@@ -147,7 +138,7 @@ export class RazorDocumentManager implements IRazorDocumentManager {
147138 return vscode . Disposable . from ( watcher , didCreateRegistration , didOpenRegistration , didCloseRegistration ) ;
148139 }
149140
150- private _getDocument ( uri : vscode . Uri ) {
141+ private _getDocument ( uri : vscode . Uri ) : IRazorDocument {
151142 const path = getUriPath ( uri ) ;
152143 let document = this . findDocument ( path ) ;
153144
@@ -159,7 +150,7 @@ export class RazorDocumentManager implements IRazorDocumentManager {
159150 document = this . addDocument ( uri ) ;
160151 }
161152
162- return document ;
153+ return document ! ;
163154 }
164155
165156 private async openDocument ( uri : vscode . Uri ) {
@@ -182,10 +173,6 @@ export class RazorDocumentManager implements IRazorDocumentManager {
182173 await vscode . commands . executeCommand ( razorInitializeCommand , pipeName ) ;
183174 await this . serverClient . connectNamedPipe ( pipeName ) ;
184175
185- for ( const document of this . documents ) {
186- await this . ensureDocumentAndProjectedDocumentsOpen ( document ) ;
187- }
188-
189176 this . onRazorInitializedEmitter . fire ( ) ;
190177 }
191178 }
@@ -205,7 +192,7 @@ export class RazorDocumentManager implements IRazorDocumentManager {
205192 this . notifyDocumentChange ( document , RazorDocumentChangeKind . closed ) ;
206193 }
207194
208- private addDocument ( uri : vscode . Uri ) {
195+ private addDocument ( uri : vscode . Uri ) : IRazorDocument {
209196 const path = getUriPath ( uri ) ;
210197 let document = this . findDocument ( path ) ;
211198 if ( document ) {
@@ -261,10 +248,6 @@ export class RazorDocumentManager implements IRazorDocumentManager {
261248 ) {
262249 // We allow re-setting of the updated content from the same doc sync version in the case
263250 // of project or file import changes.
264-
265- // Make sure the document is open, because updating will cause a didChange event to fire.
266- await vscode . workspace . openTextDocument ( document . csharpDocument . uri ) ;
267-
268251 const csharpProjectedDocument = projectedDocument as CSharpProjectedDocument ;
269252
270253 // If the language server is telling us that the previous document was empty, then we should clear
@@ -275,7 +258,17 @@ export class RazorDocumentManager implements IRazorDocumentManager {
275258 csharpProjectedDocument . clear ( ) ;
276259 }
277260
278- csharpProjectedDocument . update ( updateBufferRequest . changes , updateBufferRequest . hostDocumentVersion ) ;
261+ if ( document . isOpen ) {
262+ // Make sure the document is open, because updating will cause a didChange event to fire.
263+ await vscode . workspace . openTextDocument ( document . csharpDocument . uri ) ;
264+
265+ csharpProjectedDocument . update ( updateBufferRequest . changes , updateBufferRequest . hostDocumentVersion ) ;
266+ } else {
267+ csharpProjectedDocument . storeEdits (
268+ updateBufferRequest . changes ,
269+ updateBufferRequest . hostDocumentVersion
270+ ) ;
271+ }
279272
280273 this . notifyDocumentChange ( document , RazorDocumentChangeKind . csharpChanged ) ;
281274 } else {
@@ -342,22 +335,4 @@ export class RazorDocumentManager implements IRazorDocumentManager {
342335
343336 this . onChangeEmitter . fire ( args ) ;
344337 }
345-
346- private async ensureDocumentAndProjectedDocumentsOpen ( document : IRazorDocument ) {
347- // vscode.workspace.openTextDocument may send a textDocument/didOpen
348- // request to the C# language server. We need to keep track of
349- // this to make sure we don't send a duplicate request later on.
350- const razorUri = vscode . Uri . file ( document . path ) ;
351- if ( ! this . isRazorDocumentOpenInCSharpWorkspace ( razorUri ) ) {
352- this . didOpenRazorCSharpDocument ( razorUri ) ;
353-
354- // Need to tell the Razor server that the document is open, or it won't generate C# code
355- // for it, and our projected document will always be empty, until the user manually
356- // opens the razor file.
357- await vscode . workspace . openTextDocument ( razorUri ) ;
358- }
359-
360- await vscode . workspace . openTextDocument ( document . csharpDocument . uri ) ;
361- await vscode . workspace . openTextDocument ( document . htmlDocument . uri ) ;
362- }
363338}
0 commit comments