-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Track opening, closing and changing of virtual documents that don't exist on disk. - Updated the various document selectors to properly include virtual files. #2431
- Loading branch information
1 parent
b77d89f
commit 24663d9
Showing
3 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import {workspace, TextDocument} from 'vscode'; | ||
import {OmniSharpServer} from '../omnisharp/server'; | ||
import * as serverUtils from '../omnisharp/utils'; | ||
import { FileChangeType } from '../omnisharp/protocol'; | ||
import { IDisposable } from '../Disposable'; | ||
import CompositeDisposable from '../CompositeDisposable'; | ||
|
||
function trackCurrentVirtualDocuments(server: OmniSharpServer) { | ||
let registration = server.onProjectAdded(() => { | ||
for (let i = 0; i < workspace.textDocuments.length; i++) { | ||
let document = workspace.textDocuments[i]; | ||
|
||
if (!shouldIgnoreDocument(document, server)) { | ||
openVirtualDocument(document, server); | ||
} | ||
} | ||
|
||
registration.dispose(); | ||
}); | ||
} | ||
|
||
function trackFutureVirtualDocuments(server: OmniSharpServer): IDisposable { | ||
let onTextDocumentOpen = workspace.onDidOpenTextDocument(document => { | ||
if (shouldIgnoreDocument(document, server)) { | ||
return; | ||
} | ||
|
||
openVirtualDocument(document, server); | ||
}); | ||
|
||
let onTextDocumentClose = workspace.onDidCloseTextDocument(document => { | ||
if (shouldIgnoreDocument(document, server)) { | ||
return; | ||
} | ||
|
||
closeVirtualDocument(document, server); | ||
}); | ||
|
||
// We already track text document changes for virtual documents in our change forwarder. | ||
return new CompositeDisposable( | ||
onTextDocumentOpen, | ||
onTextDocumentClose); | ||
} | ||
|
||
function shouldIgnoreDocument(document: TextDocument, server: OmniSharpServer): boolean { | ||
if (document.uri.scheme === 'file' || document.languageId !== 'csharp') { | ||
// We're only interested in non-physical CSharp documents. | ||
return true; | ||
} | ||
|
||
if (!server.isRunning()) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function openVirtualDocument(document: TextDocument, server: OmniSharpServer) { | ||
let req = { FileName: document.uri.path, changeType: FileChangeType.Create }; | ||
serverUtils.filesChanged(server, [req]) | ||
.catch(err => { | ||
console.warn(`[o] failed to forward virtual document change event for ${document.uri.path}`, err); | ||
return err; | ||
}); | ||
|
||
serverUtils.updateBuffer(server, { Buffer: document.getText(), FileName: document.fileName }) | ||
.catch(err => { | ||
console.warn(`[o] failed to forward virtual document change event for ${document.uri.path}`, err); | ||
return err; | ||
}); | ||
} | ||
|
||
function closeVirtualDocument(document: TextDocument, server: OmniSharpServer) { | ||
let req = { FileName: document.uri.path, changeType: FileChangeType.Delete }; | ||
serverUtils.filesChanged(server, [req]).catch(err => { | ||
console.warn(`[o] failed to forward virtual document change event for ${document.uri.path}`, err); | ||
return err; | ||
}); | ||
} | ||
|
||
export default function trackVirtualDocuments(server: OmniSharpServer): IDisposable { | ||
trackCurrentVirtualDocuments(server); | ||
let disposable = trackFutureVirtualDocuments(server); | ||
|
||
return disposable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters