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

printer: implement --max-columns-preview-before #2683

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
85 changes: 85 additions & 0 deletions crates/core/flags/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub(super) const FLAGS: &[&dyn Flag] = &[
&LineRegexp,
&MaxColumns,
&MaxColumnsPreview,
&MaxColumnsPreviewBefore,
&MaxCount,
&MaxDepth,
&MaxFilesize,
Expand All @@ -118,6 +119,7 @@ pub(super) const FLAGS: &[&dyn Flag] = &[
&Passthru,
&PCRE2,
&PCRE2Version,
&PerMatch,
&Pre,
&PreGlob,
&Pretty,
Expand Down Expand Up @@ -3782,6 +3784,53 @@ fn test_max_columns_preview() {
assert_eq!(false, args.max_columns_preview);
}

/// --max-columns-preview-before
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better name?

#[derive(Debug)]
struct MaxColumnsPreviewBefore;

impl Flag for MaxColumnsPreviewBefore {
fn is_switch(&self) -> bool {
false
}
fn name_long(&self) -> &'static str {
"max-columns-preview-before"
}
fn doc_variable(&self) -> Option<&'static str> {
Some("NUM")
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"Columns to be printed before the first match in the preview."
}
fn doc_long(&self) -> &'static str {
r"
Set the maximum amount of columns before the first match should be printed in
the preview.
.sp
When the \flag{max-columns-preview} flag is used, ripgrep will print a preview
start from the beginning of the line. When this flag is combined with
\flag{max-columns-preview}, the preview will start \fINUM\fP columns before the
first match.
.sp
If the \flag{max-columns-preview} flag is not set, then this has no effect.
"
}

fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
args.max_columns_preview_before =
Some(convert::u64(&v.unwrap_value())?);
Ok(())
}
}

#[cfg(test)]
#[test]
fn test_max_columns_preview_before() {
// TODO
}

/// -m/--max-count
#[derive(Debug)]
struct MaxCount;
Expand Down Expand Up @@ -5349,6 +5398,42 @@ fn test_pcre2_version() {
assert_eq!(Some(SpecialMode::VersionPCRE2), args.special);
}

/// --per-match
Copy link
Author

@kkocdko kkocdko Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expose this to cli.

#[derive(Debug)]
struct PerMatch;

impl Flag for PerMatch {
fn is_switch(&self) -> bool {
true
}
fn name_long(&self) -> &'static str {
"per-match"
}
fn doc_category(&self) -> Category {
Category::Output
}
fn doc_short(&self) -> &'static str {
r"TODO"
}
fn doc_long(&self) -> &'static str {
r"
TODO
"
}

fn update(&self, v: FlagValue, args: &mut LowArgs) -> anyhow::Result<()> {
assert!(v.unwrap_switch(), "--per-match does not have a negation");
args.per_match = true;
Ok(())
}
}

#[cfg(test)]
#[test]
fn test_per_match() {
// TODO
}

/// --pre
#[derive(Debug)]
struct Pre;
Expand Down
7 changes: 6 additions & 1 deletion crates/core/flags/hiargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub(crate) struct HiArgs {
line_number: bool,
max_columns: Option<u64>,
max_columns_preview: bool,
max_columns_preview_before: Option<u64>,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can combine this to max_columns_preview: Option<u64>, but will cause breaking changes. Is this acceptable?

max_count: Option<u64>,
max_depth: Option<usize>,
max_filesize: Option<u64>,
Expand All @@ -87,6 +88,7 @@ pub(crate) struct HiArgs {
paths: Paths,
path_terminator: Option<u8>,
patterns: Patterns,
per_match: bool,
pre: Option<PathBuf>,
pre_globs: ignore::overrides::Override,
quiet: bool,
Expand Down Expand Up @@ -281,6 +283,7 @@ impl HiArgs {
line_number,
max_columns: low.max_columns,
max_columns_preview: low.max_columns_preview,
max_columns_preview_before: low.max_columns_preview_before,
max_count: low.max_count,
max_depth: low.max_depth,
max_filesize: low.max_filesize,
Expand All @@ -301,6 +304,7 @@ impl HiArgs {
globs,
path_separator: low.path_separator,
path_terminator,
per_match: low.per_match,
pre: low.pre,
pre_globs,
quiet: low.quiet,
Expand Down Expand Up @@ -606,13 +610,14 @@ impl HiArgs {
.heading(self.heading)
.hyperlink(self.hyperlink_config.clone())
.max_columns_preview(self.max_columns_preview)
.max_columns_preview_before(self.max_columns_preview_before)
.max_columns(self.max_columns)
.max_matches(self.max_count)
.only_matching(self.only_matching)
.path(self.with_filename)
.path_terminator(self.path_terminator.clone())
.per_match_one_line(true)
.per_match(self.vimgrep)
.per_match(self.per_match || self.vimgrep)
.replacement(self.replace.clone().map(|r| r.into()))
.separator_context(self.context_separator.clone().into_bytes())
.separator_field_context(
Expand Down
2 changes: 2 additions & 0 deletions crates/core/flags/lowargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub(crate) struct LowArgs {
pub(crate) logging: Option<LoggingMode>,
pub(crate) max_columns: Option<u64>,
pub(crate) max_columns_preview: bool,
pub(crate) max_columns_preview_before: Option<u64>,
pub(crate) max_count: Option<u64>,
pub(crate) max_depth: Option<usize>,
pub(crate) max_filesize: Option<u64>,
Expand All @@ -92,6 +93,7 @@ pub(crate) struct LowArgs {
pub(crate) one_file_system: bool,
pub(crate) only_matching: bool,
pub(crate) path_separator: Option<u8>,
pub(crate) per_match: bool,
pub(crate) pre: Option<PathBuf>,
pub(crate) pre_glob: Vec<String>,
pub(crate) quiet: bool,
Expand Down
22 changes: 22 additions & 0 deletions crates/printer/src/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct Config {
replacement: Arc<Option<Vec<u8>>>,
max_columns: Option<u64>,
max_columns_preview: bool,
max_columns_preview_before: Option<u64>,
max_matches: Option<u64>,
column: bool,
byte_offset: bool,
Expand All @@ -72,6 +73,7 @@ impl Default for Config {
replacement: Arc::new(None),
max_columns: None,
max_columns_preview: false,
max_columns_preview_before: None,
max_matches: None,
column: false,
byte_offset: false,
Expand Down Expand Up @@ -326,6 +328,19 @@ impl StandardBuilder {
self
}

/// Set the maximum amount of columns before the first match should be
/// printed in the preview.
///
/// When this is not set (the default), the preview will print from the
/// beginning of the line.
pub fn max_columns_preview_before(
&mut self,
limit: Option<u64>,
) -> &mut StandardBuilder {
self.config.max_columns_preview_before = limit;
self
}

/// Set the maximum amount of matching lines that are printed.
///
/// If multi line search is enabled and a match spans multiple lines, then
Expand Down Expand Up @@ -1360,6 +1375,13 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> {
) -> io::Result<()> {
if self.config().max_columns_preview {
let original = line;
if let Some(p) = self.config().max_columns_preview_before {
let start = matches[0].start().saturating_sub(p as usize);
if start != 0 {
self.write(b"[... omitted start of long line] ")?;
line = line.with_start(start);
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare to the full --column-context-before + --column-context-after support that needs to modify everywhere, the implementation code here is quiet simple.

let end = bytes[line]
.grapheme_indices()
.map(|(_, end, _)| end)
Expand Down
Loading