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

+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include <compare>
#include <cstddef>
#include <cstdint>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace vls::features::formatting {
struct Position {
std::uint32_t line{}, character{};
auto operator<=>(const Position &) const = default;
};
struct Range {
Position start, end;
auto operator<=>(const Range &) const = default;
};
struct Edit {
Range range;
std::string replacement;
auto operator<=>(const Edit &) const = default;
};
// LSP formatting options, bounded by the server before they get here.
struct Options {
std::uint32_t tab_size{4};
bool insert_spaces{true};
};
struct Limits {
std::size_t outputBytes{32 * 1024 * 1024};
std::size_t tabSize{64};
};
// clang-format arguments. With FILE_STYLE the workspace .clang-format is used
// (--style=file); otherwise the LSP options become an explicit LLVM-based
// style. The path only names the file, nothing is read from it here.
[[nodiscard]] std::vector<std::string> arguments(const Options &, std::string_view path,
bool file_style, const Limits & = Limits{});
// A single whole-document replacement when the formatted text differs, or
// nothing when it is identical or exceeds the bound. The range end covers the
// last character of the original text.
[[nodiscard]] std::vector<Edit> edits(std::string_view original, std::string_view formatted,
const Limits & = Limits{});
} // namespace vls::features::formatting
@@ -60,6 +60,10 @@ class EditorDiagnostics {
selection_ranges(std::string_view uri, std::span<const Documents::Position>) const;
// Foldable brace blocks, comments, regions and include groups.
[[nodiscard]] Void::LanguageServer::Protocol::Json folding_ranges(std::string_view uri) const;
// Whole-document formatting through clang-format. Empty when the text is
// already formatted; null when the formatter is unavailable or failed.
[[nodiscard]] Void::LanguageServer::Protocol::Json
format(std::string_view uri, std::uint32_t tab_size, bool insert_spaces) const;
// Reference-count lenses for the records the index holds for one document.
[[nodiscard]] Void::LanguageServer::Protocol::Json code_lenses(std::string_view uri) const;
// Quick fixes suggested by the compiler for diagnostics overlapping the