From f97cf4236d9784e13629a61066a8b9c6324e1e5d Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Tue, 9 Jun 2020 23:58:12 +0200 Subject: [PATCH] Fix #70: overlapping keyword matches show reminder text for both keywords, now longest match is used --- CHANGES.txt | 4 +++- src/data/keyword.cpp | 23 ++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 004a04d0..bb0b77d2 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -7,7 +7,9 @@ ------------------------------------------------------------------------------ Bug fixes: - * Fixed keywords after atoms (#67) + * Fixed: keywords after atoms were not showing up (#67) + * Fixed: multiple keywords that matched in the same place both showed reminder text. (#70) + Now, when there are overlapping matches the longest one is used. ------------------------------------------------------------------------------ -- 2.1.0, 2020-06-01 diff --git a/src/data/keyword.cpp b/src/data/keyword.cpp index 6f4efbc6..b969e4be 100644 --- a/src/data/keyword.cpp +++ b/src/data/keyword.cpp @@ -443,13 +443,16 @@ void keyword_matches(const String& untagged_str, unordered_set k } } void sort_keyword_matches(vector& matches) { - // sort matches by their start position sort(matches.begin(), matches.end(), [](KeywordMatch const& a, KeywordMatch const& b) { + // sort matches by their start position if (a.pos < b.pos) return true; if (a.pos > b.pos) return false; // otherwise sort by matching set keywords (non-fixed) first if (a.keyword->fixed < b.keyword->fixed) return true; if (a.keyword->fixed > b.keyword->fixed) return false; + // otherwise sort by longest match first + if (a.match[0].length() > b.match[0].length()) return true; + if (a.match[0].length() < b.match[0].length()) return false; // otherwise sort by name return a.keyword->keyword < b.keyword->keyword; }); @@ -522,17 +525,15 @@ String expand_keywords(const String& tagged_str, vector const& mat if (atom == 0) { // don't expand keywords that are inside tags while (match_it != matches.end() && match_it->pos <= untagged_pos) { - if (match_it->pos > untagged_pos) { + if (match_it->pos == untagged_pos) { + // try to expand + auto [match,new_it] = expand_keyword(it, end, *match_it, expand_type, out, options); ++match_it; - continue; - } - // try to expand - auto [match,new_it] = expand_keyword(it, end, *match_it, expand_type, out, options); - if (match) { - untagged_pos += untagged_length(it,new_it); - it = new_it; - ++match_it; - goto after_match; + if (match) { + untagged_pos += untagged_length(it,new_it); + it = new_it; + goto after_match; + } } else { ++match_it; }