Skip to content

Commit e2c446b

Browse files
committed
Fix shift click sorting when items sorted by name, fixes #1135
1 parent 0c081b7 commit e2c446b

File tree

1 file changed

+51
-64
lines changed

1 file changed

+51
-64
lines changed

src/tab.rs

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,73 +3007,60 @@ impl Tab {
30073007
.select_range
30083008
.map_or(Some((click_i, click_i)), |r| Some((r.0, click_i)));
30093009
if let Some(range) = self.select_range {
3010-
let min = range.0.min(range.1);
3011-
let max = range.0.max(range.1);
3012-
let (sort_name, sort_direction, _) = self.sort_options();
3013-
//TODO: this assumes the default sort order!
3014-
if sort_name == HeadingOptions::Name && sort_direction {
3015-
// A default/unsorted tab's view is consistent with how the
3016-
// Items are laid out internally (items_opt), so Items can be
3017-
// linearly selected
3018-
if let Some(ref mut items) = self.items_opt {
3019-
for item in items.iter_mut().skip(min).take(max - min + 1) {
3010+
let range_min = range.0.min(range.1);
3011+
let range_max = range.0.max(range.1);
3012+
// A sorted tab's items can't be linearly selected
3013+
// Let's say we have:
3014+
// index | file
3015+
// 0 | file0
3016+
// 1 | file1
3017+
// 2 | file2
3018+
// This is both the default sort and internal ordering
3019+
// When sorted it may be displayed as:
3020+
// 1 | file1
3021+
// 0 | file0
3022+
// 2 | file2
3023+
// However, the internal ordering is still the same thus
3024+
// linearly selecting items doesn't work. Shift selecting
3025+
// file0 and file2 would select indices 0 to 2 when it should
3026+
// select indices 0 AND 2 from items_opt
3027+
let indices: Vec<_> = self
3028+
.column_sort()
3029+
.map(|sorted| sorted.into_iter().map(|(i, _)| i).collect())
3030+
.unwrap_or_else(|| {
3031+
let len = self
3032+
.items_opt
3033+
.as_deref()
3034+
.map(|items| items.len())
3035+
.unwrap_or_default();
3036+
(0..len).collect()
3037+
});
3038+
3039+
// Find the true indices for the min and max element w.r.t.
3040+
// a sorted tab.
3041+
let min = indices
3042+
.iter()
3043+
.position(|&offset| offset == range_min)
3044+
.unwrap_or_default();
3045+
// We can't skip `min_real` elements here because the index of
3046+
// `max` may actually be before `min` in a sorted tab
3047+
let max = indices
3048+
.iter()
3049+
.position(|&offset| offset == range_max)
3050+
.unwrap_or(indices.len());
3051+
let min_real = min.min(max);
3052+
let max_real = max.max(min);
3053+
3054+
if let Some(ref mut items) = self.items_opt {
3055+
for index in indices
3056+
.into_iter()
3057+
.skip(min_real)
3058+
.take(max_real - min_real + 1)
3059+
{
3060+
if let Some(item) = items.get_mut(index) {
30203061
item.selected = true;
30213062
}
30223063
}
3023-
} else {
3024-
// A sorted tab's items can't be linearly selected
3025-
// Let's say we have:
3026-
// index | file
3027-
// 0 | file0
3028-
// 1 | file1
3029-
// 2 | file2
3030-
// This is both the default sort and internal ordering
3031-
// When sorted it may be displayed as:
3032-
// 1 | file1
3033-
// 0 | file0
3034-
// 2 | file2
3035-
// However, the internal ordering is still the same thus
3036-
// linearly selecting items doesn't work. Shift selecting
3037-
// file0 and file2 would select indices 0 to 2 when it should
3038-
// select indices 0 AND 2 from items_opt
3039-
let indices: Vec<_> = self
3040-
.column_sort()
3041-
.map(|sorted| sorted.into_iter().map(|(i, _)| i).collect())
3042-
.unwrap_or_else(|| {
3043-
let len = self
3044-
.items_opt
3045-
.as_deref()
3046-
.map(|items| items.len())
3047-
.unwrap_or_default();
3048-
(0..len).collect()
3049-
});
3050-
3051-
// Find the true indices for the min and max element w.r.t.
3052-
// a sorted tab.
3053-
let min = indices
3054-
.iter()
3055-
.position(|&offset| offset == min)
3056-
.unwrap_or_default();
3057-
// We can't skip `min_real` elements here because the index of
3058-
// `max` may actually be before `min` in a sorted tab
3059-
let max = indices
3060-
.iter()
3061-
.position(|&offset| offset == max)
3062-
.unwrap_or(indices.len());
3063-
let min_real = min.min(max);
3064-
let max_real = max.max(min);
3065-
3066-
if let Some(ref mut items) = self.items_opt {
3067-
for index in indices
3068-
.into_iter()
3069-
.skip(min_real)
3070-
.take(max_real - min_real + 1)
3071-
{
3072-
if let Some(item) = items.get_mut(index) {
3073-
item.selected = true;
3074-
}
3075-
}
3076-
}
30773064
}
30783065
}
30793066
self.clicked = click_i_opt;

0 commit comments

Comments
 (0)