@@ -4,7 +4,7 @@ import { commands, Disposable, Event, EventEmitter, ExtensionContext, TextDocume
44import { Commands , DiffWithCommandArgs , DiffWithCommandArgsRevision , DiffWithPreviousCommandArgs , DiffWithWorkingCommandArgs , openEditor , OpenFileInRemoteCommandArgs } from '../commands' ;
55import { UriComparer } from '../comparers' ;
66import { ExtensionKey , GitExplorerFilesLayout , IConfig } from '../configuration' ;
7- import { CommandContext , setCommandContext , WorkspaceState } from '../constants' ;
7+ import { CommandContext , GlyphChars , setCommandContext , WorkspaceState } from '../constants' ;
88import { BranchHistoryNode , CommitFileNode , CommitNode , ExplorerNode , HistoryNode , MessageNode , RepositoryNode , StashNode } from './explorerNodes' ;
99import { GitService , GitUri , RepoChangedReasons } from '../gitService' ;
1010
@@ -59,6 +59,10 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
5959
6060 const editorChangedFn = Functions . debounce ( this . onActiveEditorChanged , 500 ) ;
6161 context . subscriptions . push ( window . onDidChangeActiveTextEditor ( editorChangedFn , this ) ) ;
62+
63+ const visibleEditorsChangedFn = Functions . debounce ( this . onVisibleEditorsChanged , 500 ) ;
64+ context . subscriptions . push ( window . onDidChangeVisibleTextEditors ( visibleEditorsChangedFn , this ) ) ;
65+
6266 context . subscriptions . push ( workspace . onDidChangeConfiguration ( this . onConfigurationChanged , this ) ) ;
6367
6468 this . onConfigurationChanged ( ) ;
@@ -70,7 +74,7 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
7074
7175 async getChildren ( node ?: ExplorerNode ) : Promise < ExplorerNode [ ] > {
7276 if ( this . _root === undefined ) {
73- if ( this . _view === GitExplorerView . History ) return [ new MessageNode ( ' No active file; no history to show' ) ] ;
77+ if ( this . _view === GitExplorerView . History ) return [ new MessageNode ( ` No active file ${ GlyphChars . Dash } no history to show` ) ] ;
7478 return [ ] ;
7579 }
7680
@@ -90,8 +94,10 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
9094 }
9195
9296 private getHistoryNode ( editor : TextEditor | undefined ) : ExplorerNode | undefined {
93- if ( window . visibleTextEditors . length === 0 ) return undefined ;
94- if ( editor === undefined ) return this . _root ;
97+ // If we have no active editor, or no visible editors, or no trackable visible editors reset the view
98+ if ( editor === undefined || window . visibleTextEditors . length === 0 || ! window . visibleTextEditors . some ( e => e . document && this . git . isTrackable ( e . document . uri ) ) ) return undefined ;
99+ // If we do have a visible trackable editor, don't change from the last state (avoids issues when focus switches to the problems/output/debug console panes)
100+ if ( editor . document === undefined || ! this . git . isTrackable ( editor . document . uri ) ) return this . _root ;
95101
96102 const uri = this . git . getGitUriForFile ( editor . document . uri ) || new GitUri ( editor . document . uri , { repoPath : this . git . repoPath , fileName : editor . document . uri . fsPath } ) ;
97103 if ( UriComparer . equals ( uri , this . _root && this . _root . uri ) ) return this . _root ;
@@ -134,14 +140,26 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
134140 this . _root = this . getRootNode ( window . activeTextEditor ) ;
135141 this . refresh ( ) ;
136142 }
137- }
143+ }
138144
139145 private onRepoChanged ( reasons : RepoChangedReasons [ ] ) {
140146 if ( this . _view !== GitExplorerView . Repository ) return ;
141147
142148 this . refresh ( ) ;
143149 }
144150
151+ private onVisibleEditorsChanged ( editors : TextEditor [ ] ) {
152+ if ( this . _view !== GitExplorerView . History ) return ;
153+
154+ // If we have no visible editors, or no trackable visible editors reset the view
155+ if ( editors . length === 0 || ! editors . some ( e => e . document && this . git . isTrackable ( e . document . uri ) ) ) {
156+ if ( this . _root === undefined ) return ;
157+
158+ this . _root = undefined ;
159+ this . refresh ( ) ;
160+ }
161+ }
162+
145163 refresh ( node ?: ExplorerNode , root ?: ExplorerNode ) {
146164 if ( root === undefined && this . _view === GitExplorerView . History ) {
147165 this . _root = this . getRootNode ( window . activeTextEditor ) ;
@@ -178,6 +196,7 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
178196
179197 this . setView ( view ) ;
180198
199+ this . _root = undefined ;
181200 this . _root = this . getRootNode ( window . activeTextEditor ) ;
182201 this . refresh ( ) ;
183202 }
0 commit comments