|
6 | 6 | import * as vscode from 'vscode'; |
7 | 7 | import router from '@/router'; |
8 | 8 | import { PageType } from '@/router/types'; |
| 9 | +import { getChangedFileFromSourceControl } from '@/commands/editor'; |
9 | 10 | import { GitHub1sFileSystemProvider } from '@/providers/fileSystemProvider'; |
10 | 11 |
|
11 | | -export const registerVSCodeEventListeners = () => { |
| 12 | +const handleRouterOnActiveEditorChange = async ( |
| 13 | + editor: vscode.TextEditor | undefined |
| 14 | +) => { |
12 | 15 | // replace current url when user change active editor |
13 | | - vscode.window.onDidChangeActiveTextEditor(async (editor) => { |
14 | | - const { owner, repo, ref, pageType } = await router.getState(); |
15 | | - const activeFileUri = editor?.document.uri; |
16 | | - |
17 | | - // only `tree/blob` page will replace url with the active editor change |
18 | | - if (![PageType.TREE, PageType.BLOB].includes(pageType)) { |
19 | | - return; |
20 | | - } |
21 | | - |
22 | | - // if the file which not belong to current workspace is opened, or no file |
23 | | - // is opened, only retain `owner` and `repo` (and `ref` if need) in browser url |
24 | | - if ( |
25 | | - !activeFileUri || |
26 | | - activeFileUri?.authority || |
27 | | - activeFileUri?.scheme !== GitHub1sFileSystemProvider.scheme |
28 | | - ) { |
29 | | - const browserPath = |
30 | | - ref.toUpperCase() === 'HEAD' |
31 | | - ? `/${owner}/${repo}` |
32 | | - : `/${owner}/${repo}/tree/${ref}`; |
33 | | - router.history.replace(browserPath); |
34 | | - return; |
35 | | - } |
36 | | - |
37 | | - const browserPath = `/${owner}/${repo}/blob/${ref}${activeFileUri.path}`; |
| 16 | + const { owner, repo, ref, pageType } = await router.getState(); |
| 17 | + const activeFileUri = editor?.document.uri; |
| 18 | + |
| 19 | + // only `tree/blob` page will replace url with the active editor change |
| 20 | + if (![PageType.TREE, PageType.BLOB].includes(pageType)) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // if the file which not belong to current workspace is opened, or no file |
| 25 | + // is opened, only retain `owner` and `repo` (and `ref` if need) in browser url |
| 26 | + if ( |
| 27 | + !activeFileUri || |
| 28 | + activeFileUri?.authority || |
| 29 | + activeFileUri?.scheme !== GitHub1sFileSystemProvider.scheme |
| 30 | + ) { |
| 31 | + const browserPath = |
| 32 | + ref.toUpperCase() === 'HEAD' |
| 33 | + ? `/${owner}/${repo}` |
| 34 | + : `/${owner}/${repo}/tree/${ref}`; |
38 | 35 | router.history.replace(browserPath); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const browserPath = `/${owner}/${repo}/blob/${ref}${activeFileUri.path}`; |
| 40 | + router.history.replace(browserPath); |
| 41 | +}; |
| 42 | + |
| 43 | +// if the `Open Changes` Button should show in editor title |
| 44 | +const handleOpenChangesContextOnActiveEditorChange = async ( |
| 45 | + editor: vscode.TextEditor | undefined |
| 46 | +) => { |
| 47 | + const changedFile = editor?.document.uri |
| 48 | + ? await getChangedFileFromSourceControl(editor.document.uri) |
| 49 | + : undefined; |
| 50 | + |
| 51 | + return vscode.commands.executeCommand( |
| 52 | + 'setContext', |
| 53 | + 'github1s.context.showOpenChangesInEditorTitle', |
| 54 | + !!changedFile |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +export const registerVSCodeEventListeners = () => { |
| 59 | + vscode.window.onDidChangeActiveTextEditor(async (editor) => { |
| 60 | + handleRouterOnActiveEditorChange(editor); |
| 61 | + handleOpenChangesContextOnActiveEditorChange(editor); |
39 | 62 | }); |
40 | 63 | }; |
0 commit comments