From ef2287e5236fb3a369c96a44b3468d473cff9a79 Mon Sep 17 00:00:00 2001 From: HerrintonDarkholme <2883231+HerringtonDarkholme@users.noreply.github.com> Date: Sun, 9 Jul 2023 14:34:11 -0700 Subject: [PATCH] feat: add --no-stdin documentation fix ast-grep/ast-grep#491 --- website/guide/tooling-overview.md | 23 ++++++++++++++++++++++- website/reference/cli.md | 6 ++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/website/guide/tooling-overview.md b/website/guide/tooling-overview.md index d30d4dcb..a11067a3 100644 --- a/website/guide/tooling-overview.md +++ b/website/guide/tooling-overview.md @@ -24,7 +24,7 @@ Pressing `y` will accept the rewrite, `n` will skip it, `e` will open the file i Example: -``` +```bash sg scan --interactive ``` @@ -125,6 +125,27 @@ You can invoke sg, the command-line interface for ast-grep, as a subprocess to s * For the `run` command, you must specify the language of the StdIn code with `--lang` or `-l` flag. For example: `echo "print('Hello world')" | sg run --lang python`. This is because ast-grep cannot infer code language without file extension. * Similarly, you can only `scan` StdIn code against _one single rule_, specified by `--rule` or `-r` flag. The rule must match the language of the StdIn code. For example: `echo "print('Hello world')" | sg scan --rule "python-rule.yml"` +### Disable StdIn Mode + +Sometimes ast-grep is launched from another process, but you still need it to search files instead of parsing from StdIn. + +You can use ast-grep's command line argument `--no-stdin` to disable StdIn mode. +The environment variable `AST_GREP_NO_STDIN` will also disable StdIn mode. + + +For example, this shell script will start a live ast-grep session via fzf. Note `--no-stdin` is used here to prevernt ast-grep from searching standard input from fzf. + +```bash +SG_PREFIX="sg run --color=always --no-stdin -p " +INITIAL_QUERY="${*:-}" +: | fzf --ansi --disabled --query "$INITIAL_QUERY" \ + --bind "start:reload:$SG_PREFIX {q}" \ + --bind "change:reload:sleep 0.1; $SG_PREFIX {q} || true" \ + --delimiter : \ + --preview 'bat --color=always {1} --highlight-line {2}' \ + --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \ + --bind 'enter:become(vim {1} +{2})' +``` ## Colorful Output diff --git a/website/reference/cli.md b/website/reference/cli.md index 08e48136..19447960 100644 --- a/website/reference/cli.md +++ b/website/reference/cli.md @@ -25,11 +25,12 @@ sg run [OPTIONS] --pattern [PATHS]... | | --debug-query | Print query pattern's tree-sitter AST. Requires lang be set explicitly. | | -l| --lang `` | The language of the pattern query. ast-grep will infer the language based on file extension if this option is omitted. | | -i| --interactive | Start interactive edit session. Code rewrite only happens inside a session. | -| | --accept-all | Apply all rewrite without confirmation if true. | +| -A| --accept-all | Apply all rewrite without confirmation if true. | | | --json | Output matches in structured JSON text useful for tools like `jq`. Conflicts with interactive| | | --heading `` | Print file names before each file's matches. Default is auto: print heading for tty but not for piped output [default: auto] [possible values: always, never, auto] | | | --color `` | Controls output color [default: auto] | | | --no-ignore `` | Do not respect hidden file system or ignore files (.gitignore, .ignore, etc.). You can suppress multiple ignore files by passing `no-ignore` multiple times [possible values: hidden, dot, exclude, global, parent, vcs] | +| | --no-stdin | Disable search code from StdIn. See [link](/guide/tooling-overview.html#disable-stdin-mode) | |-h | --help | Print help | ## scan @@ -54,8 +55,9 @@ sg scan [OPTIONS] [PATHS]... | | --color ``|Controls output color [default: auto]| | | --report-style `` | [default: rich] [possible values: rich, medium, short] | | --json |Output matches in structured JSON text. This is useful for tools like jq. Conflicts with color and report-style.| -| | --accept-all | Apply all rewrite without confirmation | +| -A | --accept-all | Apply all rewrite without confirmation | | | --no-ignore `` | Do not respect ignore files. You can suppress multiple ignore files by passing `no-ignore` multiple times [possible values: hidden, dot, exclude, global, parent, vcs] | +| | --no-stdin | Disable search code from StdIn. See [link](/guide/tooling-overview.html#disable-stdin-mode) | | -h| --help|Print help| ## test