11import { env , Range , Uri , window } from 'vscode' ;
2+ import { Schemes } from '../constants' ;
23import type { Container } from '../container' ;
34import { command } from '../system/-webview/command' ;
45import { openTextEditor } from '../system/-webview/vscode/editors' ;
@@ -11,59 +12,68 @@ export class OpenFileFromRemoteCommand extends GlCommandBase {
1112 }
1213
1314 async execute ( ) : Promise < void > {
14- let clipboard : string | undefined = await env . clipboard . readText ( ) ;
15- try {
16- Uri . parse ( clipboard , true ) ;
17- } catch {
18- clipboard = undefined ;
19- }
15+ await openFileOreRevisionFromRemote ( this . container , 'file' ) ;
16+ }
17+ }
2018
21- const url = await window . showInputBox ( {
22- prompt : 'Enter a remote file url to open' ,
23- placeHolder : 'Remote file url' ,
24- value : clipboard ,
25- ignoreFocusOut : true ,
26- } ) ;
27- if ( url == null || url . length === 0 ) return ;
19+ @command ( )
20+ export class OpenRevisionFromRemoteCommand extends GlCommandBase {
21+ constructor ( private readonly container : Container ) {
22+ super ( 'gitlens.openRevisionFromRemote' ) ;
23+ }
2824
29- let local = await this . container . git . getLocalInfoFromRemoteUri ( Uri . parse ( url ) ) ;
30- if ( local == null ) {
31- local = await this . container . git . getLocalInfoFromRemoteUri ( Uri . parse ( url ) , { validate : false } ) ;
32- if ( local == null ) {
33- void window . showWarningMessage ( 'Unable to parse the provided remote url.' ) ;
25+ async execute ( ) : Promise < void > {
26+ await openFileOreRevisionFromRemote ( this . container , 'revision' ) ;
27+ }
28+ }
3429
35- return ;
36- }
30+ async function openFileOreRevisionFromRemote ( container : Container , type : 'file' | 'revision' ) : Promise < void > {
31+ let clipboard : string | undefined = await env . clipboard . readText ( ) ;
32+ try {
33+ Uri . parse ( clipboard , true ) ;
34+ } catch {
35+ clipboard = undefined ;
36+ }
3737
38- const confirm = 'Open File...' ;
39- const pick = await window . showWarningMessage (
40- 'Unable to find a workspace folder that matches the provided remote url. ',
41- confirm ,
42- ) ;
43- if ( pick !== confirm ) return ;
44- }
38+ const url = await window . showInputBox ( {
39+ prompt : 'Enter a remote file url to open' ,
40+ placeHolder : 'Remote file url',
41+ value : clipboard ,
42+ ignoreFocusOut : true ,
43+ } ) ;
44+ if ( ! url ?. length ) return ;
4545
46- let selection ;
47- if ( local . startLine ) {
48- if ( local . endLine ) {
49- selection = new Range ( local . startLine - 1 , 0 , local . endLine , 0 ) ;
50- } else {
51- selection = new Range ( local . startLine - 1 , 0 , local . startLine - 1 , 0 ) ;
52- }
53- }
46+ const local = await container . git . getLocalInfoFromRemoteUri ( Uri . parse ( url ) ) ;
47+ if ( local == null ) {
48+ void window . showWarningMessage ( 'Unable to parse the provided remote url.' ) ;
49+ return ;
50+ }
5451
55- try {
56- await openTextEditor ( local . uri , { selection : selection , throwOnError : true } ) ;
57- } catch {
58- const uris = await window . showOpenDialog ( {
59- title : 'Open local file' ,
60- defaultUri : local . uri ,
61- canSelectMany : false ,
62- canSelectFolders : false ,
63- } ) ;
64- if ( uris == null || uris . length === 0 ) return ;
52+ let { uri } = local ;
53+ if ( type === 'revision' && uri . scheme === Schemes . File && local . rev ) {
54+ uri = ( await container . git . getBestRevisionUri ( local . repoPath , local . uri . fsPath , local . rev ) ) ?? uri ;
55+ }
6556
66- await openTextEditor ( uris [ 0 ] ) ;
57+ let selection ;
58+ if ( local . startLine ) {
59+ if ( local . endLine ) {
60+ selection = new Range ( local . startLine - 1 , 0 , local . endLine , 0 ) ;
61+ } else {
62+ selection = new Range ( local . startLine - 1 , 0 , local . startLine - 1 , 0 ) ;
6763 }
6864 }
65+
66+ try {
67+ await openTextEditor ( uri , { selection : selection , throwOnError : true } ) ;
68+ } catch {
69+ const uris = await window . showOpenDialog ( {
70+ title : 'Open local file' ,
71+ defaultUri : uri ,
72+ canSelectMany : false ,
73+ canSelectFolders : false ,
74+ } ) ;
75+ if ( ! uris ?. length ) return ;
76+
77+ await openTextEditor ( uris [ 0 ] ) ;
78+ }
6979}
0 commit comments