Skip to content

Commit e079f7a

Browse files
authored
refactor: manage route in extension (conwnet#223)
1 parent ec29be2 commit e079f7a

24 files changed

Lines changed: 907 additions & 1080 deletions

File tree

extensions/github1s/package.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"onCommand:github1s.update-token",
1515
"onCommand:github1s.clear-token",
1616
"onCommand:github1s.get-current-authority",
17-
"onCommand:github1s.switch-branch",
18-
"onCommand:github1s.switch-tag",
17+
"onCommand:github1s.checkout-ref",
1918
"onView:github1s"
2019
],
2120
"browser": "./dist/extension",
@@ -58,13 +57,8 @@
5857
"category": "GitHub1s"
5958
},
6059
{
61-
"command": "github1s.switch-branch",
62-
"title": "Switch to Another Branch",
63-
"category": "GitHub1s"
64-
},
65-
{
66-
"command": "github1s.switch-tag",
67-
"title": "Switch to Another Tag",
60+
"command": "github1s.checkout-ref",
61+
"title": "Checkout to...",
6862
"category": "GitHub1s"
6963
},
7064
{
@@ -87,6 +81,7 @@
8781
"@apollo/client": "^3.3.9",
8882
"fuse.js": "^6.4.6",
8983
"graphql": "^15.5.0",
84+
"history": "^5.0.0",
9085
"js-base64": "^3.6.0",
9186
"json-stable-stringify": "^1.0.1",
9287
"p-finally": "^2.0.1",

extensions/github1s/src/commands.ts

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55

66
import * as vscode from 'vscode';
77
import { getExtensionContext } from '@/helpers/context';
8-
import {
9-
getRepositoryBranchRefs,
10-
getRepositoryTagRefs,
11-
getCurrentRef,
12-
getCurrentAuthority,
13-
changeCurrentRef,
14-
} from '@/helpers/git-ref';
8+
import router from '@/router';
9+
import repository from '@/repository';
1510
import { validateToken } from '@/interfaces/github-api-rest';
1611

1712
export const commandValidateToken = (silent: boolean = false) => {
@@ -111,53 +106,45 @@ export const commandClearToken = (silent: boolean = false) => {
111106
});
112107
};
113108

114-
export const commandGetCurrentAuthority = (): Promise<string> =>
115-
getCurrentAuthority();
109+
export const commandGetCurrentAuthority = () => router.getAuthority();
116110

117-
export const commandSwitchBranch = () => {
118-
return Promise.all([getRepositoryBranchRefs(), getCurrentRef()]).then(
119-
([repositoryBranches, currentRef]) =>
120-
vscode.window
121-
.showQuickPick(
122-
repositoryBranches.map((item) => item.name),
123-
{ placeHolder: currentRef }
124-
)
125-
.then((newRef: string) => {
126-
return (
127-
newRef &&
128-
changeCurrentRef(newRef).then((newRef) => {
129-
vscode.window.showInformationMessage(
130-
`Switch to branch: ${newRef}`
131-
);
132-
})
133-
);
134-
})
111+
export const commandCheckoutRef = async () => {
112+
const [branchRefs, tagRefs] = await Promise.all([
113+
repository.getBranches(),
114+
repository.getTags(),
115+
]);
116+
const branchPickerItems: vscode.QuickPickItem[] = branchRefs.map(
117+
(branchRef) => ({
118+
label: branchRef.name,
119+
description: (branchRef.object?.sha || '').slice(0, 8),
120+
})
135121
);
136-
};
122+
const tagPickerItems: vscode.QuickPickItem[] = tagRefs.map((tagRef) => ({
123+
label: tagRef.name,
124+
description: `Tag at ${(tagRef.object?.sha || '').slice(0, 8)}`,
125+
}));
137126

138-
export const commandSwitchTag = () => {
139-
return Promise.all([getRepositoryTagRefs(), getCurrentRef()]).then(
140-
([repositoryBranches, currentRef]) =>
141-
vscode.window
142-
.showQuickPick(
143-
repositoryBranches.map((item) => item.name),
144-
{ placeHolder: currentRef }
145-
)
146-
.then((newRef: string) => {
147-
return (
148-
newRef &&
149-
changeCurrentRef(newRef).then((newRef) => {
150-
vscode.window.showInformationMessage(
151-
`Switch to branch: ${newRef}`
152-
);
153-
})
154-
);
155-
})
127+
const quickPick = vscode.window.createQuickPick();
128+
const routerState = await router.getState();
129+
quickPick.placeholder = routerState.ref;
130+
quickPick.items = [...branchPickerItems, ...tagPickerItems];
131+
132+
quickPick.show();
133+
const choice = await new Promise<vscode.QuickPickItem | undefined>(
134+
(resolve) => quickPick.onDidAccept(() => resolve(quickPick.activeItems[0]))
156135
);
136+
quickPick.hide();
137+
138+
const targetRef = choice?.label || quickPick.value;
139+
if (targetRef.toUpperCase() === 'HEAD') {
140+
router.replace(`/${routerState.owner}/${routerState.repo}`);
141+
return;
142+
}
143+
router.replace(`/${routerState.owner}/${routerState.repo}/tree/${targetRef}`);
157144
};
158145

159146
export const commandOpenGitpod = () => {
160-
return getCurrentAuthority().then((currentAuthority) => {
147+
return router.getAuthority().then((currentAuthority) => {
161148
const [currentOwner, currentRepo] = currentAuthority.split('+');
162149
vscode.commands.executeCommand(
163150
'vscode.open',

extensions/github1s/src/extension.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
commandUpdateToken,
1111
commandValidateToken,
1212
commandClearToken,
13-
commandSwitchBranch,
14-
commandSwitchTag,
13+
commandCheckoutRef,
1514
commandGetCurrentAuthority,
1615
commandOpenGitpod,
1716
} from '@/commands';
@@ -23,9 +22,18 @@ import {
2322
} from '@/providers';
2423
import { showSponsors } from '@/sponsors';
2524
import { showGitpod } from '@/gitpod';
25+
import router from '@/router';
26+
import { activateSourceControl } from '@/source-control';
27+
import { registerEventListeners } from '@/listeners';
28+
import { PageType } from './router/types';
2629

2730
export async function activate(context: vscode.ExtensionContext) {
31+
// set the global context for convenient
2832
setExtensionContext(context);
33+
// Ensure the router has been initialized at first
34+
await router.initialize();
35+
// register the necessary event listeners
36+
registerEventListeners();
2937

3038
// providers
3139
const fsProvider = new GitHub1sFileSystemProvider();
@@ -61,37 +69,51 @@ export async function activate(context: vscode.ExtensionContext) {
6169

6270
// commands
6371
context.subscriptions.push(
72+
// validate GitHub OAuth Token
6473
vscode.commands.registerCommand(
6574
'github1s.validate-token',
6675
commandValidateToken
67-
)
68-
);
69-
context.subscriptions.push(
70-
vscode.commands.registerCommand('github1s.update-token', commandUpdateToken)
71-
);
72-
context.subscriptions.push(
73-
vscode.commands.registerCommand('github1s.clear-token', commandClearToken)
74-
);
75-
context.subscriptions.push(
76+
),
77+
// update GitHub OAuth Token
78+
vscode.commands.registerCommand(
79+
'github1s.update-token',
80+
commandUpdateToken
81+
),
82+
// clear GitHub OAuth Token
83+
vscode.commands.registerCommand('github1s.clear-token', commandClearToken),
84+
85+
// get current authority (`${owner}+${repo}+${ref}`)
7686
vscode.commands.registerCommand(
7787
'github1s.get-current-authority',
7888
commandGetCurrentAuthority
79-
)
80-
);
81-
context.subscriptions.push(
89+
),
90+
91+
// checkout to other branch/tag/commit
8292
vscode.commands.registerCommand(
83-
'github1s.switch-branch',
84-
commandSwitchBranch
85-
)
86-
);
87-
context.subscriptions.push(
88-
vscode.commands.registerCommand('github1s.switch-tag', commandSwitchTag)
89-
);
90-
context.subscriptions.push(
93+
'github1s.checkout-ref',
94+
commandCheckoutRef
95+
),
96+
97+
// open current repository on gitpod
9198
vscode.commands.registerCommand('github1s.open-gitpod', commandOpenGitpod)
9299
);
93100

101+
// activate SourceControl features,
102+
activateSourceControl();
103+
94104
// sponsors in Status Bar
95105
showSponsors();
96106
await showGitpod();
107+
108+
// open corresponding editor if there is a filePath specified in browser url
109+
const { filePath, pageType } = await router.getState();
110+
if (filePath && [PageType.TREE, PageType.BLOB].includes(pageType)) {
111+
vscode.commands.executeCommand(
112+
pageType === PageType.TREE ? 'revealInExplorer' : 'vscode.open',
113+
vscode.Uri.parse('').with({
114+
scheme: GitHub1sFileSystemProvider.scheme,
115+
path: filePath,
116+
})
117+
);
118+
}
97119
}

extensions/github1s/src/gitpod.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
*/
55

66
import * as vscode from 'vscode';
7-
import { getCurrentAuthority } from '@/helpers/git-ref';
7+
import router from '@/router';
88

9-
const getGitpodRepoUri = () =>
10-
getCurrentAuthority().then((currentAuthority) => {
11-
const [currentOwner, currentRepo] = currentAuthority.split('+');
12-
return vscode.Uri.parse(
13-
`https://gitpod.io/#https://github.com/${currentOwner}/${currentRepo}`
14-
);
15-
});
9+
const getGitpodRepoUri = async () => {
10+
const { owner, repo } = await router.getState();
11+
return vscode.Uri.parse(
12+
`https://gitpod.io/#https://github.com/${owner}/${repo}`
13+
);
14+
};
1615

1716
export const showGitpod = async () => {
1817
const gitpodRepoUri = await getGitpodRepoUri();

0 commit comments

Comments
 (0)