refactor(editor): share the compdb entry query with a new CLI command
This commit is contained in:
1 parent
af735c28ff
commit
f5eb489e79
9 files changed
+221
-64
No files matched your search
+22
-63
@@ -1626,75 +1626,34 @@ std::string compdb_entries_project(const DaemonOptions &options, std::string_vie
|
||||
"compdb_entries: no compilation database is published; run compdb export first");
|
||||
}
|
||||
|
||||
auto text = Void::FileSystem::read_file(compdb_path);
|
||||
if (!text) {
|
||||
return error_response(std::format("compdb_entries: {}", text.error().format()));
|
||||
Editor::CompdbQuery query;
|
||||
query.file_filter = file_filter;
|
||||
query.limit = limit;
|
||||
auto queried = Editor::query_compdb_entries(compdb_path, query);
|
||||
if (!queried) {
|
||||
return error_response(std::format("compdb_entries: {}", queried.error().format()));
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*text);
|
||||
if (!parsed) {
|
||||
return error_response("compdb_entries: compilation database is not valid JSON");
|
||||
}
|
||||
const auto *entries = std::get_if<JsonValue::Array>(&parsed->value);
|
||||
if (entries == nullptr) {
|
||||
return error_response("compdb_entries: compilation database must be a JSON array");
|
||||
}
|
||||
|
||||
JsonValue::Array matched;
|
||||
std::size_t total = 0;
|
||||
for (const auto &entry : *entries) {
|
||||
const auto *object = std::get_if<JsonValue::Object>(&entry.value);
|
||||
if (object == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const auto file_it = object->find("file");
|
||||
if (file_it == object->end()) {
|
||||
continue;
|
||||
}
|
||||
const auto *file = std::get_if<std::string>(&file_it->second.value);
|
||||
if (file == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (!file_filter.empty() &&
|
||||
!std::string_view{*file}.ends_with(std::string_view{file_filter})) {
|
||||
continue;
|
||||
}
|
||||
++total;
|
||||
if (matched.size() >= limit) {
|
||||
continue;
|
||||
}
|
||||
for (const auto &entry : queried->entries) {
|
||||
JsonValue::Object projected;
|
||||
projected.emplace("file", JsonValue{*file});
|
||||
const auto copy_string = [&](std::string_view field_name) {
|
||||
if (const auto it = object->find(field_name); it != object->end()) {
|
||||
if (const auto *value = std::get_if<std::string>(&it->second.value)) {
|
||||
projected.emplace(std::string{field_name}, JsonValue{*value});
|
||||
}
|
||||
}
|
||||
};
|
||||
copy_string("directory");
|
||||
copy_string("output");
|
||||
copy_string("command");
|
||||
if (const auto it = object->find("arguments"); it != object->end()) {
|
||||
if (const auto *arguments = std::get_if<JsonValue::Array>(&it->second.value)) {
|
||||
JsonValue::Array bounded;
|
||||
for (const auto &argument : *arguments) {
|
||||
const auto *value = std::get_if<std::string>(&argument.value);
|
||||
if (value == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (bounded.size() >= kMaxCompdbArguments) {
|
||||
projected.emplace("arguments_truncated", JsonValue{true});
|
||||
break;
|
||||
}
|
||||
bounded.emplace_back(value->size() > kMaxCompdbArgumentBytes
|
||||
? value->substr(0, kMaxCompdbArgumentBytes)
|
||||
: *value);
|
||||
}
|
||||
projected.emplace("arguments", JsonValue{std::move(bounded)});
|
||||
}
|
||||
projected.emplace("file", JsonValue{entry.file});
|
||||
if (!entry.directory.empty()) {
|
||||
projected.emplace("directory", JsonValue{entry.directory});
|
||||
}
|
||||
if (!entry.output.empty()) {
|
||||
projected.emplace("output", JsonValue{entry.output});
|
||||
}
|
||||
if (!entry.command.empty()) {
|
||||
projected.emplace("command", JsonValue{entry.command});
|
||||
}
|
||||
JsonValue::Array arguments;
|
||||
for (const auto &argument : entry.arguments) {
|
||||
arguments.emplace_back(argument);
|
||||
}
|
||||
projected.emplace("arguments", JsonValue{std::move(arguments)});
|
||||
matched.emplace_back(std::move(projected));
|
||||
}
|
||||
const std::size_t total = queried->total;
|
||||
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
|
||||
Reference in new issue
Block a user