From bab54c29ac3b78ee86f2baf7564ed586eff5d414 Mon Sep 17 00:00:00 2001 From: "Michael Z. Kadaner" Date: Wed, 27 Dec 2023 21:51:14 -0800 Subject: [PATCH] Addressed review comment. --- far/vmenu.cpp | 75 +++++++++++++++++++++++++-------------------------- far/vmenu.hpp | 4 +-- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/far/vmenu.cpp b/far/vmenu.cpp index 138329a90d..f1246cf402 100644 --- a/far/vmenu.cpp +++ b/far/vmenu.cpp @@ -171,8 +171,7 @@ int find_nearest(std::ranges::contiguous_range auto const& Range, const int Pos, : FindPos(drop(Pos), take(Pos) | reverse)); } -template -std::pair Intersect(std::pair A, std::pair B) +std::pair Intersect(std::pair A, std::pair B) { assert(A.first < A.second); assert(B.first < B.second); @@ -181,13 +180,12 @@ std::pair Intersect(std::pair A, std::pair B) std::swap(A, B); if (A.second <= B.first) - return { {}, {} }; + return {}; return { B.first, std::min(A.second, B.second) }; } -template -void MarkupSliceBoundaries(std::pair Segment, std::ranges::input_range auto const& Slices, std::weakly_incrementable auto Markup) +void MarkupSliceBoundaries(std::pair Segment, std::ranges::input_range auto const& Slices, std::vector& Markup) { assert(Segment.first < Segment.second); @@ -201,15 +199,15 @@ void MarkupSliceBoundaries(std::pair Segment, std::ranges::input_range aut if (Intersection.first == Intersection.second) continue; - *Markup = Intersection.first; ++Markup; - *Markup = Intersection.second; ++Markup; + Markup.emplace_back(Intersection.first); + Markup.emplace_back(Intersection.second); Segment.first = Intersection.second; if (Segment.first == Segment.second) return; } - *Markup = Segment.second; + Markup.emplace_back(Segment.second); } //может иметь фокус @@ -2066,7 +2064,8 @@ void VMenu::DisplayObject() if (!CheckFlags(VMENU_DISABLEDRAWBACKGROUND) && !CheckFlags(VMENU_LISTBOX)) { - // BUGBUG, dead code -- 2023-07-08 MZK: Is it though? I've got here when pressed hotkey of "Select search area" combobox on "find file" dialog. + // BUGBUG, dead code -- 2023-07-08 MZK: I've got here when pressed hotkey of "Select search area" combobox on "find file" dialog. + // It may be related to the CheckFlags(VMENU_DISABLEDRAWBACKGROUND) part of the condition only. Need to investigate. if (BoxType==SHORT_DOUBLE_BOX || BoxType==SHORT_SINGLE_BOX) { @@ -2390,12 +2389,12 @@ void VMenu::ApplySeparatorName(const MenuItemEx& CurItem, string& separator) con auto NameWidth{ std::min(CurItem.Name.size(), separator.size() - 2) }; auto NamePos{ (separator.size() - NameWidth) / 2 }; - separator.at(NamePos - 1) = L' '; + separator[NamePos - 1] = L' '; separator.replace(NamePos, NameWidth, fit_to_left(CurItem.Name, NameWidth)); - separator.at(NamePos + NameWidth) = L' '; + separator[NamePos + NameWidth] = L' '; } -void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, const int Y, std::vector& HighlightMarkup, const string& BlankLine) const +void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, const int Y, std::vector& HighlightMarkup, string_view BlankLine) const { if (!Layout.Text) return; @@ -2419,7 +2418,7 @@ void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout MarkupSliceBoundaries( std::pair{ CurPos, EndPos }, CurItem.Annotations | std::views::transform([](const auto Ann) { return std::pair{ Ann.first, Ann.first + Ann.second }; }), - std::back_inserter(HighlightMarkup)); + HighlightMarkup); } else if (HotkeyPos != string::npos || CurItem.AutoHotkey) { @@ -2427,7 +2426,7 @@ void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout MarkupSliceBoundaries( std::pair{ CurPos, EndPos }, std::views::single(std::pair{ HighlightPos, HighlightPos + 1 }), - std::back_inserter(HighlightMarkup)); + HighlightMarkup); } else { @@ -2444,7 +2443,7 @@ void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout } SetColor(ColorIndices.Normal); - Text(string_view{ BlankLine.begin(), BlankLine.begin() + (TextBegin + TextWidth - WhereX()) }); + Text(BlankLine.substr(0, TextBegin + TextWidth - WhereX())); const auto DrawDecorator = [&](const int X, std::tuple ColorAndChar) { @@ -3159,32 +3158,32 @@ TEST_CASE("intersect.segments") TEST_CASE("markup.slice.boundaries") { - static struct test_data + static const struct test_data { std::pair Segment; - std::vector> Slices; - std::vector Markup; + std::initializer_list> Slices; + std::initializer_list Markup; } TestDataPoints[] = { - { { 20, 50 }, { { { 10, 15 } } }, { { 50 } } }, - { { 20, 50 }, { { { 10, 20 } } }, { { 50 } } }, - { { 20, 50 }, { { { 10, 30 } } }, { { 20, 30, 50 } } }, - { { 20, 50 }, { { { 10, 50 } } }, { { 20, 50 } } }, - { { 20, 50 }, { { { 10, 70 } } }, { { 20, 50 } } }, - { { 20, 50 }, { { { 20, 30 } } }, { { 20, 30, 50 } } }, - { { 20, 50 }, { { { 20, 50 } } }, { { 20, 50 } } }, - { { 20, 50 }, { { { 20, 70 } } }, { { 20, 50 } } }, - { { 20, 50 }, { { { 30, 40 } } }, { { 30, 40, 50 } } }, - { { 20, 50 }, { { { 30, 50 } } }, { { 30, 50 } } }, - { { 20, 50 }, { { { 30, 70 } } }, { { 30, 50 } } }, - { { 20, 50 }, { { { 50, 50 } } }, { { 50 } } }, - { { 20, 50 }, { { { 50, 70 } } }, { { 50 } } }, - { { 20, 50 }, { { { 60, 70 } } }, { { 50 } } }, - { { 20, 50 }, { { { 40, 30 } } }, { { 50 } } }, - { { 20, 70 }, { { { 30, 40 }, { 50, 60 } } }, { { 30, 40, 50, 60, 70 } } }, - { { 20, 70 }, { { { 30, 40 }, { 40, 60 } } }, { { 30, 40, 40, 60, 70 } } }, - { { 20, 70 }, { { { 30, 50 }, { 40, 60 } } }, { { 30, 50, 50, 60, 70 } } }, - { { 20, 70 }, { { { 50, 60 }, { 30, 40 } } }, { { 50, 60, 70 } } }, + { { 20, 50 }, { { 10, 15 } }, { 50 } }, + { { 20, 50 }, { { 10, 20 } }, { 50 } }, + { { 20, 50 }, { { 10, 30 } }, { 20, 30, 50 } }, + { { 20, 50 }, { { 10, 50 } }, { 20, 50 } }, + { { 20, 50 }, { { 10, 70 } }, { 20, 50 } }, + { { 20, 50 }, { { 20, 30 } }, { 20, 30, 50 } }, + { { 20, 50 }, { { 20, 50 } }, { 20, 50 } }, + { { 20, 50 }, { { 20, 70 } }, { 20, 50 } }, + { { 20, 50 }, { { 30, 40 } }, { 30, 40, 50 } }, + { { 20, 50 }, { { 30, 50 } }, { 30, 50 } }, + { { 20, 50 }, { { 30, 70 } }, { 30, 50 } }, + { { 20, 50 }, { { 50, 50 } }, { 50 } }, + { { 20, 50 }, { { 50, 70 } }, { 50 } }, + { { 20, 50 }, { { 60, 70 } }, { 50 } }, + { { 20, 50 }, { { 40, 30 } }, { 50 } }, + { { 20, 70 }, { { 30, 40 }, { 50, 60 } }, { 30, 40, 50, 60, 70 } }, + { { 20, 70 }, { { 30, 40 }, { 40, 60 } }, { 30, 40, 40, 60, 70 } }, + { { 20, 70 }, { { 30, 50 }, { 40, 60 } }, { 30, 50, 50, 60, 70 } }, + { { 20, 70 }, { { 50, 60 }, { 30, 40 } }, { 50, 60, 70 } }, }; std::vector Markup; @@ -3192,7 +3191,7 @@ TEST_CASE("markup.slice.boundaries") for (const auto& TestDataPoint : TestDataPoints) { Markup.clear(); - MarkupSliceBoundaries(TestDataPoint.Segment, TestDataPoint.Slices, std::back_inserter(Markup)); + MarkupSliceBoundaries(TestDataPoint.Segment, TestDataPoint.Slices, Markup); REQUIRE(std::ranges::equal(TestDataPoint.Markup, Markup)); } } diff --git a/far/vmenu.hpp b/far/vmenu.hpp index 213b148d2e..c12df1946c 100644 --- a/far/vmenu.hpp +++ b/far/vmenu.hpp @@ -309,7 +309,7 @@ class VMenu final: public Modal void DrawSeparator(size_t CurItemIndex, int BoxType, int Y) const; void ConnectSeparator(size_t CurItemIndex, string& separator, int BoxType) const; void ApplySeparatorName(const MenuItemEx& CurItem, string& separator) const; - void DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, int Y, std::vector& HighlightMarkup, const string& BlankLine) const; + void DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, int Y, std::vector& HighlightMarkup, string_view BlankLine) const; [[nodiscard]] size_t CalculateMaxLineWidth() const; @@ -331,7 +331,7 @@ class VMenu final: public Modal void UpdateSelectPos(); void EnableFilter(bool Enable); - [[nodiscard]] FarColor GetColor(vmenu_colors ColorIndex) const noexcept { return Colors[std::to_underlying(ColorIndex)]; } + [[nodiscard]] const FarColor& GetColor(vmenu_colors ColorIndex) const noexcept { return Colors[std::to_underlying(ColorIndex)]; } void SetColor(vmenu_colors ColorIndex) const; size_t Text(string_view Str) const; size_t Text(wchar_t Char) const;