forked from sourcegraph/sourcegraph-vscode-DEPRECATED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesTreeDataProvider.ts
186 lines (176 loc) · 7.28 KB
/
FilesTreeDataProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as vscode from 'vscode'
import { log } from '../log'
import { SourcegraphFileSystemProvider } from './SourcegraphFileSystemProvider'
import { SourcegraphUri } from './SourcegraphUri'
export class FilesTreeDataProvider implements vscode.TreeDataProvider<string> {
constructor(public readonly fs: SourcegraphFileSystemProvider) {
fs.onDidDownloadRepositoryFilenames(() => this.didChangeTreeData.fire(undefined))
}
private isTreeViewVisible = false
private isExpandedNode = new Set<string>()
private treeView: vscode.TreeView<string> | undefined
private activeUri: vscode.Uri | undefined
private didFocusToken = new vscode.CancellationTokenSource()
private treeItemCache = new Map<string, vscode.TreeItem>()
private readonly didChangeTreeData = new vscode.EventEmitter<string | undefined>()
public readonly onDidChangeTreeData: vscode.Event<string | undefined> = this.didChangeTreeData.event
public activeTextDocument(): SourcegraphUri | undefined {
return this.activeUri && this.activeUri.scheme === 'sourcegraph'
? this.fs.sourcegraphUri(this.activeUri)
: undefined
}
public setTreeView(treeView: vscode.TreeView<string>): void {
this.treeView = treeView
treeView.onDidChangeVisibility(event => {
const didBecomeVisible = !this.isTreeViewVisible && event.visible
this.isTreeViewVisible = event.visible
if (didBecomeVisible) {
this.didFocus(this.activeUri).then(
() => {},
() => {}
)
}
})
treeView.onDidExpandElement(event => {
this.isExpandedNode.add(event.element)
})
treeView.onDidCollapseElement(event => {
this.isExpandedNode.delete(event.element)
})
}
public async getParent(uriString?: string): Promise<string | undefined> {
// Implementation note: this method is not implemented as
// `SourcegraphUri.parse(uri).parentUri()` because that would return
// URIs to directories that don't exist because they have no siblings
// and are therefore automatically merged with their parent. For example,
// imagine the following folder structure:
// .gitignore
// .github/workflows/ci.yml
// src/command.ts
// src/browse.ts
// The parent of `.github/workflows/ci.yml` is `.github/` because the `workflows/`
// directory has no sibling.
if (!uriString) {
return undefined
}
const uri = SourcegraphUri.parse(uriString)
if (!uri.path) {
return undefined
}
let ancestor: string | undefined = uri.repositoryUri()
let children = await this.getChildren(ancestor)
while (ancestor) {
const isParent = children?.includes(uriString)
if (isParent) {
break
}
ancestor = children?.find(childUri => {
const child = SourcegraphUri.parse(childUri)
return child.path && uri.path?.startsWith(child.path + '/')
})
if (!ancestor) {
log.error(`getParent(${uriString || 'undefined'}) nothing startsWith`)
throw new Error('BOOM')
}
children = await this.getChildren(ancestor)
}
return ancestor
}
public async getChildren(uriString?: string): Promise<string[] | undefined> {
try {
if (!uriString) {
const repos = [...this.fs.allRepositoryUris()]
return repos.map(repo => repo.replace('https://', 'sourcegraph://'))
}
const uri = SourcegraphUri.parse(uriString)
const tree = await this.fs.getFileTree(uri)
const directChildren = tree.directChildren(uri.path || '')
for (const child of directChildren) {
this.treeItemCache.set(child, this.newTreeItem(SourcegraphUri.parse(child), uri, directChildren.length))
}
return directChildren
} catch (error) {
log.error(`getChildren(${uriString || ''})`, error)
return Promise.resolve(undefined)
}
}
public async focusActiveFile(): Promise<void> {
await vscode.commands.executeCommand('sourcegraph.files.focus')
await this.didFocus(this.activeUri)
}
public async didFocus(vscodeUri: vscode.Uri | undefined): Promise<void> {
log.appendLine(`didFocus=${vscodeUri?.toString(true) || 'undefined'}`)
this.didFocusToken.cancel()
this.didFocusToken = new vscode.CancellationTokenSource()
this.activeUri = vscodeUri
if (vscodeUri && vscodeUri.scheme === 'sourcegraph' && this.treeView && this.isTreeViewVisible) {
const uri = this.fs.sourcegraphUri(vscodeUri)
await this.fs.downloadFiles(uri)
await this.didFocusString(uri, true, this.didFocusToken.token)
}
}
public async getTreeItem(uriString: string): Promise<vscode.TreeItem> {
try {
const fromCache = this.treeItemCache.get(uriString)
if (fromCache) {
return fromCache
}
const uri = SourcegraphUri.parse(uriString)
const parentUri = await this.getParent(uri.uri)
return this.newTreeItem(uri, parentUri ? SourcegraphUri.parse(parentUri) : undefined, 0)
} catch (error) {
log.error(`getTreeItem(${uriString})`, error)
return {}
}
}
private async didFocusString(
uri: SourcegraphUri,
isDestinationNode: boolean,
token: vscode.CancellationToken
): Promise<void> {
try {
if (this.treeView) {
const parent = await this.getParent(uri.uri)
if (parent && !this.isExpandedNode.has(parent)) {
await this.didFocusString(SourcegraphUri.parse(parent), false, token)
}
if (token.isCancellationRequested) {
return
}
await this.treeView.reveal(uri.uri, {
focus: true,
select: isDestinationNode,
expand: !isDestinationNode,
})
}
} catch (error) {
log.error(`didFocusString(${uri.uri})`, error)
}
}
private newTreeItem(
uri: SourcegraphUri,
parent: SourcegraphUri | undefined,
parentChildrenCount: number
): vscode.TreeItem {
const command = uri.isFile()
? {
command: 'extension.openFile',
title: 'Open file',
arguments: [uri.uri],
}
: undefined
return {
id: uri.uri,
label: uri.treeItemLabel(parent),
tooltip: uri.uri.replace('sourcegraph://', 'https://'),
collapsibleState: uri.isFile()
? vscode.TreeItemCollapsibleState.None
: parentChildrenCount === 1
? vscode.TreeItemCollapsibleState.Expanded
: vscode.TreeItemCollapsibleState.Collapsed,
command,
resourceUri: vscode.Uri.parse(uri.uri),
contextValue: uri.isFile() ? 'file' : 'directory',
}
}
}