A bounded scanner recognizes namespace, class/struct, enum and function declarations at line starts from document text alone, skipping preprocessor lines, comments and control statements. documentSymbol merges it with observer-fed index records, so the generic path has a real outline with no compiler-specific artifact. Registered feature tests cover recognition, call exclusion, comments, ranges and bounds; the generic stdio session returned main as a function symbol.
78 lines
2.9 KiB
C++
78 lines
2.9 KiB
C++
#include <vls/features/Outline.hpp>
|
|
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace vls::features::outline;
|
|
using Void::Testing::expect;
|
|
using Void::Testing::require;
|
|
|
|
std::vector<std::string> names(const std::vector<Symbol> &symbols) {
|
|
std::vector<std::string> result;
|
|
for (const auto &symbol : symbols)
|
|
result.push_back(symbol.name);
|
|
return result;
|
|
}
|
|
} // namespace
|
|
|
|
VOID_TEST(Outline, recognizes_namespaces_classes_and_enums) {
|
|
const std::string text = "namespace demo {\n"
|
|
"struct Point {\n"
|
|
" int x;\n"
|
|
"};\n"
|
|
"enum class Color { red };\n"
|
|
"class Widget;\n"
|
|
"}\n";
|
|
const auto symbols = outline(text);
|
|
expect(names(symbols) == std::vector<std::string>{"demo", "Point", "Color", "Widget"});
|
|
expect(symbols[0].kind == Kind::namespace_);
|
|
expect(symbols[1].kind == Kind::class_);
|
|
expect(symbols[2].kind == Kind::enum_);
|
|
expect(symbols[3].kind == Kind::class_);
|
|
}
|
|
|
|
VOID_TEST(Outline, distinguishes_functions_from_call_statements) {
|
|
const std::string text = "int compute(int value) { return value; }\n"
|
|
"void run() {\n"
|
|
" compute(1);\n"
|
|
" if (compute(2)) {}\n"
|
|
" auto x = compute(3);\n"
|
|
"}\n";
|
|
const auto symbols = outline(text);
|
|
expect(names(symbols) == std::vector<std::string>{"compute", "run"});
|
|
expect(symbols[0].kind == Kind::function);
|
|
}
|
|
|
|
VOID_TEST(Outline, skips_preprocessor_and_comments) {
|
|
const std::string text = "#define fake(x) x\n"
|
|
"// int commented();\n"
|
|
"/* void blocked();\n"
|
|
" int still_blocked(); */\n"
|
|
"int real();\n";
|
|
expect(names(outline(text)) == std::vector<std::string>{"real"});
|
|
}
|
|
|
|
VOID_TEST(Outline, ranges_cover_the_line_and_the_name) {
|
|
const std::string text = " int value(int a) { return a; }\n";
|
|
const auto symbols = outline(text);
|
|
require(symbols.size() == 1);
|
|
expect(symbols[0].range.start == Position{0, 0});
|
|
expect(symbols[0].range.end.character == text.size() - 1);
|
|
expect(symbols[0].selection_range.start == Position{0, 6});
|
|
expect(symbols[0].selection_range.end == Position{0, 11});
|
|
}
|
|
|
|
VOID_TEST(Outline, is_bounded) {
|
|
std::string text;
|
|
for (int line = 0; line != 20; ++line)
|
|
text += "int f" + std::to_string(line) + "();\n";
|
|
expect(outline(text).size() == 20);
|
|
expect(outline(text, Limits{.symbols = 3}).size() == 3);
|
|
expect(outline(" int " + std::string(300, 'x') + "();\n", Limits{.nameBytes = 8}).empty());
|
|
expect(outline(text, Limits{.scanBytes = 10}).size() == 1);
|
|
}
|