Skip to content

Commit

Permalink
feat(cli): -j aka --join-language-scopes flag
Browse files Browse the repository at this point in the history
See updated README for more info
  • Loading branch information
alexpovel committed Aug 17, 2024
1 parent 3e7305b commit 2c1b9e8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Bird:
@classmethod
def from_egg(egg):
"""Create a bird from an egg."""
pass
pass # No bird here yet!


def register_bird(bird: Bird, db: Db) -> None:
Expand Down Expand Up @@ -154,6 +154,21 @@ $ cat birds.py | srgn --python 'class' --python 'doc-strings' # From earlier exa

```

No docstrings outside `class` bodies are surfaced!

The [`-j` flag](#help-output) changes this behavior: from intersecting left-to-right, to
running all queries independently and joining their results, allowing you to search
multiple ways at once:

```console
$ cat birds.py | srgn -j --python 'comments' --python 'doc-strings' 'bird[^s]'
8: """A bird!"""
19: """Create a bird from an egg."""
20: pass # No bird here yet!
24: """Registers a bird."""

```

#### Working recursively

If standard input is not given, `srgn` knows how to find relevant source files
Expand Down Expand Up @@ -1354,6 +1369,19 @@ Options (global):
The default is to return the input unchanged (without failure).

-j, --join-language-scopes
Join (logical 'OR') multiple language scopes, instead of intersecting them.
The default when multiple language scopes are given is to intersect their
scopes, left to right. For example, `--go func --go strings` will first
scope down to `func` bodies, then look for strings only within those. This
flag instead joins (in the set logic sense) all scopes. The example would
then scope any `func` bodies, and any strings, anywhere. Language scopers
can then also be given in any order.
No effect if only a single language scope is given. Also does not affect
non-language scopers (regex pattern etc.), which always intersect.

--line-numbers
Prepend line numbers to output.

Expand Down
25 changes: 23 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,17 @@ fn apply(
) -> std::result::Result<bool, ApplicationError> {
debug!("Building view.");
let mut builder = ScopedViewBuilder::new(source);
for scoper in language_scopers {
builder.explode(scoper);

if args.options.join_language_scopes {
// All at once, as a slice: hits a specific, 'joining' `impl`
builder.explode(&language_scopers);
} else {
// One by one: hits a different, 'intersecting' `impl`
for scoper in language_scopers {
builder.explode(scoper);
}
}

builder.explode(general_scoper);
let mut view = builder.build();
debug!("Done building view: {view:?}");
Expand Down Expand Up @@ -1064,6 +1072,19 @@ mod cli {
/// The default is to return the input unchanged (without failure).
#[arg(long, verbatim_doc_comment)]
pub fail_none: bool,
/// Join (logical 'OR') multiple language scopes, instead of intersecting them.
///
/// The default when multiple language scopes are given is to intersect their
/// scopes, left to right. For example, `--go func --go strings` will first
/// scope down to `func` bodies, then look for strings only within those. This
/// flag instead joins (in the set logic sense) all scopes. The example would
/// then scope any `func` bodies, and any strings, anywhere. Language scopers
/// can then also be given in any order.
///
/// No effect if only a single language scope is given. Also does not affect
/// non-language scopers (regex pattern etc.), which always intersect.
#[arg(short('j'), long, verbatim_doc_comment)]
pub join_language_scopes: bool,
/// Prepend line numbers to output.
#[arg(long, verbatim_doc_comment)]
pub line_numbers: bool,
Expand Down

0 comments on commit 2c1b9e8

Please sign in to comment.