ICodeEditor.getContribution can return null

This commit is contained in:
Alex Dima
2021-12-06 17:56:55 +01:00
parent 2f898c4c98
commit 2fc1dac229
67 changed files with 229 additions and 177 deletions

View File

@@ -229,7 +229,7 @@ export abstract class EditorCommand extends Command {
/**
* Create a command class that is bound to a certain editor contribution.
*/
public static bindToContribution<T extends IEditorContribution>(controllerGetter: (editor: ICodeEditor) => T): EditorControllerCommand<T> {
public static bindToContribution<T extends IEditorContribution>(controllerGetter: (editor: ICodeEditor) => T | null): EditorControllerCommand<T> {
return class EditorControllerCommandImpl extends EditorCommand {
private readonly _callback: (controller: T, args: any) => void;
@@ -242,7 +242,7 @@ export abstract class EditorCommand extends Command {
public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void {
const controller = controllerGetter(editor);
if (controller) {
this._callback(controllerGetter(editor), args);
this._callback(controller, args);
}
}
};
@@ -351,6 +351,25 @@ export abstract class EditorAction extends EditorCommand {
public abstract run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void>;
}
interface IEditorContributionCtorWithGet<T extends IEditorContribution> {
get(editor: ICodeEditor): T | null;
}
export abstract class EditorControllerAction<T extends IEditorContribution> extends EditorAction {
protected abstract controller: IEditorContributionCtorWithGet<T>;
run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {
const controller = this.controller.get(editor);
if (!controller) {
return;
}
return this.runControllerAction(accessor, editor, controller, args);
}
abstract runControllerAction(accessor: ServicesAccessor, editor: ICodeEditor, controller: T, args: any): void | Promise<void>;
}
export type EditorActionImplementation = (accessor: ServicesAccessor, editor: ICodeEditor, args: any) => boolean | Promise<void>;
export class MultiEditorAction extends EditorAction {