Skip to content

Commit

Permalink
simplify how alternative commands are defined, #73081
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Nov 22, 2019
1 parent 8fc9c48 commit a88a14d
Showing 1 changed file with 14 additions and 34 deletions.
48 changes: 14 additions & 34 deletions src/vs/editor/contrib/gotoSymbol/goToCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<void> {

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 {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
}
Expand All @@ -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"),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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"),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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"),
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit a88a14d

Please sign in to comment.