feat(daemon): serve bounded compile command entries to editor extensions
This commit is contained in:
1 parent
afea6f3fad
commit
40fef50a6f
9 files changed
+237
-2
No files matched your search
@@ -19,6 +19,7 @@
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
@@ -60,6 +61,9 @@ constexpr std::size_t kMaxDescribeProfileBytes = 128U;
|
||||
constexpr std::size_t kMaxDescribeTargetNames = 128U;
|
||||
constexpr std::size_t kMaxDescribeTargetNameBytes = 200U;
|
||||
constexpr std::size_t kMaxBuildTargets = 64U;
|
||||
constexpr std::size_t kMaxCompdbArguments = 256U;
|
||||
constexpr std::size_t kMaxCompdbArgumentBytes = 1024U;
|
||||
constexpr std::size_t kMaxCompdbEntries = 500U;
|
||||
constexpr std::size_t kMaxPendingChanges = 4096U;
|
||||
constexpr std::int64_t kMaxWatchWaitMs = 5000;
|
||||
constexpr std::int64_t kDefaultWatchWaitMs = 2000;
|
||||
@@ -946,6 +950,109 @@ std::string graph_project(const DaemonOptions &options, std::string_view profile
|
||||
",\"graph\":" + Util::json_quote(rendered) + '}';
|
||||
}
|
||||
|
||||
// Returns bounded compile_commands entries from the active compilation database, optionally
|
||||
// filtered by a path suffix. Editor extensions use this instead of parsing the file themselves.
|
||||
std::string compdb_entries_project(const DaemonOptions &options, std::string_view file_filter,
|
||||
std::size_t limit) {
|
||||
auto selection = Editor::read_selection_state(options.project_root);
|
||||
if (!selection) {
|
||||
return error_response(std::format("compdb_entries: {}", selection.error().format()));
|
||||
}
|
||||
std::filesystem::path compdb_path;
|
||||
if (selection->has_value()) {
|
||||
compdb_path = (*selection)->selected_compdb_path;
|
||||
} else {
|
||||
std::error_code ec;
|
||||
const auto root_alias = options.build_root / "compile_commands.json";
|
||||
if (std::filesystem::exists(root_alias, ec)) {
|
||||
compdb_path = root_alias;
|
||||
}
|
||||
}
|
||||
if (compdb_path.empty()) {
|
||||
return error_response(
|
||||
"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()));
|
||||
}
|
||||
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;
|
||||
}
|
||||
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)});
|
||||
}
|
||||
}
|
||||
matched.emplace_back(std::move(projected));
|
||||
}
|
||||
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
object.emplace("op", JsonValue{std::string{"compdb_entries"}});
|
||||
object.emplace("compdb", JsonValue{compdb_path.string()});
|
||||
object.emplace("total", JsonValue{static_cast<std::int64_t>(total)});
|
||||
object.emplace("returned", JsonValue{static_cast<std::int64_t>(matched.size())});
|
||||
object.emplace("entries", JsonValue{std::move(matched)});
|
||||
return serialize_response(JsonValue{std::move(object)});
|
||||
}
|
||||
|
||||
// Switches the active editor profile to an already-published compilation database.
|
||||
std::string compdb_use_project(const DaemonOptions &options, std::string_view profile_name) {
|
||||
auto state =
|
||||
@@ -1332,6 +1439,28 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "editor_status") {
|
||||
return {editor_status_project(options), false};
|
||||
}
|
||||
if (*name == "compdb_entries") {
|
||||
std::string file_filter;
|
||||
if (const auto entry = object->find("file"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
if (value == nullptr || value->size() > 4096) {
|
||||
return {error_response("\"file\" must be a bounded string"), false};
|
||||
}
|
||||
file_filter = *value;
|
||||
}
|
||||
std::size_t limit = 50;
|
||||
if (const auto entry = object->find("limit"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::int64_t>(&entry->second.value);
|
||||
if (value == nullptr || *value < 0 ||
|
||||
*value > static_cast<std::int64_t>(kMaxCompdbEntries)) {
|
||||
return {error_response(std::format("\"limit\" must be an integer in [0, {}]",
|
||||
kMaxCompdbEntries)),
|
||||
false};
|
||||
}
|
||||
limit = static_cast<std::size_t>(*value);
|
||||
}
|
||||
return {compdb_entries_project(options, file_filter, limit), false};
|
||||
}
|
||||
if (*name == "compdb_use") {
|
||||
const auto entry = object->find("profile");
|
||||
if (entry == object->end()) {
|
||||
|
||||
Reference in new issue
Block a user