Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Project/use line hint for search (#58871)

mertkanakkoc9375695626f1
diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs
index 90b4a6a5997..12a3a263d2d 100644
--- a/crates/project/src/project.rs
+++ b/crates/project/src/project.rs
@@ -5681,7 +5681,7 @@ impl Project {
}
});
- while let Some(buffer) = new_matches.next().await {
+ while let Some((buffer, _)) = new_matches.next().await {
let buffer_id = this.update(cx, |this, cx| {
this.create_buffer_for_peer(&buffer, peer_id, cx).to_proto()
});
diff --git a/crates/project/src/project_search.rs b/crates/project/src/project_search.rs
index 600e7aa5932..971581dcf1a 100644
--- a/crates/project/src/project_search.rs
+++ b/crates/project/src/project_search.rs
@@ -16,7 +16,7 @@ use fs::Fs;
use futures::FutureExt as _;
use futures::{SinkExt, StreamExt, select_biased, stream::FuturesOrdered};
use gpui::{App, AppContext, AsyncApp, BackgroundExecutor, Entity, Priority, Task};
-use language::{Buffer, BufferSnapshot};
+use language::{Buffer, BufferSnapshot, Point};
use parking_lot::Mutex;
use postage::oneshot;
use rpc::{AnyProtoClient, proto};
@@ -27,7 +27,7 @@ use worktree::{Entry, ProjectEntryId, Snapshot, Worktree, WorktreeSettings};
use crate::{
Project, ProjectItem, ProjectPath, RemotelyCreatedModels,
buffer_store::BufferStore,
- search::{SearchQuery, SearchResult},
+ search::{LineHint, SearchQuery, SearchResult},
worktree_store::WorktreeStore,
};
@@ -61,7 +61,7 @@ enum SearchKind {
#[must_use]
pub struct SearchResultsHandle {
results: Receiver<SearchResult>,
- matching_buffers: Receiver<Entity<Buffer>>,
+ matching_buffers: Receiver<(Entity<Buffer>, LineHint)>,
trigger_search: Box<dyn FnOnce(&mut App) -> Task<()> + Send + Sync>,
}
@@ -76,7 +76,7 @@ impl SearchResultsHandle {
rx: self.results,
}
}
- pub fn matching_buffers(self, cx: &mut App) -> SearchResults<Entity<Buffer>> {
+ pub fn matching_buffers(self, cx: &mut App) -> SearchResults<(Entity<Buffer>, LineHint)> {
SearchResults {
task_handle: (self.trigger_search)(cx),
rx: self.matching_buffers,
@@ -179,12 +179,15 @@ impl Search {
let open_buffers = Arc::new(open_buffers);
let executor = cx.background_executor().clone();
let (tx, rx) = unbounded();
- let (grab_buffer_snapshot_tx, grab_buffer_snapshot_rx) = unbounded();
+ let (grab_buffer_snapshot_tx, grab_buffer_snapshot_rx) =
+ unbounded::<(Entity<Buffer>, LineHint)>();
let matching_buffers = grab_buffer_snapshot_rx.clone();
let trigger_search = Box::new(move |cx: &mut App| {
cx.spawn(async move |cx| {
for buffer in unnamed_buffers {
- _ = grab_buffer_snapshot_tx.send(buffer).await;
+ _ = grab_buffer_snapshot_tx
+ .send((buffer, LineHint::default()))
+ .await;
}
let (find_all_matches_tx, find_all_matches_rx) =
@@ -196,7 +199,10 @@ impl Search {
let fill_requests = cx
.background_spawn(async move {
for buffer in open_buffers {
- if let Err(_) = grab_buffer_snapshot_tx.send(buffer).await {
+ if let Err(_) = grab_buffer_snapshot_tx
+ .send((buffer, LineHint::default()))
+ .await
+ {
return;
}
}
@@ -304,8 +310,9 @@ impl Search {
let forward_buffers = cx.background_spawn(async move {
while let Ok(buffer) = buffer_rx.recv().await {
- let _ =
- grab_buffer_snapshot_tx.send(buffer.await?).await;
+ let _ = grab_buffer_snapshot_tx
+ .send((buffer.await?, LineHint::default()))
+ .await;
}
anyhow::Ok(())
});
@@ -377,7 +384,6 @@ impl Search {
)
} else {
drop(find_all_matches_tx);
-
None
};
let ensure_matches_are_reported_in_order = if should_find_all_matches {
@@ -387,6 +393,7 @@ impl Search {
)
} else {
drop(tx);
+
None
};
@@ -412,7 +419,7 @@ impl Search {
worktrees: Vec<Entity<Worktree>>,
query: Arc<SearchQuery>,
tx: Sender<InputPath>,
- results: Sender<oneshot::Receiver<ProjectPath>>,
+ results: Sender<oneshot::Receiver<(ProjectPath, LineHint)>>,
results_tx: Sender<SearchResult>,
) -> impl AsyncFnOnce(&mut AsyncApp) {
async move |cx| {
@@ -495,18 +502,22 @@ impl Search {
}
async fn maintain_sorted_search_results(
- rx: Receiver<oneshot::Receiver<ProjectPath>>,
- paths_for_full_scan: Sender<ProjectPath>,
+ rx: Receiver<oneshot::Receiver<(ProjectPath, LineHint)>>,
+ paths_for_full_scan: Sender<(ProjectPath, LineHint)>,
limit: usize,
) {
let mut rx = pin!(rx);
let mut matched = 0;
while let Some(mut next_path_result) = rx.next().await {
- let Some(successful_path) = next_path_result.next().await else {
+ let Some((successful_path, line_hint)) = next_path_result.next().await else {
// This file did not produce a match, hence skip it.
continue;
};
- if paths_for_full_scan.send(successful_path).await.is_err() {
+ if paths_for_full_scan
+ .send((successful_path, line_hint))
+ .await
+ .is_err()
+ {
return;
};
matched += 1;
@@ -519,23 +530,26 @@ impl Search {
/// Background workers cannot open buffers by themselves, hence main thread will do it on their behalf.
async fn open_buffers(
buffer_store: Entity<BufferStore>,
- rx: Receiver<ProjectPath>,
- find_all_matches_tx: Sender<Entity<Buffer>>,
+ rx: Receiver<(ProjectPath, LineHint)>,
+ find_all_matches_tx: Sender<(Entity<Buffer>, LineHint)>,
mut cx: AsyncApp,
) {
let mut rx = pin!(rx.ready_chunks(64));
_ = maybe!(async move {
while let Some(requested_paths) = rx.next().await {
+ let line_hints: Vec<LineHint> =
+ requested_paths.iter().map(|(_, line)| *line).collect();
let mut buffers = buffer_store.update(&mut cx, |this, cx| {
requested_paths
.into_iter()
- .map(|path| this.open_buffer(path, cx))
+ .map(|(path, _)| this.open_buffer(path, cx))
.collect::<FuturesOrdered<_>>()
});
-
+ let mut line_hints = line_hints.into_iter();
while let Some(buffer) = buffers.next().await {
+ let line_hint = line_hints.next().unwrap_or(LineHint::default());
if let Some(buffer) = buffer.log_err() {
- find_all_matches_tx.send(buffer).await?;
+ find_all_matches_tx.send((buffer, line_hint)).await?;
}
}
}
@@ -545,20 +559,23 @@ impl Search {
}
async fn grab_buffer_snapshots(
- rx: Receiver<Entity<Buffer>>,
- find_all_matches_tx: Sender<(
- Entity<Buffer>,
- BufferSnapshot,
- oneshot::Sender<(Entity<Buffer>, Vec<Range<language::Anchor>>)>,
- )>,
+ rx: Receiver<(Entity<Buffer>, LineHint)>,
+ find_all_matches_tx: Sender<FindAllMatchesRequest>,
results: Sender<oneshot::Receiver<(Entity<Buffer>, Vec<Range<language::Anchor>>)>>,
mut cx: AsyncApp,
) {
_ = maybe!(async move {
- while let Ok(buffer) = rx.recv().await {
+ while let Ok((buffer, line_hint)) = rx.recv().await {
let snapshot = buffer.read_with(&mut cx, |this, _| this.snapshot());
let (tx, rx) = oneshot::channel();
- find_all_matches_tx.send((buffer, snapshot, tx)).await?;
+ find_all_matches_tx
+ .send(FindAllMatchesRequest {
+ buffer,
+ snapshot,
+ line_hint,
+ report_matches: tx,
+ })
+ .await?;
results.send(rx).await?;
}
debug_assert!(rx.is_empty());
@@ -645,11 +662,7 @@ struct Worker {
candidates: FindSearchCandidates,
/// Ok, we're back in background: run full scan & find all matches in a given buffer snapshot.
/// Then, when you're done, share them via the channel you were given.
- find_all_matches_rx: Receiver<(
- Entity<Buffer>,
- BufferSnapshot,
- oneshot::Sender<(Entity<Buffer>, Vec<Range<language::Anchor>>)>,
- )>,
+ find_all_matches_rx: Receiver<FindAllMatchesRequest>,
}
impl Worker {
@@ -730,20 +743,28 @@ struct RequestHandler<'worker> {
}
impl RequestHandler<'_> {
- async fn handle_find_all_matches(
- &self,
- (buffer, snapshot, mut report_matches): (
- Entity<Buffer>,
- BufferSnapshot,
- oneshot::Sender<(Entity<Buffer>, Vec<Range<language::Anchor>>)>,
- ),
- ) {
+ async fn handle_find_all_matches(&self, request: FindAllMatchesRequest) {
+ let FindAllMatchesRequest {
+ buffer,
+ snapshot,
+ line_hint,
+ mut report_matches,
+ } = request;
+ let range_offset = if line_hint > 0 {
+ snapshot.point_to_offset(Point::new(line_hint, 0))
+ } else {
+ 0
+ };
+ let subrange = (range_offset > 0).then(|| range_offset..snapshot.len());
let ranges = self
.query
- .search(&snapshot, None)
+ .search(&snapshot, subrange)
.await
.iter()
- .map(|range| snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end))
+ .map(|range| {
+ snapshot.anchor_before(range.start + range_offset)
+ ..snapshot.anchor_after(range.end + range_offset)
+ })
.collect::<Vec<_>>();
_ = report_matches.send((buffer, ranges)).await;
@@ -777,9 +798,9 @@ impl RequestHandler<'_> {
return Ok(());
}
- if self.query.detect(file).await.unwrap_or(false) {
+ if let Some(line_hint) = self.query.detect(file).await.ok().flatten() {
// Yes, we should scan the whole file.
- entry.should_scan_tx.send(entry.path).await?;
+ entry.should_scan_tx.send((entry.path, line_hint)).await?;
}
Ok(())
}
@@ -816,10 +837,13 @@ impl RequestHandler<'_> {
// The buffer is already in memory and that's the version we want to scan;
// hence skip the dilly-dally and look for all matches straight away.
should_scan_tx
- .send(ProjectPath {
- worktree_id: snapshot.id(),
- path: entry.path.clone(),
- })
+ .send((
+ ProjectPath {
+ worktree_id: snapshot.id(),
+ path: entry.path.clone(),
+ },
+ LineHint::default(),
+ ))
.await?;
} else {
self.confirm_contents_will_match_tx
@@ -843,13 +867,20 @@ impl RequestHandler<'_> {
struct InputPath {
entry: Entry,
snapshot: Snapshot,
- should_scan_tx: oneshot::Sender<ProjectPath>,
+ should_scan_tx: oneshot::Sender<(ProjectPath, LineHint)>,
}
struct MatchingEntry {
worktree_root: Arc<Path>,
path: ProjectPath,
- should_scan_tx: oneshot::Sender<ProjectPath>,
+ should_scan_tx: oneshot::Sender<(ProjectPath, LineHint)>,
+}
+
+struct FindAllMatchesRequest {
+ buffer: Entity<Buffer>,
+ snapshot: BufferSnapshot,
+ line_hint: LineHint,
+ report_matches: oneshot::Sender<(Entity<Buffer>, Vec<Range<language::Anchor>>)>,
}
/// This struct encapsulates the logic to decide whether a given gitignored directory should be
diff --git a/crates/project/src/search.rs b/crates/project/src/search.rs
index 316bb673a9a..c2804f853a3 100644
--- a/crates/project/src/search.rs
+++ b/crates/project/src/search.rs
@@ -1,5 +1,5 @@
use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
-use anyhow::Result;
+use anyhow::{Ok, Result};
use client::proto;
use fancy_regex::{Captures, Regex, RegexBuilder};
use gpui::Entity;
@@ -45,6 +45,8 @@ pub struct SearchInputs {
buffers: Option<Vec<Entity<Buffer>>>,
}
+pub type LineHint = u32;
+
impl SearchInputs {
pub fn as_str(&self) -> &str {
self.query.as_ref()
@@ -390,10 +392,10 @@ impl SearchQuery {
pub(crate) async fn detect(
&self,
mut reader: BufReader<Box<dyn Read + Send + Sync>>,
- ) -> Result<bool> {
+ ) -> Result<Option<LineHint>> {
let query_str = self.as_str();
if query_str.is_empty() {
- return Ok(false);
+ return Ok(None);
}
// Yield from this function every 20KB scanned.
@@ -405,12 +407,17 @@ impl SearchQuery {
if query_str.contains('\n') {
reader.read_to_string(&mut text)?;
text::LineEnding::normalize(&mut text);
- Ok(search.is_match(&text))
+ if search.is_match(&text) {
+ Ok(Some(LineHint::default()))
+ } else {
+ Ok(None)
+ }
} else {
let mut bytes_read = 0;
+ let mut line_number: LineHint = LineHint::default();
while reader.read_line(&mut text)? > 0 {
if search.is_match(&text) {
- return Ok(true);
+ return Ok(Some(line_number));
}
bytes_read += text.len();
if bytes_read >= YIELD_THRESHOLD {
@@ -418,8 +425,9 @@ impl SearchQuery {
smol::future::yield_now().await;
}
text.clear();
+ line_number += 1;
}
- Ok(false)
+ Ok(None)
}
}
Self::Regex {
@@ -429,12 +437,17 @@ impl SearchQuery {
if *multiline {
reader.read_to_string(&mut text)?;
text::LineEnding::normalize(&mut text);
- Ok(regex.is_match(&text)?)
+ if regex.is_match(&text)? {
+ Ok(Some(LineHint::default()))
+ } else {
+ Ok(None)
+ }
} else {
let mut bytes_read = 0;
+ let mut line_number: LineHint = LineHint::default();
while reader.read_line(&mut text)? > 0 {
if regex.is_match(&text)? {
- return Ok(true);
+ return Ok(Some(line_number));
}
bytes_read += text.len();
if bytes_read >= YIELD_THRESHOLD {
@@ -442,8 +455,9 @@ impl SearchQuery {
smol::future::yield_now().await;
}
text.clear();
+ line_number += 1;
}
- Ok(false)
+ Ok(None)
}
}
}
@@ -556,7 +570,7 @@ impl SearchQuery {
yield_now().await;
}
- if let Ok(mat) = mat {
+ if let std::result::Result::Ok(mat) = mat {
matches.push(mat.start()..mat.end());
}
}
diff --git a/crates/remote_server/src/headless_project.rs b/crates/remote_server/src/headless_project.rs
index c4897cd4d05..e5b2ef47dea 100644
--- a/crates/remote_server/src/headless_project.rs
+++ b/crates/remote_server/src/headless_project.rs
@@ -1107,7 +1107,7 @@ impl HeadlessProject {
}
});
- while let Some(buffer) = new_matches.next().await {
+ while let Some((buffer, _)) = new_matches.next().await {
let _ = buffer_store
.update(cx, |this, cx| {
this.create_buffer_for_peer(&buffer, REMOTE_SERVER_PEER_ID, cx)