Skip to content

Commit b41804f

Browse files
committed
Adds ViewFileNode base for consolidation
Avoids dangerous `any` cast when setting FileNode parent in FolderNode
1 parent 28c993c commit b41804f

13 files changed

+59
-47
lines changed

src/views/nodes/UncommittedFileNode.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ import { GitFile } from '../../git/models/file';
88
import { dirname, joinPaths } from '../../system/path';
99
import type { ViewsWithCommits } from '../viewBase';
1010
import type { FileNode } from './folderNode';
11-
import { ContextValues, ViewNode } from './viewNode';
12-
13-
export class UncommittedFileNode extends ViewNode<ViewsWithCommits> implements FileNode {
14-
public readonly file: GitFile;
15-
public readonly repoPath: string;
11+
import type { ViewNode } from './viewNode';
12+
import { ContextValues, ViewFileNode } from './viewNode';
1613

14+
export class UncommittedFileNode extends ViewFileNode<ViewsWithCommits> implements FileNode {
1715
constructor(view: ViewsWithCommits, parent: ViewNode, repoPath: string, file: GitFile) {
18-
super(GitUri.fromFile(file, repoPath), view, parent);
19-
20-
this.repoPath = repoPath;
21-
this.file = file;
16+
super(GitUri.fromFile(file, repoPath), view, parent, file);
2217
}
2318

2419
override toClipboard(): string {

src/views/nodes/branchTrackingStatusFilesNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export class BranchTrackingStatusFilesNode extends ViewNode<ViewsWithCommits> {
8686
new StatusFileNode(
8787
this.view,
8888
this,
89-
this.repoPath,
9089
files[files.length - 1],
90+
this.repoPath,
9191
files.map(s => s.commit),
9292
this.direction,
9393
),

src/views/nodes/commitFileNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ export class CommitFileNode<TView extends View = ViewsWithCommits | FileHistoryV
1818
constructor(
1919
view: TView,
2020
parent: ViewNode,
21-
public readonly file: GitFile,
21+
file: GitFile,
2222
public commit: GitCommit,
2323
private readonly _options: {
2424
branch?: GitBranch;
2525
selection?: Selection;
2626
unpublished?: boolean;
2727
} = {},
2828
) {
29-
super(GitUri.fromFile(file, commit.repoPath, commit.sha), view, parent);
29+
super(GitUri.fromFile(file, commit.repoPath, commit.sha), view, parent, file);
3030
}
3131

3232
override toClipboard(): string {

src/views/nodes/fileRevisionAsCommitNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class FileRevisionAsCommitNode extends ViewRefFileNode<ViewsWithCommits |
2424
constructor(
2525
view: ViewsWithCommits | FileHistoryView | LineHistoryView,
2626
parent: ViewNode,
27-
public readonly file: GitFile,
27+
file: GitFile,
2828
public commit: GitCommit,
2929
private readonly _options: {
3030
branch?: GitBranch;
@@ -33,7 +33,7 @@ export class FileRevisionAsCommitNode extends ViewRefFileNode<ViewsWithCommits |
3333
unpublished?: boolean;
3434
} = {},
3535
) {
36-
super(GitUri.fromFile(file, commit.repoPath, commit.sha), view, parent);
36+
super(GitUri.fromFile(file, commit.repoPath, commit.sha), view, parent, file);
3737
}
3838

3939
override toClipboard(): string {

src/views/nodes/folderNode.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ import { sortCompare } from '../../system/string';
77
import type { FileHistoryView } from '../fileHistoryView';
88
import type { StashesView } from '../stashesView';
99
import type { ViewsWithCommits } from '../viewBase';
10+
import type { ViewFileNode } from './viewNode';
1011
import { ContextValues, ViewNode } from './viewNode';
1112

12-
export interface FileNode extends ViewNode {
13+
export interface FileNode extends ViewFileNode {
1314
folderName: string;
14-
label?: string;
1515
priority: number;
16+
17+
label?: string;
1618
relativePath?: string;
17-
root?: HierarchicalItem<FileNode>;
19+
20+
// root?: HierarchicalItem<FileNode>;
1821
}
1922

2023
export class FolderNode extends ViewNode<ViewsWithCommits | FileHistoryView | StashesView> {
2124
readonly priority: number = 1;
2225

2326
constructor(
2427
view: ViewsWithCommits | FileHistoryView | StashesView,
25-
parent: ViewNode,
28+
protected override parent: ViewNode,
2629
public readonly repoPath: string,
2730
public readonly folderName: string,
2831
public readonly root: HierarchicalItem<FileNode>,
@@ -56,7 +59,7 @@ export class FolderNode extends ViewNode<ViewsWithCommits | FileHistoryView | St
5659
children.push(
5760
new FolderNode(
5861
this.view,
59-
this.folderName ? this : this.parent!,
62+
this.folderName ? this : this.parent,
6063
this.repoPath,
6164
folder.name,
6265
folder,
@@ -68,7 +71,7 @@ export class FolderNode extends ViewNode<ViewsWithCommits | FileHistoryView | St
6871
}
6972

7073
// Make sure to set the parent
71-
(folder.value as any).parent = this.folderName ? this : this.parent!;
74+
folder.value.parent = this.folderName ? this : this.parent;
7275
folder.value.relativePath = this.root.relativePath;
7376
children.push(folder.value);
7477
}

src/views/nodes/mergeConflictFileNode.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import type { ViewsWithCommits } from '../viewBase';
1111
import type { FileNode } from './folderNode';
1212
import { MergeConflictCurrentChangesNode } from './mergeConflictCurrentChangesNode';
1313
import { MergeConflictIncomingChangesNode } from './mergeConflictIncomingChangesNode';
14-
import { ContextValues, ViewNode } from './viewNode';
14+
import type { ViewNode } from './viewNode';
15+
import { ContextValues, ViewFileNode } from './viewNode';
1516

16-
export class MergeConflictFileNode extends ViewNode<ViewsWithCommits> implements FileNode {
17+
export class MergeConflictFileNode extends ViewFileNode<ViewsWithCommits> implements FileNode {
1718
constructor(
1819
view: ViewsWithCommits,
1920
parent: ViewNode,
21+
file: GitFile,
2022
public readonly status: GitMergeStatus | GitRebaseStatus,
21-
public readonly file: GitFile,
2223
) {
23-
super(GitUri.fromFile(file, status.repoPath, status.HEAD.ref), view, parent);
24+
super(GitUri.fromFile(file, status.repoPath, status.HEAD.ref), view, parent, file);
2425
}
2526

2627
override toClipboard(): string {
@@ -35,10 +36,6 @@ export class MergeConflictFileNode extends ViewNode<ViewsWithCommits> implements
3536
return this.file.path;
3637
}
3738

38-
get repoPath(): string {
39-
return this.status.repoPath;
40-
}
41-
4239
getChildren(): ViewNode[] {
4340
return [
4441
new MergeConflictCurrentChangesNode(this.view, this, this.status, this.file),

src/views/nodes/mergeStatusNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class MergeStatusNode extends ViewNode<ViewsWithCommits> {
4545
if (this.status?.hasConflicts !== true) return [];
4646

4747
let children: FileNode[] = this.status.conflicts.map(
48-
f => new MergeConflictFileNode(this.view, this, this.mergeStatus, f),
48+
f => new MergeConflictFileNode(this.view, this, f, this.mergeStatus),
4949
);
5050

5151
if (this.view.config.files.layout !== ViewFilesLayout.List) {

src/views/nodes/rebaseStatusNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class RebaseStatusNode extends ViewNode<ViewsWithCommits> {
5252

5353
async getChildren(): Promise<ViewNode[]> {
5454
let children: FileNode[] =
55-
this.status?.conflicts.map(f => new MergeConflictFileNode(this.view, this, this.rebaseStatus, f)) ?? [];
55+
this.status?.conflicts.map(f => new MergeConflictFileNode(this.view, this, f, this.rebaseStatus)) ?? [];
5656

5757
if (this.view.config.files.layout !== ViewFilesLayout.List) {
5858
const hierarchy = makeHierarchical(

src/views/nodes/resultsFileNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ export class ResultsFileNode extends ViewRefFileNode implements FileNode {
1818
view: View,
1919
parent: ViewNode,
2020
repoPath: string,
21-
public readonly file: GitFile,
21+
file: GitFile,
2222
public readonly ref1: string,
2323
public readonly ref2: string,
2424
private readonly direction: 'ahead' | 'behind' | undefined,
2525
) {
26-
super(GitUri.fromFile(file, repoPath, ref1 || ref2), view, parent);
26+
super(GitUri.fromFile(file, repoPath, ref1 || ref2), view, parent, file);
2727
}
2828

2929
override toClipboard(): string {

src/views/nodes/resultsFilesNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface FilesQueryResults {
3232
export class ResultsFilesNode extends ViewNode<ViewsWithCommits> {
3333
constructor(
3434
view: ViewsWithCommits,
35-
protected override readonly parent: ViewNode,
35+
protected override parent: ViewNode,
3636
public readonly repoPath: string,
3737
public readonly ref1: string,
3838
public readonly ref2: string,

0 commit comments

Comments
 (0)