diff --git a/CMakeLists.txt b/CMakeLists.txt index b285eac..c783b1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ add_library(vls_protocol STATIC src/Protocol/Framing.cpp src/Protocol/Json.cpp src/Build/ProviderFile.cpp src/Build/CommandImport.cpp src/Build/VbsProvider.cpp src/Build/CompdbProvider.cpp src/DocumentSync.cpp src/WorkspaceSession.cpp src/Server.cpp src/semantic/Diagnostics.cpp src/semantic/SourceCatalog.cpp src/semantic/StockCompiler.cpp src/semantic/EditorDiagnostics.cpp src/semantic/Occurrences.cpp src/scheduler/Scheduler.cpp - src/modules/ModuleGraph.cpp src/modules/ArtifactCache.cpp src/modules/GccArtifacts.cpp src/modules/ScheduledArtifacts.cpp src/index/SymbolIndex.cpp src/navigation/Navigation.cpp src/reflection/Provenance.cpp src/features/Completion.cpp src/features/IndexNavigation.cpp src/features/SignatureHelp.cpp src/features/DocumentSymbols.cpp src/features/TextStructure.cpp src/features/CodeActions.cpp src/features/SemanticTokens.cpp src/features/InlayHints.cpp src/features/FileOperations.cpp src/features/IncludeUpdate.cpp src/features/CallHierarchy.cpp src/features/CodeLens.cpp src/features/DocumentLinks.cpp src/features/Formatting.cpp src/features/TypeHierarchy.cpp src/debug/live/LiveSession.cpp src/modules/ModuleWorkQueue.cpp src/modules/GccScan.cpp src/modules/ScanCommand.cpp) + src/modules/ModuleGraph.cpp src/modules/ArtifactCache.cpp src/modules/GccArtifacts.cpp src/modules/ScheduledArtifacts.cpp src/index/SymbolIndex.cpp src/navigation/Navigation.cpp src/reflection/Provenance.cpp src/features/Completion.cpp src/features/IndexNavigation.cpp src/features/SignatureHelp.cpp src/features/DocumentSymbols.cpp src/features/TextStructure.cpp src/features/CodeActions.cpp src/features/SemanticTokens.cpp src/features/InlayHints.cpp src/features/FileOperations.cpp src/features/IncludeUpdate.cpp src/features/CallHierarchy.cpp src/features/CodeLens.cpp src/features/DocumentLinks.cpp src/features/Formatting.cpp src/features/TypeHierarchy.cpp src/features/Outline.cpp src/debug/live/LiveSession.cpp src/modules/ModuleWorkQueue.cpp src/modules/GccScan.cpp src/modules/ScanCommand.cpp) target_include_directories(vls_protocol PUBLIC "${PROJECT_SOURCE_DIR}/include") target_link_libraries(vls_protocol PUBLIC Void::Data PRIVATE Void::Unicode Void::Text Void::Crypto Void::System) target_compile_options(vls_protocol PUBLIC -freflection PRIVATE -Wall -Wextra -Wpedantic) diff --git a/build.cpp b/build.cpp index 7d826ba..96f3509 100644 --- a/build.cpp +++ b/build.cpp @@ -81,6 +81,7 @@ void build(Void::Build::Project &project) { "src/features/DocumentLinks.cpp", "src/features/Formatting.cpp", "src/features/TypeHierarchy.cpp", + "src/features/Outline.cpp", "src/debug/live/LiveSession.cpp", "src/modules/ModuleWorkQueue.cpp", "src/modules/GccScan.cpp", @@ -165,7 +166,7 @@ void build(Void::Build::Project &project) { "tests/features/FileOperationsTests.cpp", "tests/features/IncludeUpdateTests.cpp", "tests/features/CallHierarchyTests.cpp", "tests/features/CodeLensTests.cpp", "tests/features/DocumentLinksTests.cpp", "tests/features/FormattingTests.cpp", - "tests/features/TypeHierarchyTests.cpp"}); + "tests/features/TypeHierarchyTests.cpp", "tests/features/OutlineTests.cpp"}); auto live_worker = project.executable("vls_live_worker_fixture"); live_worker.cxx_standard(CxxStandard::Cxx26) .sources({"tests/debug/LiveWorkerFixture.cpp"}) diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index d7b580e..04cbe84 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -61,6 +61,25 @@ start coordinate only, so ranges are zero-width until an end is available. LSP adapters and index/rename/debugging integration remain open. `vbs test` passed all 17 native suites; CMake passed the reflection and trace suites. +## December 6 generic document outline + +`include/vls/features/Outline.hpp` adds a compiler-independent outline: a +bounded, conservative scanner recognizes namespace, class/struct, enum and +function declarations at line starts, skipping preprocessor lines, comments and +control statements, and never reporting a bare call statement as a function. +`EditorDiagnostics::document_symbols` merges it (deduplicated by name and +position) with the observer-fed index records, so the generic path has a real +outline without any compiler-specific artifact. + +In the D-032 session (plain compilation database, no observer): +``` +documentSymbol: [('main', 12)] +selectionRange served: True +hover error: -32601 +``` +Registered `vls_feature_tests` grew to 109 cases. All 18 native suites and all +18 CMake tests passed. + ## December 6 generic-compiler degradation (D-032) With a plain compilation database and no occurrence observer, the server diff --git a/include/vls/features/Outline.hpp b/include/vls/features/Outline.hpp new file mode 100644 index 0000000..e85b153 --- /dev/null +++ b/include/vls/features/Outline.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace vls::features::outline { + +struct Position { + std::uint32_t line{}, character{}; + auto operator<=>(const Position &) const = default; +}; +struct Range { + Position start, end; + auto operator<=>(const Range &) const = default; +}; + +// LSP SymbolKind subset this generic outline can prove from text alone. +enum class Kind { namespace_ = 3, class_ = 5, method = 6, function = 12, enum_ = 10 }; + +struct Symbol { + std::string name; + Kind kind{Kind::function}; + Range range; // the declaration line + Range selection_range; // the name + auto operator<=>(const Symbol &) const = default; +}; + +struct Limits { + std::size_t symbols{4096}; + std::size_t nameBytes{256}; + std::size_t scanBytes{8 * 1024 * 1024}; + std::size_t lineBytes{64 * 1024}; +}; + +// A conservative, compiler-independent outline: namespace, class/struct, +// enum and function declarations recognized at the start of a physical line. +// Preprocessor lines, comments and control statements are skipped, and a +// call statement without a type prefix is never reported as a function. +[[nodiscard]] std::vector outline(std::string_view text, const Limits & = Limits{}); + +} // namespace vls::features::outline diff --git a/src/features/Outline.cpp b/src/features/Outline.cpp new file mode 100644 index 0000000..383a3a7 --- /dev/null +++ b/src/features/Outline.cpp @@ -0,0 +1,147 @@ +#include + +#include +#include + +namespace vls::features::outline { +namespace { +bool word(char ch) { + return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || + ch == '_'; +} +bool identifier(std::string_view text) { + if (text.empty() || (text.front() >= '0' && text.front() <= '9')) + return false; + return std::ranges::all_of(text, word); +} +bool type_char(char ch) { + return word(ch) || ch == ':' || ch == '<' || ch == '>' || ch == ',' || ch == '*' || ch == '&' || + ch == ' '; +} +constexpr std::array reserved{"if", "for", "while", "switch", "catch", "return", "sizeof", + "else", "do", "case", "throw", "new", "delete", "static_assert"}; +} // namespace + +std::vector outline(std::string_view text, const Limits &limits) { + std::vector symbols; + const auto stop = std::min(text.size(), limits.scanBytes); + std::size_t offset{}; + std::uint32_t line_number{}; + bool block_comment{}; + while (offset < stop) { + auto end = text.find('\n', offset); + if (end == std::string_view::npos || end > stop) + end = stop; + auto line = text.substr(offset, end - offset); + if (line.size() > limits.lineBytes) { + offset = end + 1; + ++line_number; + continue; + } + const auto first = line.find_first_not_of(" \t"); + if (first != std::string_view::npos) + line.remove_prefix(first); + const auto indent = first == std::string_view::npos ? 0 : first; + bool skip = line.empty() || line.starts_with("#") || line.starts_with("//") || + line.starts_with("/*") || block_comment; + if (!skip && line.starts_with("*")) + skip = true; + if (!skip) { + std::optional kind; + std::size_t name_at{}; + std::size_t name_length{}; + auto word_at = [&line](std::size_t at) { + const auto begin = line.find_first_not_of(" \t", at); + if (begin == std::string_view::npos) + return std::pair{std::string_view::npos, std::string_view{}}; + auto finish = begin; + while (finish < line.size() && word(line[finish])) + ++finish; + return std::pair{begin, line.substr(begin, finish - begin)}; + }; + const auto [first_word_at, first_word] = word_at(0); + if (first_word == "namespace") { + const auto [at, name] = word_at(first_word_at + first_word.size()); + if (!name.empty() && name != "{") { + kind = Kind::namespace_; + name_at = at; + name_length = name.size(); + } + } else if (first_word == "class" || first_word == "struct" || first_word == "union" || + first_word == "enum") { + auto at = first_word_at + first_word.size(); + const auto [next_at, next] = word_at(at); + auto name_at_candidate = next_at; + auto name = next; + if (next == "class") { + const auto [after_at, after] = word_at(next_at + next.size()); + name_at_candidate = after_at; + name = after; + } + if (!name.empty() && identifier(name)) { + kind = first_word == "enum" ? Kind::enum_ + : first_word == "class" ? Kind::class_ + : first_word == "struct" ? Kind::class_ + : Kind::class_; + name_at = name_at_candidate; + name_length = name.size(); + } + } else if (!first_word.empty() && + std::ranges::find(reserved, first_word) == reserved.end() && + first_word_at == 0) { + // A declaration line: TYPE... NAME ( with no '=' before NAME. + std::size_t cursor{}; + const auto open = line.find('('); + const auto assign = line.find('='); + if (open != std::string_view::npos && + (assign == std::string_view::npos || assign > open)) { + const auto prefix = line.substr(0, open); + if (std::ranges::all_of(prefix, type_char)) { + auto name_end = prefix.find_last_not_of(" \t"); + if (name_end != std::string_view::npos) { + auto name_start = name_end; + while (name_start > 0 && word(prefix[name_start - 1])) + --name_start; + const auto name = prefix.substr(name_start, name_end - name_start + 1); + // A bare call statement has no type prefix. + if (name_start > 0 && identifier(name)) { + kind = Kind::function; + name_at = name_start; + name_length = name.size(); + } + } + } + } + } + if (kind && name_length <= limits.nameBytes) { + const auto name = line.substr(name_at, name_length); + if (symbols.size() < limits.symbols) + symbols.push_back( + {std::string{name}, + *kind, + {{line_number, 0}, + {line_number, static_cast(line.size() + indent)}}, + {{line_number, static_cast(name_at + indent)}, + {line_number, + static_cast(name_at + name_length + indent)}}}); + } + } + // Carry block-comment state across lines. + for (std::size_t index = 0; index < line.size(); ++index) { + if (!block_comment && index + 1 < line.size() && line[index] == '/' && + line[index + 1] == '*') { + block_comment = true; + ++index; + } else if (block_comment && index + 1 < line.size() && line[index] == '*' && + line[index + 1] == '/') { + block_comment = false; + ++index; + } + } + offset = end + 1; + ++line_number; + } + return symbols; +} + +} // namespace vls::features::outline diff --git a/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index 8a5c927..b608314 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -1150,6 +1151,26 @@ Json EditorDiagnostics::document_symbols(std::string_view uri) const { } Json::Array symbols; std::set> seen; + // Text-proven outline entries work without any compiler-specific observer. + if (const auto snapshot = s.documents.snapshot(uri)) + for (const auto &symbol : features::outline::outline(snapshot->text)) { + if (!seen.emplace(symbol.name, static_cast(symbol.kind), symbol.range.start.line, + symbol.range.start.character) + .second) + continue; + Json::Object entry; + entry.emplace("name", Json{symbol.name}); + entry.emplace("kind", Json{static_cast(symbol.kind)}); + entry.emplace("range", range_json(Documents::Range{ + {symbol.range.start.line, symbol.range.start.character}, + {symbol.range.end.line, symbol.range.end.character}})); + entry.emplace( + "selectionRange", + range_json(Documents::Range{ + {symbol.selection_range.start.line, symbol.selection_range.start.character}, + {symbol.selection_range.end.line, symbol.selection_range.end.character}})); + symbols.push_back(Json{std::move(entry)}); + } for (const auto &record : records.symbols) { const auto &location = record.location; if (!seen.emplace(record.name, static_cast(record.kind), location.line, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 28e375a..c36861d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 features/FormattingTests.cpp features/TypeHierarchyTests.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 features/TypeHierarchyTests.cpp features/OutlineTests.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) diff --git a/tests/features/OutlineTests.cpp b/tests/features/OutlineTests.cpp new file mode 100644 index 0000000..eba6695 --- /dev/null +++ b/tests/features/OutlineTests.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include + +#include +#include + +namespace { +using namespace vls::features::outline; +using Void::Testing::expect; +using Void::Testing::require; + +std::vector names(const std::vector &symbols) { + std::vector 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{"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{"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{"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); +}