Skip to content

Commit

Permalink
print: if we have a target name, print FQN for easy copy/pasting.
Browse files Browse the repository at this point in the history
The reason to print rules is often to find something specific
to then pass to bazel. Make this simple by printing the full target
name behind the filename:

```
 # bant/util/BUILD:114:1-7: //bant/util:glob-match-builder_test
```
  • Loading branch information
hzeller committed Jun 29, 2024
1 parent f3d7ef7 commit 846c066
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions bant/frontend/parsed-project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,27 @@ void PrintProject(Session &session, const BazelPattern &pattern,

query::FindTargetsAllowEmptyName(
file_content->ast, {}, [&](const query::Result &result) {
std::optional<BazelTarget> maybe_target;
if (!result.name.empty()) {
maybe_target = BazelTarget::ParseFrom(result.name, package);
}
if (!pattern.is_recursive()) {
// Need more specific check if matches.
auto self = BazelTarget::ParseFrom(result.name, package);
if (!self.has_value() || !pattern.Match(*self)) {
// If pattern requires some match, need to check now.
if (!maybe_target.has_value() || !pattern.Match(*maybe_target)) {
return;
}
}

// TODO: instead of just marking the range of the function name,
// show the range the whole function covers until closed parenthesis.
// TODO: if isatty(out), color filename gray
std::stringstream tmp_out;
if (flags.do_color) tmp_out << "\033[2;37m";
tmp_out << "# " << project.Loc(result.node->identifier()->id()) << "\n";
tmp_out << "# " << project.Loc(result.node->identifier()->id());
if (maybe_target.has_value()) { // only has value if target with name.
tmp_out << " " << *maybe_target;
}
if (flags.do_color) tmp_out << "\033[0m";
tmp_out << "\n";
PrintVisitor printer(tmp_out, regex.get(), flags.do_color);
printer.WalkNonNull(result.node);
tmp_out << "\n";
Expand Down

0 comments on commit 846c066

Please sign in to comment.