diff options
author | Andrew Gallant <jamslam@gmail.com> | 2023-11-22 16:04:26 -0500 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2023-11-25 15:03:53 -0500 |
commit | 038524a580072e9346ee9f782679618e380f4f2e (patch) | |
tree | 6a62cc085639a86ab1415ab17fe813f5e949c2d7 /crates | |
parent | 8f9557d183d91aca8f72ce8c8401bcfb221e76c6 (diff) |
printer: trim before applying max column windowing
Previously, we were applying the -M/--max-columns flag *before* triming
prefix ASCII whitespace. But this doesn't make a whole lot of sense. We
should be trimming first, but the result of trimming is ultimately what
we'll be printing and that's what -M/--max-columns should be applied to.
Fixes #2458
Diffstat (limited to 'crates')
-rw-r--r-- | crates/printer/src/standard.rs | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/crates/printer/src/standard.rs b/crates/printer/src/standard.rs index 2287b5d7..78489906 100644 --- a/crates/printer/src/standard.rs +++ b/crates/printer/src/standard.rs @@ -1100,13 +1100,14 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { let mut count = 0; let mut stepper = LineStep::new(line_term, 0, bytes.len()); while let Some((start, end)) = stepper.next(bytes) { - let line = Match::new(start, end); + let mut line = Match::new(start, end); self.write_prelude( self.sunk.absolute_byte_offset() + line.start() as u64, self.sunk.line_number().map(|n| n + count), Some(matches[0].start() as u64 + 1), )?; count += 1; + self.trim_ascii_prefix(bytes, &mut line); if self.exceeds_max_columns(&bytes[line]) { self.write_exceeded_line(bytes, line, matches, &mut midx)?; } else { @@ -1189,12 +1190,12 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { Some(m.start().saturating_sub(line.start()) as u64 + 1), )?; count += 1; + self.trim_line_terminator(bytes, &mut line); + self.trim_ascii_prefix(bytes, &mut line); if self.exceeds_max_columns(&bytes[line]) { self.write_exceeded_line(bytes, line, &[m], &mut 0)?; continue; } - self.trim_line_terminator(bytes, &mut line); - self.trim_ascii_prefix(bytes, &mut line); while !line.is_empty() { if m.end() <= line.start() { @@ -1246,6 +1247,14 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { #[inline(always)] fn write_line(&self, line: &[u8]) -> io::Result<()> { + let line = if !self.config().trim_ascii { + line + } else { + let lineterm = self.searcher.line_terminator(); + let full_range = Match::new(0, line.len()); + let range = trim_ascii_prefix(lineterm, line, full_range); + &line[range] + }; if self.exceeds_max_columns(line) { let range = Match::new(0, line.len()); self.write_exceeded_line( @@ -1255,7 +1264,8 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { &mut 0, )?; } else { - self.write_trim(line)?; + // self.write_trim(line)?; + self.write(line)?; if !self.has_line_terminator(line) { self.write_line_term()?; } @@ -1274,7 +1284,8 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { return self.write_line(bytes); } - let line = Match::new(0, bytes.len()); + let mut line = Match::new(0, bytes.len()); + self.trim_ascii_prefix(bytes, &mut line); if self.exceeds_max_columns(bytes) { self.write_exceeded_line(bytes, line, matches, &mut 0) } else { @@ -1298,7 +1309,6 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { match_index: &mut usize, ) -> io::Result<()> { self.trim_line_terminator(bytes, &mut line); - self.trim_ascii_prefix(bytes, &mut line); if matches.is_empty() { self.write(&bytes[line])?; return Ok(()); @@ -1535,15 +1545,6 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> { Ok(()) } - fn write_trim(&self, buf: &[u8]) -> io::Result<()> { - if !self.config().trim_ascii { - return self.write(buf); - } - let mut range = Match::new(0, buf.len()); - self.trim_ascii_prefix(buf, &mut range); - self.write(&buf[range]) - } - fn write(&self, buf: &[u8]) -> io::Result<()> { self.wtr().borrow_mut().write_all(buf) } |