Skip to content

Commit

Permalink
Make CodeEditorWidget predicate injectable
Browse files Browse the repository at this point in the history
This allows downstream projects to add custom editors (not text) to the predicate and support the following scenarios for those editors:
- editor/title contribution
- command workbench.action.closeActiveEditor
- command workbench.action.closeOtherEditors
- command workbench.action.closeEditorsInGroup
- command workbench.action.closeEditorsInOtherGroups
- command workbench.action.closeEditorsToTheLeft
- command workbench.action.closeEditorsToTheRight
- command workbench.action.closeAllEditors

Signed-off-by: Amiram Wingarten <amiram.wingarten@sap.com>
  • Loading branch information
amiramw committed Oct 28, 2020
1 parent e07af50 commit b3aa18f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import { ApplicationShellMouseTracker } from '@theia/core/lib/browser/shell/appl
import { CommandService } from '@theia/core/lib/common/command';
import TheiaURI from '@theia/core/lib/common/uri';
import { EditorManager } from '@theia/editor/lib/browser';
import { CodeEditorWidget } from '@theia/plugin-ext/lib/main/browser/menus/menus-contribution-handler';
import {
CodeEditorWidgetUtil
} from '@theia/plugin-ext/lib/main/browser/menus/menus-contribution-handler';
import {
TextDocumentShowOptions,
Location,
Expand Down Expand Up @@ -102,6 +104,8 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
protected readonly quickOpenWorkspace: QuickOpenWorkspace;
@inject(TerminalService)
protected readonly terminalService: TerminalService;
@inject(CodeEditorWidgetUtil)
protected readonly codeEditorWidgetUtil: CodeEditorWidgetUtil;

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(VscodeCommands.OPEN, {
Expand Down Expand Up @@ -233,7 +237,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
return (resourceUri && resourceUri.toString()) === uriString;
});
}
if (CodeEditorWidget.is(widget)) {
if (this.codeEditorWidgetUtil.is(widget)) {
await this.shell.closeWidget(widget.id);
}
}
Expand All @@ -249,7 +253,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
});
}
for (const widget of this.shell.widgets) {
if (CodeEditorWidget.is(widget) && widget !== editor) {
if (this.codeEditorWidgetUtil.is(widget) && widget !== editor) {
await this.shell.closeWidget(widget.id);
}
}
Expand All @@ -269,7 +273,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
const tabBar = this.shell.getTabBarFor(editor);
if (tabBar) {
this.shell.closeTabs(tabBar,
({ owner }) => CodeEditorWidget.is(owner)
({ owner }) => this.codeEditorWidgetUtil.is(owner)
);
}
}
Expand All @@ -283,7 +287,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
for (const tabBar of this.shell.allTabBars) {
if (tabBar !== editorTabBar) {
this.shell.closeTabs(tabBar,
({ owner }) => CodeEditorWidget.is(owner)
({ owner }) => this.codeEditorWidgetUtil.is(owner)
);
}
}
Expand All @@ -303,7 +307,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
left = false;
return false;
}
return left && CodeEditorWidget.is(owner);
return left && this.codeEditorWidgetUtil.is(owner);
}
);
}
Expand All @@ -323,7 +327,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
left = false;
return false;
}
return !left && CodeEditorWidget.is(owner);
return !left && this.codeEditorWidgetUtil.is(owner);
}
);
}
Expand All @@ -334,7 +338,7 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
execute: async () => {
const promises = [];
for (const widget of this.shell.widgets) {
if (CodeEditorWidget.is(widget)) {
if (this.codeEditorWidgetUtil.is(widget)) {
promises.push(this.shell.closeWidget(widget.id));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ import { TIMELINE_ITEM_CONTEXT_MENU } from '@theia/timeline/lib/browser/timeline
import { TimelineItem } from '@theia/timeline/lib/common/timeline-model';

type CodeEditorWidget = EditorWidget | WebviewWidget;
export namespace CodeEditorWidget {
export function is(arg: any): arg is CodeEditorWidget {
@injectable()
export class CodeEditorWidgetUtil {
is(arg: any): arg is CodeEditorWidget {
return arg instanceof EditorWidget || arg instanceof WebviewWidget;
}
export function getResourceUri(editor: CodeEditorWidget): CodeUri | undefined {
getResourceUri(editor: CodeEditorWidget): CodeUri | undefined {
const resourceUri = Navigatable.is(editor) && editor.getResourceUri();
return resourceUri ? resourceUri['codeUri'] : undefined;
}
Expand Down Expand Up @@ -88,6 +89,9 @@ export class MenusContributionPointHandler {
@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

@inject(CodeEditorWidgetUtil)
protected readonly codeEditorWidgetUtil: CodeEditorWidgetUtil;

handle(plugin: DeployedPlugin): Disposable {
const allMenus = plugin.contributes && plugin.contributes.menus;
if (!allMenus) {
Expand All @@ -104,9 +108,9 @@ export class MenusContributionPointHandler {
} else if (location === 'editor/title') {
for (const action of allMenus[location]) {
toDispose.push(this.registerTitleAction(location, action, {
execute: widget => CodeEditorWidget.is(widget) && this.commands.executeCommand(action.command, CodeEditorWidget.getResourceUri(widget)),
isEnabled: widget => CodeEditorWidget.is(widget) && this.commands.isEnabled(action.command, CodeEditorWidget.getResourceUri(widget)),
isVisible: widget => CodeEditorWidget.is(widget) && this.commands.isVisible(action.command, CodeEditorWidget.getResourceUri(widget))
execute: widget => this.codeEditorWidgetUtil.is(widget) && this.commands.executeCommand(action.command, this.codeEditorWidgetUtil.getResourceUri(widget)),
isEnabled: widget => this.codeEditorWidgetUtil.is(widget) && this.commands.isEnabled(action.command, this.codeEditorWidgetUtil.getResourceUri(widget)),
isVisible: widget => this.codeEditorWidgetUtil.is(widget) && this.commands.isVisible(action.command, this.codeEditorWidgetUtil.getResourceUri(widget))
}));
}
} else if (location === 'view/title') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { PluginFrontendViewContribution } from './plugin-frontend-view-contribut
import { PluginExtDeployCommandService } from './plugin-ext-deploy-command';
import { EditorModelService } from './text-editor-model-service';
import { UntitledResourceResolver } from './editor/untitled-resource';
import { MenusContributionPointHandler } from './menus/menus-contribution-handler';
import { CodeEditorWidgetUtil, MenusContributionPointHandler } from './menus/menus-contribution-handler';
import { PluginContributionHandler } from './plugin-contribution-handler';
import { PluginViewRegistry, PLUGIN_VIEW_CONTAINER_FACTORY_ID, PLUGIN_VIEW_FACTORY_ID, PLUGIN_VIEW_DATA_FACTORY_ID } from './view/plugin-view-registry';
import { TextContentResourceResolver } from './workspace-main';
Expand Down Expand Up @@ -185,6 +185,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(LabelProviderContribution).toService(PluginIconThemeService);

bind(MenusContributionPointHandler).toSelf().inSingletonScope();
bind(CodeEditorWidgetUtil).toSelf().inSingletonScope();
bind(KeybindingsContributionPointHandler).toSelf().inSingletonScope();
bind(PluginContributionHandler).toSelf().inSingletonScope();

Expand Down

0 comments on commit b3aa18f

Please sign in to comment.