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.
62 lines
2.4 KiB
C++
62 lines
2.4 KiB
C++
#include <vls/features/Formatting.hpp>
|
|
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace vls::features::formatting;
|
|
using Void::Testing::expect;
|
|
using Void::Testing::require;
|
|
|
|
bool contains(const std::vector<std::string> &arguments, std::string_view value) {
|
|
return std::ranges::find(arguments, value) != arguments.end();
|
|
}
|
|
} // namespace
|
|
|
|
VOID_TEST(Formatting, arguments_use_the_workspace_style_with_an_lsp_fallback) {
|
|
const auto arguments = vls::features::formatting::arguments(
|
|
Options{.tab_size = 2, .insert_spaces = true}, "/project/main.cpp", false);
|
|
expect(arguments[0] == "--assume-filename=/project/main.cpp");
|
|
expect(arguments[1].find("IndentWidth: 2") != std::string::npos);
|
|
expect(arguments[1].find("UseTab: Never") != std::string::npos);
|
|
const auto workspace =
|
|
vls::features::formatting::arguments(Options{}, "/project/main.cpp", true);
|
|
expect(workspace.size() == 2);
|
|
expect(workspace[1] == "--style=file");
|
|
}
|
|
|
|
VOID_TEST(Formatting, arguments_honor_tabs_and_bound_the_tab_size) {
|
|
const auto tabs = vls::features::formatting::arguments(
|
|
Options{.tab_size = 8, .insert_spaces = false}, "/project/main.cpp", false);
|
|
expect(tabs[1].find("UseTab: ForIndentation") != std::string::npos);
|
|
const auto bounded =
|
|
vls::features::formatting::arguments(Options{.tab_size = 1000, .insert_spaces = true},
|
|
"/project/main.cpp", false, Limits{.tabSize = 16});
|
|
expect(bounded[1].find("IndentWidth: 16") != std::string::npos);
|
|
}
|
|
|
|
VOID_TEST(Formatting, identical_text_produces_no_edit) {
|
|
expect(edits("int x = 1;\n", "int x = 1;\n").empty());
|
|
expect(edits("int x = 1;\n", "").empty());
|
|
}
|
|
|
|
VOID_TEST(Formatting, changed_text_replaces_the_whole_document) {
|
|
const std::string original = "int x=1;\nint y;\n";
|
|
const std::string formatted = "int x = 1;\nint y;\n";
|
|
const auto result = edits(original, formatted);
|
|
require(result.size() == 1);
|
|
expect(result.front().range.start == Position{0, 0});
|
|
expect(result.front().range.end == Position{2, 0});
|
|
expect(result.front().replacement == formatted);
|
|
}
|
|
|
|
VOID_TEST(Formatting, output_is_bounded) {
|
|
const auto result = edits("a\n", "formatted\n", Limits{.outputBytes = 4});
|
|
expect(result.empty());
|
|
expect(contains(arguments(Options{}, "/p/a.cpp", true), "--style=file"));
|
|
}
|