|
5 | 5 |
|
6 | 6 | import * as vscode from 'vscode'; |
7 | 7 | import router from '@/router'; |
| 8 | +import { PageType } from '@/router/types'; |
| 9 | +import { GitHub1sFileSearchProvider } from '@/providers/fileSearchProvider'; |
| 10 | + |
| 11 | +// current editor is recovered by vscode, but should be closed now |
| 12 | +const shouldClosedThisEditor = async (editor) => { |
| 13 | + const { pullNumber } = await router.getState(); |
| 14 | + const resourceUriQuery = editor?.document.uri.query; |
| 15 | + const resourcePullNumber = resourceUriQuery?.match(/\bpull=(\d+)/)?.[1]; |
| 16 | + |
| 17 | + return resourcePullNumber && +resourcePullNumber !== pullNumber; |
| 18 | +}; |
8 | 19 |
|
9 | 20 | export const registerVSCodeEventListeners = () => { |
10 | 21 | // replace current url when user change active editor |
11 | 22 | vscode.window.onDidChangeActiveTextEditor(async (editor) => { |
12 | | - const filePath = editor?.document.uri.path || ''; |
13 | | - const { owner, repo, ref } = await router.getState(); |
| 23 | + const { owner, repo, ref, pageType, pullNumber } = await router.getState(); |
| 24 | + const activeFileUri = editor?.document.uri; |
| 25 | + |
| 26 | + if (activeFileUri?.scheme !== GitHub1sFileSearchProvider.scheme) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (await shouldClosedThisEditor(editor)) { |
| 31 | + vscode.commands.executeCommand( |
| 32 | + 'workbench.action.closeActiveEditor', |
| 33 | + activeFileUri |
| 34 | + ); |
| 35 | + return; |
| 36 | + } |
14 | 37 |
|
15 | | - // if no file opened and the branch is HEAD current, only retain owner and repo in url |
16 | | - const browserPath = |
17 | | - !filePath && ref.toUpperCase() === 'HEAD' |
18 | | - ? `/${owner}/${repo}` |
19 | | - : `/${owner}/${repo}/${filePath ? 'blob' : 'tree'}/${ref}${filePath}`; |
20 | | - router.history.replace(browserPath); |
| 38 | + // only `tree/blob` page will replace url with the active editor change |
| 39 | + if ([PageType.TREE, PageType.BLOB].includes(pageType)) { |
| 40 | + const filePath = activeFileUri?.path || ''; |
| 41 | + // if no file opened and the branch is HEAD current, only retain owner and repo in url |
| 42 | + const browserPath = |
| 43 | + !filePath && ref.toUpperCase() === 'HEAD' |
| 44 | + ? `/${owner}/${repo}` |
| 45 | + : `/${owner}/${repo}/${filePath ? 'blob' : 'tree'}/${ref}${filePath}`; |
| 46 | + router.history.replace(browserPath); |
| 47 | + } |
21 | 48 | }); |
22 | 49 | }; |
0 commit comments