Skip to content
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

Support for viewing type information for types not in the source #666

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/features/definitionMetadataDocumentProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { workspace, Uri, TextDocument, Disposable, TextDocumentContentProvider} from 'vscode';
import { MetadataResponse } from '../omnisharp/protocol';

export class DefinitionMetadataDocumentProvider implements TextDocumentContentProvider, Disposable {
private _scheme = "omnisharp-metadata";
private _registration : Disposable;
private _documents: Map<string, MetadataResponse>;
private _documentClosedSubscription: Disposable;

constructor() {
this._documents = new Map<string, MetadataResponse>();
this._documentClosedSubscription = workspace.onDidCloseTextDocument(this.onTextDocumentClosed);
}

private onTextDocumentClosed(document: TextDocument) : void {
this._documents.delete(document.uri.toString());
}

public dispose() : void {
this._registration.dispose();
this._documentClosedSubscription.dispose();
this._documents.clear();
}

public addMetadataResponse(metadataResponse: MetadataResponse) : Uri {
const uri = this.createUri(metadataResponse);

this._documents.set(uri.toString(), metadataResponse);

return uri;
}

public register() : void {
this._registration = workspace.registerTextDocumentContentProvider(this._scheme, this);
}

public provideTextDocumentContent(uri : Uri) : string {
return this._documents.get(uri.toString()).Source;
}

private createUri(metadataResponse: MetadataResponse) : Uri {
return Uri.parse(this._scheme + "://" +
metadataResponse.SourceName.replace(/\\/g, "/")
.replace(/(.*)\/(.*)/g, "$1/[metadata] $2"));
}
}
41 changes: 35 additions & 6 deletions src/features/definitionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,49 @@
'use strict';

import AbstractSupport from './abstractProvider';
import {MetadataRequest, GoToDefinitionRequest, MetadataSource} from '../omnisharp/protocol';
import * as serverUtils from '../omnisharp/utils';
import {createRequest, toLocation} from '../omnisharp/typeConvertion';
import {TextDocument, Position, Location, CancellationToken, DefinitionProvider} from 'vscode';
import {Uri, TextDocument, Position, Location, CancellationToken, DefinitionProvider} from 'vscode';
import {DefinitionMetadataDocumentProvider} from './definitionMetadataDocumentProvider';


export default class CSharpDefinitionProvider extends AbstractSupport implements DefinitionProvider {
private _definitionMetadataDocumentProvider: DefinitionMetadataDocumentProvider;

constructor(server, definitionMetadataDocumentProvider: DefinitionMetadataDocumentProvider) {
super(server);
this._definitionMetadataDocumentProvider = definitionMetadataDocumentProvider;
}

public provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Promise<Location> {

let req = createRequest(document, position);
let req = <GoToDefinitionRequest>createRequest(document, position);
req.WantMetadata = true;

return serverUtils.goToDefinition(this._server, req, token).then(gotoDefinitionResponse => {

if (gotoDefinitionResponse && gotoDefinitionResponse.FileName) {
return toLocation(gotoDefinitionResponse);
} else if (gotoDefinitionResponse.MetadataSource) {
const metadataSource: MetadataSource = gotoDefinitionResponse.MetadataSource;

return serverUtils.getMetadata(this._server, <MetadataRequest> {
Timeout: 5000,
AssemblyName: metadataSource.AssemblyName,
VersionNumber: metadataSource.VersionNumber,
ProjectName: metadataSource.ProjectName,
Language: metadataSource.Language,
TypeName: metadataSource.TypeName
}).then(metadataResponse => {
if (!metadataResponse || !metadataResponse.Source || !metadataResponse.SourceName) {
return;
}

return serverUtils.goToDefinition(this._server, req, token).then(value => {
if (value && value.FileName) {
return toLocation(value);
}
const uri: Uri = this._definitionMetadataDocumentProvider.addMetadataResponse(metadataResponse);
return new Location(uri, new Position(gotoDefinitionResponse.Line - 1, gotoDefinitionResponse.Column - 1));
});
}
});
}
}
16 changes: 11 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import * as coreclrdebug from './coreclr-debug/activate';
import {addAssetsIfNecessary} from './assets';
import * as vscode from 'vscode';
import TelemetryReporter from 'vscode-extension-telemetry';
import {DefinitionMetadataDocumentProvider} from './features/definitionMetadataDocumentProvider';


export function activate(context: vscode.ExtensionContext): any {

const extensionId = 'ms-vscode.csharp';
const extension = vscode.extensions.getExtension(extensionId);
const extensionVersion = extension.packageJSON.version;
Expand All @@ -48,7 +50,11 @@ export function activate(context: vscode.ExtensionContext): any {

disposables.push(server.onServerStart(() => {
// register language feature provider on start
localDisposables.push(vscode.languages.registerDefinitionProvider(_selector, new DefinitionProvider(server)));
const definitionMetadataDocumentProvider = new DefinitionMetadataDocumentProvider();
definitionMetadataDocumentProvider.register();
localDisposables.push(definitionMetadataDocumentProvider);

localDisposables.push(vscode.languages.registerDefinitionProvider(_selector, new DefinitionProvider(server, definitionMetadataDocumentProvider)));
localDisposables.push(vscode.languages.registerCodeLensProvider(_selector, new CodeLensProvider(server)));
localDisposables.push(vscode.languages.registerDocumentHighlightProvider(_selector, new DocumentHighlightProvider(server)));
localDisposables.push(vscode.languages.registerDocumentSymbolProvider(_selector, new DocumentSymbolProvider(server)));
Expand All @@ -74,7 +80,7 @@ export function activate(context: vscode.ExtensionContext): any {

disposables.push(registerCommands(server, context.extensionPath));
disposables.push(reportStatus(server));

disposables.push(server.onServerStart(() => {
// Update or add tasks.json and launch.json
addAssetsIfNecessary(server);
Expand All @@ -89,10 +95,10 @@ export function activate(context: vscode.ExtensionContext): any {
advisor.dispose();
server.stop();
}));

// activate coreclr-debug
coreclrdebug.activate(context, reporter);

context.subscriptions.push(...disposables);
}

27 changes: 27 additions & 0 deletions src/omnisharp/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export module Requests {
export const SignatureHelp = '/signatureHelp';
export const TypeLookup = '/typelookup';
export const UpdateBuffer = '/updatebuffer';
export const Metadata = '/metadata';
}

export interface Request {
Expand All @@ -36,6 +37,11 @@ export interface Request {
Changes?: LinePositionSpanTextChange[];
}

export interface GoToDefinitionRequest extends Request
{
WantMetadata?: boolean;
}

export interface LinePositionSpanTextChange {
NewText: string;
StartLine: number;
Expand All @@ -44,6 +50,23 @@ export interface LinePositionSpanTextChange {
EndColumn: number;
}

export interface MetadataSource {
AssemblyName: string;
ProjectName: string;
VersionNumber: string;
Language: string;
TypeName: string;
}

export interface MetadataRequest extends MetadataSource {
Timeout?: number;
}

export interface MetadataResponse {
SourceName: string;
Source: string;
}

export interface UpdateBufferRequest extends Request {
FromDisk?: boolean;
}
Expand Down Expand Up @@ -119,6 +142,10 @@ export interface ResourceLocation {
Column: number;
}

export interface GoToDefinitionResponse extends ResourceLocation {
MetadataSource?: MetadataSource;
}

export interface Error {
Message: string;
Line: number;
Expand Down
8 changes: 4 additions & 4 deletions src/omnisharp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export abstract class OmnisharpServer {
return {
path: config.get<string>('omnisharp'),
usesMono: config.get<boolean>('omnisharpUsesMono')
}
};
}

private _recordRequestDelay(requestName: string, elapsedTime: number) {
Expand All @@ -122,7 +122,7 @@ export abstract class OmnisharpServer {
tracker.reportDelay(elapsedTime);
}

private _reportTelemetry() {
private _reportTelemetry() {
const delayTrackers = this._delayTrackers;

for (const path in delayTrackers) {
Expand Down Expand Up @@ -235,7 +235,7 @@ export abstract class OmnisharpServer {

private _start(launchTarget: LaunchTarget): Promise<void> {
const options = this._readOptions();

let flavor: omnisharp.Flavor;
if (options.path !== undefined && options.usesMono === true) {
flavor = omnisharp.Flavor.Mono;
Expand Down Expand Up @@ -416,7 +416,7 @@ export abstract class OmnisharpServer {

throw err;
}

const config = vscode.workspace.getConfiguration();
const proxy = config.get<string>('http.proxy');
const strictSSL = config.get('http.proxyStrictSSL', true);
Expand Down
8 changes: 6 additions & 2 deletions src/omnisharp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function getCodeActions(server: OmnisharpServer, request: protocol.V2.Get
return server.makeRequest<protocol.V2.GetCodeActionsResponse>(protocol.V2.Requests.GetCodeActions, request, token);
}

export function goToDefinition(server: OmnisharpServer, request: protocol.Request, token: vscode.CancellationToken) {
return server.makeRequest<protocol.ResourceLocation>(protocol.Requests.GoToDefinition, request);
export function goToDefinition(server: OmnisharpServer, request: protocol.GoToDefinitionRequest, token: vscode.CancellationToken) {
return server.makeRequest<protocol.GoToDefinitionResponse>(protocol.Requests.GoToDefinition, request);
}

export function rename(server: OmnisharpServer, request: protocol.RenameRequest, token: vscode.CancellationToken) {
Expand All @@ -73,6 +73,10 @@ export function updateBuffer(server: OmnisharpServer, request: protocol.UpdateBu
return server.makeRequest<boolean>(protocol.Requests.UpdateBuffer, request);
}

export function getMetadata(server: OmnisharpServer, request: protocol.MetadataRequest) {
return server.makeRequest<protocol.MetadataResponse>(protocol.Requests.Metadata, request);
}

export function getTestStartInfo(server: OmnisharpServer, request: protocol.V2.GetTestStartInfoRequest) {
return server.makeRequest<protocol.V2.GetTestStartInfoResponse>(protocol.V2.Requests.GetTestStartInfo, request);
}
Expand Down