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
@@ -1043,6 +1043,85 @@ generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &opt
|
||||
return report;
|
||||
}
|
||||
|
||||
Void::Result::Result<CompdbQueryResult, Void::Result::Error>
|
||||
query_compdb_entries(const std::filesystem::path &compdb_path, const CompdbQuery &query) {
|
||||
constexpr std::size_t kMaxArguments = 256;
|
||||
constexpr std::size_t kMaxArgumentBytes = 1024;
|
||||
|
||||
auto text = Void::FileSystem::read_file(compdb_path);
|
||||
if (!text) {
|
||||
return std::unexpected{text.error()};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*text);
|
||||
if (!parsed) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("compilation database '{}' is not valid JSON", compdb_path.string()))};
|
||||
}
|
||||
const auto *entries = std::get_if<Void::Json::Strict::Value::Array>(&parsed->value);
|
||||
if (!entries) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
"compilation database must be a JSON array")};
|
||||
}
|
||||
|
||||
CompdbQueryResult result;
|
||||
result.compdb_path = compdb_path;
|
||||
for (const auto &entry : *entries) {
|
||||
const auto *object = std::get_if<Void::Json::Strict::Value::Object>(&entry.value);
|
||||
if (!object) {
|
||||
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 (!query.file_filter.empty() &&
|
||||
!std::string_view{*file}.ends_with(query.file_filter)) {
|
||||
continue;
|
||||
}
|
||||
++result.total;
|
||||
if (result.entries.size() >= query.limit) {
|
||||
continue;
|
||||
}
|
||||
CompdbEntry projected;
|
||||
projected.file = *file;
|
||||
const auto read = [&](std::string_view name, std::string &out) {
|
||||
if (const auto it = object->find(std::string{name}); it != object->end()) {
|
||||
if (const auto *value = std::get_if<std::string>(&it->second.value)) {
|
||||
out = *value;
|
||||
}
|
||||
}
|
||||
};
|
||||
read("directory", projected.directory);
|
||||
read("output", projected.output);
|
||||
read("command", projected.command);
|
||||
if (const auto it = object->find("arguments"); it != object->end()) {
|
||||
if (const auto *arguments =
|
||||
std::get_if<Void::Json::Strict::Value::Array>(&it->second.value)) {
|
||||
for (const auto &argument : *arguments) {
|
||||
const auto *value = std::get_if<std::string>(&argument.value);
|
||||
if (value == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (projected.arguments.size() >= kMaxArguments) {
|
||||
break;
|
||||
}
|
||||
projected.arguments.push_back(value->size() > kMaxArgumentBytes
|
||||
? value->substr(0, kMaxArgumentBytes)
|
||||
: *value);
|
||||
}
|
||||
}
|
||||
}
|
||||
result.entries.push_back(std::move(projected));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string format_ide_query_json(const Graph::BuildGraph &graph,
|
||||
const std::filesystem::path &compdb_path,
|
||||
const std::filesystem::path &sdk_include_dir) {
|
||||
|
||||
Reference in new issue
Block a user