Skip to content

Commit

Permalink
Track virtual documents.
Browse files Browse the repository at this point in the history
- Track opening, closing and changing of virtual documents that don't exist on disk.
- Updated the various document selectors to properly include virtual files.

dotnet#2431
  • Loading branch information
NTaylorMullen committed Jul 25, 2018
1 parent b77d89f commit 24663d9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/features/diagnosticsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class DiagnosticsProvider extends AbstractSupport {
// Go ahead and check for diagnostics in the currently visible editors.
for (let editor of vscode.window.visibleTextEditors) {
let document = editor.document;
if (document.languageId === 'csharp' && document.uri.scheme === 'file') {
if (document.languageId === 'csharp') {
this._validateDocument(document);
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ class DiagnosticsProvider extends AbstractSupport {
}

private _onDocumentAddOrChange(document: vscode.TextDocument): void {
if (document.languageId === 'csharp' && document.uri.scheme === 'file') {
if (document.languageId === 'csharp') {
this._validateDocument(document);
this._validateProject();
}
Expand Down
91 changes: 91 additions & 0 deletions src/features/virtualDocumentTracker.ts
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;
}
3 changes: 2 additions & 1 deletion src/omnisharp/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import { NetworkSettingsProvider } from '../NetworkSettings';
import CompositeDisposable from '../CompositeDisposable';
import Disposable from '../Disposable';
import OptionProvider from '../observers/OptionProvider';
import trackVirtualDocuments from '../features/virtualDocumentTracker';
import { StructureProvider } from '../features/structureProvider';

export let omnisharp: OmniSharpServer;

export async function activate(context: vscode.ExtensionContext, packageJSON: any, platformInfo: PlatformInformation, provider: NetworkSettingsProvider, eventStream: EventStream, optionProvider: OptionProvider, extensionPath: string) {
const documentSelector: vscode.DocumentSelector = {
language: 'csharp',
scheme: 'file' // only files from disk
};

const options = optionProvider.GetLatestOptions();
Expand Down Expand Up @@ -81,6 +81,7 @@ export async function activate(context: vscode.ExtensionContext, packageJSON: an
localDisposables.add(vscode.languages.registerCodeActionsProvider(documentSelector, codeActionProvider));
localDisposables.add(reportDiagnostics(server, advisor));
localDisposables.add(forwardChanges(server));
localDisposables.add(trackVirtualDocuments(server));
localDisposables.add(vscode.languages.registerFoldingRangeProvider(documentSelector, new StructureProvider(server)));
}));

Expand Down

0 comments on commit 24663d9

Please sign in to comment.