Skip to content

Commit

Permalink
register vscode.openWith api (eclipse-theia#9881)
Browse files Browse the repository at this point in the history
Signed-off-by: Ju, Gyeong-min <[email protected]>
  • Loading branch information
amos42 authored Sep 9, 2021
1 parent a6bfede commit 10d5275
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
CommonCommands,
NavigatableWidget,
open,
OpenerService,
OpenerService, OpenHandler,
QuickInputService,
Saveable,
TabBar,
Expand Down Expand Up @@ -76,12 +76,17 @@ import {
fromDefinition,
toDefinition
} from '@theia/plugin-ext/lib/main/browser/callhierarchy/callhierarchy-type-converters';
import { CustomEditorOpener } from '@theia/plugin-ext/lib/main/browser/custom-editors/custom-editor-opener';

export namespace VscodeCommands {
export const OPEN: Command = {
id: 'vscode.open'
};

export const OPEN_WITH: Command = {
id: 'vscode.openWith'
};

export const OPEN_FOLDER: Command = {
id: 'vscode.openFolder'
};
Expand Down Expand Up @@ -132,29 +137,81 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
@inject(MonacoTextModelService)
protected readonly textModelService: MonacoTextModelService;

private async openWith(commandId: string, resource: URI, columnOrOptions?: ViewColumn | TextDocumentShowOptions, openerId?: string): Promise<boolean> {
if (!resource) {
throw new Error(`${commandId} command requires at least URI argument.`);
}
if (!URI.isUri(resource)) {
throw new Error(`Invalid argument for ${commandId} command with URI argument. Found ${resource}`);
}

let options: TextDocumentShowOptions | undefined;
if (typeof columnOrOptions === 'number') {
options = {
viewColumn: columnOrOptions
};
} else if (columnOrOptions) {
options = {
...columnOrOptions
};
}

const uri = new TheiaURI(resource);
const editorOptions = DocumentsMainImpl.toEditorOpenerOptions(this.shell, options);

let opener: OpenHandler | undefined;
if (typeof openerId === 'string') {
const lowerViewType = openerId.toLowerCase();
const openers = await this.openerService.getOpeners();
for (const opnr of openers) {
const idLowerCase = opnr.id.toLowerCase();
if (lowerViewType === idLowerCase) {
opener = opnr;
break;
}
}
} else {
opener = await this.openerService.getOpener(uri, editorOptions);
}

if (opener) {
await opener.open(uri, editorOptions);
return true;
}

return false;
}

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(VscodeCommands.OPEN, {
isVisible: () => false,
execute: async (resource: URI, columnOrOptions?: ViewColumn | TextDocumentShowOptions) => {
if (!resource) {
throw new Error(`${VscodeCommands.OPEN.id} command requires at least URI argument.`);
const result = await this.openWith(VscodeCommands.OPEN.id, resource, columnOrOptions);
if (!result) {
throw new Error(`Could not find an editor for ${resource}`);
}
if (!URI.isUri(resource)) {
throw new Error(`Invalid argument for ${VscodeCommands.OPEN.id} command with URI argument. Found ${resource}`);
}
});

commands.registerCommand(VscodeCommands.OPEN_WITH, {
isVisible: () => false,
execute: async (resource: URI, viewType: string, columnOrOptions?: ViewColumn | TextDocumentShowOptions) => {
if (!viewType) {
throw new Error(`Running the contributed command: ${VscodeCommands.OPEN_WITH} failed.`);
}

let options: TextDocumentShowOptions | undefined;
if (typeof columnOrOptions === 'number') {
options = {
viewColumn: columnOrOptions
};
} else if (columnOrOptions) {
options = {
...columnOrOptions
};
if (viewType.toLowerCase() === 'default') {
return commands.executeCommand(VscodeCommands.OPEN.id, resource, columnOrOptions);
}

let result = await this.openWith(VscodeCommands.OPEN_WITH.id, resource, columnOrOptions, viewType);
if (!result) {
result = await this.openWith(VscodeCommands.OPEN_WITH.id, resource, columnOrOptions, CustomEditorOpener.toCustomEditorId(viewType));
}

if (!result) {
throw new Error(`Could not find an editor for '${viewType}'`);
}
const editorOptions = DocumentsMainImpl.toEditorOpenerOptions(this.shell, options);
await open(this.openerService, new TheiaURI(resource), editorOptions);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ export class CustomEditorOpener implements OpenHandler {
@inject(ApplicationShell) protected readonly shell: ApplicationShell,
@inject(WidgetManager) protected readonly widgetManager: WidgetManager
) {
this.id = `custom-editor-${this.editor.viewType}`;
this.id = CustomEditorOpener.toCustomEditorId(this.editor.viewType);
this.label = this.editor.displayName;
}

static toCustomEditorId(editorViewType: string): string {
return `custom-editor-${editorViewType}`;
}

canHandle(uri: URI): number {
if (this.matches(this.editor.selector, uri)) {
return this.getPriority();
Expand Down

0 comments on commit 10d5275

Please sign in to comment.