Skip to content

Commit

Permalink
Add support for --sourcery argument
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Oct 5, 2022
1 parent 2892b77 commit 90b3d65
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -1084,6 +1084,20 @@ Flag and option. Only applicable if the pattern is a `Callable`. Indicates wheth

Flag. An intelligent version of `--ignorecase`. If the pattern does **not** contain any uppercase characters, it will act as if `--ignorecase` was specified. Otherwise it is ignored.

--sourcery
----------

Flag. Mainly intended for Raku Programming Language core developers. If specified, indicates that the pattern should be interpreted as code specifying a simple call to a subroutine, or a simple call to a method, optionally with arguments. The search result will then contain the source locations of subroutine / method that is expected to be able to handle that call.

Compatible with the `--edit`, `--vimgrep` and the implicit `per-line` option.

```bash
# edit the location(s) of the "say" sub handling a single string
$ rak --sourcery 'say "foo"' --edit
```

Requires that the [`sourcery`](https://raku.land/zef:lizmat/sourcery) module is installed.

--stats
-------

Expand Down
22 changes: 22 additions & 0 deletions doc/App-Rak.rakudoc
Expand Up @@ -1337,6 +1337,28 @@ Flag. An intelligent version of C<--ignorecase>. If the pattern does
B<not> contain any uppercase characters, it will act as if C<--ignorecase>
was specified. Otherwise it is ignored.

=head2 --sourcery

Flag. Mainly intended for Raku Programming Language core developers.
If specified, indicates that the pattern should be interpreted as code
specifying a simple call to a subroutine, or a simple call to a method,
optionally with arguments. The search result will then contain the source
locations of subroutine / method that is expected to be able to handle that
call.

Compatible with the C<--edit>, C<--vimgrep> and the implicit C<per-line>
option.

=begin code :lang<bash>

# edit the location(s) of the "say" sub handling a single string
$ rak --sourcery 'say "foo"' --edit

=end code

Requires that the L<C<sourcery>|https://raku.land/zef:lizmat/sourcery> module
is installed.

=head2 --stats

Flag. Also show statistics about the search operation after having shown
Expand Down
68 changes: 61 additions & 7 deletions lib/App/Rak.rakumod
Expand Up @@ -155,6 +155,8 @@ my $config-file := $*HOME.add('.rak-config.json');
my $GitBlameFile;
my $TextCSV;
my &edit-files;
my &sourcery;
my &sourcery-pattern;

# Variables for grouping options given
my $verbose; # process verbose
Expand Down Expand Up @@ -989,6 +991,15 @@ my sub external-execution(str $name, $value --> Nil) {
!! (%filesystem{$name} := $value);
}

# check sourcery availability
my sub check-sourcery(str $name) {
unless &edit-files {
CATCH { meh-not-installed 'sourcery', $name }
(&sourcery, &sourcery-pattern) =
"use sourcery; &sourcery, &needle".EVAL;
}
}

# check Edit::Files availability
my sub check-EditFiles(str $name) {
unless &edit-files {
Expand Down Expand Up @@ -1653,6 +1664,11 @@ my sub option-smartcase($value --> Nil) {
set-global-flag('smartcase', $value);
}

my sub option-sourcery($value --> Nil) {
check-sourcery('sourcery');
set-result-flag('sourcery', $value);
}

my sub option-stats($value --> Nil) {
set-rak-flag('stats', $value);
}
Expand Down Expand Up @@ -2098,6 +2114,13 @@ my sub action-csv-per-line(--> Nil) {
}

my sub action-edit(--> Nil) {

if %result<sourcery>:delete {
meh-for 'edit', <output-file pager result filesystem modify csv>;
edit-files sourcery $pattern.trim;
return;
}

%rak<max-matches-per-source> := $_
with %result<max-matches-per-file>:delete;
my $find := %result<find>:delete;
Expand Down Expand Up @@ -2363,14 +2386,39 @@ my sub action-per-file(--> Nil) {
my sub action-per-line(--> Nil) {
meh-for 'per-line', <csv modify>;

prepare-needle;
move-filesystem-options-to-rak;
move-result-options-to-rak;
if %result<sourcery>:delete {
meh-for 'sourcery', <filesystem>;

$pattern := $pattern.trim;
my $sourcery := sourcery $pattern;

# The default in rak already does the right thing
%rak<produce-many> := $action
if $action.defined && !($action<> =:= True);
# normalize the sourcery results
my %result;
for $sourcery -> (:key($file), :value($linenumber)) {
(%result{$file} // (%result{$file} := [])).push: $linenumber;
}

# Decide on whether it's a match by the line number for that file
my @sources = %result.keys>>.IO;
my %linenrs = @sources.map: * => 0;
$needle := -> $ {
++%linenrs{$*SOURCE} (elem) %result{$*SOURCE}
}
$pattern := sourcery-pattern $pattern; # for highlighting
%rak<sources> := @sources; # only these files
}

else {
prepare-needle;
move-filesystem-options-to-rak;

# The default in rak already does the right thing
%rak<produce-many> := $action
if $action.defined && !($action<> =:= True);

}

move-result-options-to-rak;
run-rak;
rak-results;
rak-stats;
Expand Down Expand Up @@ -2506,9 +2554,15 @@ my sub action-version(--> Nil) {
}

my sub action-vimgrep(--> Nil) {

if %result<sourcery>:delete {
meh-for 'vimgrep', <output-file pager result filesystem modify csv>;
sayer "$_.key():$_.value()::" for sourcery $pattern.trim;
return;
}

%rak<max-matches-per-source> := $_
with %result<max-matches-per-file>:delete;

meh-for 'vimgrep', <result modify csv>;

prepare-needle;
Expand Down
1 change: 1 addition & 0 deletions resources/help.txt
Expand Up @@ -134,6 +134,7 @@ Special options:
--edit[=editor] Go edit the result in an editor, (default EDITOR or vim)
--vimgrep Produce output in :vimgrep format
--checkout=string Checkout branch if pattern matches single branch name
--sourcery Treat pattern as code indicating candidates for inspection

Option management:
--save=name Translate --name to all other options specified,
Expand Down
14 changes: 14 additions & 0 deletions resources/help/special.txt
Expand Up @@ -25,3 +25,17 @@ name matches the pattern.

Take the search result as positiuons in the code to go edit from
within "vim" using the :vimgrep option.

--sourcery

Mainly intended for Raku Programming Language core developers.
If specified, indicates that the pattern should be interpreted
as code specifying a simple call to a subroutine, or a simple
call to a method, optionally with arguments. The search result
will then contain the source locations of subroutine / method
that is expected to be able to handle that call. Compatible
with the --edit, --vimgrep and the implicit per-line option.

Example:
# edit the location(s) of the "say" sub handling a single string
$ rak --sourcery 'say "foo"' --edit

0 comments on commit 90b3d65

Please sign in to comment.