Select all matches (#2717)

Closes https://github.com/zed-industries/community/issues/75
Closes https://github.com/zed-industries/community/issues/1749

The PR 

* changes keybindings for `Editor && mode == auto_height` context:
before, `alt-enter` and `alt-shift-enter` added new lines in such
editors, including the one from buffer search.

New bindings are the same as in `Editor && mode == full` context.

* adds `search::SelectAllMatches` action and binds it to `Alt + Enter`
by default, to select all matches of a buffer search

The behavior mimics VSCode: we do not move the screen even if all
selections are out of the visible range (Cmd+G will navigate there) and
allow reselecting the results from both pane and search field, as long
as the search is not dismissed.

Release Notes:

- Added `search::SelectAllMatches` (`Alt + Enter` default) action to
place carets and select all buffer search results
([#75](https://github.com/zed-industries/community/issues/75),
[#1749](https://github.com/zed-industries/community/issues/1749)).
This commit is contained in:
Kirill Bulatov
2023-07-14 21:37:04 +03:00
committed by GitHub
12 changed files with 337 additions and 31 deletions
+32 -5
View File
@@ -887,10 +887,20 @@ pub(crate) enum BufferSearchHighlights {}
impl SearchableItem for Editor {
type Match = Range<Anchor>;
fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {
fn to_search_event(
&mut self,
event: &Self::Event,
_: &mut ViewContext<Self>,
) -> Option<SearchEvent> {
match event {
Event::BufferEdited => Some(SearchEvent::MatchesInvalidated),
Event::SelectionsChanged { .. } => Some(SearchEvent::ActiveMatchChanged),
Event::SelectionsChanged { .. } => {
if self.selections.disjoint_anchors().len() == 1 {
Some(SearchEvent::ActiveMatchChanged)
} else {
None
}
}
_ => None,
}
}
@@ -941,6 +951,11 @@ impl SearchableItem for Editor {
});
}
fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
self.unfold_ranges(matches.clone(), false, false, cx);
self.change_selections(None, cx, |s| s.select_ranges(matches));
}
fn match_index_for_direction(
&mut self,
matches: &Vec<Range<Anchor>>,
@@ -949,8 +964,16 @@ impl SearchableItem for Editor {
cx: &mut ViewContext<Self>,
) -> usize {
let buffer = self.buffer().read(cx).snapshot(cx);
let cursor = self.selections.newest_anchor().head();
if matches[current_index].start.cmp(&cursor, &buffer).is_gt() {
let current_index_position = if self.selections.disjoint_anchors().len() == 1 {
self.selections.newest_anchor().head()
} else {
matches[current_index].start
};
if matches[current_index]
.start
.cmp(&current_index_position, &buffer)
.is_gt()
{
if direction == Direction::Prev {
if current_index == 0 {
current_index = matches.len() - 1;
@@ -958,7 +981,11 @@ impl SearchableItem for Editor {
current_index -= 1;
}
}
} else if matches[current_index].end.cmp(&cursor, &buffer).is_lt() {
} else if matches[current_index]
.end
.cmp(&current_index_position, &buffer)
.is_lt()
{
if direction == Direction::Next {
current_index = 0;
}
+2 -2
View File
@@ -16,13 +16,13 @@ use crate::{
Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset,
};
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PendingSelection {
pub selection: Selection<Anchor>,
pub mode: SelectMode,
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct SelectionsCollection {
display_map: ModelHandle<DisplayMap>,
buffer: ModelHandle<MultiBuffer>,