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

Fix "go to definition" from metadata within the same metadata file #1772

Merged
Merged
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
14 changes: 8 additions & 6 deletions src/features/definitionMetadataDocumentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ export default class DefinitionMetadataDocumentProvider implements TextDocumentC
}

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

const uri = this.createUri(metadataResponse.SourceName);
this._documents.set(uri.toString(), metadataResponse);

return uri;
}

public getExistingMetadataResponseUri(sourceName: string) : Uri {
return this.createUri(sourceName);
}

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

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

private createUri(metadataResponse: MetadataResponse) : Uri {
private createUri(sourceName: string) : Uri {
return Uri.parse(this.scheme + "://" +
metadataResponse.SourceName.replace(/\\/g, "/")
.replace(/(.*)\/(.*)/g, "$1/[metadata] $2"));
sourceName.replace(/\\/g, "/").replace(/(.*)\/(.*)/g, "$1/[metadata] $2"));
}
}
14 changes: 13 additions & 1 deletion src/features/definitionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import AbstractSupport from './abstractProvider';
import {MetadataRequest, GoToDefinitionRequest, MetadataSource} from '../omnisharp/protocol';
import * as serverUtils from '../omnisharp/utils';
import {createRequest, toLocation} from '../omnisharp/typeConvertion';
import {createRequest, toLocation, toLocationFromUri} from '../omnisharp/typeConvertion';
import {Uri, TextDocument, Position, Location, CancellationToken, DefinitionProvider} from 'vscode';
import DefinitionMetadataDocumentProvider from './definitionMetadataDocumentProvider';
import TelemetryReporter from 'vscode-extension-telemetry';
Expand All @@ -29,11 +29,23 @@ export default class CSharpDefinitionProvider extends AbstractSupport implements

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

// the defintion is in source
if (gotoDefinitionResponse && gotoDefinitionResponse.FileName) {

// if it is part of an already used metadata file, retrieve its uri instead of going to the physical file
if (gotoDefinitionResponse.FileName.startsWith("$metadata$")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add something to the OmniSharp response to indicate that the result is in metadata, rather than checking the file name. Not a blocking a issue, but I wanted to capture it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I agree, good point

const uri = this._definitionMetadataDocumentProvider.getExistingMetadataResponseUri(gotoDefinitionResponse.FileName);
return toLocationFromUri(uri, gotoDefinitionResponse);
}

// if it is a normal source definition, convert the response to a location
return toLocation(gotoDefinitionResponse);

// the definition is in metadata
} else if (gotoDefinitionResponse.MetadataSource) {
const metadataSource: MetadataSource = gotoDefinitionResponse.MetadataSource;

// go to metadata endpoint for more information
return serverUtils.getMetadata(this._server, <MetadataRequest> {
Timeout: 5000,
AssemblyName: metadataSource.AssemblyName,
Expand Down
8 changes: 6 additions & 2 deletions src/omnisharp/typeConvertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import * as vscode from 'vscode';

export function toLocation(location: protocol.ResourceLocation | protocol.QuickFix): vscode.Location {
const fileName = vscode.Uri.file(location.FileName);
return toLocationFromUri(fileName, location);
}

export function toLocationFromUri(uri: vscode.Uri, location: protocol.ResourceLocation | protocol.QuickFix): vscode.Location {
const position = new vscode.Position(location.Line - 1, location.Column - 1);

const endLine = (<protocol.QuickFix>location).EndLine;
const endColumn = (<protocol.QuickFix>location).EndColumn;

if (endLine !== undefined && endColumn !== undefined) {
const endPosition = new vscode.Position(endLine - 1, endColumn - 1);
return new vscode.Location(fileName, new vscode.Range(position, endPosition));
return new vscode.Location(uri, new vscode.Range(position, endPosition));
}

return new vscode.Location(fileName, position);
return new vscode.Location(uri, position);
}

export function toRange(rangeLike: { Line: number; Column: number; EndLine: number; EndColumn: number; }): vscode.Range {
Expand Down