diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 77c7cdb59ba2d..b666a0f7e4301 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -715,20 +715,32 @@ export interface Location { */ range: IRange; } -/** - * The definition of a symbol represented as one or many [locations](#Location). - * For most programming languages there is only one location at which a symbol is - * defined. - */ -export type Definition = Location | Location[]; -export interface DefinitionLink { - origin?: IRange; +export interface LocationLink { + /** + * A range to select where this link originates from. + */ + originSelectionRange?: IRange; + + /** + * The target uri this link points to. + */ uri: URI; + + /** + * The full range this link points to. + */ range: IRange; - selectionRange?: IRange; + + /** + * A range to select this link points to. Must be contained + * in `LocationLink.range`. + */ + targetSelectionRange?: IRange; } +export type Definition = Location | Location[] | LocationLink[]; + /** * The definition provider interface defines the contract between extensions and * the [go to definition](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-definition) @@ -738,7 +750,7 @@ export interface DefinitionProvider { /** * Provide the definition of the symbol at the given position and document. */ - provideDefinition(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideDefinition(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** @@ -750,7 +762,7 @@ export interface DeclarationProvider { /** * Provide the declaration of the symbol at the given position and document. */ - provideDeclaration(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideDeclaration(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** @@ -761,7 +773,7 @@ export interface ImplementationProvider { /** * Provide the implementation of the symbol at the given position and document. */ - provideImplementation(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideImplementation(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** @@ -772,7 +784,7 @@ export interface TypeDefinitionProvider { /** * Provide the type definition of the symbol at the given position and document. */ - provideTypeDefinition(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideTypeDefinition(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** diff --git a/src/vs/editor/contrib/goToDefinition/goToDefinition.ts b/src/vs/editor/contrib/goToDefinition/goToDefinition.ts index 6eb05aa38daf5..c21bfc601938c 100644 --- a/src/vs/editor/contrib/goToDefinition/goToDefinition.ts +++ b/src/vs/editor/contrib/goToDefinition/goToDefinition.ts @@ -9,7 +9,7 @@ import { onUnexpectedExternalError } from 'vs/base/common/errors'; import { registerDefaultLanguageCommand } from 'vs/editor/browser/editorExtensions'; import { Position } from 'vs/editor/common/core/position'; import { ITextModel } from 'vs/editor/common/model'; -import { DefinitionLink, DefinitionProviderRegistry, ImplementationProviderRegistry, TypeDefinitionProviderRegistry, DeclarationProviderRegistry, ProviderResult } from 'vs/editor/common/modes'; +import { LocationLink, DefinitionProviderRegistry, ImplementationProviderRegistry, TypeDefinitionProviderRegistry, DeclarationProviderRegistry, ProviderResult } from 'vs/editor/common/modes'; import { LanguageFeatureRegistry } from 'vs/editor/common/modes/languageFeatureRegistry'; @@ -17,12 +17,12 @@ function getDefinitions( model: ITextModel, position: Position, registry: LanguageFeatureRegistry, - provide: (provider: T, model: ITextModel, position: Position) => ProviderResult -): Promise { + provide: (provider: T, model: ITextModel, position: Position) => ProviderResult +): Promise { const provider = registry.ordered(model); // get results - const promises = provider.map((provider): Promise => { + const promises = provider.map((provider): Promise => { return Promise.resolve(provide(provider, model, position)).then(undefined, err => { onUnexpectedExternalError(err); return null; @@ -34,25 +34,25 @@ function getDefinitions( } -export function getDefinitionsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { +export function getDefinitionsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { return getDefinitions(model, position, DefinitionProviderRegistry, (provider, model, position) => { return provider.provideDefinition(model, position, token); }); } -export function getDeclarationsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { +export function getDeclarationsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { return getDefinitions(model, position, DeclarationProviderRegistry, (provider, model, position) => { return provider.provideDeclaration(model, position, token); }); } -export function getImplementationsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { +export function getImplementationsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { return getDefinitions(model, position, ImplementationProviderRegistry, (provider, model, position) => { return provider.provideImplementation(model, position, token); }); } -export function getTypeDefinitionsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { +export function getTypeDefinitionsAtPosition(model: ITextModel, position: Position, token: CancellationToken): Promise { return getDefinitions(model, position, TypeDefinitionProviderRegistry, (provider, model, position) => { return provider.provideTypeDefinition(model, position, token); }); diff --git a/src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts b/src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts index 5acca6b5149cd..48cd1ad95d7a2 100644 --- a/src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts +++ b/src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts @@ -15,7 +15,7 @@ import * as corePosition from 'vs/editor/common/core/position'; import { Range } from 'vs/editor/common/core/range'; import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; import { ITextModel, IWordAtPosition } from 'vs/editor/common/model'; -import { DefinitionLink, Location } from 'vs/editor/common/modes'; +import { LocationLink, Location } from 'vs/editor/common/modes'; import { MessageController } from 'vs/editor/contrib/message/messageController'; import { PeekContext } from 'vs/editor/contrib/referenceSearch/peekViewWidget'; import { ReferencesController } from 'vs/editor/contrib/referenceSearch/referencesController'; @@ -69,7 +69,7 @@ export class DefinitionAction extends EditorAction { // * remove falsy references // * find reference at the current pos let idxOfCurrent = -1; - const result: DefinitionLink[] = []; + const result: LocationLink[] = []; for (const reference of references) { if (!reference || !reference.range) { continue; @@ -113,7 +113,7 @@ export class DefinitionAction extends EditorAction { return definitionPromise; } - protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { + protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { return getDefinitionsAtPosition(model, position, token); } @@ -256,7 +256,7 @@ export class PeekDefinitionAction extends DefinitionAction { export class DeclarationAction extends DefinitionAction { - protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { + protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { return getDeclarationsAtPosition(model, position, token); } @@ -320,7 +320,7 @@ export class PeekDeclarationAction extends DeclarationAction { } export class ImplementationAction extends DefinitionAction { - protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { + protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { return getImplementationsAtPosition(model, position, token); } @@ -378,7 +378,7 @@ export class PeekImplementationAction extends ImplementationAction { } export class TypeDefinitionAction extends DefinitionAction { - protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { + protected _getTargetLocationForPosition(model: ITextModel, position: corePosition.Position, token: CancellationToken): Promise { return getTypeDefinitionsAtPosition(model, position, token); } diff --git a/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts b/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts index d33f2b9ac68bd..55e2e6e0047e4 100644 --- a/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts +++ b/src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts @@ -12,7 +12,7 @@ import { MarkdownString } from 'vs/base/common/htmlContent'; import { IModeService } from 'vs/editor/common/services/modeService'; import { Range } from 'vs/editor/common/core/range'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import { DefinitionProviderRegistry, DefinitionLink } from 'vs/editor/common/modes'; +import { DefinitionProviderRegistry, LocationLink } from 'vs/editor/common/modes'; import { ICodeEditor, IMouseTarget, MouseTargetType } from 'vs/editor/browser/editorBrowser'; import { registerEditorContribution } from 'vs/editor/browser/editorExtensions'; import { getDefinitionsAtPosition } from './goToDefinition'; @@ -35,7 +35,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC private toUnhook: IDisposable[]; private decorations: string[]; private currentWordUnderMouse: IWordAtPosition; - private previousPromise: CancelablePromise; + private previousPromise: CancelablePromise; constructor( editor: ICodeEditor, @@ -152,8 +152,8 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC const previewValue = this.getPreviewValue(textEditorModel, startLineNumber); let wordRange: Range; - if (result.origin) { - wordRange = Range.lift(result.origin); + if (result.originSelectionRange) { + wordRange = Range.lift(result.originSelectionRange); } else { wordRange = new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn); } @@ -281,7 +281,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC DefinitionProviderRegistry.has(this.editor.getModel()); } - private findDefinition(target: IMouseTarget, token: CancellationToken): Promise { + private findDefinition(target: IMouseTarget, token: CancellationToken): Promise { const model = this.editor.getModel(); if (!model) { return Promise.resolve(null); diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index b484673dbb573..4ec47e159d8cf 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -5018,20 +5018,28 @@ declare namespace monaco.languages { range: IRange; } - /** - * The definition of a symbol represented as one or many [locations](#Location). - * For most programming languages there is only one location at which a symbol is - * defined. - */ - export type Definition = Location | Location[]; - - export interface DefinitionLink { - origin?: IRange; + export interface LocationLink { + /** + * A range to select where this link originates from. + */ + originSelectionRange?: IRange; + /** + * The target uri this link points to. + */ uri: Uri; + /** + * The full range this link points to. + */ range: IRange; - selectionRange?: IRange; + /** + * A range to select this link points to. Must be contained + * in `LocationLink.range`. + */ + targetSelectionRange?: IRange; } + export type Definition = Location | Location[] | LocationLink[]; + /** * The definition provider interface defines the contract between extensions and * the [go to definition](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-definition) @@ -5041,7 +5049,7 @@ declare namespace monaco.languages { /** * Provide the definition of the symbol at the given position and document. */ - provideDefinition(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideDefinition(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** @@ -5053,7 +5061,7 @@ declare namespace monaco.languages { /** * Provide the declaration of the symbol at the given position and document. */ - provideDeclaration(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideDeclaration(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** @@ -5064,7 +5072,7 @@ declare namespace monaco.languages { /** * Provide the implementation of the symbol at the given position and document. */ - provideImplementation(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideImplementation(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** @@ -5075,7 +5083,7 @@ declare namespace monaco.languages { /** * Provide the type definition of the symbol at the given position and document. */ - provideTypeDefinition(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; + provideTypeDefinition(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult; } /** diff --git a/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts b/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts index 86411c0fc92eb..eeec73908cf91 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts @@ -69,17 +69,17 @@ export class MainThreadLanguageFeatures implements MainThreadLanguageFeaturesSha } } - private static _reviveDefinitionLinkDto(data: DefinitionLinkDto): modes.DefinitionLink; - private static _reviveDefinitionLinkDto(data: DefinitionLinkDto[]): modes.DefinitionLink[]; - private static _reviveDefinitionLinkDto(data: DefinitionLinkDto | DefinitionLinkDto[]): modes.DefinitionLink | modes.DefinitionLink[] { + private static _reviveLocationLinkDto(data: DefinitionLinkDto): modes.LocationLink; + private static _reviveLocationLinkDto(data: DefinitionLinkDto[]): modes.LocationLink[]; + private static _reviveLocationLinkDto(data: DefinitionLinkDto | DefinitionLinkDto[]): modes.LocationLink | modes.LocationLink[] { if (!data) { - return data; + return data; } else if (Array.isArray(data)) { - data.forEach(l => MainThreadLanguageFeatures._reviveDefinitionLinkDto(l)); - return data; + data.forEach(l => MainThreadLanguageFeatures._reviveLocationLinkDto(l)); + return data; } else { data.uri = URI.revive(data.uri); - return data; + return data; } } @@ -150,8 +150,8 @@ export class MainThreadLanguageFeatures implements MainThreadLanguageFeaturesSha $registerDefinitionSupport(handle: number, selector: ISerializedDocumentFilter[]): void { this._registrations[handle] = modes.DefinitionProviderRegistry.register(typeConverters.LanguageSelector.from(selector), { - provideDefinition: (model, position, token): Promise => { - return this._proxy.$provideDefinition(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveDefinitionLinkDto); + provideDefinition: (model, position, token): Promise => { + return this._proxy.$provideDefinition(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveLocationLinkDto); } }); } @@ -159,23 +159,23 @@ export class MainThreadLanguageFeatures implements MainThreadLanguageFeaturesSha $registerDeclarationSupport(handle: number, selector: ISerializedDocumentFilter[]): void { this._registrations[handle] = modes.DeclarationProviderRegistry.register(typeConverters.LanguageSelector.from(selector), { provideDeclaration: (model, position, token) => { - return this._proxy.$provideDeclaration(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveDefinitionLinkDto); + return this._proxy.$provideDeclaration(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveLocationLinkDto); } }); } $registerImplementationSupport(handle: number, selector: ISerializedDocumentFilter[]): void { this._registrations[handle] = modes.ImplementationProviderRegistry.register(typeConverters.LanguageSelector.from(selector), { - provideImplementation: (model, position, token): Promise => { - return this._proxy.$provideImplementation(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveDefinitionLinkDto); + provideImplementation: (model, position, token): Promise => { + return this._proxy.$provideImplementation(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveLocationLinkDto); } }); } $registerTypeDefinitionSupport(handle: number, selector: ISerializedDocumentFilter[]): void { this._registrations[handle] = modes.TypeDefinitionProviderRegistry.register(typeConverters.LanguageSelector.from(selector), { - provideTypeDefinition: (model, position, token): Promise => { - return this._proxy.$provideTypeDefinition(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveDefinitionLinkDto); + provideTypeDefinition: (model, position, token): Promise => { + return this._proxy.$provideTypeDefinition(handle, model.uri, position, token).then(MainThreadLanguageFeatures._reviveLocationLinkDto); } }); } diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 9bd72240a4605..53f6578b4b9cc 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -804,10 +804,10 @@ export interface LocationDto { } export interface DefinitionLinkDto { - origin?: IRange; + originSelectionRange?: IRange; uri: UriComponents; range: IRange; - selectionRange?: IRange; + targetSelectionRange?: IRange; } export interface WorkspaceSymbolDto extends IdObject { diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index de1cd42dbfd6b..0801f8f05d930 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -143,7 +143,7 @@ class CodeLensAdapter { } } -function convertToDefinitionLinks(value: vscode.Definition): modes.DefinitionLink[] { +function convertToLocationLinks(value: vscode.Definition): modes.LocationLink[] { if (Array.isArray(value)) { return (value as (vscode.DefinitionLink | vscode.Location)[]).map(typeConvert.DefinitionLink.from); } else if (value) { @@ -159,10 +159,10 @@ class DefinitionAdapter { private readonly _provider: vscode.DefinitionProvider ) { } - provideDefinition(resource: URI, position: IPosition, token: CancellationToken): Promise { + provideDefinition(resource: URI, position: IPosition, token: CancellationToken): Promise { let doc = this._documents.getDocumentData(resource).document; let pos = typeConvert.Position.to(position); - return asPromise(() => this._provider.provideDefinition(doc, pos, token)).then(convertToDefinitionLinks); + return asPromise(() => this._provider.provideDefinition(doc, pos, token)).then(convertToLocationLinks); } } @@ -173,10 +173,10 @@ class DeclarationAdapter { private readonly _provider: vscode.DeclarationProvider ) { } - provideDeclaration(resource: URI, position: IPosition, token: CancellationToken): Promise { + provideDeclaration(resource: URI, position: IPosition, token: CancellationToken): Promise { let doc = this._documents.getDocumentData(resource).document; let pos = typeConvert.Position.to(position); - return asPromise(() => this._provider.provideDeclaration(doc, pos, token)).then(convertToDefinitionLinks); + return asPromise(() => this._provider.provideDeclaration(doc, pos, token)).then(convertToLocationLinks); } } @@ -187,10 +187,10 @@ class ImplementationAdapter { private readonly _provider: vscode.ImplementationProvider ) { } - provideImplementation(resource: URI, position: IPosition, token: CancellationToken): Promise { + provideImplementation(resource: URI, position: IPosition, token: CancellationToken): Promise { let doc = this._documents.getDocumentData(resource).document; let pos = typeConvert.Position.to(position); - return asPromise(() => this._provider.provideImplementation(doc, pos, token)).then(convertToDefinitionLinks); + return asPromise(() => this._provider.provideImplementation(doc, pos, token)).then(convertToLocationLinks); } } @@ -201,10 +201,10 @@ class TypeDefinitionAdapter { private readonly _provider: vscode.TypeDefinitionProvider ) { } - provideTypeDefinition(resource: URI, position: IPosition, token: CancellationToken): Promise { + provideTypeDefinition(resource: URI, position: IPosition, token: CancellationToken): Promise { const doc = this._documents.getDocumentData(resource).document; const pos = typeConvert.Position.to(position); - return asPromise(() => this._provider.provideTypeDefinition(doc, pos, token)).then(convertToDefinitionLinks); + return asPromise(() => this._provider.provideTypeDefinition(doc, pos, token)).then(convertToLocationLinks); } } @@ -1060,7 +1060,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape { return this._createDisposable(handle); } - $provideDefinition(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { + $provideDefinition(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { return this._withAdapter(handle, DefinitionAdapter, adapter => adapter.provideDefinition(URI.revive(resource), position, token)); } @@ -1070,7 +1070,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape { return this._createDisposable(handle); } - $provideDeclaration(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { + $provideDeclaration(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { return this._withAdapter(handle, DeclarationAdapter, adapter => adapter.provideDeclaration(URI.revive(resource), position, token)); } @@ -1080,7 +1080,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape { return this._createDisposable(handle); } - $provideImplementation(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { + $provideImplementation(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { return this._withAdapter(handle, ImplementationAdapter, adapter => adapter.provideImplementation(URI.revive(resource), position, token)); } @@ -1090,7 +1090,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape { return this._createDisposable(handle); } - $provideTypeDefinition(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { + $provideTypeDefinition(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Promise { return this._withAdapter(handle, TypeDefinitionAdapter, adapter => adapter.provideTypeDefinition(URI.revive(resource), position, token)); } diff --git a/src/vs/workbench/api/node/extHostTypeConverters.ts b/src/vs/workbench/api/node/extHostTypeConverters.ts index d1a3c87893666..81e7989997223 100644 --- a/src/vs/workbench/api/node/extHostTypeConverters.ts +++ b/src/vs/workbench/api/node/extHostTypeConverters.ts @@ -583,16 +583,16 @@ export namespace location { } export namespace DefinitionLink { - export function from(value: vscode.Location | vscode.DefinitionLink): modes.DefinitionLink { + export function from(value: vscode.Location | vscode.DefinitionLink): modes.LocationLink { const definitionLink = value; const location = value; return { - origin: definitionLink.originSelectionRange + originSelectionRange: definitionLink.originSelectionRange ? Range.from(definitionLink.originSelectionRange) : undefined, uri: definitionLink.targetUri ? definitionLink.targetUri : location.uri, range: Range.from(definitionLink.targetRange ? definitionLink.targetRange : location.range), - selectionRange: definitionLink.targetSelectionRange + targetSelectionRange: definitionLink.targetSelectionRange ? Range.from(definitionLink.targetSelectionRange) : undefined, };