@@ -1987,28 +1987,29 @@ impl Editor {
19871987 }
19881988
19891989 pub fn focus ( & mut self , view_id : ViewId ) {
1990- let prev_id = std:: mem:: replace ( & mut self . tree . focus , view_id) ;
1991-
1992- // if leaving the view: mode should reset and the cursor should be
1993- // within view
1994- if prev_id != view_id {
1995- self . enter_normal_mode ( ) ;
1996- self . ensure_cursor_in_view ( view_id) ;
1990+ if self . tree . focus == view_id {
1991+ return ;
1992+ }
19971993
1998- // Update jumplist selections with new document changes.
1999- for ( view, _focused) in self . tree . views_mut ( ) {
2000- let doc = doc_mut ! ( self , & view. doc) ;
2001- view. sync_changes ( doc) ;
2002- }
2003- let view = view ! ( self , view_id) ;
1994+ // Reset mode to normal and ensure any pending changes are committed in the old document.
1995+ self . enter_normal_mode ( ) ;
1996+ let ( view, doc) = current ! ( self ) ;
1997+ doc. append_changes_to_history ( view) ;
1998+ self . ensure_cursor_in_view ( view_id) ;
1999+ // Update jumplist selections with new document changes.
2000+ for ( view, _focused) in self . tree . views_mut ( ) {
20042001 let doc = doc_mut ! ( self , & view. doc) ;
2005- doc. mark_as_focused ( ) ;
2006- let focus_lost = self . tree . get ( prev_id) . doc ;
2007- dispatch ( DocumentFocusLost {
2008- editor : self ,
2009- doc : focus_lost,
2010- } ) ;
2002+ view. sync_changes ( doc) ;
20112003 }
2004+
2005+ let prev_id = std:: mem:: replace ( & mut self . tree . focus , view_id) ;
2006+ doc_mut ! ( self ) . mark_as_focused ( ) ;
2007+
2008+ let focus_lost = self . tree . get ( prev_id) . doc ;
2009+ dispatch ( DocumentFocusLost {
2010+ editor : self ,
2011+ doc : focus_lost,
2012+ } ) ;
20122013 }
20132014
20142015 pub fn focus_next ( & mut self ) {
0 commit comments