Skip to content

Commit 36ad962

Browse files
connor15mccHerringtonDarkholme
authored andcommitted
feat(scan): support context, before/after flags
fix ast-grep#1549 Add support for `--before`, `--after` | `--context` CLI flags for `sg scan`, matching the existing flags for `sg fix` When developing complex rules (often those that necessitate a yaml rule file to begin with), this context can be instrumental in evaluating the quality of a fix.
1 parent 50bf83d commit 36ad962

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

crates/cli/src/run.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,6 @@ pub struct RunArg {
116116
/// and to disable heading when piping to another program or redirected to files.
117117
#[clap(long, default_value = "auto", value_name = "WHEN")]
118118
heading: Heading,
119-
120-
// context related options
121-
/// Show NUM lines after each match.
122-
///
123-
/// It conflicts with both the -C/--context flag.
124-
#[clap(
125-
short = 'A',
126-
long,
127-
default_value = "0",
128-
conflicts_with = "context",
129-
value_name = "NUM"
130-
)]
131-
after: u16,
132-
133-
/// Show NUM lines before each match.
134-
///
135-
/// It conflicts with both the -C/--context flag.
136-
#[clap(
137-
short = 'B',
138-
long,
139-
default_value = "0",
140-
conflicts_with = "context",
141-
value_name = "NUM"
142-
)]
143-
before: u16,
144-
145-
/// Show NUM lines around each match.
146-
///
147-
/// This is equivalent to providing both the
148-
/// -B/--before and -A/--after flags with the same value.
149-
/// It conflicts with both the -B/--before and -A/--after flags.
150-
#[clap(short = 'C', long, default_value = "0", value_name = "NUM")]
151-
context: u16,
152119
}
153120

154121
impl RunArg {
@@ -170,10 +137,10 @@ impl RunArg {
170137
// Every run will include Search or Replace
171138
// Search or Replace by arguments `pattern` and `rewrite` passed from CLI
172139
pub fn run_with_pattern(arg: RunArg) -> Result<()> {
173-
let context = if arg.context != 0 {
174-
(arg.context, arg.context)
140+
let context = if arg.output.context != 0 {
141+
(arg.output.context, arg.output.context)
175142
} else {
176-
(arg.before, arg.after)
143+
(arg.output.before, arg.output.after)
177144
};
178145
if let Some(json) = arg.output.json {
179146
let printer = JSONPrinter::stdout(json).context(context);
@@ -405,10 +372,10 @@ mod test {
405372
json: None,
406373
update_all: false,
407374
tracing: Default::default(),
375+
before: 0,
376+
after: 0,
377+
context: 0,
408378
},
409-
before: 0,
410-
after: 0,
411-
context: 0,
412379
}
413380
}
414381

crates/cli/src/scan.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ pub struct ScanArg {
7474

7575
pub fn run_with_config(arg: ScanArg) -> Result<()> {
7676
register_custom_language(arg.config.clone())?;
77+
let context = if arg.output.context != 0 {
78+
(arg.output.context, arg.output.context)
79+
} else {
80+
(arg.output.before, arg.output.after)
81+
};
7782
if let Some(_format) = &arg.format {
7883
let printer = CloudPrinter::stdout();
7984
return run_scan(arg, printer);
@@ -82,7 +87,9 @@ pub fn run_with_config(arg: ScanArg) -> Result<()> {
8287
let printer = JSONPrinter::stdout(json);
8388
return run_scan(arg, printer);
8489
}
85-
let printer = ColoredPrinter::stdout(arg.output.color).style(arg.report_style);
90+
let printer = ColoredPrinter::stdout(arg.output.color)
91+
.style(arg.report_style)
92+
.context(context);
8693
let interactive = arg.output.needs_interactive();
8794
if interactive {
8895
let from_stdin = arg.input.stdin;
@@ -383,6 +390,9 @@ rule:
383390
update_all: false,
384391
color: ColorArg::Never,
385392
tracing: Default::default(),
393+
before: 0,
394+
after: 0,
395+
context: 0,
386396
},
387397
format: None,
388398
}

crates/cli/src/utils/args.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,39 @@ pub struct OutputArgs {
153153
/// tracing information outputs to stderr and does not affect the result of the search.
154154
#[clap(long, default_value = "nothing", value_name = "LEVEL")]
155155
pub tracing: Tracing,
156+
157+
// context related options
158+
/// Show NUM lines after each match.
159+
///
160+
/// It conflicts with both the -C/--context flag.
161+
#[clap(
162+
short = 'A',
163+
long,
164+
default_value = "0",
165+
conflicts_with = "context",
166+
value_name = "NUM"
167+
)]
168+
pub after: u16,
169+
170+
/// Show NUM lines before each match.
171+
///
172+
/// It conflicts with both the -C/--context flag.
173+
#[clap(
174+
short = 'B',
175+
long,
176+
default_value = "0",
177+
conflicts_with = "context",
178+
value_name = "NUM"
179+
)]
180+
pub before: u16,
181+
182+
/// Show NUM lines around each match.
183+
///
184+
/// This is equivalent to providing both the
185+
/// -B/--before and -A/--after flags with the same value.
186+
/// It conflicts with both the -B/--before and -A/--after flags.
187+
#[clap(short = 'C', long, default_value = "0", value_name = "NUM")]
188+
pub context: u16,
156189
}
157190

158191
impl OutputArgs {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy