Prepopulate project search query when deploying it from an editor

This commit is contained in:
Antonio Scandurra 2022-06-07 10:02:04 +02:00
parent 6a086f62d5
commit 6b4e7da7d6
3 changed files with 63 additions and 38 deletions

View file

@ -1,6 +1,6 @@
pub use buffer_search::BufferSearchBar;
use editor::{Anchor, MultiBufferSnapshot};
use gpui::{actions, impl_internal_actions, MutableAppContext};
use editor::{display_map::ToDisplayPoint, Anchor, Bias, Editor, MultiBufferSnapshot};
use gpui::{actions, impl_internal_actions, MutableAppContext, ViewHandle};
pub use project_search::{ProjectSearchBar, ProjectSearchView};
use std::{
cmp::{self, Ordering},
@ -90,3 +90,30 @@ pub(crate) fn match_index_for_direction(
};
index
}
pub(crate) fn query_suggestion_for_editor(
editor: &ViewHandle<Editor>,
cx: &mut MutableAppContext,
) -> String {
let display_map = editor
.update(cx, |editor, cx| editor.snapshot(cx))
.display_snapshot;
let selection = editor.read(cx).selections.newest::<usize>(cx);
if selection.start == selection.end {
let point = selection.start.to_display_point(&display_map);
let range = editor::movement::surrounding_word(&display_map, point);
let range = range.start.to_offset(&display_map, Bias::Left)
..range.end.to_offset(&display_map, Bias::Right);
let text: String = display_map.buffer_snapshot.text_for_range(range).collect();
if text.trim().is_empty() {
String::new()
} else {
text
}
} else {
display_map
.buffer_snapshot
.text_for_range(selection.start..selection.end)
.collect()
}
}