zephyrproject-rtos / west

West, Zephyr's meta-tool
https://docs.zephyrproject.org/latest/guides/west/index.html
Apache License 2.0
214 stars 116 forks source link

Feature request: Ability for `west grep` to output relative or absolute paths #714

Open jhedberg opened 1 week ago

jhedberg commented 1 week ago

Currently, west grep groups the output per-module, having a "header" line for each module and then results relative to that module's root directory. This may be fine for manual/human processing of the output, but it makes it more difficult to integrate it with an editor (to automatically be able to jump to the file/line in question) or even for a human to just copy-paste the full file location to the command line.

Current output:

❯ west grep lfs_format
=== manifest (zephyr):
subsys/fs/littlefs_fs.c:            ret = lfs_format(&fs->lfs, &fs->cfg);
subsys/fs/littlefs_fs.c:    ret = lfs_format(&fs->lfs, &fs->cfg);
=== littlefs (modules/fs/littlefs):
README.md:        lfs_format(&lfs, &cfg);
benches/bench_dir.toml:    lfs_format(&lfs, cfg) => 0;

Desired output:

❯ west topdir
/Users/johedber/src/zephyr
❯ pwd
/Users/johedber/src/zephyr/zephyr
❯ west grep --relative lfs_format
subsys/fs/littlefs_fs.c:            ret = lfs_format(&fs->lfs, &fs->cfg);
subsys/fs/littlefs_fs.c:    ret = lfs_format(&fs->lfs, &fs->cfg);
../modules/fs/littlefs/README.md:        lfs_format(&lfs, &cfg);
../modules/fs/littlefs/benches/bench_dir.toml:    lfs_format(&lfs, cfg) => 0;

Alternative (or in addition to the above):

❯ west topdir
/Users/johedber/src/zephyr
❯ pwd
/Users/johedber/src/zephyr/zephyr
❯ west grep --absolute lfs_format
/Users/johedber/src/zephyr/zephyr/subsys/fs/littlefs_fs.c:          ret = lfs_format(&fs->lfs, &fs->cfg);
/Users/johedber/src/zephyr/zephyr/subsys/fs/littlefs_fs.c:  ret = lfs_format(&fs->lfs, &fs->cfg);
/Users/johedber/src/zephyr/modules/fs/littlefs/README.md:        lfs_format(&lfs, &cfg);
/Users/johedber/src/zephyr/modules/fs/littlefs/benches/bench_dir.toml:    lfs_format(&lfs, cfg) => 0;
jhedberg commented 1 week ago

Note to self: The editor integration is, at least in the short term, probably easiest done by avoiding west grep and instead using ripgrep as follows:

rg --vimgrep $(west topdir) -e <pattern>