@@ -9,6 +9,7 @@ import { PageType } from '@/router/types';
99import { setVSCodeContext } from '@/helpers/vscode' ;
1010import { getChangedFileFromSourceControl } from '@/commands/editor' ;
1111import { GitHub1sFileSystemProvider } from '@/providers/fileSystemProvider' ;
12+ import { debounce } from '@/helpers/func' ;
1213
1314const handleRouterOnActiveEditorChange = async (
1415 editor : vscode . TextEditor | undefined
@@ -33,12 +34,12 @@ const handleRouterOnActiveEditorChange = async (
3334 ref . toUpperCase ( ) === 'HEAD'
3435 ? `/${ owner } /${ repo } `
3536 : `/${ owner } /${ repo } /tree/${ ref } ` ;
36- router . history . replace ( browserPath ) ;
37+ router . history . replace ( { pathname : browserPath , hash : '' } ) ;
3738 return ;
3839 }
3940
4041 const browserPath = `/${ owner } /${ repo } /blob/${ ref } ${ activeFileUri . path } ` ;
41- router . history . replace ( browserPath ) ;
42+ router . history . replace ( { pathname : browserPath , hash : '' } ) ;
4243} ;
4344
4445// if the `Open Changes` Button should show in editor title
@@ -60,10 +61,51 @@ const handleGutterBlameOpeningContextOnActiveEditorChange = async () => {
6061 return setVSCodeContext ( 'github1s.context.gutterBlameOpening' , false ) ;
6162} ;
6263
64+ // get the line anchor hash (`#L27-L39`) from the editor.selection
65+ const getAnchorHashFromSelection = ( selection : vscode . Selection ) => {
66+ const { start, end } = selection ;
67+
68+ // the cursor move to somewhere but nothing is selected
69+ if ( start . line === end . line && start . character === end . character ) {
70+ return '' ;
71+ }
72+ if ( start . line === end . line ) {
73+ return `#L${ start . line + 1 } ` ;
74+ }
75+ return `#L${ start . line + 1 } -L${ end . line + 1 } ` ;
76+ } ;
77+
78+ // add the line number anchor when user selection lines in a editor
79+ const handleRouterOnTextEditorSelectionChange = async (
80+ editor : vscode . TextEditor
81+ ) => {
82+ const { owner, repo, ref, pageType } = await router . getState ( ) ;
83+
84+ // only add the line number anchor when pageType is PageType.BLOB
85+ if ( pageType !== PageType . BLOB || ! editor . selection ) {
86+ return ;
87+ }
88+
89+ const activeFileUri = editor ?. document . uri ;
90+ const anchorText = getAnchorHashFromSelection ( editor . selection ) ;
91+ const browserPath = `/${ owner } /${ repo } /blob/${ ref } ${ activeFileUri . path } ` ;
92+
93+ router . history . replace ( { pathname : browserPath , hash : anchorText } ) ;
94+ } ;
95+
6396export const registerVSCodeEventListeners = ( ) => {
64- vscode . window . onDidChangeActiveTextEditor ( async ( editor ) => {
97+ vscode . window . onDidChangeActiveTextEditor ( ( editor ) => {
6598 handleRouterOnActiveEditorChange ( editor ) ;
6699 handleOpenChangesContextOnActiveEditorChange ( editor ) ;
67100 handleGutterBlameOpeningContextOnActiveEditorChange ( ) ;
68101 } ) ;
102+
103+ // debounce to update the browser url
104+ const debouncedSelectionChangeRouterHandler = debounce (
105+ handleRouterOnTextEditorSelectionChange ,
106+ 100
107+ ) ;
108+ vscode . window . onDidChangeTextEditorSelection ( ( event ) => {
109+ debouncedSelectionChangeRouterHandler ( event . textEditor ) ;
110+ } ) ;
69111} ;
0 commit comments