#include #include #include #include #include #include #include namespace { using namespace vls::features::structure; using Void::Testing::expect; using Void::Testing::require; bool contains(const std::vector &folds, std::uint32_t start, std::uint32_t end, std::string_view kind = {}) { return std::ranges::any_of(folds, [&](const Folding &fold) { return fold.start_line == start && fold.end_line == end && (kind.empty() || fold.kind == kind); }); } } // namespace VOID_TEST(TextStructure, selection_chain_nests_token_line_block_document) { const std::string text = "int main() {\n int value = 17;\n}\n"; const auto chain = selection_chain(text, {1, 9}); require(chain.size() >= 4); expect(chain[0] == Range{{1, 8}, {1, 13}}); // value expect(chain[1] == Range{{1, 0}, {1, 19}}); // full line expect(chain[2] == Range{{0, 11}, {2, 1}}); // brace block expect(chain.back() == Range{{0, 0}, {3, 0}}); // whole document } VOID_TEST(TextStructure, selection_chain_picks_innermost_blocks_first) { const std::string text = "void f() {\n if (x) {\n g();\n }\n}\n"; const auto chain = selection_chain(text, {2, 9}); require(chain.size() >= 4); expect(chain[1] == Range{{2, 0}, {2, 12}}); const auto inner = std::ranges::find_if(chain, [](const Range &range) { return range.start == Position{1, 11} && range.end == Position{3, 5}; }); expect(inner != chain.end()); const auto outer = std::ranges::find_if(chain, [](const Range &range) { return range.start == Position{0, 9} && range.end == Position{4, 1}; }); expect(outer != chain.end()); expect(inner < outer); } VOID_TEST(TextStructure, selection_chain_has_no_token_outside_a_word) { const std::string text = "int x = 1;\n"; const auto chain = selection_chain(text, {0, 4}); require(chain.size() >= 2); expect(chain.front() == Range{{0, 4}, {0, 5}}); expect(chain[1] == Range{{0, 0}, {0, 10}}); expect(selection_chain(text, {0, 0}, Limits{.selectionDepth = 0}).empty()); expect(selection_chain(text, {0, 1}, Limits{.selectionDepth = 1}).size() == 1); } VOID_TEST(TextStructure, selection_chain_bounds_the_block_window) { std::string text = "void f() {\n"; for (int line = 0; line != 40; ++line) text += " // padding line\n"; text += " g();\n}\n"; Limits limits; limits.scanBytes = 32; const auto chain = selection_chain(text, {41, 6}, limits); const auto block = std::ranges::find_if( chain, [](const Range &range) { return range.start == Position{0, 10}; }); expect(block == chain.end()); expect(selection_chain(text, {41, 6}).size() > chain.size()); } VOID_TEST(TextStructure, folds_multiline_braces_only) { const std::string text = "void f() {\n if (x) {\n g();\n }\n h();\n}\n"; const auto folds = folding_ranges(text); expect(contains(folds, 1, 3)); expect(contains(folds, 0, 5)); expect(folding_ranges("void g() { h(); }\n").empty()); } VOID_TEST(TextStructure, braces_in_literals_and_comments_do_not_fold) { const std::string text = "const char *s = \"{\";\n// {\n/* {\n} */\nauto c = '}';\n"; const auto folds = folding_ranges(text); expect(folds.size() == 1); expect(contains(folds, 2, 3, "comment")); } VOID_TEST(TextStructure, folds_comments_and_preprocessor_regions) { const std::string text = "#ifdef A\nint a;\n#endif\n// one\n// two\n// three\n/* block\n " "comment */\n#region R\nint b;\n#endregion\n"; const auto folds = folding_ranges(text); expect(contains(folds, 0, 2, "region")); expect(contains(folds, 3, 5, "comment")); expect(contains(folds, 6, 7, "comment")); expect(contains(folds, 8, 10, "region")); expect(!contains(folds, 4, 5, "comment")); } VOID_TEST(TextStructure, folds_include_groups) { const std::string text = "#include \n#include \n\nint x;\n#include \n"; const auto folds = folding_ranges(text); expect(contains(folds, 0, 1, "imports")); expect(!contains(folds, 4, 4, "imports")); } VOID_TEST(TextStructure, folds_are_bounded_and_sorted) { std::string text; for (int block = 0; block != 20; ++block) text += "void f" + std::to_string(block) + "() {\n g();\n}\n"; Limits limits; limits.folds = 3; const auto folds = folding_ranges(text, limits); expect(folds.size() == 3); expect(std::ranges::is_sorted(folds, [](const Folding &left, const Folding &right) { return std::tie(left.start_line, left.end_line) < std::tie(right.start_line, right.end_line); })); }