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.

Respect regex assertions more faithfully with search (#62158)

Piotr Osiewicz1ade7854f224
diff --git a/crates/project/src/project_search.rs b/crates/project/src/project_search.rs
index dd1d4575fe8..8b8bdac3010 100644
--- a/crates/project/src/project_search.rs
+++ b/crates/project/src/project_search.rs
@@ -27,7 +27,7 @@ use worktree::{Entry, ProjectEntryId, Snapshot, Worktree, WorktreeSettings};
use crate::{
Project, ProjectItem, ProjectPath, RemotelyCreatedModels,
buffer_store::BufferStore,
- search::{LineHint, SearchQuery, SearchResult},
+ search::{MatchPositionHint, SearchQuery, SearchResult},
worktree_store::WorktreeStore,
};
@@ -61,7 +61,7 @@ enum SearchKind {
#[must_use]
pub struct SearchResultsHandle {
results: Receiver<SearchResult>,
- matching_buffers: Receiver<(Entity<Buffer>, LineHint)>,
+ matching_buffers: Receiver<(Entity<Buffer>, MatchPositionHint)>,
trigger_search: Box<dyn FnOnce(&mut App) -> Task<()> + Send + Sync>,
}
@@ -76,7 +76,10 @@ impl SearchResultsHandle {
rx: self.results,
}
}
- pub fn matching_buffers(self, cx: &mut App) -> SearchResults<(Entity<Buffer>, LineHint)> {
+ pub fn matching_buffers(
+ self,
+ cx: &mut App,
+ ) -> SearchResults<(Entity<Buffer>, MatchPositionHint)> {
SearchResults {
task_handle: (self.trigger_search)(cx),
rx: self.matching_buffers,
@@ -180,13 +183,13 @@ impl Search {
let executor = cx.background_executor().clone();
let (tx, rx) = unbounded();
let (grab_buffer_snapshot_tx, grab_buffer_snapshot_rx) =
- unbounded::<(Entity<Buffer>, LineHint)>();
+ unbounded::<(Entity<Buffer>, MatchPositionHint)>();
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, LineHint::default()))
+ .send((buffer, MatchPositionHint::default()))
.await;
}
@@ -200,7 +203,7 @@ impl Search {
.background_spawn(async move {
for buffer in open_buffers {
if let Err(_) = grab_buffer_snapshot_tx
- .send((buffer, LineHint::default()))
+ .send((buffer, MatchPositionHint::default()))
.await
{
return;
@@ -311,7 +314,7 @@ 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?, LineHint::default()))
+ .send((buffer.await?, MatchPositionHint::default()))
.await;
}
anyhow::Ok(())
@@ -419,7 +422,7 @@ impl Search {
worktrees: Vec<Entity<Worktree>>,
query: Arc<SearchQuery>,
tx: Sender<InputPath>,
- results: Sender<oneshot::Receiver<(ProjectPath, LineHint)>>,
+ results: Sender<oneshot::Receiver<(ProjectPath, MatchPositionHint)>>,
results_tx: Sender<SearchResult>,
) -> impl AsyncFnOnce(&mut AsyncApp) {
async move |cx| {
@@ -502,8 +505,8 @@ impl Search {
}
async fn maintain_sorted_search_results(
- rx: Receiver<oneshot::Receiver<(ProjectPath, LineHint)>>,
- paths_for_full_scan: Sender<(ProjectPath, LineHint)>,
+ rx: Receiver<oneshot::Receiver<(ProjectPath, MatchPositionHint)>>,
+ paths_for_full_scan: Sender<(ProjectPath, MatchPositionHint)>,
limit: usize,
) {
let mut rx = pin!(rx);
@@ -530,14 +533,14 @@ 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, LineHint)>,
- find_all_matches_tx: Sender<(Entity<Buffer>, LineHint)>,
+ rx: Receiver<(ProjectPath, MatchPositionHint)>,
+ find_all_matches_tx: Sender<(Entity<Buffer>, MatchPositionHint)>,
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> =
+ let line_hints: Vec<MatchPositionHint> =
requested_paths.iter().map(|(_, line)| *line).collect();
let mut buffers = buffer_store.update(&mut cx, |this, cx| {
requested_paths
@@ -547,7 +550,7 @@ impl Search {
});
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());
+ let line_hint = line_hints.next().unwrap_or(MatchPositionHint::default());
if let Some(buffer) = buffer.log_err() {
find_all_matches_tx.send((buffer, line_hint)).await?;
}
@@ -559,7 +562,7 @@ impl Search {
}
async fn grab_buffer_snapshots(
- rx: Receiver<(Entity<Buffer>, LineHint)>,
+ rx: Receiver<(Entity<Buffer>, MatchPositionHint)>,
find_all_matches_tx: Sender<FindAllMatchesRequest>,
results: Sender<oneshot::Receiver<(Entity<Buffer>, Vec<Range<language::Anchor>>)>>,
mut cx: AsyncApp,
@@ -750,11 +753,14 @@ impl RequestHandler<'_> {
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 range_offset = match line_hint {
+ MatchPositionHint::Line(line_number) if line_number > 0 => {
+ snapshot.point_to_offset(Point::new(line_number, 0))
+ }
+ MatchPositionHint::ByteOffset(offset) => offset,
+ _ => 0,
};
+
let subrange = (range_offset > 0).then(|| range_offset..snapshot.len());
let ranges = self
.query
@@ -842,7 +848,7 @@ impl RequestHandler<'_> {
worktree_id: snapshot.id(),
path: entry.path.clone(),
},
- LineHint::default(),
+ MatchPositionHint::default(),
))
.await?;
} else {
@@ -867,19 +873,19 @@ impl RequestHandler<'_> {
struct InputPath {
entry: Entry,
snapshot: Snapshot,
- should_scan_tx: oneshot::Sender<(ProjectPath, LineHint)>,
+ should_scan_tx: oneshot::Sender<(ProjectPath, MatchPositionHint)>,
}
struct MatchingEntry {
worktree_root: Arc<Path>,
path: ProjectPath,
- should_scan_tx: oneshot::Sender<(ProjectPath, LineHint)>,
+ should_scan_tx: oneshot::Sender<(ProjectPath, MatchPositionHint)>,
}
struct FindAllMatchesRequest {
buffer: Entity<Buffer>,
snapshot: BufferSnapshot,
- line_hint: LineHint,
+ line_hint: MatchPositionHint,
report_matches: oneshot::Sender<(Entity<Buffer>, Vec<Range<language::Anchor>>)>,
}
diff --git a/crates/project/src/search.rs b/crates/project/src/search.rs
index d000d8cf297..d11f82d0091 100644
--- a/crates/project/src/search.rs
+++ b/crates/project/src/search.rs
@@ -8,6 +8,7 @@ use language::{Buffer, BufferSnapshot, CharKind};
use smol::future::yield_now;
use std::{
borrow::Cow,
+ collections::BTreeSet,
io::{BufRead, BufReader, Read},
ops::Range,
sync::{Arc, LazyLock},
@@ -45,7 +46,17 @@ pub struct SearchInputs {
buffers: Option<Vec<Entity<Buffer>>>,
}
-pub type LineHint = u32;
+#[derive(Clone, Copy, Debug)]
+pub enum MatchPositionHint {
+ Line(u32),
+ ByteOffset(usize),
+}
+
+impl Default for MatchPositionHint {
+ fn default() -> Self {
+ Self::Line(0)
+ }
+}
impl SearchInputs {
pub fn as_str(&self) -> &str {
@@ -74,7 +85,6 @@ pub enum SearchQuery {
Regex {
regex: Regex,
replacement: Option<String>,
- multiline: bool,
whole_word: bool,
case_sensitive: bool,
include_ignored: bool,
@@ -244,19 +254,14 @@ impl SearchQuery {
pattern = word_pattern
}
- let multiline = pattern.contains('\n') || pattern.contains("\\n");
- if multiline {
- pattern.insert_str(0, "(?m)");
- }
-
let regex = RegexBuilder::new(&pattern)
.case_insensitive(!case_sensitive)
+ .multi_line(true)
.crlf(true)
.build()?;
Ok(Self::Regex {
regex,
replacement: None,
- multiline,
whole_word,
case_sensitive,
include_ignored,
@@ -390,10 +395,10 @@ impl SearchQuery {
}
}
- pub(crate) async fn detect(
+ pub async fn detect(
&self,
mut reader: BufReader<Box<dyn Read + Send + Sync>>,
- ) -> Result<Option<LineHint>> {
+ ) -> Result<Option<MatchPositionHint>> {
let query_str = self.as_str();
if query_str.is_empty() {
return Ok(None);
@@ -409,16 +414,16 @@ impl SearchQuery {
reader.read_to_string(&mut text)?;
text::LineEnding::normalize(&mut text);
if search.is_match(&text) {
- Ok(Some(LineHint::default()))
+ Ok(Some(MatchPositionHint::default()))
} else {
Ok(None)
}
} else {
let mut bytes_read = 0;
- let mut line_number: LineHint = LineHint::default();
+ let mut line_number = u32::default();
while reader.read_line(&mut text)? > 0 {
if search.is_match(&text) {
- return Ok(Some(line_number));
+ return Ok(Some(MatchPositionHint::Line(line_number)));
}
bytes_read += text.len();
if bytes_read >= YIELD_THRESHOLD {
@@ -431,33 +436,14 @@ impl SearchQuery {
Ok(None)
}
}
- Self::Regex {
- regex, multiline, ..
- } => {
+ Self::Regex { regex, .. } => {
let mut text = String::new();
- if *multiline {
- reader.read_to_string(&mut text)?;
- text::LineEnding::normalize(&mut text);
- if regex.is_match(&text)? {
- Ok(Some(LineHint::default()))
- } else {
- Ok(None)
- }
+
+ reader.read_to_string(&mut text)?;
+ text::LineEnding::normalize(&mut text);
+ if let Some(m) = regex.find(&text)? {
+ Ok(Some(MatchPositionHint::ByteOffset(m.start())))
} 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(Some(line_number));
- }
- bytes_read += text.len();
- if bytes_read >= YIELD_THRESHOLD {
- bytes_read = 0;
- smol::future::yield_now().await;
- }
- text.clear();
- line_number += 1;
- }
Ok(None)
}
}
@@ -562,42 +548,27 @@ impl SearchQuery {
}
Self::Regex {
- regex, multiline, ..
+ regex,
+ one_match_per_line,
+ ..
} => {
- if *multiline {
- let text = rope.to_string();
- for (ix, mat) in regex.find_iter(&text).enumerate() {
- if (ix + 1) % YIELD_INTERVAL == 0 {
- yield_now().await;
- }
-
- if let std::result::Result::Ok(mat) = mat {
- matches.push(mat.start()..mat.end());
- }
+ let text = rope.to_string();
+ let mut seen_lines = BTreeSet::default();
+ for (ix, mat) in regex.find_iter(&text).enumerate() {
+ if (ix + 1) % YIELD_INTERVAL == 0 {
+ yield_now().await;
}
- } else {
- let mut line = String::new();
- let mut line_offset = 0;
- for (chunk_ix, chunk) in rope.chunks().chain(["\n"]).enumerate() {
- if (chunk_ix + 1) % YIELD_INTERVAL == 0 {
- yield_now().await;
- }
- for (newline_ix, text) in chunk.split('\n').enumerate() {
- if newline_ix > 0 {
- for mat in regex.find_iter(&line).flatten() {
- let start = line_offset + mat.start();
- let end = line_offset + mat.end();
- matches.push(start..end);
- if self.one_match_per_line() == Some(true) {
- break;
- }
- }
-
- line_offset += line.len() + 1;
- line.clear();
- }
- line.push_str(text);
+ if let std::result::Result::Ok(mat) = mat {
+ let should_push = if *one_match_per_line {
+ // ensure that only one match per line is returned.
+ let pos = buffer.offset_to_point(mat.start());
+ seen_lines.insert(pos.row)
+ } else {
+ true
+ };
+ if should_push {
+ matches.push(mat.start()..mat.end());
}
}
}
@@ -691,19 +662,6 @@ impl SearchQuery {
}
}
- /// Whether this search should replace only one match per line, instead of
- /// all matches.
- /// Returns `None` for text searches, as only regex searches support this
- /// option.
- pub fn one_match_per_line(&self) -> Option<bool> {
- match self {
- Self::Regex {
- one_match_per_line, ..
- } => Some(*one_match_per_line),
- Self::Text { .. } => None,
- }
- }
-
pub fn search_str(&self, text: &str) -> Vec<Range<usize>> {
if self.as_str().is_empty() {
return Vec::new();
@@ -729,27 +687,9 @@ impl SearchQuery {
matches.push(mat.start()..mat.end());
}
}
- Self::Regex {
- regex,
- multiline,
- one_match_per_line,
- ..
- } => {
- if *multiline {
- for mat in regex.find_iter(text).flatten() {
- matches.push(mat.start()..mat.end());
- }
- } else {
- let mut line_offset = 0;
- for line in text.split('\n') {
- for mat in regex.find_iter(line).flatten() {
- matches.push((line_offset + mat.start())..(line_offset + mat.end()));
- if *one_match_per_line {
- break;
- }
- }
- line_offset += line.len() + 1;
- }
+ Self::Regex { regex, .. } => {
+ for mat in regex.find_iter(text).flatten() {
+ matches.push(mat.start()..mat.end());
}
}
}
diff --git a/crates/project/tests/integration/search.rs b/crates/project/tests/integration/search.rs
index 98cf7dc90ab..d961c87bceb 100644
--- a/crates/project/tests/integration/search.rs
+++ b/crates/project/tests/integration/search.rs
@@ -1,3 +1,5 @@
+use std::io::BufReader;
+
use language::Buffer;
use project::search::SearchQuery;
use text::Rope;
@@ -178,3 +180,47 @@ async fn test_multiline_regex(cx: &mut gpui::TestAppContext) {
let results = search_query.search(&snapshot, None).await;
assert_eq!(results, vec![0..6, 12..18]);
}
+
+#[gpui::test]
+async fn regex_with_eol_detects_lines() {
+ let re = SearchQuery::regex(
+ "Bool$",
+ false,
+ false,
+ false,
+ false,
+ PathMatcher::default(),
+ PathMatcher::default(),
+ false,
+ None,
+ )
+ .unwrap();
+ let input = " Bool\nsomething else";
+ let result = re
+ .detect(BufReader::new(Box::new(input.as_bytes())))
+ .await
+ .unwrap();
+ assert!(result.is_some());
+}
+
+#[gpui::test]
+async fn multi_line_regex_detects_matches() {
+ let re = SearchQuery::regex(
+ "Bool$\nbool",
+ false,
+ false,
+ false,
+ false,
+ PathMatcher::default(),
+ PathMatcher::default(),
+ false,
+ None,
+ )
+ .unwrap();
+ let input = " Bool\nbool";
+ let result = re
+ .detect(BufReader::new(Box::new(input.as_bytes())))
+ .await
+ .unwrap();
+ assert!(result.is_some());
+}