@@ -169,20 +169,27 @@ impl DiffComponent {
169
169
let hash = hash ( & diff) ;
170
170
171
171
if self . current . hash != hash {
172
+ let reset_selection = self . current . path != path;
173
+
172
174
self . current = Current {
173
175
path,
174
176
is_stage,
175
177
hash,
176
178
} ;
177
179
178
- self . selected_hunk = Self :: find_selected_hunk (
179
- & diff,
180
- self . selection . get_start ( ) ,
181
- ) ;
182
-
183
180
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
+ }
186
193
}
187
194
188
195
Ok ( ( ) )
@@ -215,10 +222,15 @@ impl DiffComponent {
215
222
}
216
223
} ;
217
224
218
- let new_start = cmp:: min ( max, new_start) ;
225
+ self . update_selection ( new_start) ;
226
+ }
227
+ }
219
228
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) ;
220
233
self . selection = Selection :: Single ( new_start) ;
221
-
222
234
self . selected_hunk =
223
235
Self :: find_selected_hunk ( diff, new_start) ;
224
236
}
0 commit comments