Merge pull request #77 from zed-industries/merge-selections-larger-syntax-node

Merge selections correctly when selecting larger syntax node
This commit is contained in:
Antonio Scandurra 2021-05-31 16:55:21 +02:00 committed by GitHub
commit 3597305fe3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1829,7 +1829,7 @@ impl Editor {
let mut stack = mem::take(&mut self.select_larger_syntax_node_stack);
let mut selected_larger_node = false;
let old_selections = self.selections(app).to_vec();
let mut new_selections = Vec::new();
let mut new_selection_ranges = Vec::new();
for selection in &old_selections {
let old_range = selection.start.to_offset(buffer)..selection.end.to_offset(buffer);
let mut new_range = old_range.clone();
@ -1843,17 +1843,22 @@ impl Editor {
}
selected_larger_node |= new_range != old_range;
new_selections.push(Selection {
id: selection.id,
start: buffer.anchor_before(new_range.start),
end: buffer.anchor_before(new_range.end),
reversed: selection.reversed,
goal: SelectionGoal::None,
});
new_selection_ranges.push((selection.id, new_range, selection.reversed));
}
if selected_larger_node {
stack.push(old_selections);
new_selection_ranges.sort_unstable_by_key(|(_, range, _)| range.start.clone());
let new_selections = new_selection_ranges
.into_iter()
.map(|(id, range, reversed)| Selection {
id,
start: buffer.anchor_before(range.start),
end: buffer.anchor_before(range.end),
reversed,
goal: SelectionGoal::None,
})
.collect();
self.update_selections(new_selections, true, cx);
}
self.select_larger_syntax_node_stack = stack;
@ -4039,7 +4044,7 @@ mod tests {
});
assert_eq!(
view.read_with(&cx, |view, cx| view.selection_ranges(cx)),
&[DisplayPoint::new(0, 0)..DisplayPoint::new(5, 0)]
&[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)]
);
// Trying to expand the selected syntax node one more time has no effect.
@ -4048,7 +4053,7 @@ mod tests {
});
assert_eq!(
view.read_with(&cx, |view, cx| view.selection_ranges(cx)),
&[DisplayPoint::new(0, 0)..DisplayPoint::new(5, 0)]
&[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)]
);
view.update(&mut cx, |view, cx| {