Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit bc84f57

Browse files
committed
fix: better handling of tree views
1 parent 3b43063 commit bc84f57

6 files changed

Lines changed: 81 additions & 12 deletions

File tree

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@
9797
{
9898
"command": "extension.switchGitRevision",
9999
"category": "Sourcegraph",
100-
"title": "Switch git revision"
100+
"title": "View File at Git Revision",
101+
"enablement": "resourceScheme == sourcegraph || focusedView == 'sourcegraph.files'"
102+
},
103+
{
104+
"command": "extension.openFileInBrowser",
105+
"category": "Sourcegraph",
106+
"title": "Open File in Web Browser",
107+
"enablement": "resourceScheme == sourcegraph || focusedView == 'sourcegraph.files'"
101108
},
102109
{
103110
"command": "extension.newNotebook",
@@ -163,7 +170,9 @@
163170
}
164171
],
165172
"view/item/context": [
166-
{"command": "extension.goToFileInFolder", "when": "view == sourcegraph.files && viewItem == directory"}
173+
{"command": "extension.goToFileInFolder", "when": "view == sourcegraph.files && viewItem == directory"},
174+
{"command": "extension.switchGitRevision", "when": "view == sourcegraph.files && viewItem == file"},
175+
{"command": "extension.openFileInBrowser", "when": "view == sourcegraph.files && viewItem == file"}
167176
]
168177
},
169178
"keybindings": [
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import open from 'open'
2+
import { SourcegraphTreeDataProvider } from '../file-system/SourcegraphTreeDataProvider'
3+
import { SourcegraphUri } from '../file-system/SourcegraphUri'
4+
5+
export async function openFileInBrowserCommand(
6+
tree: SourcegraphTreeDataProvider,
7+
uriString: string | undefined
8+
): Promise<void> {
9+
const activeTextDocument = uriString ? SourcegraphUri.parse(uriString) : tree.activeTextDocument()
10+
if (!activeTextDocument || !activeTextDocument.path) {
11+
return
12+
}
13+
14+
await open(activeTextDocument.uri.replace('sourcegraph://', 'https://'))
15+
}

src/commands/switchGitRevisionCommand.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { SourcegraphTreeDataProvider } from '../file-system/SourcegraphTreeDataProvider'
2+
import { SourcegraphUri } from '../file-system/SourcegraphUri'
23
import { log } from '../log'
34
import { GitReference, gitReferencesQuery } from '../queries/gitReferencesQuery'
45
import { openSourcegraphUriCommand } from './openSourcegraphUriCommand'
56
import { SourcegraphQuickPick } from './SourcegraphQuickPick'
67

7-
export async function switchGitRevisionCommand(tree: SourcegraphTreeDataProvider): Promise<void> {
8+
export async function switchGitRevisionCommand(
9+
tree: SourcegraphTreeDataProvider,
10+
uriString: string | undefined
11+
): Promise<void> {
812
const quick = new SourcegraphQuickPick(tree.fs)
913
quick.pick.title = 'Search for a git branch, git tag or a git commit'
10-
const activeTextDocument = tree.activeTextDocument()
14+
const activeTextDocument = uriString ? SourcegraphUri.parse(uriString) : tree.activeTextDocument()
1115
if (!activeTextDocument || !activeTextDocument.path) {
1216
return
1317
}

src/extension.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { SourcegraphDefinitionProvider } from './code-intel/SourcegraphDefinitio
1717
import { SourcegraphReferenceProvider } from './code-intel/SourcegraphReferenceProvider'
1818
import { SourcegraphTreeDataProvider } from './file-system/SourcegraphTreeDataProvider'
1919
import { switchGitRevisionCommand } from './commands/switchGitRevisionCommand'
20+
import { openFileInBrowserCommand } from './commands/openFileInBrowserCommand'
2021

2122
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
2223
const { version } = require('../package.json')
@@ -105,7 +106,17 @@ export function activate(context: vscode.ExtensionContext): void {
105106
context.subscriptions.push(
106107
vscode.commands.registerCommand(
107108
'extension.switchGitRevision',
108-
handleCommandErrors('extension.switchGitRevision', () => switchGitRevisionCommand(treeDataProvider))
109+
handleCommandErrors('extension.switchGitRevision', (uri: string | undefined) =>
110+
switchGitRevisionCommand(treeDataProvider, uri)
111+
)
112+
)
113+
)
114+
context.subscriptions.push(
115+
vscode.commands.registerCommand(
116+
'extension.openFileInBrowser',
117+
handleCommandErrors('extension.openFileInBrowser', (uri: string | undefined) =>
118+
openFileInBrowserCommand(treeDataProvider, uri)
119+
)
109120
)
110121
)
111122
context.subscriptions.push(

src/file-system/SourcegraphFileSystemProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ export class SourcegraphFileSystemProvider implements vscode.FileSystemProvider
255255
vscode.window
256256
.withProgress(
257257
{
258-
location: vscode.ProgressLocation.SourceControl,
259-
title: `Downloading files for the repository ${uri.repositoryName}`,
258+
location: vscode.ProgressLocation.Window,
259+
title: `Loading ${uri.repositoryName}`,
260260
},
261261
async progress => {
262262
try {

src/file-system/SourcegraphTreeDataProvider.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,41 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
4242
})
4343
}
4444

45-
public getParent(uriString?: string): string | undefined {
45+
public async getParent(uriString?: string): Promise<string | undefined> {
4646
if (!uriString) {
4747
return undefined
4848
}
49-
return SourcegraphUri.parse(uriString).parentUri()
49+
const uri = SourcegraphUri.parse(uriString)
50+
if (!uri.path) {
51+
return undefined
52+
}
53+
let ancestor: string | undefined = uri.repositoryUri()
54+
let children = await this.getChildren(ancestor)
55+
while (ancestor) {
56+
for (const childName of children || []) {
57+
log.appendLine(`child=${childName}`)
58+
}
59+
const isParent = children?.includes(uriString)
60+
if (isParent) {
61+
break
62+
}
63+
ancestor = children?.find(childUri => {
64+
const child = SourcegraphUri.parse(childUri)
65+
return child.path && uri.path?.startsWith(child.path + '/')
66+
})
67+
if (!ancestor) {
68+
log.error(`getParent(${uriString || 'undefined'}) nothing startsWith`)
69+
throw new Error('BOOM')
70+
}
71+
children = await this.getChildren(ancestor)
72+
}
73+
log.appendLine(`getParent(${uriString || 'undefined'}) ancestor=${ancestor || 'undefined'}`)
74+
return ancestor
75+
// let parentUri = SourcegraphUri.parse(uriString).parentUri()
76+
// while (parentUri && !this.treeItemCache.has(parentUri)) {
77+
// parentUri = SourcegraphUri.parse(parentUri).parentUri()
78+
// }
79+
// return parentUri
5080
}
5181

5282
public async getChildren(uriString?: string): Promise<string[] | undefined> {
@@ -84,14 +114,14 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
84114
}
85115
}
86116

87-
public getTreeItem(uriString: string): vscode.TreeItem {
117+
public async getTreeItem(uriString: string): Promise<vscode.TreeItem> {
88118
try {
89119
const fromCache = this.treeItemCache.get(uriString)
90120
if (fromCache) {
91121
return fromCache
92122
}
93123
const uri = SourcegraphUri.parse(uriString)
94-
const parentUri = uri.parentUri()
124+
const parentUri = await this.getParent(uri.uri)
95125
return this.newTreeItem(uri, parentUri ? SourcegraphUri.parse(parentUri) : undefined, 0)
96126
} catch (error) {
97127
log.error(`getTreeItem(${uriString})`, error)
@@ -106,7 +136,7 @@ export class SourcegraphTreeDataProvider implements vscode.TreeDataProvider<stri
106136
): Promise<void> {
107137
try {
108138
if (this.treeView) {
109-
const parent = uri.parentUri()
139+
const parent = await this.getParent(uri.uri)
110140
if (parent && !this.isExpandedNode.has(parent)) {
111141
await this.didFocusString(SourcegraphUri.parse(parent), false, token)
112142
}

0 commit comments

Comments
 (0)