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:
1 parent
7acbfc4029
commit
3344a9f170
12 files changed
+247
-5
No files matched your search
@@ -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))
|
||||
|
||||
Reference in new issue
Block a user