11import * as vscode from 'vscode'
22import { log } from '../log'
33import { SourcegraphFileSystemProvider } from './SourcegraphFileSystemProvider'
4- import { emptyCancelationToken } from './emptyCancelationToken'
54import { SourcegraphUri } from './SourcegraphUri'
65
76export class SourcegraphTreeDataProvider implements vscode . TreeDataProvider < string > {
@@ -14,6 +13,7 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
1413 private treeView : vscode . TreeView < string > | undefined
1514 private activeUri : vscode . Uri | undefined
1615 private didFocusToken = new vscode . CancellationTokenSource ( )
16+ private treeItemCache = new Map < string , vscode . TreeItem > ( )
1717 private readonly didChangeTreeData = new vscode . EventEmitter < string | undefined > ( )
1818 public readonly onDidChangeTreeData : vscode . Event < string | undefined > = this . didChangeTreeData . event
1919
@@ -52,7 +52,11 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
5252 }
5353 const uri = SourcegraphUri . parse ( uriString )
5454 const tree = await this . fs . getFileTree ( uri )
55- return tree . directChildren ( uri . path || '' )
55+ const directChildren = tree . directChildren ( uri . path || '' )
56+ for ( const child of directChildren ) {
57+ this . treeItemCache . set ( child , this . newTreeItem ( SourcegraphUri . parse ( child ) , uri , directChildren . length ) )
58+ }
59+ return directChildren
5660 } catch ( error ) {
5761 log . error ( `getChildren(${ uriString || '' } )` , error )
5862 return Promise . resolve ( undefined )
@@ -75,29 +79,18 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
7579 }
7680 }
7781
78- public async getTreeItem ( uriString : string ) : Promise < vscode . TreeItem > {
82+ public getTreeItem ( uriString : string ) : vscode . TreeItem {
7983 try {
80- const uri = SourcegraphUri . parse ( uriString )
81- const label = await this . treeItemLabel ( uri )
82- const collapsibleState = await this . getCollapsibleState ( uri )
83- const command = uri . isFile ( )
84- ? {
85- command : 'extension.openFile' ,
86- title : 'Open file' ,
87- arguments : [ uri . uri ] ,
88- }
89- : undefined
90- return {
91- id : uri . uri ,
92- label,
93- tooltip : uri . uri . replace ( 'sourcegraph://' , 'https://' ) ,
94- collapsibleState,
95- command,
96- resourceUri : vscode . Uri . parse ( uri . uri ) ,
84+ const fromCache = this . treeItemCache . get ( uriString )
85+ if ( fromCache ) {
86+ return fromCache
9787 }
88+ const uri = SourcegraphUri . parse ( uriString )
89+ const parentUri = uri . parentUri ( )
90+ return this . newTreeItem ( uri , parentUri ? SourcegraphUri . parse ( parentUri ) : undefined , 0 )
9891 } catch ( error ) {
9992 log . error ( `getTreeItem(${ uriString } )` , error )
100- return Promise . resolve ( { } )
93+ return { }
10194 }
10295 }
10396
@@ -126,33 +119,73 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
126119 }
127120 }
128121
129- private async treeItemLabel ( uri : SourcegraphUri ) : Promise < string > {
122+ private treeItemLabel ( uri : SourcegraphUri , parent ?: SourcegraphUri ) : string {
130123 if ( uri . path ) {
131- return uri . basename ( )
132- }
133- const metadata = await this . fs . repositoryMetadata ( uri . repositoryName , emptyCancelationToken ( ) )
134- let revision = uri . revision
135- if ( metadata ?. defaultBranch && ( ! revision || revision === metadata ?. defaultOid ) ) {
136- revision = metadata . defaultBranch
124+ if ( parent ?. path ) {
125+ return uri . path . slice ( parent . path . length + 1 )
126+ }
127+ return uri . path
137128 }
138- return `${ uri . repositoryName } @ ${ revision || '' } `
129+ return `${ uri . repositoryName } ${ uri . revisionPart ( ) } `
139130 }
140131
141- private async getCollapsibleState ( uri : SourcegraphUri ) : Promise < vscode . TreeItemCollapsibleState > {
142- if ( uri . isFile ( ) ) {
143- return vscode . TreeItemCollapsibleState . None
144- }
145- const parentUri = uri . parentUri ( )
146- if ( parentUri ) {
147- const parent = SourcegraphUri . parse ( parentUri )
148- if ( parent . path ) {
149- const tree = await this . fs . getFileTree ( parent )
150- const directChildren = tree . directChildren ( parent . path )
151- if ( directChildren && directChildren . length === 1 ) {
152- return vscode . TreeItemCollapsibleState . Expanded
153- }
154- }
132+ // private collapsibleState(uri: SourcegraphUri): vscode.TreeItemCollapsibleState {
133+ // if (uri.isFile()) {
134+ // return vscode.TreeItemCollapsibleState.None
135+ // }
136+ // const parentUri = uri.parentUri()
137+ // if (parentUri && this.treeItemCache.get(parentUri) === 1) {
138+ // return vscode.TreeItemCollapsibleState.Expanded
139+ // }
140+ // return vscode.TreeItemCollapsibleState.Collapsed
141+ // }
142+ // if (uri.isFile()) {
143+ // return vscode.TreeItemCollapsibleState.None
144+ // }
145+ // const parentUri = uri.parentUri()
146+ // let result = vscode.TreeItemCollapsibleState.Collapsed
147+ // if (parentUri) {
148+ // const parent = SourcegraphUri.parse(parentUri)
149+ // const tree = await this.fs.getFileTree(parent)
150+ // const fromCache = this.directChildrenCount.get(parentUri)
151+ // if (fromCache) {
152+ // return fromCache
153+ // }
154+ // if (parent.path) {
155+ // const directChildren = tree.directChildren(parent.path)
156+ // if (directChildren && directChildren.length === 1) {
157+ // result = vscode.TreeItemCollapsibleState.Expanded
158+ // }
159+ // }
160+ // this.directChildrenCount.set(parentUri, result)
161+ // }
162+ // return result
163+ // }
164+ private newTreeItem (
165+ uri : SourcegraphUri ,
166+ parent : SourcegraphUri | undefined ,
167+ parentChildrenCount : number
168+ ) : vscode . TreeItem {
169+ const command = uri . isFile ( )
170+ ? {
171+ command : 'extension.openFile' ,
172+ title : 'Open file' ,
173+ arguments : [ uri . uri ] ,
174+ }
175+ : undefined
176+ const label = this . treeItemLabel ( uri , parent )
177+
178+ return {
179+ id : uri . uri ,
180+ label,
181+ tooltip : uri . uri . replace ( 'sourcegraph://' , 'https://' ) ,
182+ collapsibleState : uri . isFile ( )
183+ ? vscode . TreeItemCollapsibleState . None
184+ : parentChildrenCount === 1
185+ ? vscode . TreeItemCollapsibleState . Expanded
186+ : vscode . TreeItemCollapsibleState . Collapsed ,
187+ command,
188+ resourceUri : vscode . Uri . parse ( uri . uri ) ,
155189 }
156- return vscode . TreeItemCollapsibleState . Collapsed
157190 }
158191}
0 commit comments