Support `target(target_type, name)` in `gn edit` Change-Id: Ic0b136bb5ddebb969411bc9ff56161bd6a6a6964 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/26300 Reviewed-by: Takuto Ikuta <tikuta@google.com> Commit-Queue: Matt Stark <msta@google.com>
diff --git a/src/gn/build_file_editor.cc b/src/gn/build_file_editor.cc index cf63da2..3e3ec0e 100644 --- a/src/gn/build_file_editor.cc +++ b/src/gn/build_file_editor.cc
@@ -12,6 +12,7 @@ #include "gn/command_format.h" #include "gn/edit_subcommands.h" #include "gn/filesystem_utils.h" +#include "gn/functions.h" #include "gn/input_file.h" #include "gn/label.h" #include "gn/loader.h" @@ -572,10 +573,16 @@ tree_root_.get(), [this, &filter](TreeNode& node_ref) -> std::optional<EditTarget> { if (auto* func = node_ref->AsFunctionCallMut()) { - if (func->block() && func->args() && - func->args()->contents().size() == 1) { - if (auto name = - AsStringLiteral(func->args()->contents()[0].get())) { + if (func->block() && func->args()) { + std::optional<std::string> name; + const auto& args = func->args()->contents(); + if (args.size() == 1) { + name = AsStringLiteral(args[0].get()); + } else if (args.size() == 2 && + func->function().value() == functions::kTarget) { + name = AsStringLiteral(args[1].get()); + } + if (name) { EditTarget target{ .is_explicit = true, .label = Label(source_file_.GetDir(), *name),
diff --git a/src/gn/edit_command_unittest.cc b/src/gn/edit_command_unittest.cc index 31276ea..21a2472 100644 --- a/src/gn/edit_command_unittest.cc +++ b/src/gn/edit_command_unittest.cc
@@ -103,7 +103,7 @@ TEST_F(EditCommandTest, MultipleTargetsSubset) { EXPECT_SUCCESS(DoEdit("set testonly true", {"//:foo"}, R"( -executable("foo") { +target(my_target_type, "foo") { testonly = false } executable("bar") { @@ -111,7 +111,7 @@ } )"), Edited(R"( -executable("foo") { +target(my_target_type, "foo") { testonly = true }