Skip to content

Commit

Permalink
Less efficient but more correct
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Aug 12, 2023
1 parent 2b5b7f8 commit 8a5eed2
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/handlers/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ impl<'a> StateMachine<'a> {
// (At the time of writing, we are in this
// arm iff we are handling `ripgrep --json`
// output.)
let (new_code, shift) = expand_tabs(&grep_line.code, &self.config.tab_cfg);
grep_line.code = new_code.into();
let (expanded_code, shift) = expand_tabs(&grep_line.code, &self.config.tab_cfg);
grep_line.code = expanded_code.into();
grep_line.submatches = Some(
submatches
.iter()
.map(|(a, b)| (a + shift, b + shift))
.collect(),
);
make_style_sections(
&grep_line.code,
&mut submatches.iter().map(|(a, b)| (a + shift, b + shift)),
&grep_line.submatches.unwrap(),
self.config.grep_match_word_style,
self.config.grep_match_line_style,
)
Expand Down Expand Up @@ -305,11 +311,17 @@ impl<'a> StateMachine<'a> {
// (At the time of writing, we are in this
// arm iff we are handling `ripgrep --json`
// output.)
let (new_code, shift) = expand_tabs(&grep_line.code, &self.config.tab_cfg);
grep_line.code = new_code.into();
let (expanded_code, shift) = expand_tabs(&grep_line.code, &self.config.tab_cfg);
grep_line.code = expanded_code.into();
grep_line.submatches = Some(
submatches
.iter()
.map(|(a, b)| (a + shift, b + shift))
.collect(),
);
make_style_sections(
&grep_line.code,
&mut submatches.iter().map(|(a, b)| (a + shift, b + shift)),
&grep_line.submatches.unwrap(),
self.config.grep_match_word_style,
self.config.grep_match_line_style,
)
Expand Down Expand Up @@ -346,13 +358,14 @@ impl<'a> StateMachine<'a> {

fn make_style_sections<'a>(
line: &'a str,
submatches: &mut dyn Iterator<Item = (usize, usize)>,
submatches: &[(usize, usize)],
match_style: Style,
non_match_style: Style,
) -> StyleSectionSpecifier<'a> {
let mut sections = Vec::new();
let mut curr = 0;
for (start, end) in submatches {
for (start_, end_) in submatches {
let (start, end) = (*start_, *end_);
if start > curr {
sections.push((non_match_style, &line[curr..start]))
};
Expand Down

0 comments on commit 8a5eed2

Please sign in to comment.