-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track virtual documents. #2436
Merged
Merged
Track virtual documents. #2436
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,97 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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, Uri} 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'; | ||
import { EventStream } from '../EventStream'; | ||
import { DocumentSynchronizationFailure } from '../omnisharp/loggingEvents'; | ||
|
||
function trackCurrentVirtualDocuments(server: OmniSharpServer, eventStream: EventStream) { | ||
let registration = server.onProjectAdded(async () => { | ||
registration.dispose(); | ||
|
||
for (let i = 0; i < workspace.textDocuments.length; i++) { | ||
let document = workspace.textDocuments[i]; | ||
|
||
if (!shouldIgnoreDocument(document, server)) { | ||
await openVirtualDocument(document, server, eventStream); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function trackFutureVirtualDocuments(server: OmniSharpServer, eventStream: EventStream): IDisposable { | ||
let onTextDocumentOpen = workspace.onDidOpenTextDocument(async document => { | ||
if (shouldIgnoreDocument(document, server)) { | ||
return; | ||
} | ||
|
||
await openVirtualDocument(document, server, eventStream); | ||
}); | ||
|
||
let onTextDocumentClose = workspace.onDidCloseTextDocument(async document => { | ||
if (shouldIgnoreDocument(document, server)) { | ||
return; | ||
} | ||
|
||
await closeVirtualDocument(document, server, eventStream); | ||
}); | ||
|
||
// 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; | ||
} | ||
|
||
async function openVirtualDocument(document: TextDocument, server: OmniSharpServer, eventStream: EventStream) { | ||
let req = { FileName: document.uri.path, changeType: FileChangeType.Create }; | ||
try { | ||
await serverUtils.filesChanged(server, [req]); | ||
await serverUtils.updateBuffer(server, { Buffer: document.getText(), FileName: document.fileName }); | ||
} | ||
catch (error) { | ||
logSynchronizationFailure(document.uri, error, server, eventStream); | ||
} | ||
} | ||
|
||
async function closeVirtualDocument(document: TextDocument, server: OmniSharpServer, eventStream: EventStream) { | ||
let req = { FileName: document.uri.path, changeType: FileChangeType.Delete }; | ||
try { | ||
await serverUtils.filesChanged(server, [req]); | ||
} | ||
catch (error) { | ||
logSynchronizationFailure(document.uri, error, server, eventStream); | ||
} | ||
} | ||
|
||
function logSynchronizationFailure(uri: Uri, error: any, server: OmniSharpServer, eventStream: EventStream) { | ||
if (server.isRunning()) { | ||
eventStream.post(new DocumentSynchronizationFailure(uri.path, error)); | ||
} | ||
} | ||
|
||
export default function trackVirtualDocuments(server: OmniSharpServer, eventStream: EventStream): IDisposable { | ||
trackCurrentVirtualDocuments(server, eventStream); | ||
let disposable = trackFutureVirtualDocuments(server, eventStream); | ||
|
||
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
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
61 changes: 61 additions & 0 deletions
61
test/integrationTests/virtualDocumentTracker.integration.test.ts
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,61 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
import { should, expect } from 'chai'; | ||
import { activateCSharpExtension } from './integrationHelpers'; | ||
import testAssetWorkspace from './testAssets/testAssetWorkspace'; | ||
import { IDisposable } from '../../src/Disposable'; | ||
|
||
const chai = require('chai'); | ||
chai.use(require('chai-arrays')); | ||
chai.use(require('chai-fs')); | ||
|
||
suite(`Virtual Document Tracking ${testAssetWorkspace.description}`, function () { | ||
let virtualScheme: string = "virtual"; | ||
let virtualDocumentRegistration: IDisposable; | ||
|
||
suiteSetup(async function () { | ||
should(); | ||
await testAssetWorkspace.restore(); | ||
await activateCSharpExtension(); | ||
|
||
let virtualCSharpDocumentProvider = new VirtualCSharpDocumentProvider(); | ||
virtualDocumentRegistration = vscode.workspace.registerTextDocumentContentProvider(virtualScheme, virtualCSharpDocumentProvider); | ||
}); | ||
|
||
suiteTeardown(async () => { | ||
await testAssetWorkspace.cleanupWorkspace(); | ||
virtualDocumentRegistration.dispose(); | ||
}); | ||
|
||
test("Virtual documents are operated on.", async () => { | ||
let virtualUri = vscode.Uri.parse(`${virtualScheme}://${testAssetWorkspace.projects[0].projectDirectoryPath}/_virtualFile.cs`); | ||
await vscode.workspace.openTextDocument(virtualUri); | ||
|
||
let position = new vscode.Position(2, 4); | ||
let completionItems = <vscode.CompletionList>await vscode.commands.executeCommand("vscode.executeCompletionItemProvider", virtualUri, position); | ||
|
||
expect(completionItems.items).to.satisfy(() => { | ||
let item = completionItems.items.find(item => { | ||
return item.label === "while"; | ||
}); | ||
|
||
return item; | ||
}); | ||
}); | ||
}); | ||
|
||
class VirtualCSharpDocumentProvider implements vscode.TextDocumentContentProvider { | ||
onDidChange?: vscode.Event<vscode.Uri>; | ||
|
||
provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<string> { | ||
return `namespace Test | ||
{ | ||
|
||
}`; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based a lot of these changes off of the forwardChanges handler and I didn't see any tests. What's the test guidance to test my virtual document tracker in this repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't yet have a strong unit testing story for this, but it is possible to write integration tests -- take a look at the
integrationTests
folder. Not sure if there's an easy way to create virtual documents programmatically without installing another extension though. The "code action rename" test covers this type of scenario.