mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-26 03:59:55 +00:00
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:
commit
3597305fe3
1 changed files with 15 additions and 10 deletions
|
@ -1829,7 +1829,7 @@ impl Editor {
|
||||||
let mut stack = mem::take(&mut self.select_larger_syntax_node_stack);
|
let mut stack = mem::take(&mut self.select_larger_syntax_node_stack);
|
||||||
let mut selected_larger_node = false;
|
let mut selected_larger_node = false;
|
||||||
let old_selections = self.selections(app).to_vec();
|
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 {
|
for selection in &old_selections {
|
||||||
let old_range = selection.start.to_offset(buffer)..selection.end.to_offset(buffer);
|
let old_range = selection.start.to_offset(buffer)..selection.end.to_offset(buffer);
|
||||||
let mut new_range = old_range.clone();
|
let mut new_range = old_range.clone();
|
||||||
|
@ -1843,17 +1843,22 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
selected_larger_node |= new_range != old_range;
|
selected_larger_node |= new_range != old_range;
|
||||||
new_selections.push(Selection {
|
new_selection_ranges.push((selection.id, new_range, selection.reversed));
|
||||||
id: selection.id,
|
|
||||||
start: buffer.anchor_before(new_range.start),
|
|
||||||
end: buffer.anchor_before(new_range.end),
|
|
||||||
reversed: selection.reversed,
|
|
||||||
goal: SelectionGoal::None,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if selected_larger_node {
|
if selected_larger_node {
|
||||||
stack.push(old_selections);
|
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.update_selections(new_selections, true, cx);
|
||||||
}
|
}
|
||||||
self.select_larger_syntax_node_stack = stack;
|
self.select_larger_syntax_node_stack = stack;
|
||||||
|
@ -4039,7 +4044,7 @@ mod tests {
|
||||||
});
|
});
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
view.read_with(&cx, |view, cx| view.selection_ranges(cx)),
|
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.
|
// Trying to expand the selected syntax node one more time has no effect.
|
||||||
|
@ -4048,7 +4053,7 @@ mod tests {
|
||||||
});
|
});
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
view.read_with(&cx, |view, cx| view.selection_ranges(cx)),
|
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| {
|
view.update(&mut cx, |view, cx| {
|
||||||
|
|
Loading…
Reference in a new issue