From c75d5fed27beb73bb172ce63cf50315f0f468d0c Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 27 Jul 2020 01:35:01 -0400 Subject: [PATCH] Fixes more context menu commands Caused by 6b27a480c9c1ba88e7a5ecf4c4417734f1b02506 --- src/commands/common.ts | 56 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/commands/common.ts b/src/commands/common.ts index 490142a..ebc4bc3 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -173,8 +173,7 @@ export async function getRepoPathOrPrompt(title: string, uri?: Uri) { } export interface CommandContextParsingOptions { - editor: boolean; - uri: boolean; + expectsEditor: boolean; } export interface CommandBaseContext { @@ -335,7 +334,7 @@ export abstract class Command implements Disposable { return `command:${command}?${encodeURIComponent(JSON.stringify(args))}`; } - protected readonly contextParsingOptions: CommandContextParsingOptions = { editor: false, uri: false }; + protected readonly contextParsingOptions: CommandContextParsingOptions = { expectsEditor: false }; private readonly _disposable: Disposable; @@ -380,23 +379,39 @@ export abstract class Command implements Disposable { let firstArg = args[0]; - if (options.editor && (firstArg == null || (firstArg.id != null && firstArg.document?.uri != null))) { - editor = firstArg; - args = args.slice(1); - firstArg = args[0]; - } + if (options.expectsEditor) { + if (firstArg == null || (firstArg.id != null && firstArg.document?.uri != null)) { + editor = firstArg; + args = args.slice(1); + firstArg = args[0]; + } - if (options.uri && (firstArg == null || firstArg instanceof Uri)) { - const [uri, ...rest] = args as [Uri, any]; - if (uri != null) { - const uris = rest[0]; - if (uris != null && Array.isArray(uris) && uris.length !== 0 && uris[0] instanceof Uri) { - return [{ command: command, type: 'uris', editor: editor, uri: uri, uris: uris }, rest.slice(1)]; + if (args.length > 0 && (firstArg == null || firstArg instanceof Uri)) { + const [uri, ...rest] = args as [Uri, any]; + if (uri != null) { + // If the uri matches the active editor, then pass the active editor + if (editor == null && uri.toString() === window.activeTextEditor?.document.uri.toString()) { + editor = window.activeTextEditor; + } else { + // eslint-disable-next-line no-debugger + debugger; + } + + const uris = rest[0]; + if (uris != null && Array.isArray(uris) && uris.length !== 0 && uris[0] instanceof Uri) { + return [ + { command: command, type: 'uris', editor: editor, uri: uri, uris: uris }, + rest.slice(1), + ]; + } + return [{ command: command, type: 'uri', editor: editor, uri: uri }, rest]; } - return [{ command: command, type: 'uri', editor: editor, uri: uri }, rest]; - } - args = args.slice(1); + args = args.slice(1); + } else if (editor == null) { + // If we are expecting an editor and we have no uri, then pass the active editor + editor = window.activeTextEditor; + } } if (firstArg instanceof ViewNode) { @@ -438,7 +453,7 @@ export abstract class Command implements Disposable { } export abstract class ActiveEditorCommand extends Command { - protected readonly contextParsingOptions: CommandContextParsingOptions = { editor: true, uri: true }; + protected readonly contextParsingOptions: CommandContextParsingOptions = { expectsEditor: true }; constructor(command: Commands | Commands[]) { super(command); @@ -449,10 +464,7 @@ export abstract class ActiveEditorCommand extends Command { } protected _execute(command: string, ...args: any[]): any { - // Only include the editor if there are no args - return args.length === 0 - ? super._execute(command, window.activeTextEditor) - : super._execute(command, undefined, ...args); + return super._execute(command, undefined, ...args); } abstract execute(editor?: TextEditor, ...args: any[]): any;