Skip to content

Commit c184dc4

Browse files
committed
Moves node utils to its own file
1 parent b6e28b2 commit c184dc4

File tree

8 files changed

+144
-96
lines changed

8 files changed

+144
-96
lines changed

src/constants.views.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,27 @@ export const viewIdsByDefaultContainerId = new Map<ViewContainerIds | CoreViewCo
8686
]);
8787

8888
export type TreeViewRefNodeTypes = 'branch' | 'commit' | 'stash' | 'tag';
89+
export const treeViewRefNodeTypes: TreeViewRefNodeTypes[] = ['branch', 'commit', 'stash', 'tag'];
8990
export type TreeViewRefFileNodeTypes = 'commit-file' | 'file-commit' | 'results-file' | 'stash-file';
91+
export const treeViewRefFileNodeTypes: TreeViewRefFileNodeTypes[] = [
92+
'commit-file',
93+
'file-commit',
94+
'results-file',
95+
'stash-file',
96+
];
9097
export type TreeViewFileNodeTypes =
9198
| TreeViewRefFileNodeTypes
9299
| 'conflict-file'
93-
| 'folder'
100+
// | 'folder'
94101
| 'status-file'
95102
| 'uncommitted-file';
103+
export const treeViewFileNodeTypes: TreeViewFileNodeTypes[] = [
104+
...treeViewRefFileNodeTypes,
105+
'conflict-file',
106+
// 'folder',
107+
'status-file',
108+
'uncommitted-file',
109+
];
96110
export type TreeViewSubscribableNodeTypes =
97111
| 'commits-current-branch'
98112
| 'compare-branch'
@@ -123,6 +137,7 @@ export type TreeViewNodeTypes =
123137
| 'draft'
124138
| 'drafts'
125139
| 'drafts-code-suggestions'
140+
| 'folder'
126141
| 'grouping'
127142
| 'launchpad'
128143
| 'launchpad-item'

src/env/node/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isBranch } from '../../git/models/branch';
44
import { isCommit } from '../../git/models/commit';
55
import { isRepository } from '../../git/models/repository';
66
import { isTag } from '../../git/models/tag';
7-
import { isViewNode } from '../../views/nodes/abstract/viewNode';
7+
import { isViewNode } from '../../views/nodes/utils/-webview/node.utils';
88

99
export function loggingJsonReplacer(key: string, value: unknown): unknown {
1010
if (key === '' || value == null || typeof value !== 'object') return value;

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { BufferedLogChannel, getLoggableName, Logger } from './system/logger';
3232
import { flatten } from './system/object';
3333
import { Stopwatch } from './system/stopwatch';
3434
import { compare, fromString, satisfies } from './system/version';
35-
import { isViewNode } from './views/nodes/abstract/viewNode';
35+
import { isViewNode } from './views/nodes/utils/-webview/node.utils';
3636
import './commands';
3737

3838
export async function activate(context: ExtensionContext): Promise<GitLensApi | undefined> {

src/system/decorators/-webview/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isBranch } from '../../../git/models/branch';
55
import { isCommit } from '../../../git/models/commit';
66
import { isRepository } from '../../../git/models/repository';
77
import { isTag } from '../../../git/models/tag';
8-
import { isViewNode } from '../../../views/nodes/abstract/viewNode';
8+
import { isViewNode } from '../../../views/nodes/utils/-webview/node.utils';
99

1010
export function defaultResolver(...args: unknown[]): string {
1111
if (args.length === 0) return '';

src/views/nodes/abstract/viewFileNode.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ export abstract class ViewFileNode<
3131
}
3232
}
3333

34-
export function isViewFileNode(node: unknown): node is ViewFileNode {
35-
return node instanceof ViewFileNode;
36-
}
37-
3834
export function getFileTooltip(
3935
file: GitFile | GitStatusFile,
4036
suffix?: string,

src/views/nodes/abstract/viewNode.ts

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,9 @@ import { gate } from '../../../system/decorators/-webview/gate';
3131
import { debug, logName } from '../../../system/decorators/log';
3232
import { is as isA } from '../../../system/function';
3333
import { getLoggableName } from '../../../system/logger';
34-
import type { LaunchpadItemNode } from '../../launchpadView';
3534
import type { View } from '../../viewBase';
36-
import type { BranchNode } from '../branchNode';
37-
import type { BranchTrackingStatusFilesNode } from '../branchTrackingStatusFilesNode';
38-
import type { BranchTrackingStatus, BranchTrackingStatusNode } from '../branchTrackingStatusNode';
39-
import type { CodeSuggestionsNode } from '../codeSuggestionsNode';
40-
import type { CommitFileNode } from '../commitFileNode';
41-
import type { CommitNode } from '../commitNode';
42-
import type { CompareBranchNode } from '../compareBranchNode';
43-
import type { CompareResultsNode } from '../compareResultsNode';
44-
import type { FileRevisionAsCommitNode } from '../fileRevisionAsCommitNode';
45-
import type { FolderNode } from '../folderNode';
46-
import type { LineHistoryTrackerNode } from '../lineHistoryTrackerNode';
47-
import type { MergeConflictFileNode } from '../mergeConflictFileNode';
48-
import type { PullRequestNode } from '../pullRequestNode';
49-
import type { RepositoryNode } from '../repositoryNode';
50-
import type { ResultsCommitsNode } from '../resultsCommitsNode';
51-
import type { ResultsFileNode } from '../resultsFileNode';
52-
import type { ResultsFilesNode } from '../resultsFilesNode';
53-
import type { StashFileNode } from '../stashFileNode';
54-
import type { StashNode } from '../stashNode';
55-
import type { StatusFileNode } from '../statusFileNode';
56-
import type { TagNode } from '../tagNode';
57-
import type { UncommittedFileNode } from '../UncommittedFileNode';
58-
import type { RepositoryFolderNode } from './repositoryFolderNode';
35+
import type { BranchTrackingStatus } from '../branchTrackingStatusNode';
36+
import type { TreeViewNodesByType } from '../utils/-webview/node.utils';
5937

6038
export const enum ContextValues {
6139
ActiveFileHistory = 'gitlens:history:active:file',
@@ -413,63 +391,3 @@ export function canViewDismissNode(view: View): view is View & { dismissNode(nod
413391
export function getNodeRepoPath(node?: ViewNode): string | undefined {
414392
return canGetNodeRepoPath(node) ? node.repoPath : undefined;
415393
}
416-
417-
// prettier-ignore
418-
type TreeViewNodesByType = {
419-
[T in TreeViewNodeTypes]: T extends 'branch'
420-
? BranchNode
421-
: T extends 'commit'
422-
? CommitNode
423-
: T extends 'commit-file'
424-
? CommitFileNode
425-
: T extends 'compare-branch'
426-
? CompareBranchNode
427-
: T extends 'compare-results'
428-
? CompareResultsNode
429-
: T extends 'conflict-file'
430-
? MergeConflictFileNode
431-
: T extends 'drafts-code-suggestions'
432-
? CodeSuggestionsNode
433-
: T extends 'file-commit'
434-
? FileRevisionAsCommitNode
435-
: T extends 'folder'
436-
? FolderNode
437-
: T extends 'launchpad-item'
438-
? LaunchpadItemNode
439-
: T extends 'line-history-tracker'
440-
? LineHistoryTrackerNode
441-
: T extends 'pullrequest'
442-
? PullRequestNode
443-
: T extends 'repository'
444-
? RepositoryNode
445-
: T extends 'repo-folder'
446-
? RepositoryFolderNode
447-
: T extends 'results-commits'
448-
? ResultsCommitsNode
449-
: T extends 'results-file'
450-
? ResultsFileNode
451-
: T extends 'results-files'
452-
? ResultsFilesNode
453-
: T extends 'stash'
454-
? StashNode
455-
: T extends 'stash-file'
456-
? StashFileNode
457-
: T extends 'status-file'
458-
? StatusFileNode
459-
: T extends 'tag'
460-
? TagNode
461-
: T extends 'tracking-status'
462-
? BranchTrackingStatusNode
463-
: T extends 'tracking-status-files'
464-
? BranchTrackingStatusFilesNode
465-
: T extends 'uncommitted-file'
466-
? UncommittedFileNode
467-
: ViewNode<T>;
468-
};
469-
470-
export function isViewNode(node: unknown): node is ViewNode;
471-
export function isViewNode<T extends keyof TreeViewNodesByType>(node: unknown, type: T): node is TreeViewNodesByType[T];
472-
export function isViewNode<T extends keyof TreeViewNodesByType>(node: unknown, type?: T): node is ViewNode {
473-
if (node == null) return false;
474-
return node instanceof ViewNode ? type == null || node.type === type : false;
475-
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import type { TreeViewNodeTypes } from '../../../../constants.views';
2+
import { treeViewFileNodeTypes, treeViewRefFileNodeTypes, treeViewRefNodeTypes } from '../../../../constants.views';
3+
import type { LaunchpadItemNode } from '../../../launchpadView';
4+
import type { RepositoryFolderNode } from '../../abstract/repositoryFolderNode';
5+
import { ViewNode } from '../../abstract/viewNode';
6+
import type { BranchNode } from '../../branchNode';
7+
import type { BranchTrackingStatusFilesNode } from '../../branchTrackingStatusFilesNode';
8+
import type { BranchTrackingStatusNode } from '../../branchTrackingStatusNode';
9+
import type { CodeSuggestionsNode } from '../../codeSuggestionsNode';
10+
import type { CommitFileNode } from '../../commitFileNode';
11+
import type { CommitNode } from '../../commitNode';
12+
import type { CompareBranchNode } from '../../compareBranchNode';
13+
import type { CompareResultsNode } from '../../compareResultsNode';
14+
import type { FileRevisionAsCommitNode } from '../../fileRevisionAsCommitNode';
15+
import type { FolderNode } from '../../folderNode';
16+
import type { LineHistoryTrackerNode } from '../../lineHistoryTrackerNode';
17+
import type { MergeConflictFileNode } from '../../mergeConflictFileNode';
18+
import type { PullRequestNode } from '../../pullRequestNode';
19+
import type { RepositoryNode } from '../../repositoryNode';
20+
import type { ResultsCommitsNode } from '../../resultsCommitsNode';
21+
import type { ResultsFileNode } from '../../resultsFileNode';
22+
import type { ResultsFilesNode } from '../../resultsFilesNode';
23+
import type { StashFileNode } from '../../stashFileNode';
24+
import type { StashNode } from '../../stashNode';
25+
import type { StatusFileNode } from '../../statusFileNode';
26+
import type { TagNode } from '../../tagNode';
27+
import type { UncommittedFileNode } from '../../UncommittedFileNode';
28+
29+
// prettier-ignore
30+
export type TreeViewNodesByType = {
31+
[T in TreeViewNodeTypes]: T extends 'branch'
32+
? BranchNode
33+
: T extends 'commit'
34+
? CommitNode
35+
: T extends 'commit-file'
36+
? CommitFileNode
37+
: T extends 'compare-branch'
38+
? CompareBranchNode
39+
: T extends 'compare-results'
40+
? CompareResultsNode
41+
: T extends 'conflict-file'
42+
? MergeConflictFileNode
43+
: T extends 'drafts-code-suggestions'
44+
? CodeSuggestionsNode
45+
: T extends 'file-commit'
46+
? FileRevisionAsCommitNode
47+
: T extends 'folder'
48+
? FolderNode
49+
: T extends 'launchpad-item'
50+
? LaunchpadItemNode
51+
: T extends 'line-history-tracker'
52+
? LineHistoryTrackerNode
53+
: T extends 'pullrequest'
54+
? PullRequestNode
55+
: T extends 'repository'
56+
? RepositoryNode
57+
: T extends 'repo-folder'
58+
? RepositoryFolderNode
59+
: T extends 'results-commits'
60+
? ResultsCommitsNode
61+
: T extends 'results-file'
62+
? ResultsFileNode
63+
: T extends 'results-files'
64+
? ResultsFilesNode
65+
: T extends 'stash'
66+
? StashNode
67+
: T extends 'stash-file'
68+
? StashFileNode
69+
: T extends 'status-file'
70+
? StatusFileNode
71+
: T extends 'tag'
72+
? TagNode
73+
: T extends 'tracking-status'
74+
? BranchTrackingStatusNode
75+
: T extends 'tracking-status-files'
76+
? BranchTrackingStatusFilesNode
77+
: T extends 'uncommitted-file'
78+
? UncommittedFileNode
79+
: ViewNode<T>;
80+
};
81+
82+
type FilterNodesByType<T extends keyof TreeViewNodesByType | (keyof TreeViewNodesByType)[]> =
83+
T extends keyof TreeViewNodesByType
84+
? TreeViewNodesByType[T]
85+
: T extends (keyof TreeViewNodesByType)[]
86+
? TreeViewNodesByType[T[number]]
87+
: never;
88+
89+
export function isViewNode(node: unknown): node is ViewNode;
90+
export function isViewNode<T extends keyof TreeViewNodesByType | (keyof TreeViewNodesByType)[]>(
91+
node: unknown,
92+
type: T,
93+
): node is FilterNodesByType<T>;
94+
export function isViewNode<T extends keyof TreeViewNodesByType>(node: unknown, type?: T | T[]): node is ViewNode {
95+
if (node == null || !(node instanceof ViewNode)) return false;
96+
97+
if (type == null) return true;
98+
if (Array.isArray(type)) {
99+
return type.includes(node.type);
100+
}
101+
return node.type === type;
102+
}
103+
104+
export function isViewFileNode(node: unknown): node is FilterNodesByType<typeof treeViewFileNodeTypes> {
105+
return isViewNode(node, treeViewFileNodeTypes);
106+
}
107+
108+
export function isViewFileOrFolderNode(
109+
node: unknown,
110+
): node is FilterNodesByType<'folder' | (typeof treeViewFileNodeTypes)[number]> {
111+
return isViewNode(node, 'folder') || isViewNode(node, treeViewFileNodeTypes);
112+
}
113+
114+
export function isViewRefFileNode(node: unknown): node is FilterNodesByType<typeof treeViewRefFileNodeTypes> {
115+
return isViewNode(node, treeViewRefFileNodeTypes);
116+
}
117+
118+
export function isViewRefNode(node: unknown): node is FilterNodesByType<typeof treeViewRefNodeTypes> {
119+
return isViewNode(node, treeViewRefNodeTypes);
120+
}

src/webviews/plus/timeline/timelineWebview.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import { debounce } from '../../../system/function';
2121
import { filter } from '../../../system/iterable';
2222
import { flatten } from '../../../system/object';
2323
import { getSettledValue } from '../../../system/promise';
24-
import { isViewFileNode } from '../../../views/nodes/abstract/viewFileNode';
25-
import { isViewNode } from '../../../views/nodes/abstract/viewNode';
24+
import { isViewFileOrFolderNode } from '../../../views/nodes/utils/-webview/node.utils';
2625
import type { IpcMessage } from '../../protocol';
2726
import type { WebviewHost, WebviewProvider, WebviewShowingArgs } from '../../webviewProvider';
2827
import type { WebviewShowOptions } from '../../webviewsController';
@@ -94,7 +93,7 @@ export class TimelineWebviewProvider implements WebviewProvider<State, State, Ti
9493
if (arg != null) {
9594
if (arg instanceof Uri) {
9695
uri = arg;
97-
} else if (isViewFileNode(arg)) {
96+
} else if (isViewFileOrFolderNode(arg)) {
9897
uri = arg.uri;
9998
} else if (isSerializedState<State>(arg) && arg.state.uri != null) {
10099
uri = Uri.parse(arg.state.uri);
@@ -127,7 +126,7 @@ export class TimelineWebviewProvider implements WebviewProvider<State, State, Ti
127126
if (arg != null) {
128127
if (arg instanceof Uri) {
129128
uri = arg;
130-
} else if (isViewNode(arg) && (arg.type === 'folder' || isViewFileNode(arg))) {
129+
} else if (isViewFileOrFolderNode(arg)) {
131130
uri = arg.uri;
132131
} else if (isSerializedState<State>(arg)) {
133132
this._context.period = arg.state.period;

0 commit comments

Comments
 (0)