Skip to content

Commit 25e8854

Browse files
Merge pull request #1772 from filipw/feature/better-metadata-navigation
Fix "go to definition" from metadata within the same metadata file
2 parents 92e6442 + 95e4d45 commit 25e8854

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/features/definitionMetadataDocumentProvider.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@ export default class DefinitionMetadataDocumentProvider implements TextDocumentC
2323
}
2424

2525
public addMetadataResponse(metadataResponse: MetadataResponse) : Uri {
26-
const uri = this.createUri(metadataResponse);
27-
26+
const uri = this.createUri(metadataResponse.SourceName);
2827
this._documents.set(uri.toString(), metadataResponse);
2928

3029
return uri;
3130
}
3231

32+
public getExistingMetadataResponseUri(sourceName: string) : Uri {
33+
return this.createUri(sourceName);
34+
}
35+
3336
public register() : void {
3437
this._registration = workspace.registerTextDocumentContentProvider(this.scheme, this);
3538
}
3639

37-
public provideTextDocumentContent(uri : Uri) : string {
40+
public provideTextDocumentContent(uri: Uri) : string {
3841
return this._documents.get(uri.toString()).Source;
3942
}
4043

41-
private createUri(metadataResponse: MetadataResponse) : Uri {
44+
private createUri(sourceName: string) : Uri {
4245
return Uri.parse(this.scheme + "://" +
43-
metadataResponse.SourceName.replace(/\\/g, "/")
44-
.replace(/(.*)\/(.*)/g, "$1/[metadata] $2"));
46+
sourceName.replace(/\\/g, "/").replace(/(.*)\/(.*)/g, "$1/[metadata] $2"));
4547
}
4648
}

src/features/definitionProvider.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import AbstractSupport from './abstractProvider';
99
import {MetadataRequest, GoToDefinitionRequest, MetadataSource} from '../omnisharp/protocol';
1010
import * as serverUtils from '../omnisharp/utils';
11-
import {createRequest, toLocation} from '../omnisharp/typeConvertion';
11+
import {createRequest, toLocation, toLocationFromUri} from '../omnisharp/typeConvertion';
1212
import {Uri, TextDocument, Position, Location, CancellationToken, DefinitionProvider} from 'vscode';
1313
import DefinitionMetadataDocumentProvider from './definitionMetadataDocumentProvider';
1414
import TelemetryReporter from 'vscode-extension-telemetry';
@@ -29,11 +29,23 @@ export default class CSharpDefinitionProvider extends AbstractSupport implements
2929

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

32+
// the defintion is in source
3233
if (gotoDefinitionResponse && gotoDefinitionResponse.FileName) {
34+
35+
// if it is part of an already used metadata file, retrieve its uri instead of going to the physical file
36+
if (gotoDefinitionResponse.FileName.startsWith("$metadata$")) {
37+
const uri = this._definitionMetadataDocumentProvider.getExistingMetadataResponseUri(gotoDefinitionResponse.FileName);
38+
return toLocationFromUri(uri, gotoDefinitionResponse);
39+
}
40+
41+
// if it is a normal source definition, convert the response to a location
3342
return toLocation(gotoDefinitionResponse);
43+
44+
// the definition is in metadata
3445
} else if (gotoDefinitionResponse.MetadataSource) {
3546
const metadataSource: MetadataSource = gotoDefinitionResponse.MetadataSource;
3647

48+
// go to metadata endpoint for more information
3749
return serverUtils.getMetadata(this._server, <MetadataRequest> {
3850
Timeout: 5000,
3951
AssemblyName: metadataSource.AssemblyName,

src/omnisharp/typeConvertion.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ import * as vscode from 'vscode';
99

1010
export function toLocation(location: protocol.ResourceLocation | protocol.QuickFix): vscode.Location {
1111
const fileName = vscode.Uri.file(location.FileName);
12+
return toLocationFromUri(fileName, location);
13+
}
14+
15+
export function toLocationFromUri(uri: vscode.Uri, location: protocol.ResourceLocation | protocol.QuickFix): vscode.Location {
1216
const position = new vscode.Position(location.Line - 1, location.Column - 1);
1317

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

1721
if (endLine !== undefined && endColumn !== undefined) {
1822
const endPosition = new vscode.Position(endLine - 1, endColumn - 1);
19-
return new vscode.Location(fileName, new vscode.Range(position, endPosition));
23+
return new vscode.Location(uri, new vscode.Range(position, endPosition));
2024
}
2125

22-
return new vscode.Location(fileName, position);
26+
return new vscode.Location(uri, position);
2327
}
2428

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

0 commit comments

Comments
 (0)