diff --git a/src/navigation.cpp b/src/navigation.cpp index 7e83071..e221e09 100644 --- a/src/navigation.cpp +++ b/src/navigation.cpp @@ -137,29 +137,34 @@ auto map_sections(const std::vector& lines, std::string_view term) { const auto begin = boundaries[i - 1]; const auto end = boundaries[i]; + if (begin == end) { continue; } + const bool title_match = heading(lines[begin]) && lower(lines[begin]).find(query) != std::string::npos; bool body_match = false; + for (auto j = begin; j < end && !body_match; ++j) { body_match = lower(lines[j]).find(query) != std::string::npos; } + if (body_match) { matches.push_back({begin, end, title_match}); } } - std::stable_sort(matches.begin(), matches.end(), [](const section& a, const section& b) - { - return a.title_match > b.title_match; - }); + std::stable_sort(matches.begin(), matches.end(), [](const section& a, const section& b){ + return a.title_match > b.title_match; + }); + std::string output; std::size_t shown_lines = 0; std::size_t shown_sections = 0; bool truncated = false; + for (const auto& match : matches) { if (shown_sections == 3) @@ -167,6 +172,7 @@ auto map_sections(const std::vector& lines, std::string_view term) truncated = true; break; } + ++shown_sections; for (auto i = match.begin; i < match.end; ++i) { @@ -178,19 +184,23 @@ auto map_sections(const std::vector& lines, std::string_view term) output += lines[i] + '\n'; ++shown_lines; } + if (truncated) { break; } } + if (matches.empty()) { return "No relevant project-map section found.\n"; } + if (truncated) { output += "TRUNCATED (limit: 3 sections, 120 lines, 16384 bytes)\n"; } + return output; } } // namespace rl