Serve formatting through clang-format

Per the user's decision, textDocument/formatting runs clang-format as a
bounded child process with a 5s deadline and capped streams. A bounded
core builds the arguments (workspace .clang-format when present,
otherwise the LSP tab options as an explicit LLVM style) and the
whole-document edit, refusing output over 32 MiB. Registered feature
tests cover the arguments, bounds and edit shape; a real stdio session
formatted a misindented document. clang-format becomes a runtime
dependency of formatting.
This commit is contained in:
faumaray committed 2026-12-05 21:30:44 +03:00
1 parent 7acbfc4029
commit 3344a9f170
12 files changed
+247 -5

No files matched your search

+19
View File
@@ -22,6 +22,8 @@ namespace {
namespace Protocol = Void::LanguageServer::Protocol;
using Protocol::as_array;
using Protocol::as_bool;
using Protocol::as_int64;
using Protocol::as_object;
using Protocol::as_string;
using Protocol::find_member;
@@ -321,6 +323,8 @@ DispatchResult Server::handle(std::string_view body) {
{"workspace", Json{std::move(workspace)}}};
capabilities.emplace("documentLinkProvider",
Json{Json::Object{{"resolveProvider", Json{false}}}});
if (diagnostics_)
capabilities.emplace("documentFormattingProvider", Json{true});
if (diagnostics_) {
capabilities.emplace("documentSymbolProvider", Json{true});
capabilities.emplace("selectionRangeProvider", Json{true});
@@ -495,6 +499,21 @@ DispatchResult Server::handle(std::string_view body) {
}
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
}
if (*method == "textDocument/formatting") {
if (!diagnostics_)
return error(id, -32601, "Method not found");
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
const auto *options = as_object(member(params, "options"));
const auto tab_size = options ? as_int64(find_member(*options, "tabSize")) : std::nullopt;
const auto insert_spaces =
options ? as_bool(find_member(*options, "insertSpaces")) : std::nullopt;
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri) || !tab_size ||
*tab_size < 0 || *tab_size > 64 || !insert_spaces)
return error(id, -32602, "Expected textDocument.uri and bounded formatting options");
return reply(
id, "result",
diagnostics_->format(*uri, static_cast<std::uint32_t>(*tab_size), *insert_spaces));
}
if (*method == "textDocument/documentLink") {
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri))
+46
View File
@@ -0,0 +1,46 @@
#include <vls/features/Formatting.hpp>
#include <algorithm>
#include <ranges>
namespace vls::features::formatting {
namespace {
Position end_of(std::string_view text) {
Position position;
for (const char ch : text) {
if (ch == '\n') {
++position.line;
position.character = 0;
} else {
++position.character;
}
}
return position;
}
} // namespace
std::vector<std::string> arguments(const Options &options, std::string_view path, bool file_style,
const Limits &limits) {
std::vector<std::string> result{"--assume-filename=" + std::string{path}};
if (file_style) {
result.push_back("--style=file");
return result;
}
const auto tab_size = std::min<std::uint32_t>(options.tab_size, limits.tabSize);
result.push_back("--style={BasedOnStyle: LLVM, IndentWidth: " + std::to_string(tab_size) +
", UseTab: " + (options.insert_spaces ? "Never" : "ForIndentation") + "}");
return result;
}
std::vector<Edit> edits(std::string_view original, std::string_view formatted,
const Limits &limits) {
std::vector<Edit> result;
if (formatted.empty() || formatted.size() > limits.outputBytes)
return result;
if (formatted == original)
return result;
result.push_back({{{0, 0}, end_of(original)}, std::string{formatted}});
return result;
}
} // namespace vls::features::formatting
+42
View File
@@ -5,6 +5,7 @@
#include <vls/features/CodeLens.hpp>
#include <vls/features/Completion.hpp>
#include <vls/features/DocumentSymbols.hpp>
#include <vls/features/Formatting.hpp>
#include <vls/features/IndexNavigation.hpp>
#include <vls/features/InlayHints.hpp>
#include <vls/features/SemanticTokens.hpp>
@@ -17,6 +18,8 @@
#include <vls/navigation/Navigation.hpp>
#include <vls/scheduler/Scheduler.hpp>
#include <Void/System/Process.hpp>
#include <algorithm>
#include <array>
#include <chrono>
@@ -1041,6 +1044,45 @@ Json EditorDiagnostics::completion_resolve(const Json &item) const {
return Json{std::move(enriched)};
}
Json EditorDiagnostics::format(std::string_view uri, std::uint32_t tab_size,
bool insert_spaces) const {
const auto snapshot = state_->documents.snapshot(uri);
if (!snapshot)
return Json{nullptr};
const auto path = snapshot->identity.canonical_path.native();
Void::Process::SpawnOptions spawn;
spawn.executable = "clang-format";
std::error_code error;
const bool file_style =
fs::exists(snapshot->identity.canonical_path.parent_path() / ".clang-format", error) ||
fs::exists(state_->root / ".clang-format", error);
spawn.arguments = features::formatting::arguments({tab_size, insert_spaces}, path, file_style);
spawn.search_path = true;
spawn.working_directory = snapshot->identity.canonical_path.parent_path();
auto child = Void::Process::spawn(spawn);
if (!child)
return Json{nullptr};
auto produced = Void::Process::communicate_blocking(
*child, snapshot->text,
{.max_stdout_bytes = features::formatting::Limits{}.outputBytes,
.max_stderr_bytes = 16 * 1024,
.max_line_bytes = 64 * 1024,
.max_stdin_bytes = snapshot->text.size()},
5s, {});
if (!produced || produced->timed_out || produced->cancelled || produced->overflowed ||
!produced->status.exited || produced->status.code != 0)
return Json{nullptr};
const auto edits = features::formatting::edits(snapshot->text, produced->out);
Json::Array result;
for (const auto &edit : edits)
result.push_back(Json{Json::Object{
{"range",
range_json(Documents::Range{{edit.range.start.line, edit.range.start.character},
{edit.range.end.line, edit.range.end.character}})},
{"newText", Json{edit.replacement}}}});
return Json{std::move(result)};
}
Json EditorDiagnostics::code_lenses(std::string_view uri) const {
auto &s = *state_;
const auto identity = Documents::normalize_uri(uri);