Skip to content

Commit

Permalink
fix: vscode.open command arguments (#1243)
Browse files Browse the repository at this point in the history
* fix: vscode.open command arguments

* fix: vscode.openWith
  • Loading branch information
Aaaaash authored Jun 29, 2022
1 parent d4ec500 commit 31b43e5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
95 changes: 52 additions & 43 deletions packages/extension/src/browser/extension.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
StatusBarEntryAccessor,
} from '@opensumi/ide-core-browser/lib/services/status-bar-service';
import { IResourceOpenOptions, WorkbenchEditorService, EditorGroupColumn } from '@opensumi/ide-editor';
import { IEditorOpenType } from '@opensumi/ide-editor/lib/common/editor';
import { IWindowDialogService } from '@opensumi/ide-overlay';
import { IWebviewService } from '@opensumi/ide-webview';
import type { ITextEditorOptions } from '@opensumi/monaco-editor-core/esm/vs/platform/editor/common/editor';
Expand Down Expand Up @@ -327,57 +328,26 @@ export class ExtensionCommandContribution implements CommandContribution {
registry.registerCommand(VSCodeBuiltinCommands.OPEN, {
execute: (
uriComponents: UriComponents,
columnOrOptions?: ViewColumn | TextDocumentShowOptions,
columnAndOptions?: [ViewColumn?, TextDocumentShowOptions?],
label?: string,
) => {
const uri = URI.from(uriComponents);
const options: IResourceOpenOptions = {};
if (columnOrOptions) {
if (typeof columnOrOptions === 'number') {
options.groupIndex = columnOrOptions;
} else {
options.groupIndex = columnOrOptions.viewColumn;
options.focus = options.preserveFocus = columnOrOptions.preserveFocus;
// 这个range 可能是 vscode.range, 因为不会经过args转换
if (columnOrOptions.selection && isLikelyVscodeRange(columnOrOptions.selection)) {
columnOrOptions.selection = fromRange(columnOrOptions.selection);
}
if (Array.isArray(columnOrOptions.selection) && columnOrOptions.selection.length === 2) {
const [start, end] = columnOrOptions.selection;
options.range = {
startLineNumber: start.line + 1,
startColumn: start.character + 1,
endLineNumber: end.line + 1,
endColumn: end.character + 1,
};
} else {
options.range = columnOrOptions.selection;
}
options.preview = columnOrOptions.preview;
}
}
if (label) {
options.label = label;
}
return this.workbenchEditorService.open(uri, options);
return this.doOpenWith(uri, columnAndOptions, label, undefined);
},
});

registry.registerCommand(VSCodeBuiltinCommands.OPEN_WITH, {
execute: (resource: UriComponents, id: string, columnAndOptions?: [EditorGroupColumn?, ITextEditorOptions?]) => {
execute: (resource: UriComponents, id: string, columnAndOptions?: [ViewColumn?, TextDocumentShowOptions?]) => {
const uri = URI.from(resource);
const options: IResourceOpenOptions = {};
const [columnArg] = columnAndOptions ?? [];
if (id !== 'default') {
options.forceOpenType = {
type: 'component',
componentId: `${CUSTOM_EDITOR_SCHEME}-${id}`,
};
}
if (typeof columnArg === 'number') {
options.groupIndex = columnArg;
}
return this.workbenchEditorService.open(uri, options);
// 指定使用某种 editor 打开资源,如果 id 传入 default,则使用默认的 editor
const openType: IEditorOpenType | undefined =
id === 'default'
? undefined
: {
type: 'component',
componentId: `${CUSTOM_EDITOR_SCHEME}-${id}`,
};
return this.doOpenWith(uri, columnAndOptions, undefined, openType);
},
});

Expand Down Expand Up @@ -461,6 +431,45 @@ export class ExtensionCommandContribution implements CommandContribution {
});
}

private doOpenWith(
uri: URI,
columnAndOptions?: [ViewColumn?, TextDocumentShowOptions?],
label?: string,
forceOpenType?: IEditorOpenType | undefined,
) {
const [columnArg, optionsArg] = columnAndOptions ?? [];
const options: IResourceOpenOptions = {};
if (typeof columnArg === 'number') {
options.groupIndex = columnArg;
}
if (optionsArg) {
options.focus = options.preserveFocus = optionsArg.preserveFocus;
// 这个range 可能是 vscode.range, 因为不会经过args转换
if (optionsArg.selection && isLikelyVscodeRange(optionsArg.selection)) {
optionsArg.selection = fromRange(optionsArg.selection);
}
if (Array.isArray(optionsArg.selection) && optionsArg.selection.length === 2) {
const [start, end] = optionsArg.selection;
options.range = {
startLineNumber: start.line + 1,
startColumn: start.character + 1,
endLineNumber: end.line + 1,
endColumn: end.character + 1,
};
} else {
options.range = optionsArg.selection;
}
options.preview = optionsArg.preview;
}
if (forceOpenType) {
options.forceOpenType = forceOpenType;
}
if (label) {
options.label = label;
}
return this.workbenchEditorService.open(uri, options);
}

private asQuickOpenItems(activated: {
node?: ActivatedExtension[] | undefined;
worker?: ActivatedExtension[] | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as typeConverters from '../../../common/vscode/converter';
import * as types from '../../../common/vscode/ext-types';
import * as modes from '../../../common/vscode/model.api';


import { CommandsConverter } from './ext.host.command';

type IPosition = modes.Position;
Expand Down Expand Up @@ -617,7 +616,7 @@ export const newCommands: ApiCommand[] = [
'Opens the provided resources in the diff editor to compare their contents.',
[
ApiCommandArgument.Uri.with('left', 'Left-hand side resource of the diff editor'),
ApiCommandArgument.Uri.with('right', 'Rigth-hand side resource of the diff editor'),
ApiCommandArgument.Uri.with('right', 'Right-hand side resource of the diff editor'),
ApiCommandArgument.String.with('title', 'Human readable title for the diff editor').optional(),
new ApiCommandArgument<
typeConverters.TextEditorOpenOptions | undefined,
Expand Down

0 comments on commit 31b43e5

Please sign in to comment.