1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) Microsoft Corporation. All rights reserved.
3+ * Licensed under the MIT License. See License.txt in the project root for license information.
4+ *--------------------------------------------------------------------------------------------*/
5+
6+ import { workspace , TextDocument , Uri } from 'vscode' ;
7+ import { OmniSharpServer } from '../omnisharp/server' ;
8+ import * as serverUtils from '../omnisharp/utils' ;
9+ import { FileChangeType } from '../omnisharp/protocol' ;
10+ import { IDisposable } from '../Disposable' ;
11+ import CompositeDisposable from '../CompositeDisposable' ;
12+ import { EventStream } from '../EventStream' ;
13+ import { DocumentSynchronizationFailure } from '../omnisharp/loggingEvents' ;
14+
15+ function trackCurrentVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) {
16+ let registration = server . onProjectAdded ( async ( ) => {
17+ registration . dispose ( ) ;
18+
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+ }
25+ }
26+ } ) ;
27+ }
28+
29+ function trackFutureVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) : IDisposable {
30+ let onTextDocumentOpen = workspace . onDidOpenTextDocument ( async document => {
31+ if ( shouldIgnoreDocument ( document , server ) ) {
32+ return ;
33+ }
34+
35+ await openVirtualDocument ( document , server , eventStream ) ;
36+ } ) ;
37+
38+ let onTextDocumentClose = workspace . onDidCloseTextDocument ( async document => {
39+ if ( shouldIgnoreDocument ( document , server ) ) {
40+ return ;
41+ }
42+
43+ await closeVirtualDocument ( document , server , eventStream ) ;
44+ } ) ;
45+
46+ // We already track text document changes for virtual documents in our change forwarder.
47+ return new CompositeDisposable (
48+ onTextDocumentOpen ,
49+ onTextDocumentClose ) ;
50+ }
51+
52+ function shouldIgnoreDocument ( document : TextDocument , server : OmniSharpServer ) : boolean {
53+ if ( document . uri . scheme === 'file' || document . languageId !== 'csharp' ) {
54+ // We're only interested in non-physical CSharp documents.
55+ return true ;
56+ }
57+
58+ if ( ! server . isRunning ( ) ) {
59+ return true ;
60+ }
61+
62+ return false ;
63+ }
64+
65+ async function openVirtualDocument ( document : TextDocument , server : OmniSharpServer , eventStream : EventStream ) {
66+ let req = { FileName : document . uri . path , changeType : FileChangeType . Create } ;
67+ try {
68+ await serverUtils . filesChanged ( server , [ req ] ) ;
69+ await serverUtils . updateBuffer ( server , { Buffer : document . getText ( ) , FileName : document . fileName } ) ;
70+ }
71+ catch ( error ) {
72+ logSynchronizationFailure ( document . uri , error , server , eventStream ) ;
73+ }
74+ }
75+
76+ async function closeVirtualDocument ( document : TextDocument , server : OmniSharpServer , eventStream : EventStream ) {
77+ let req = { FileName : document . uri . path , changeType : FileChangeType . Delete } ;
78+ try {
79+ await serverUtils . filesChanged ( server , [ req ] ) ;
80+ }
81+ catch ( error ) {
82+ logSynchronizationFailure ( document . uri , error , server , eventStream ) ;
83+ }
84+ }
85+
86+ function logSynchronizationFailure ( uri : Uri , error : any , server : OmniSharpServer , eventStream : EventStream ) {
87+ if ( server . isRunning ( ) ) {
88+ eventStream . post ( new DocumentSynchronizationFailure ( uri . path , error ) ) ;
89+ }
90+ }
91+
92+ export default function trackVirtualDocuments ( server : OmniSharpServer , eventStream : EventStream ) : IDisposable {
93+ trackCurrentVirtualDocuments ( server , eventStream ) ;
94+ let disposable = trackFutureVirtualDocuments ( server , eventStream ) ;
95+
96+ return disposable ;
97+ }
0 commit comments