@@ -4,8 +4,12 @@ import { env } from "vscode";
44/**
55 * A node in the tree of files. This will be either a `FileTreeDirectory` or a `FileTreeLeaf`.
66 */
7- export abstract class FileTreeNode {
8- constructor ( private _path : string , private _name : string ) { }
7+ export abstract class FileTreeNode < T = undefined > {
8+ constructor (
9+ private _path : string ,
10+ private _name : string ,
11+ private _data ?: T ,
12+ ) { }
913
1014 public get path ( ) : string {
1115 return this . _path ;
@@ -15,32 +19,36 @@ export abstract class FileTreeNode {
1519 return this . _name ;
1620 }
1721
18- public abstract get children ( ) : readonly FileTreeNode [ ] ;
22+ public get data ( ) : T | undefined {
23+ return this . _data ;
24+ }
25+
26+ public abstract get children ( ) : ReadonlyArray < FileTreeNode < T > > ;
1927
2028 public abstract finish ( ) : void ;
2129}
2230
2331/**
2432 * A directory containing one or more files or other directories.
2533 */
26- export class FileTreeDirectory extends FileTreeNode {
34+ export class FileTreeDirectory < T = undefined > extends FileTreeNode < T > {
2735 constructor (
2836 _path : string ,
2937 _name : string ,
30- private _children : FileTreeNode [ ] = [ ] ,
38+ private _children : Array < FileTreeNode < T > > = [ ] ,
3139 ) {
3240 super ( _path , _name ) ;
3341 }
3442
35- public get children ( ) : readonly FileTreeNode [ ] {
43+ public get children ( ) : ReadonlyArray < FileTreeNode < T > > {
3644 return this . _children ;
3745 }
3846
39- public addChild ( child : FileTreeNode ) : void {
47+ public addChild ( child : FileTreeNode < T > ) : void {
4048 this . _children . push ( child ) ;
4149 }
4250
43- public createDirectory ( relativePath : string ) : FileTreeDirectory {
51+ public createDirectory ( relativePath : string ) : FileTreeDirectory < T > {
4452 if ( relativePath === "." ) {
4553 return this ;
4654 }
@@ -66,7 +74,7 @@ export class FileTreeDirectory extends FileTreeNode {
6674 child . children [ 0 ] instanceof FileTreeDirectory
6775 ) {
6876 // collapse children
69- const replacement = new FileTreeDirectory (
77+ const replacement = new FileTreeDirectory < T > (
7078 child . children [ 0 ] . path ,
7179 `${ child . name } / ${ child . children [ 0 ] . name } ` ,
7280 Array . from ( child . children [ 0 ] . children ) ,
@@ -76,12 +84,12 @@ export class FileTreeDirectory extends FileTreeNode {
7684 } ) ;
7785 }
7886
79- private createChildDirectory ( name : string ) : FileTreeDirectory {
87+ private createChildDirectory ( name : string ) : FileTreeDirectory < T > {
8088 const existingChild = this . _children . find ( ( child ) => child . name === name ) ;
8189 if ( existingChild !== undefined ) {
82- return existingChild as FileTreeDirectory ;
90+ return existingChild as FileTreeDirectory < T > ;
8391 } else {
84- const newChild = new FileTreeDirectory ( join ( this . path , name ) , name ) ;
92+ const newChild = new FileTreeDirectory < T > ( join ( this . path , name ) , name ) ;
8593 this . addChild ( newChild ) ;
8694 return newChild ;
8795 }
@@ -91,12 +99,12 @@ export class FileTreeDirectory extends FileTreeNode {
9199/**
92100 * A single file.
93101 */
94- export class FileTreeLeaf extends FileTreeNode {
95- constructor ( _path : string , _name : string ) {
96- super ( _path , _name ) ;
102+ export class FileTreeLeaf < T = undefined > extends FileTreeNode < T > {
103+ constructor ( _path : string , _name : string , _data ?: T ) {
104+ super ( _path , _name , _data ) ;
97105 }
98106
99- public get children ( ) : readonly FileTreeNode [ ] {
107+ public get children ( ) : ReadonlyArray < FileTreeNode < T > > {
100108 return [ ] ;
101109 }
102110
0 commit comments