Skip to content

Commit

Permalink
Consolidates executeCommand calls
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Feb 6, 2022
1 parent e3cfedc commit 94e1ae3
Show file tree
Hide file tree
Showing 37 changed files with 199 additions and 152 deletions.
12 changes: 6 additions & 6 deletions src/codelens/codeLensProvider.ts
Expand Up @@ -3,7 +3,6 @@ import {
CodeLens,
CodeLensProvider,
Command,
commands,
DocumentSelector,
DocumentSymbol,
Event,
Expand All @@ -20,6 +19,7 @@ import {
command,
Commands,
DiffWithPreviousCommandArgs,
executeCoreCommand,
OpenOnRemoteCommandArgs,
ShowCommitsInViewCommandArgs,
ShowQuickCommitCommandArgs,
Expand All @@ -35,7 +35,7 @@ import {
configuration,
FileAnnotationType,
} from '../configuration';
import { BuiltInCommands, Schemes } from '../constants';
import { CoreCommands, Schemes } from '../constants';
import { Container } from '../container';
import type { GitUri } from '../git/gitUri';
import { GitBlame, GitBlameLines, GitCommit } from '../git/models';
Expand Down Expand Up @@ -158,17 +158,17 @@ export class GitCodeLensProvider implements CodeLensProvider {
} else {
[blame, symbols] = await Promise.all([
this.container.git.getBlame(gitUri, document),
commands.executeCommand<SymbolInformation[]>(
BuiltInCommands.ExecuteDocumentSymbolProvider,
executeCoreCommand<[Uri], SymbolInformation[]>(
CoreCommands.ExecuteDocumentSymbolProvider,
document.uri,
),
]);
}

if (blame == null || blame?.lines.length === 0) return lenses;
} else if (languageScope.scopes.length !== 1 || !languageScope.scopes.includes(CodeLensScopes.Document)) {
symbols = await commands.executeCommand<SymbolInformation[]>(
BuiltInCommands.ExecuteDocumentSymbolProvider,
symbols = await executeCoreCommand<[Uri], SymbolInformation[]>(
CoreCommands.ExecuteDocumentSymbolProvider,
document.uri,
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/commands/browseRepoAtRevision.ts
@@ -1,5 +1,5 @@
import { commands, TextEditor, Uri } from 'vscode';
import { BuiltInCommands } from '../constants';
import { TextEditor, Uri } from 'vscode';
import { CoreCommands } from '../constants';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
Expand All @@ -10,6 +10,7 @@ import {
command,
CommandContext,
Commands,
executeCoreCommand,
getCommandUri,
openWorkspace,
OpenWorkspaceLocation,
Expand Down Expand Up @@ -75,7 +76,7 @@ export class BrowseRepoAtRevisionCommand extends ActiveEditorCommand {
});

if (!args.openInNewWindow) {
void commands.executeCommand(BuiltInCommands.FocusFilesExplorer);
void executeCoreCommand(CoreCommands.FocusFilesExplorer);
}
} catch (ex) {
Logger.error(ex, 'BrowseRepoAtRevisionCommand');
Expand Down
12 changes: 6 additions & 6 deletions src/commands/closeUnchangedFiles.ts
@@ -1,11 +1,11 @@
import { commands, TextEditor, Uri, window } from 'vscode';
import { TextEditor, Uri, window } from 'vscode';
import { TextEditorComparer, UriComparer } from '../comparers';
import { BuiltInCommands } from '../constants';
import { CoreCommands } from '../constants';
import type { Container } from '../container';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { debounce } from '../system/function';
import { Command, command, Commands, getRepoPathOrPrompt } from './common';
import { Command, command, Commands, executeCoreCommand, getRepoPathOrPrompt } from './common';

export interface CloseUnchangedFilesCommandArgs {
uris?: Uri[];
Expand Down Expand Up @@ -38,7 +38,7 @@ export class CloseUnchangedFilesCommand extends Command {
}

if (args.uris.length === 0) {
void commands.executeCommand(BuiltInCommands.CloseAllEditors);
void executeCoreCommand(CoreCommands.CloseAllEditors);

return;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ export class CloseUnchangedFilesCommand extends Command {
private async closeEditor(timeout: number = 500): Promise<TextEditor | undefined> {
const editor = window.activeTextEditor;

void (await commands.executeCommand(BuiltInCommands.CloseActiveEditor));
void (await executeCoreCommand(CoreCommands.CloseActiveEditor));

if (editor !== window.activeTextEditor) {
return window.activeTextEditor;
Expand All @@ -118,7 +118,7 @@ export class CloseUnchangedFilesCommand extends Command {
private async nextEditor(timeout: number = 500): Promise<TextEditor | undefined> {
const editor = window.activeTextEditor;

void (await commands.executeCommand(BuiltInCommands.NextEditor));
void (await executeCoreCommand(CoreCommands.NextEditor));

if (editor !== window.activeTextEditor) {
return window.activeTextEditor;
Expand Down
52 changes: 45 additions & 7 deletions src/commands/common.ts
Expand Up @@ -14,7 +14,7 @@ import {
workspace,
} from 'vscode';
import type { Action, ActionContext } from '../api/gitlens';
import { BuiltInCommands, ImageMimetypes, Schemes } from '../constants';
import { CoreCommands, CoreGitCommands, ImageMimetypes, Schemes } from '../constants';
import { Container } from '../container';
import { GitUri } from '../git/gitUri';
import {
Expand Down Expand Up @@ -183,6 +183,7 @@ export const enum Commands {
ToggleLineBlame = 'gitlens.toggleLineBlame',
ToggleReviewMode = 'gitlens.toggleReviewMode',
ToggleZenMode = 'gitlens.toggleZenMode',
ViewsCopy = 'gitlens.views.copy',
ViewsOpenDirectoryDiff = 'gitlens.views.openDirectoryDiff',
ViewsOpenDirectoryDiffWithWorking = 'gitlens.views.openDirectoryDiffWithWorking',

Expand All @@ -208,8 +209,45 @@ export function getMarkdownActionCommand<T extends ActionContext>(action: Action
});
}

export function executeCommand<T>(command: Commands, args: T) {
return commands.executeCommand(command, args);
type SupportedCommands = Commands | `gitlens.views.${string}.focus` | `gitlens.views.${string}.resetViewLocation`;

export function executeCommand<U = any>(command: SupportedCommands): Thenable<U>;
export function executeCommand<T = unknown, U = any>(command: SupportedCommands, arg: T): Thenable<U>;
export function executeCommand<T extends [...unknown[]] = [], U = any>(
command: SupportedCommands,
...args: T
): Thenable<U>;
export function executeCommand<T extends [...unknown[]] = [], U = any>(
command: SupportedCommands,
...args: T
): Thenable<U> {
return commands.executeCommand<U>(command, args);
}

export function executeCoreCommand<U = any>(command: CoreCommands): Thenable<U>;
export function executeCoreCommand<T = unknown, U = any>(command: CoreCommands, arg: T): Thenable<U>;
export function executeCoreCommand<T extends [...unknown[]] = [], U = any>(
command: CoreCommands,
...args: T
): Thenable<U>;
export function executeCoreCommand<T extends [...unknown[]] = [], U = any>(
command: CoreCommands,
...args: T
): Thenable<U> {
return commands.executeCommand<U>(command, ...args);
}

export function executeCoreGitCommand<U = any>(command: CoreGitCommands): Thenable<U>;
export function executeCoreGitCommand<T = unknown, U = any>(command: CoreGitCommands, arg: T): Thenable<U>;
export function executeCoreGitCommand<T extends [...unknown[]] = [], U = any>(
command: CoreGitCommands,
...args: T
): Thenable<U>;
export function executeCoreGitCommand<T extends [...unknown[]] = [], U = any>(
command: CoreGitCommands,
...args: T
): Thenable<U> {
return commands.executeCommand<U>(command, ...args);
}

export function executeEditorCommand<T>(command: Commands, uri: Uri | undefined, args: T) {
Expand Down Expand Up @@ -700,7 +738,7 @@ export function findOrOpenEditors(uris: Uri[]): void {
}

for (const uri of normalizedUris.values()) {
void commands.executeCommand(BuiltInCommands.Open, uri, { background: true, preview: false });
void executeCoreCommand(CoreCommands.Open, uri, { background: true, preview: false });
}
}

Expand All @@ -715,7 +753,7 @@ export async function openEditor(
}

if (uri.scheme === Schemes.GitLens && ImageMimetypes[extname(uri.fsPath)]) {
await commands.executeCommand(BuiltInCommands.Open, uri);
await executeCoreCommand(CoreCommands.Open, uri);

return undefined;
}
Expand All @@ -730,7 +768,7 @@ export async function openEditor(
} catch (ex) {
const msg: string = ex?.toString() ?? '';
if (msg.includes('File seems to be binary and cannot be opened as text')) {
await commands.executeCommand(BuiltInCommands.Open, uri);
await executeCoreCommand(CoreCommands.Open, uri);

return undefined;
}
Expand All @@ -757,7 +795,7 @@ export function openWorkspace(
return void workspace.updateWorkspaceFolders(count, 0, { uri: uri, name: options?.name });
}

return void commands.executeCommand(BuiltInCommands.OpenFolder, uri, {
return void executeCoreCommand(CoreCommands.OpenFolder, uri, {
forceNewWindow: options?.location === OpenWorkspaceLocation.NewWindow,
});
}
10 changes: 5 additions & 5 deletions src/commands/diffWith.ts
@@ -1,11 +1,11 @@
import { commands, Range, TextDocumentShowOptions, Uri, ViewColumn } from 'vscode';
import { BuiltInCommands, GlyphChars } from '../constants';
import { Range, TextDocumentShowOptions, Uri, ViewColumn } from 'vscode';
import { CoreCommands, GlyphChars } from '../constants';
import type { Container } from '../container';
import { GitCommit, GitRevision } from '../git/models';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { basename } from '../system/path';
import { command, Command, Commands } from './common';
import { command, Command, Commands, executeCoreCommand } from './common';

export interface DiffWithCommandArgsRevision {
sha: string;
Expand Down Expand Up @@ -174,8 +174,8 @@ export class DiffWithCommand extends Command {
args.showOptions.selection = new Range(args.line, 0, args.line, 0);
}

void (await commands.executeCommand(
BuiltInCommands.Diff,
void (await executeCoreCommand(
CoreCommands.Diff,
lhs ??
this.container.git.getRevisionUri(GitRevision.deletedOrMissing, args.lhs.uri.fsPath, args.repoPath),
rhs ??
Expand Down
5 changes: 3 additions & 2 deletions src/commands/git/coauthors.ts
@@ -1,8 +1,9 @@
import { commands } from 'vscode';
import { CoreCommands } from '../../constants';
import { Container } from '../../container';
import { GitContributor, Repository } from '../../git/models';
import { normalizePath } from '../../system/path';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import { executeCoreCommand } from '../common';
import {
PartialStepState,
pickContributorsStep,
Expand Down Expand Up @@ -90,7 +91,7 @@ export class CoAuthorsGitCommand extends QuickCommand<State> {
}

repo.inputBox.value = message;
void (await commands.executeCommand('workbench.view.scm'));
void (await executeCoreCommand(CoreCommands.ShowSCM));
}

protected async *steps(state: PartialStepState<State>): StepGenerator {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/git/push.ts
@@ -1,5 +1,5 @@
import { configuration } from '../../configuration';
import { BuiltInGitConfiguration, GlyphChars } from '../../constants';
import { CoreGitConfiguration, GlyphChars } from '../../constants';
import { Container } from '../../container';
import { GitBranch, GitBranchReference, GitReference, Repository } from '../../git/models';
import { Directive, DirectiveQuickPickItem, FlagsQuickPickItem } from '../../quickpicks';
Expand Down Expand Up @@ -151,7 +151,7 @@ export class PushGitCommand extends QuickCommand<State> {
}

private async *confirmStep(state: PushStepState, context: Context): AsyncStepResultGenerator<Flags[]> {
const useForceWithLease = configuration.getAny<boolean>(BuiltInGitConfiguration.UseForcePushWithLease) ?? false;
const useForceWithLease = configuration.getAny<boolean>(CoreGitConfiguration.UseForcePushWithLease) ?? false;

let step: QuickPickStep<FlagsQuickPickItem<Flags>>;

Expand Down
6 changes: 3 additions & 3 deletions src/commands/refreshHover.ts
@@ -1,6 +1,6 @@
import { commands } from 'vscode';
import { CoreCommands } from '../constants';
import type { Container } from '../container';
import { command, Command, Commands } from './common';
import { command, Command, Commands, executeCoreCommand } from './common';

@command()
export class RefreshHoverCommand extends Command {
Expand All @@ -10,6 +10,6 @@ export class RefreshHoverCommand extends Command {

async execute() {
// TODO@eamodio figure out how to really refresh/update a hover
await commands.executeCommand('editor.action.showHover');
await executeCoreCommand(CoreCommands.EditorShowHover);
}
}
11 changes: 6 additions & 5 deletions src/commands/setViewsLayout.ts
@@ -1,7 +1,8 @@
import { commands, window } from 'vscode';
import { window } from 'vscode';
import { viewsConfigKeys } from '../configuration';
import { CoreCommands } from '../constants';
import type { Container } from '../container';
import { command, Command, Commands } from './common';
import { command, Command, Commands, executeCommand, executeCoreCommand } from './common';

enum ViewsLayout {
GitLens = 'gitlens',
Expand Down Expand Up @@ -51,7 +52,7 @@ export class SetViewsLayoutCommand extends Command {
// Because of https://github.com/microsoft/vscode/issues/105774, run the command twice which seems to fix things
let count = 0;
while (count++ < 2) {
void (await commands.executeCommand('vscode.moveViews', {
void (await executeCoreCommand(CoreCommands.MoveViews, {
viewIds: viewsConfigKeys.map(view => `gitlens.views.${view}`),
destinationId: 'workbench.view.extension.gitlens',
}));
Expand All @@ -64,14 +65,14 @@ export class SetViewsLayoutCommand extends Command {
// Because of https://github.com/microsoft/vscode/issues/105774, run the command twice which seems to fix things
let count = 0;
while (count++ < 2) {
void (await commands.executeCommand('vscode.moveViews', {
void (await executeCoreCommand(CoreCommands.MoveViews, {
viewIds: viewsConfigKeys.map(view => `gitlens.views.${view}`),
destinationId: 'workbench.view.scm',
}));
}
} catch {
for (const view of viewsConfigKeys) {
void (await commands.executeCommand(`gitlens.views.${view}.resetViewLocation`));
void (await executeCommand(`gitlens.views.${view}.resetViewLocation`));
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/commands/showView.ts
@@ -1,8 +1,7 @@
import { commands } from 'vscode';
import type { Container } from '../container';
import { ContextKeys, setContext } from '../context';
import { SyncedState } from '../storage';
import { command, Command, CommandContext, Commands } from './common';
import { command, Command, CommandContext, Commands, executeCommand } from './common';

@command()
export class ShowViewCommand extends Command {
Expand Down Expand Up @@ -51,7 +50,7 @@ export class ShowViewCommand extends Command {
case Commands.ShowWelcomeView:
await setContext(ContextKeys.ViewsWelcomeVisible, true);
void this.container.storage.store(SyncedState.WelcomeViewVisible, true);
void (await commands.executeCommand('gitlens.views.welcome.focus'));
void (await executeCommand('gitlens.views.welcome.focus'));
}

return Promise.resolve(undefined);
Expand Down

0 comments on commit 94e1ae3

Please sign in to comment.