Skip to content

Commit 9734dd5

Browse files
authored
fix: the selection of the opening file in url may be cleared (#406)
1 parent 562d237 commit 9734dd5

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

extensions/github1s/src/extension.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,10 @@ const initialVSCodeState = async () => {
6161
let documentShowOptions: vscode.TextDocumentShowOptions = {};
6262
if (startLine || endLine) {
6363
const startPosition = new vscode.Position((startLine || endLine)! - 1, 0);
64-
const endPosition = new vscode.Position((endLine || startLine)! - 1, 999999);
64+
const endPosition = new vscode.Position((endLine || startLine)! - 1, 1 << 20);
6565
documentShowOptions = { selection: new vscode.Range(startPosition, endPosition) };
6666
}
67-
// TODO: the selection of the opening file may be cleared
68-
// when editor try to restore previous state in the same file
69-
vscode.commands.executeCommand(
70-
'vscode.open',
67+
vscode.window.showTextDocument(
7168
vscode.Uri.parse('').with({ scheme, path: `/${routerState.filePath}` }),
7269
documentShowOptions
7370
);

extensions/github1s/src/listeners/vscode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const handleRouterOnTextEditorSelectionChange = async (editor: vscode.TextEditor
5454
const routerParser = await router.resolveParser();
5555

5656
// only add the line number anchor when pageType is PageType.Blob
57-
if (pageType !== PageType.Blob || !editor.selection) {
57+
if (pageType !== PageType.Blob || !editor?.selection) {
5858
return;
5959
}
6060

@@ -63,11 +63,11 @@ const handleRouterOnTextEditorSelectionChange = async (editor: vscode.TextEditor
6363
repo,
6464
ref,
6565
activeFileUri.path.slice(1),
66-
editor.selection.start.line + 1,
66+
!editor.selection.isEmpty ? editor.selection.start.line + 1 : undefined,
6767
editor.selection.end.line !== editor.selection.start.line ? editor.selection.end.line + 1 : undefined
6868
);
6969

70-
router.replace(browserPath);
70+
browserPath !== (await router.getPath()) && router.replace(browserPath);
7171
};
7272

7373
// refresh file history view if active editor changed

extensions/github1s/src/router/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export class Router extends EventEmitter<RouterState> {
7070
return this._history!;
7171
}
7272

73+
public async getPath() {
74+
await this._barrier.wait();
75+
const { pathname, search, hash } = this._history!.location;
76+
return `${pathname}${search}${hash}`;
77+
}
78+
7379
// push the url with current history
7480
public async push(path: string) {
7581
await this._barrier.wait();

0 commit comments

Comments
 (0)