From a88a14d617c90398d5ed81b4031f028713da5890 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Thu, 21 Nov 2019 16:24:06 +0100 Subject: [PATCH] simplify how alternative commands are defined, #73081 --- .../editor/contrib/gotoSymbol/goToCommands.ts | 48 ++++++------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/src/vs/editor/contrib/gotoSymbol/goToCommands.ts b/src/vs/editor/contrib/gotoSymbol/goToCommands.ts index 2c7b9cccefb93..a0f7071092583 100644 --- a/src/vs/editor/contrib/gotoSymbol/goToCommands.ts +++ b/src/vs/editor/contrib/gotoSymbol/goToCommands.ts @@ -49,15 +49,16 @@ export interface SymbolNavigationActionConfig { openToSide: boolean; openInPeek: boolean; muteMessage: boolean; + alternativeCommand?: string; } abstract class SymbolNavigationAction extends EditorAction { - private readonly _configuration: SymbolNavigationActionConfig; + private readonly _config: SymbolNavigationActionConfig; constructor(configuration: SymbolNavigationActionConfig, opts: IActionOptions) { super(opts); - this._configuration = configuration; + this._config = configuration; } run(accessor: ServicesAccessor, editor: ICodeEditor): Promise { @@ -83,11 +84,11 @@ abstract class SymbolNavigationAction extends EditorAction { alert(references.ariaMessage); const referenceCount = references.references.length; - const altAction = references.referenceAt(model.uri, pos) && editor.getAction(this._getAlternativeCommand()); + const altAction = references.referenceAt(model.uri, pos) && editor.getAction(this._config.alternativeCommand || ''); if (referenceCount === 0) { // no result -> show message - if (!this._configuration.muteMessage) { + if (!this._config.muteMessage) { const info = model.getWordAtPosition(pos); MessageController.get(editor).showMessage(this._getNoResultFoundMessage(info), pos); } @@ -115,20 +116,18 @@ abstract class SymbolNavigationAction extends EditorAction { protected abstract _getNoResultFoundMessage(info: IWordAtPosition | null): string; - protected abstract _getAlternativeCommand(): string; - protected abstract _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues; private async _onResult(editorService: ICodeEditorService, symbolNavService: ISymbolNavigationService, editor: IActiveCodeEditor, model: ReferencesModel): Promise { const gotoLocation = this._getGoToPreference(editor); - if (this._configuration.openInPeek || (gotoLocation === 'peek' && model.references.length > 1)) { + if (this._config.openInPeek || (gotoLocation === 'peek' && model.references.length > 1)) { this._openInPeek(editor, model); } else { const next = model.firstReference()!; const peek = model.references.length > 1 && gotoLocation === 'gotoAndPeek'; - const targetEditor = await this._openReference(editor, editorService, next, this._configuration.openToSide, !peek); + const targetEditor = await this._openReference(editor, editorService, next, this._config.openToSide, !peek); if (peek && targetEditor) { this._openInPeek(targetEditor, model); } else { @@ -182,7 +181,7 @@ abstract class SymbolNavigationAction extends EditorAction { private _openInPeek(target: ICodeEditor, model: ReferencesModel) { let controller = ReferencesController.get(target); if (controller && target.hasModel()) { - controller.toggleWidget(target.getSelection(), createCancelablePromise(_ => Promise.resolve(model)), this._configuration.openInPeek); + controller.toggleWidget(target.getSelection(), createCancelablePromise(_ => Promise.resolve(model)), this._config.openInPeek); } else { model.dispose(); } @@ -203,10 +202,6 @@ export class DefinitionAction extends SymbolNavigationAction { : nls.localize('generic.noResults', "No definition found"); } - protected _getAlternativeCommand(): string { - return 'editor.action.goToReferences'; - } - protected _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues { return editor.getOption(EditorOption.gotoLocation).multipleDefinitions; } @@ -224,7 +219,8 @@ registerEditorAction(class GoToDefinitionAction extends DefinitionAction { super({ openToSide: false, openInPeek: false, - muteMessage: false + muteMessage: false, + alternativeCommand: 'editor.action.goToReferences' }, { id: GoToDefinitionAction.id, label: nls.localize('actions.goToDecl.label', "Go to Definition"), @@ -327,10 +323,6 @@ class DeclarationAction extends SymbolNavigationAction { : nls.localize('decl.generic.noResults', "No declaration found"); } - protected _getAlternativeCommand(): string { - return 'editor.action.goToReferences'; - } - protected _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues { return editor.getOption(EditorOption.gotoLocation).multipleDeclarations; } @@ -344,7 +336,8 @@ registerEditorAction(class GoToDeclarationAction extends DeclarationAction { super({ openToSide: false, openInPeek: false, - muteMessage: false + muteMessage: false, + alternativeCommand: 'editor.action.goToReferences' }, { id: GoToDeclarationAction.id, label: nls.localize('actions.goToDeclaration.label', "Go to Declaration"), @@ -412,10 +405,6 @@ class TypeDefinitionAction extends SymbolNavigationAction { : nls.localize('goToTypeDefinition.generic.noResults', "No type definition found"); } - protected _getAlternativeCommand(): string { - return 'editor.action.goToReferences'; - } - protected _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues { return editor.getOption(EditorOption.gotoLocation).multipleTypeDefinitions; } @@ -429,7 +418,8 @@ registerEditorAction(class GoToTypeDefinitionAction extends TypeDefinitionAction super({ openToSide: false, openInPeek: false, - muteMessage: false + muteMessage: false, + alternativeCommand: 'editor.action.goToReferences' }, { id: GoToTypeDefinitionAction.ID, label: nls.localize('actions.goToTypeDefinition.label', "Go to Type Definition"), @@ -498,10 +488,6 @@ class ImplementationAction extends SymbolNavigationAction { : nls.localize('goToImplementation.generic.noResults', "No implementation found"); } - protected _getAlternativeCommand(): string { - return ''; - } - protected _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues { return editor.getOption(EditorOption.gotoLocation).multipleImplementations; } @@ -589,10 +575,6 @@ class ReferencesAction extends SymbolNavigationAction { : nls.localize('references.noGeneric', "No references found"); } - protected _getAlternativeCommand(): string { - return ''; - } - protected _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues { return editor.getOption(EditorOption.gotoLocation).multipleReferences; } @@ -694,8 +676,6 @@ class GenericGoToLocationAction extends SymbolNavigationAction { protected _getGoToPreference(editor: IActiveCodeEditor): GoToLocationValues { return this._gotoMultipleBehaviour ?? editor.getOption(EditorOption.gotoLocation).multipleReferences; } - - protected _getAlternativeCommand() { return ''; } } CommandsRegistry.registerCommand({