Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find multiple matches on the same line. #279

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,25 +663,28 @@
}

/// Try to find a query, this is called after pressing Ctrl-F and for each key that is pressed.
/// `last_match` is the last row that was matched, `forward` indicates whether to search forward
/// or backward. Returns the row of a new match, or `None` if the search was unsuccessful.
/// `q` represents the query, `lst` is the last position that was matched, `fw` indicates
/// whether to search forward or backward. Returns the position of a new match, or `None`
/// if the search was unsuccessful.
#[allow(clippy::trivially_copy_pass_by_ref)] // This Clippy recommendation is only relevant on 32 bit platforms.
fn find(&mut self, query: &str, last_match: &Option<usize>, forward: bool) -> Option<usize> {
fn find(&mut self, q: &str, lst: &Option<(usize, usize)>, fw: bool) -> Option<(usize, usize)> {

Check warning on line 670 in src/editor.rs

View check run for this annotation

Codecov / codecov/patch

src/editor.rs#L670

Added line #L670 was not covered by tests
let num_rows = self.rows.len();
let mut current = last_match.unwrap_or_else(|| num_rows.saturating_sub(1));
// TODO: Handle multiple matches per line
for _ in 0..num_rows {
current = (current + if forward { 1 } else { num_rows - 1 }) % num_rows;
let row = &mut self.rows[current];
if let Some(cx) = slice_find(&row.chars, query.as_bytes()) {
// self.cursor.coff: Try to reset the column offset; if the match is after the offset, this
// will be updated in self.cursor.scroll() so that the result is visible
(self.cursor.x, self.cursor.y, self.cursor.coff) = (cx, current, 0);
let rx = row.cx2rx[cx];
row.match_segment = Some(rx..rx + query.len());
return Some(current);
let mut cur = lst.unwrap_or_else(|| (usize::MAX, num_rows.saturating_sub(1)));
while {

Check warning on line 673 in src/editor.rs

View check run for this annotation

Codecov / codecov/patch

src/editor.rs#L672-L673

Added lines #L672 - L673 were not covered by tests
// TODO: add find backwards on the same line

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
let row = &mut self.rows[cur.1];
let slice = if cur.0 == usize::MAX { &row.chars[..] } else { &row.chars[cur.0 + 1..] };
if let Some(cx) = slice_find(slice, q.as_bytes()) {
cur.0 = if cur.0 == usize::MAX { cx } else { cur.0 + cx + 1 };
(self.cursor.x, self.cursor.y, self.cursor.coff) = (cur.0, cur.1, 0);
let rx = row.cx2rx[cur.0];
row.match_segment = Some(rx..rx + q.len());
return Some(cur);

Check warning on line 682 in src/editor.rs

View check run for this annotation

Codecov / codecov/patch

src/editor.rs#L675-L682

Added lines #L675 - L682 were not covered by tests
}
}
cur = (usize::MAX, (cur.1 + if fw { 1 } else { num_rows - 1 }) % num_rows);

Check warning on line 684 in src/editor.rs

View check run for this annotation

Codecov / codecov/patch

src/editor.rs#L684

Added line #L684 was not covered by tests
// if it wrapped back to the starting line
cur.1 != lst.map_or(num_rows.saturating_sub(1), |lst| lst.1)

Check warning on line 686 in src/editor.rs

View check run for this annotation

Codecov / codecov/patch

src/editor.rs#L686

Added line #L686 was not covered by tests
} {}
None
}

Expand Down Expand Up @@ -736,7 +739,7 @@
/// Save(prompt buffer)
Save(String),
/// Find(prompt buffer, saved cursor state, last match)
Find(String, CursorState, Option<usize>),
Find(String, CursorState, Option<(usize, usize)>),
/// GoTo(prompt buffer)
GoTo(String),
/// Execute(prompt buffer)
Expand Down Expand Up @@ -766,7 +769,7 @@
PromptState::Completed(file_name) => ed.save_as(file_name)?,
},
Self::Find(b, saved_cursor, last_match) => {
if let Some(row_idx) = last_match {
if let Some((_, row_idx)) = last_match {

Check warning on line 772 in src/editor.rs

View check run for this annotation

Codecov / codecov/patch

src/editor.rs#L772

Added line #L772 was not covered by tests
ed.rows[row_idx].match_segment = None;
}
match process_prompt_keypress(b, key) {
Expand Down
Loading