Skip to content

Commit

Permalink
Removes statics for better tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Aug 7, 2022
1 parent 9c90205 commit 12771db
Show file tree
Hide file tree
Showing 32 changed files with 357 additions and 360 deletions.
11 changes: 5 additions & 6 deletions src/annotations/annotationProvider.ts
Expand Up @@ -24,14 +24,13 @@ export interface AnnotationContext {
}

export type TextEditorCorrelationKey = string;
export function getEditorCorrelationKey(editor: TextEditor | undefined): TextEditorCorrelationKey {
return `${editor?.document.uri.toString()}|${editor?.viewColumn}`;
}

export abstract class AnnotationProviderBase<TContext extends AnnotationContext = AnnotationContext>
implements Disposable
{
static getCorrelationKey(editor: TextEditor | undefined): TextEditorCorrelationKey {
return `${editor?.document.uri.toString()}|${editor?.viewColumn}`;
}

annotationContext: TContext | undefined;
correlationKey: TextEditorCorrelationKey;
document: TextDocument;
Expand All @@ -47,7 +46,7 @@ export abstract class AnnotationProviderBase<TContext extends AnnotationContext
public editor: TextEditor,
protected readonly trackedDocument: TrackedDocument<GitDocumentState>,
) {
this.correlationKey = AnnotationProviderBase.getCorrelationKey(this.editor);
this.correlationKey = getEditorCorrelationKey(this.editor);
this.document = this.editor.document;

this.disposable = Disposable.from(
Expand Down Expand Up @@ -121,7 +120,7 @@ export abstract class AnnotationProviderBase<TContext extends AnnotationContext
}

this.editor = editor;
this.correlationKey = AnnotationProviderBase.getCorrelationKey(editor);
this.correlationKey = getEditorCorrelationKey(editor);
this.document = editor.document;

if (this.decorations?.length) {
Expand Down
10 changes: 5 additions & 5 deletions src/annotations/fileAnnotationController.ts
Expand Up @@ -42,8 +42,8 @@ import type {
DocumentDirtyStateChangeEvent,
GitDocumentState,
} from '../trackers/gitDocumentTracker';
import type { AnnotationContext, TextEditorCorrelationKey } from './annotationProvider';
import { AnnotationProviderBase, AnnotationStatus } from './annotationProvider';
import type { AnnotationContext, AnnotationProviderBase, TextEditorCorrelationKey } from './annotationProvider';
import { AnnotationStatus, getEditorCorrelationKey } from './annotationProvider';
import { GutterBlameAnnotationProvider } from './gutterBlameAnnotationProvider';
import type { ChangesAnnotationContext } from './gutterChangesAnnotationProvider';
import { GutterChangesAnnotationProvider } from './gutterChangesAnnotationProvider';
Expand Down Expand Up @@ -259,7 +259,7 @@ export class FileAnnotationController implements Disposable {
return this.clearAll();
}

return this.clearCore(AnnotationProviderBase.getCorrelationKey(editor), reason);
return this.clearCore(getEditorCorrelationKey(editor), reason);
}

async clearAll() {
Expand All @@ -281,7 +281,7 @@ export class FileAnnotationController implements Disposable {

getProvider(editor: TextEditor | undefined): AnnotationProviderBase | undefined {
if (editor == null || editor.document == null) return undefined;
return this._annotationProviders.get(AnnotationProviderBase.getCorrelationKey(editor));
return this._annotationProviders.get(getEditorCorrelationKey(editor));
}

async show(editor: TextEditor | undefined, type: FileAnnotationType, context?: AnnotationContext): Promise<boolean>;
Expand Down Expand Up @@ -415,7 +415,7 @@ export class FileAnnotationController implements Disposable {
this._annotationProviders.delete(key);
provider.dispose();

if (this._annotationProviders.size === 0 || key === AnnotationProviderBase.getCorrelationKey(this._editor)) {
if (this._annotationProviders.size === 0 || key === getEditorCorrelationKey(this._editor)) {
await setContext(ContextKeys.AnnotationStatus, undefined);
await this.detachKeyboardHook();
}
Expand Down
165 changes: 82 additions & 83 deletions src/commands/base.ts
Expand Up @@ -9,15 +9,17 @@ import type {
import { commands, Disposable, Uri, window } from 'vscode';
import type { ActionContext } from '../api/gitlens';
import type { Commands } from '../constants';
import { GitBranch } from '../git/models/branch';
import type { GitBranch } from '../git/models/branch';
import { isBranch } from '../git/models/branch';
import type { GitStashCommit } from '../git/models/commit';
import { GitCommit } from '../git/models/commit';
import { GitContributor } from '../git/models/contributor';
import type { GitFile } from '../git/models/file';
import type { GitReference } from '../git/models/reference';
import { GitRemote } from '../git/models/remote';
import { Repository } from '../git/models/repository';
import { GitTag } from '../git/models/tag';
import type { GitTag } from '../git/models/tag';
import { isTag } from '../git/models/tag';
import { sequentialize } from '../system/function';
import { ViewNode, ViewRefNode } from '../views/nodes/viewNode';

Expand Down Expand Up @@ -89,7 +91,7 @@ export function isCommandContextViewNodeHasBranch(
): context is CommandViewNodeContext & { node: ViewNode & { branch: GitBranch } } {
if (context.type !== 'viewItem') return false;

return GitBranch.is((context.node as ViewNode & { branch: GitBranch }).branch);
return isBranch((context.node as ViewNode & { branch: GitBranch }).branch);
}

export function isCommandContextViewNodeHasCommit<T extends GitCommit | GitStashCommit>(
Expand Down Expand Up @@ -175,7 +177,7 @@ export function isCommandContextViewNodeHasTag(
): context is CommandViewNodeContext & { node: ViewNode & { tag: GitTag } } {
if (context.type !== 'viewItem') return false;

return GitTag.is((context.node as ViewNode & { tag: GitTag }).tag);
return isTag((context.node as ViewNode & { tag: GitTag }).tag);
}

export type CommandContext =
Expand Down Expand Up @@ -264,7 +266,7 @@ export abstract class Command implements Disposable {
abstract execute(...args: any[]): any;

protected _execute(command: string, ...args: any[]): Promise<unknown> {
const [context, rest] = Command.parseContext(command, { ...this.contextParsingOptions }, ...args);
const [context, rest] = parseCommandContext(command, { ...this.contextParsingOptions }, ...args);

// If there an array of contexts, then we want to execute the command for each
if (Array.isArray(context)) {
Expand All @@ -277,110 +279,107 @@ export abstract class Command implements Disposable {

return this.preExecute(context, ...rest);
}
}

private static parseContext(
command: string,
options: CommandContextParsingOptions,
...args: any[]
): [CommandContext | CommandContext[], any[]] {
let editor: TextEditor | undefined = undefined;
function parseCommandContext(
command: string,
options: CommandContextParsingOptions,
...args: any[]
): [CommandContext | CommandContext[], any[]] {
let editor: TextEditor | undefined = undefined;

let firstArg = args[0];
let 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.expectsEditor) {
if (firstArg == null || (firstArg.id != null && firstArg.document?.uri != null)) {
editor = firstArg;
args = args.slice(1);
firstArg = args[0];
}

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 (or we are in a left-hand side of a diff), then pass the active editor
if (
editor == null &&
(uri.toString() === window.activeTextEditor?.document.uri.toString() ||
command.endsWith('InDiffLeft'))
) {
editor = window.activeTextEditor;
}

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];
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 (or we are in a left-hand side of a diff), then pass the active editor
if (
editor == null &&
(uri.toString() === window.activeTextEditor?.document.uri.toString() ||
command.endsWith('InDiffLeft'))
) {
editor = window.activeTextEditor;
}

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;
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];
}

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) {
let [node, ...rest] = args as [ViewNode, unknown];
if (firstArg instanceof ViewNode) {
let [node, ...rest] = args as [ViewNode, unknown];

// If there is a node followed by an array of nodes, then we want to execute the command for each
firstArg = rest[0];
if (Array.isArray(firstArg) && firstArg[0] instanceof ViewNode) {
let nodes;
[nodes, ...rest] = rest as unknown as [ViewNode[], unknown];
// If there is a node followed by an array of nodes, then we want to execute the command for each
firstArg = rest[0];
if (Array.isArray(firstArg) && firstArg[0] instanceof ViewNode) {
let nodes;
[nodes, ...rest] = rest as unknown as [ViewNode[], unknown];

const contexts: CommandContext[] = [];
for (const n of nodes) {
if (n?.constructor === node.constructor) {
contexts.push({ command: command, type: 'viewItem', node: n, uri: n.uri });
}
const contexts: CommandContext[] = [];
for (const n of nodes) {
if (n?.constructor === node.constructor) {
contexts.push({ command: command, type: 'viewItem', node: n, uri: n.uri });
}

return [contexts, rest];
}

return [{ command: command, type: 'viewItem', node: node, uri: node.uri }, rest];
return [contexts, rest];
}

if (isScmResourceState(firstArg)) {
const states = [];
let count = 0;
for (const arg of args) {
if (!isScmResourceState(arg)) break;
return [{ command: command, type: 'viewItem', node: node, uri: node.uri }, rest];
}

count++;
states.push(arg);
}
if (isScmResourceState(firstArg)) {
const states = [];
let count = 0;
for (const arg of args) {
if (!isScmResourceState(arg)) break;

return [
{ command: command, type: 'scm-states', scmResourceStates: states, uri: states[0].resourceUri },
args.slice(count),
];
count++;
states.push(arg);
}

if (isScmResourceGroup(firstArg)) {
const groups = [];
let count = 0;
for (const arg of args) {
if (!isScmResourceGroup(arg)) break;
return [
{ command: command, type: 'scm-states', scmResourceStates: states, uri: states[0].resourceUri },
args.slice(count),
];
}

count++;
groups.push(arg);
}
if (isScmResourceGroup(firstArg)) {
const groups = [];
let count = 0;
for (const arg of args) {
if (!isScmResourceGroup(arg)) break;

return [{ command: command, type: 'scm-groups', scmResourceGroups: groups }, args.slice(count)];
count++;
groups.push(arg);
}

if (isGitTimelineItem(firstArg)) {
const [item, uri, ...rest] = args as [GitTimelineItem, Uri, any];
return [{ command: command, type: 'timeline-item:git', item: item, uri: uri }, rest];
}
return [{ command: command, type: 'scm-groups', scmResourceGroups: groups }, args.slice(count)];
}

return [{ command: command, type: 'unknown', editor: editor, uri: editor?.document.uri }, args];
if (isGitTimelineItem(firstArg)) {
const [item, uri, ...rest] = args as [GitTimelineItem, Uri, any];
return [{ command: command, type: 'timeline-item:git', item: item, uri: uri }, rest];
}

return [{ command: command, type: 'unknown', editor: editor, uri: editor?.document.uri }, args];
}

export abstract class ActiveEditorCommand extends Command {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/git/pull.ts
@@ -1,6 +1,6 @@
import { GlyphChars } from '../../constants';
import type { Container } from '../../container';
import { GitBranch } from '../../git/models/branch';
import { isBranch } from '../../git/models/branch';
import type { GitBranchReference } from '../../git/models/reference';
import { GitReference } from '../../git/models/reference';
import type { Repository } from '../../git/models/repository';
Expand Down Expand Up @@ -69,7 +69,7 @@ export class PullGitCommand extends QuickCommand<State> {
async execute(state: PullStepState) {
if (GitReference.isBranch(state.reference)) {
// Only resort to a branch fetch if the branch isn't the current one
if (!GitBranch.is(state.reference) || !state.reference.current) {
if (!isBranch(state.reference) || !state.reference.current) {
const currentBranch = await state.repos[0].getBranch();
if (currentBranch?.name !== state.reference.name) {
return state.repos[0].fetch({ branch: state.reference, pull: true });
Expand Down
16 changes: 10 additions & 6 deletions src/commands/git/push.ts
@@ -1,7 +1,7 @@
import { configuration } from '../../configuration';
import { CoreGitConfiguration, GlyphChars } from '../../constants';
import type { Container } from '../../container';
import { GitBranch } from '../../git/models/branch';
import { getRemoteNameFromBranchName } from '../../git/models/branch';
import type { GitBranchReference } from '../../git/models/reference';
import { GitReference } from '../../git/models/reference';
import type { Repository } from '../../git/models/repository';
Expand Down Expand Up @@ -329,7 +329,9 @@ export class PushGitCommand extends QuickCommand<State> {
[],
DirectiveQuickPickItem.create(Directive.Cancel, true, {
label: `Cancel ${this.title}`,
detail: `Cannot push; No commits ahead of ${GitBranch.getRemote(status.upstream)}`,
detail: `Cannot push; No commits ahead of ${getRemoteNameFromBranchName(
status.upstream,
)}`,
}),
);
}
Expand All @@ -349,10 +351,10 @@ export class PushGitCommand extends QuickCommand<State> {
label: false,
})}`
: ''
}${status?.upstream ? ` to ${GitBranch.getRemote(status.upstream)}` : ''}`;
}${status?.upstream ? ` to ${getRemoteNameFromBranchName(status.upstream)}` : ''}`;
} else {
pushDetails = `${status?.state.ahead ? ` ${pluralize('commit', status.state.ahead)}` : ''}${
status?.upstream ? ` to ${GitBranch.getRemote(status.upstream)}` : ''
status?.upstream ? ` to ${getRemoteNameFromBranchName(status.upstream)}` : ''
}`;
}

Expand All @@ -373,7 +375,9 @@ export class PushGitCommand extends QuickCommand<State> {
detail: `Will force push${useForceWithLease ? ' (with lease)' : ''} ${pushDetails}${
status != null && status.state.behind > 0
? `, overwriting ${pluralize('commit', status.state.behind)}${
status?.upstream ? ` on ${GitBranch.getRemote(status.upstream)}` : ''
status?.upstream
? ` on ${getRemoteNameFromBranchName(status.upstream)}`
: ''
}`
: ''
}`,
Expand All @@ -383,7 +387,7 @@ export class PushGitCommand extends QuickCommand<State> {
? DirectiveQuickPickItem.create(Directive.Cancel, true, {
label: `Cancel ${this.title}`,
detail: `Cannot push; ${GitReference.toString(branch)} is behind${
status?.upstream ? ` ${GitBranch.getRemote(status.upstream)}` : ''
status?.upstream ? ` ${getRemoteNameFromBranchName(status.upstream)}` : ''
} by ${pluralize('commit', status.state.behind)}`,
})
: undefined,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/openFileOnRemote.ts
Expand Up @@ -5,7 +5,7 @@ import { BranchSorting, TagSorting } from '../configuration';
import { Commands, GlyphChars } from '../constants';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { GitBranch } from '../git/models/branch';
import { getBranchNameWithoutRemote, getRemoteNameFromBranchName } from '../git/models/branch';
import { GitRevision } from '../git/models/reference';
import { RemoteResourceType } from '../git/remotes/provider';
import { Logger } from '../logger';
Expand Down Expand Up @@ -169,9 +169,9 @@ export class OpenFileOnRemoteCommand extends ActiveEditorCommand {

if (pick.refType === 'branch') {
if (pick.remote) {
args.branchOrTag = GitBranch.getNameWithoutRemote(pick.name);
args.branchOrTag = getBranchNameWithoutRemote(pick.name);

const remoteName = GitBranch.getRemote(pick.name);
const remoteName = getRemoteNameFromBranchName(pick.name);
const remote = remotes.find(r => r.name === remoteName);
if (remote != null) {
remotes = [remote];
Expand Down

0 comments on commit 12771db

Please sign in to comment.