Browse Source

Adds better typing for view commands

main
Eric Amodio 1 year ago
committed by Ramin Tadayon
parent
commit
f41af294ba
No known key found for this signature in database GPG Key ID: 79D60DDE3DFB95F5
14 changed files with 58 additions and 33 deletions
  1. +20
    -5
      src/constants.ts
  2. +1
    -1
      src/views/branchesView.ts
  3. +1
    -1
      src/views/commitsView.ts
  4. +1
    -1
      src/views/contributorsView.ts
  5. +5
    -1
      src/views/fileHistoryView.ts
  6. +1
    -1
      src/views/lineHistoryView.ts
  7. +1
    -1
      src/views/remotesView.ts
  8. +1
    -1
      src/views/repositoriesView.ts
  9. +5
    -1
      src/views/searchAndCompareView.ts
  10. +1
    -1
      src/views/stashesView.ts
  11. +1
    -1
      src/views/tagsView.ts
  12. +18
    -16
      src/views/viewBase.ts
  13. +1
    -1
      src/views/workspacesView.ts
  14. +1
    -1
      src/views/worktreesView.ts

+ 20
- 5
src/constants.ts View File

@ -392,8 +392,13 @@ export type TreeViewCommands = `gitlens.views.${
| `setShowAvatars${'On' | 'Off'}`
| `setFilesFilterOn${'Left' | 'Right'}`
| 'setFilesFilterOff'}`
| `stashes.${'copy' | 'refresh' | `setLayoutTo${'List' | 'Tree'}` | `setFilesLayoutTo${'Auto' | 'List' | 'Tree'}`}`
| `tags.${'copy' | 'refresh' | `setFilesLayoutTo${'Auto' | 'List' | 'Tree'}`}`
| `stashes.${'copy' | 'refresh' | `setFilesLayoutTo${'Auto' | 'List' | 'Tree'}`}`
| `tags.${
| 'copy'
| 'refresh'
| `setLayoutTo${'List' | 'Tree'}`
| `setFilesLayoutTo${'Auto' | 'List' | 'Tree'}`
| `setShowAvatars${'On' | 'Off'}`}`
| `workspaces.${
| 'copy'
| 'refresh'
@ -411,9 +416,19 @@ export type TreeViewCommands = `gitlens.views.${
| `setShowAvatars${'On' | 'Off'}`
| `setShowBranchComparison${'On' | 'Off'}`
| `setShowBranchPullRequest${'On' | 'Off'}`}`}`;
export type TreeViewCommandsByViewId<T extends TreeViewIds> = TreeViewCommands extends `${T}.${infer U}`
? `${U}`
: never;
type ExtractSuffix<Prefix extends string, U> = U extends `${Prefix}${infer V}` ? V : never;
type FilterCommands<Prefix extends string, U> = U extends `${Prefix}${infer V}` ? `${Prefix}${V}` : never;
export type TreeViewCommandsByViewId<T extends TreeViewIds> = FilterCommands<T, TreeViewCommands>;
export type TreeViewCommandsByViewType<T extends TreeViewTypes> = FilterCommands<
`gitlens.views.${T}.`,
TreeViewCommands
>;
export type TreeViewCommandSuffixesByViewType<T extends TreeViewTypes> = ExtractSuffix<
`gitlens.views.${T}.`,
FilterCommands<`gitlens.views.${T}.`, TreeViewCommands>
>;
export type CustomEditorTypes = 'rebase';
export type CustomEditorIds = `gitlens.${CustomEditorTypes}`;

+ 1
- 1
src/views/branchesView.ts View File

@ -93,7 +93,7 @@ export class BranchesViewNode extends RepositoriesSubscribeableNode
}
}
export class BranchesView extends ViewBase<BranchesViewNode, BranchesViewConfig> {
export class BranchesView extends ViewBase<'branches', BranchesViewNode, BranchesViewConfig> {
protected readonly configKey = 'branches';
constructor(container: Container) {

+ 1
- 1
src/views/commitsView.ts View File

@ -190,7 +190,7 @@ interface CommitsViewState {
myCommitsOnly?: boolean;
}
export class CommitsView extends ViewBase<CommitsViewNode, CommitsViewConfig> {
export class CommitsView extends ViewBase<'commits', CommitsViewNode, CommitsViewConfig> {
protected readonly configKey = 'commits';
constructor(container: Container) {

+ 1
- 1
src/views/contributorsView.ts View File

@ -111,7 +111,7 @@ export class ContributorsViewNode extends RepositoriesSubscribeableNode
}
}
export class ContributorsView extends ViewBase<ContributorsViewNode, ContributorsViewConfig> {
export class ContributorsView extends ViewBase<'contributors', ContributorsViewNode, ContributorsViewConfig> {
protected readonly configKey = 'contributors';
constructor(container: Container) {

+ 5
- 1
src/views/fileHistoryView.ts View File

@ -13,7 +13,11 @@ import { registerViewCommand } from './viewCommands';
const pinnedSuffix = ' (pinned)';
export class FileHistoryView extends ViewBase<FileHistoryTrackerNode | LineHistoryTrackerNode, FileHistoryViewConfig> {
export class FileHistoryView extends ViewBase<
'fileHistory',
FileHistoryTrackerNode | LineHistoryTrackerNode,
FileHistoryViewConfig
> {
protected readonly configKey = 'fileHistory';
private _followCursor: boolean = false;

+ 1
- 1
src/views/lineHistoryView.ts View File

@ -11,7 +11,7 @@ import { registerViewCommand } from './viewCommands';
const pinnedSuffix = ' (pinned)';
export class LineHistoryView extends ViewBase<LineHistoryTrackerNode, LineHistoryViewConfig> {
export class LineHistoryView extends ViewBase<'lineHistory', LineHistoryTrackerNode, LineHistoryViewConfig> {
protected readonly configKey = 'lineHistory';
constructor(container: Container) {

+ 1
- 1
src/views/remotesView.ts View File

@ -94,7 +94,7 @@ export class RemotesViewNode extends RepositoriesSubscribeableNode
}
}
export class RemotesView extends ViewBase<RemotesViewNode, RemotesViewConfig> {
export class RemotesView extends ViewBase<'remotes', RemotesViewNode, RemotesViewConfig> {
protected readonly configKey = 'remotes';
constructor(container: Container) {

+ 1
- 1
src/views/repositoriesView.ts View File

@ -40,7 +40,7 @@ import { WorktreesNode } from './nodes/worktreesNode';
import { ViewBase } from './viewBase';
import { registerViewCommand } from './viewCommands';
export class RepositoriesView extends ViewBase<RepositoriesNode, RepositoriesViewConfig> {
export class RepositoriesView extends ViewBase<'repositories', RepositoriesNode, RepositoriesViewConfig> {
protected readonly configKey = 'repositories';
constructor(container: Container) {

+ 5
- 1
src/views/searchAndCompareView.ts View File

@ -245,7 +245,11 @@ export class SearchAndCompareViewNode extends ViewNode {
}
}
export class SearchAndCompareView extends ViewBase<SearchAndCompareViewNode, SearchAndCompareViewConfig> {
export class SearchAndCompareView extends ViewBase<
'searchAndCompare',
SearchAndCompareViewNode,
SearchAndCompareViewConfig
> {
protected readonly configKey = 'searchAndCompare';
constructor(container: Container) {

+ 1
- 1
src/views/stashesView.ts View File

@ -88,7 +88,7 @@ export class StashesViewNode extends RepositoriesSubscribeableNode
}
}
export class StashesView extends ViewBase<StashesViewNode, StashesViewConfig> {
export class StashesView extends ViewBase<'stashes', StashesViewNode, StashesViewConfig> {
protected readonly configKey = 'stashes';
constructor(container: Container) {

+ 1
- 1
src/views/tagsView.ts View File

@ -81,7 +81,7 @@ export class TagsViewNode extends RepositoriesSubscribeableNode
}
}
export class TagsView extends ViewBase<TagsViewNode, TagsViewConfig> {
export class TagsView extends ViewBase<'tags', TagsViewNode, TagsViewConfig> {
protected readonly configKey = 'tags';
constructor(container: Container) {

+ 18
- 16
src/views/viewBase.ts View File

@ -28,7 +28,7 @@ import type {
WorktreesViewConfig,
} from '../config';
import { viewsCommonConfigKeys, viewsConfigKeys } from '../config';
import type { TreeViewCommandsByViewId, TreeViewTypes } from '../constants';
import type { TreeViewCommandSuffixesByViewType, TreeViewTypes } from '../constants';
import type { Container } from '../container';
import { executeCoreCommand } from '../system/command';
import { configuration } from '../system/configuration';
@ -94,6 +94,7 @@ export interface TreeViewNodeCollapsibleStateChangeEvent extends TreeViewExpa
}
export abstract class ViewBase<
Type extends TreeViewTypes,
RootNode extends ViewNode,
ViewConfig extends
| BranchesViewConfig
@ -110,6 +111,10 @@ export abstract class ViewBase<
| WorktreesViewConfig,
> implements TreeDataProvider<ViewNode>, Disposable
{
get id(): `gitlens.views.${Type}` {
return `gitlens.views.${this.type}`;
}
protected _onDidChangeTreeData = new EventEmitter<ViewNode | undefined>();
get onDidChangeTreeData(): Event<ViewNode | undefined> {
return this._onDidChangeTreeData.event;
@ -136,15 +141,12 @@ export abstract class ViewBase<
private readonly _lastKnownLimits = new Map<string, number | undefined>();
readonly id: `gitlens.views.${TreeViewTypes}`;
constructor(
public readonly container: Container,
public readonly type: TreeViewTypes,
public readonly type: Type,
public readonly name: string,
private readonly trackingFeature: TrackedUsageFeatures,
) {
this.id = `gitlens.views.${type}`;
this.disposables.push(once(container.onReady)(this.onReady, this));
if (this.container.debugging || configuration.get('debug')) {
@ -169,7 +171,7 @@ export abstract class ViewBase<
}
const getTreeItemFn = this.getTreeItem;
this.getTreeItem = async function (this: ViewBase<RootNode, ViewConfig>, node: ViewNode) {
this.getTreeItem = async function (this: ViewBase<Type, RootNode, ViewConfig>, node: ViewNode) {
const item = await getTreeItemFn.apply(this, [node]);
if (node.resolveTreeItem == null) {
@ -181,7 +183,7 @@ export abstract class ViewBase<
const resolveTreeItemFn = this.resolveTreeItem;
this.resolveTreeItem = async function (
this: ViewBase<RootNode, ViewConfig>,
this: ViewBase<Type, RootNode, ViewConfig>,
item: TreeItem,
node: ViewNode,
) {
@ -284,8 +286,8 @@ export abstract class ViewBase<
}
}
getQualifiedCommand(command: TreeViewCommandsByViewId<typeof this.id>) {
return `${this.id}.${command}`; // satisfies ViewsCommandsById<typeof this.id>;
getQualifiedCommand(command: TreeViewCommandSuffixesByViewType<Type>) {
return `gitlens.views.${this.type}.${command}` as const;
}
protected abstract getRoot(): RootNode;
@ -381,7 +383,7 @@ export abstract class ViewBase<
return this.tree?.visible ?? false;
}
@log<ViewBase<RootNode, ViewConfig>['findNode']>({
@log<ViewBase<Type, RootNode, ViewConfig>['findNode']>({
args: {
0: '<function>',
1: opts => `options=${JSON.stringify({ ...opts, canTraverse: undefined, token: undefined })}`,
@ -403,7 +405,7 @@ export abstract class ViewBase<
): Promise<ViewNode | undefined> {
const scope = getLogScope();
async function find(this: ViewBase<RootNode, ViewConfig>) {
async function find(this: ViewBase<Type, RootNode, ViewConfig>) {
try {
const node = await this.findNodeCoreBFS(
predicate,
@ -546,7 +548,7 @@ export abstract class ViewBase<
this.triggerNodeChange();
}
@debug<ViewBase<RootNode, ViewConfig>['refreshNode']>({ args: { 0: n => n.toString() } })
@debug<ViewBase<Type, RootNode, ViewConfig>['refreshNode']>({ args: { 0: n => n.toString() } })
async refreshNode(node: ViewNode, reset: boolean = false, force: boolean = false) {
const cancel = await node.refresh?.(reset);
if (!force && cancel === true) return;
@ -554,7 +556,7 @@ export abstract class ViewBase<
this.triggerNodeChange(node);
}
@log<ViewBase<RootNode, ViewConfig>['reveal']>({ args: { 0: n => n.toString() } })
@log<ViewBase<Type, RootNode, ViewConfig>['reveal']>({ args: { 0: n => n.toString() } })
async reveal(
node: ViewNode,
options?: {
@ -588,7 +590,7 @@ export abstract class ViewBase<
return this._lastKnownLimits.get(node.id);
}
@debug<ViewBase<RootNode, ViewConfig>['loadMoreNodeChildren']>({
@debug<ViewBase<Type, RootNode, ViewConfig>['loadMoreNodeChildren']>({
args: { 0: n => n.toString(), 2: n => n?.toString() },
})
async loadMoreNodeChildren(
@ -605,7 +607,7 @@ export abstract class ViewBase<
this._lastKnownLimits.set(node.id, node.limit);
}
@debug<ViewBase<RootNode, ViewConfig>['resetNodeLastKnownLimit']>({
@debug<ViewBase<Type, RootNode, ViewConfig>['resetNodeLastKnownLimit']>({
args: { 0: n => n.toString() },
singleLine: true,
})
@ -613,7 +615,7 @@ export abstract class ViewBase<
this._lastKnownLimits.delete(node.id);
}
@debug<ViewBase<RootNode, ViewConfig>['triggerNodeChange']>({ args: { 0: n => n?.toString() } })
@debug<ViewBase<Type, RootNode, ViewConfig>['triggerNodeChange']>({ args: { 0: n => n?.toString() } })
triggerNodeChange(node?: ViewNode) {
// Since the root node won't actually refresh, force everything
this._onDidChangeTreeData.fire(node != null && node !== this.root ? node : undefined);

+ 1
- 1
src/views/workspacesView.ts View File

@ -18,7 +18,7 @@ import { WorkspacesViewNode } from './nodes/workspacesViewNode';
import { ViewBase } from './viewBase';
import { registerViewCommand } from './viewCommands';
export class WorkspacesView extends ViewBase<WorkspacesViewNode, WorkspacesViewConfig> {
export class WorkspacesView extends ViewBase<'workspaces', WorkspacesViewNode, WorkspacesViewConfig> {
protected readonly configKey = 'repositories';
private _workspacesChangedDisposable: Disposable;
private _visibleDisposable: Disposable | undefined;

+ 1
- 1
src/views/worktreesView.ts View File

@ -92,7 +92,7 @@ export class WorktreesViewNode extends RepositoriesSubscribeableNode
}
}
export class WorktreesView extends ViewBase<WorktreesViewNode, WorktreesViewConfig> {
export class WorktreesView extends ViewBase<'worktrees', WorktreesViewNode, WorktreesViewConfig> {
protected readonly configKey = 'worktrees';
constructor(container: Container) {

Loading…
Cancel
Save