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
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user