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,7 +22,7 @@ vls_test_suite(vls_scheduler_tests scheduler/SchedulerTests.cpp)
|
||||
vls_test_suite(vls_index_tests index/SymbolIndexTests.cpp)
|
||||
vls_test_suite(vls_navigation_tests navigation/NavigationTests.cpp)
|
||||
vls_test_suite(vls_reflection_tests reflection/ProvenanceTests.cpp)
|
||||
vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp features/SemanticTokensTests.cpp features/InlayHintsTests.cpp features/FileOperationsTests.cpp features/IncludeUpdateTests.cpp features/CallHierarchyTests.cpp features/CodeLensTests.cpp features/DocumentLinksTests.cpp)
|
||||
vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp features/SemanticTokensTests.cpp features/InlayHintsTests.cpp features/FileOperationsTests.cpp features/IncludeUpdateTests.cpp features/CallHierarchyTests.cpp features/CodeLensTests.cpp features/DocumentLinksTests.cpp features/FormattingTests.cpp)
|
||||
vls_test_suite(vls_live_tests debug/LiveSessionTests.cpp debug/LiveDapTests.cpp)
|
||||
target_link_libraries(vls_live_tests PRIVATE Void::System)
|
||||
add_dependencies(vls_live_tests vls_live_worker_fixture vls-dap)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#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"));
|
||||
}
|
||||
Reference in new issue
Block a user