@@ -480,19 +480,66 @@ async function onSwitchHeaderSource(): Promise<void> {
480480 rootUri = vscode . Uri . file ( path . dirname ( fileName ) ) ; // When switching without a folder open.
481481 }
482482
483- let targetFileName : string = await clients . ActiveClient . requestSwitchHeaderSource ( rootUri , fileName ) ;
484- // If the targetFileName has a path that is a symlink target of a workspace folder,
485- // then replace the RootRealPath with the RootPath (the symlink path).
486- let targetFileNameReplaced : boolean = false ;
487- clients . forEach ( client => {
488- if ( ! targetFileNameReplaced && client . RootRealPath && client . RootPath !== client . RootRealPath
489- && targetFileName . startsWith ( client . RootRealPath ) ) {
490- targetFileName = client . RootPath + targetFileName . substring ( client . RootRealPath . length ) ;
491- targetFileNameReplaced = true ;
483+ const switchHeaderSource : ( token : vscode . CancellationToken ) => Promise < void > = async ( token : vscode . CancellationToken ) => {
484+ try {
485+ let targetFileName : string = await clients . ActiveClient . requestSwitchHeaderSource ( rootUri , fileName , token ) ;
486+ if ( ! targetFileName ) {
487+ return ;
488+ }
489+ // If the targetFileName has a path that is a symlink target of a workspace folder,
490+ // then replace the RootRealPath with the RootPath (the symlink path).
491+ let targetFileNameReplaced : boolean = false ;
492+ clients . forEach ( client => {
493+ if ( ! targetFileNameReplaced && client . RootRealPath && client . RootPath !== client . RootRealPath
494+ && targetFileName . startsWith ( client . RootRealPath ) ) {
495+ targetFileName = client . RootPath + targetFileName . substring ( client . RootRealPath . length ) ;
496+ targetFileNameReplaced = true ;
497+ }
498+ } ) ;
499+ const document : vscode . TextDocument = await vscode . workspace . openTextDocument ( targetFileName ) ;
500+ await vscode . window . showTextDocument ( document ) . then ( undefined , logAndReturn . undefined ) ;
501+ } catch ( e ) {
502+ if ( e instanceof vscode . CancellationError ) {
503+ return ;
504+ }
505+ throw e ;
492506 }
493- } ) ;
494- const document : vscode . TextDocument = await vscode . workspace . openTextDocument ( targetFileName ) ;
495- void vscode . window . showTextDocument ( document ) . then ( undefined , logAndReturn . undefined ) ;
507+ } ;
508+
509+ const tokenSource : vscode . CancellationTokenSource = new vscode . CancellationTokenSource ( ) ;
510+ try {
511+ const switchHeaderSourcePromise : Promise < void > = switchHeaderSource ( tokenSource . token ) ;
512+ const showProgress : boolean = await new Promise < boolean > ( ( resolve , reject ) => {
513+ const timer : NodeJS . Timeout = global . setTimeout ( ( ) => resolve ( true ) , 2000 ) ;
514+ void switchHeaderSourcePromise . then ( ( ) => {
515+ clearTimeout ( timer ) ;
516+ resolve ( false ) ;
517+ } , ( e ) => {
518+ clearTimeout ( timer ) ;
519+ reject ( e ) ;
520+ } ) ;
521+ } ) ;
522+
523+ if ( ! showProgress ) {
524+ await switchHeaderSourcePromise ;
525+ return ;
526+ }
527+
528+ await vscode . window . withProgress ( {
529+ location : vscode . ProgressLocation . Notification ,
530+ title : localize ( 'switch.header.source' , 'Switching Header/Source...' ) ,
531+ cancellable : true
532+ } , async ( _progress , token ) => {
533+ const cancellationListener : vscode . Disposable = token . onCancellationRequested ( ( ) => tokenSource . cancel ( ) ) ;
534+ try {
535+ await switchHeaderSourcePromise ;
536+ } finally {
537+ cancellationListener . dispose ( ) ;
538+ }
539+ } ) ;
540+ } finally {
541+ tokenSource . dispose ( ) ;
542+ }
496543}
497544
498545/**
0 commit comments