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.
51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
#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
|