@@ -169,20 +169,27 @@ impl DiffComponent {
169169 let hash = hash ( & diff) ;
170170
171171 if self . current . hash != hash {
172+ let reset_selection = self . current . path != path;
173+
172174 self . current = Current {
173175 path,
174176 is_stage,
175177 hash,
176178 } ;
177179
178- self . selected_hunk = Self :: find_selected_hunk (
179- & diff,
180- self . selection . get_start ( ) ,
181- ) ;
182-
183180 self . diff = Some ( diff) ;
184- self . scroll_top . set ( 0 ) ;
185- self . selection = Selection :: Single ( 0 ) ;
181+
182+ if reset_selection {
183+ self . scroll_top . set ( 0 ) ;
184+ self . selection = Selection :: Single ( 0 ) ;
185+ self . update_selection ( 0 ) ;
186+ } else {
187+ let old_selection = match self . selection {
188+ Selection :: Single ( line) => line,
189+ Selection :: Multiple ( start, _) => start,
190+ } ;
191+ self . update_selection ( old_selection) ;
192+ }
186193 }
187194
188195 Ok ( ( ) )
@@ -215,10 +222,15 @@ impl DiffComponent {
215222 }
216223 } ;
217224
218- let new_start = cmp:: min ( max, new_start) ;
225+ self . update_selection ( new_start) ;
226+ }
227+ }
219228
229+ fn update_selection ( & mut self , new_start : usize ) {
230+ if let Some ( diff) = & self . diff {
231+ let max = diff. lines . saturating_sub ( 1 ) as usize ;
232+ let new_start = cmp:: min ( max, new_start) ;
220233 self . selection = Selection :: Single ( new_start) ;
221-
222234 self . selected_hunk =
223235 Self :: find_selected_hunk ( diff, new_start) ;
224236 }
0 commit comments