From b5f5610cbe0f8e1900a7b422cde4e04181e8ebd7 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 16 Jun 2014 00:49:39 +0100 Subject: [PATCH] Share implementation in complete_id/complete_group_id --- src/highlighter_group.cc | 85 ++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc index 57224cf3..d9c8131d 100644 --- a/src/highlighter_group.cc +++ b/src/highlighter_group.cc @@ -64,48 +64,47 @@ HighlighterFunc HighlighterGroup::get_highlighter(StringView path) const throw group_not_found("not a group: "_str + id); } -Completions HighlighterGroup::complete_id(StringView path, ByteCount cursor_pos) const +template +Completions complete_impl(const id_map& highlighters, Condition condition, + StringView path, ByteCount cursor_pos) { auto sep_it = find(path, path_separator); StringView id(path.begin(), sep_it); if (sep_it == path.end()) - return { 0_byte, id.length(), m_highlighters.complete_id(id, cursor_pos) }; + return { 0_byte, path.length(), highlighters.complete_id_if(path, cursor_pos, condition) }; - auto it = m_highlighters.find(id); - if (it != m_highlighters.end()) + auto it = highlighters.find(id); + if (it != highlighters.end()) { const ByteCount offset = (int)(sep_it + 1 - path.begin()); cursor_pos -= offset; if (auto* group = it->second.target()) - return offset_pos(group->complete_id({sep_it+1, path.end()}, cursor_pos), offset); + return offset_pos((group->*hg_complete)({sep_it+1, path.end()}, cursor_pos), offset); if (auto* hier_group = it->second.target()) - return offset_pos(hier_group->complete_id({sep_it+1, path.end()}, cursor_pos), offset); + return offset_pos((hier_group->*hh_complete)({sep_it+1, path.end()}, cursor_pos), offset); } return {}; } +Completions HighlighterGroup::complete_id(StringView path, ByteCount cursor_pos) const +{ + return complete_impl< + &HighlighterGroup::complete_id, + &HierachicalHighlighter::complete_id + >(m_highlighters, [](const HighlighterAndId&) { return true; }, path, cursor_pos); +} + Completions HighlighterGroup::complete_group_id(StringView path, ByteCount cursor_pos) const { - auto sep_it = find(path, path_separator); - StringView id(path.begin(), sep_it); - if (sep_it == path.end()) - return { 0_byte, path.length(), m_highlighters.complete_id_if( - path, cursor_pos, [](const HighlighterAndId& func) { - return func.second.template target() or - func.second.template target(); - }) }; - - auto it = m_highlighters.find(id); - if (it != m_highlighters.end()) - { - const ByteCount offset = (int)(sep_it + 1 - path.begin()); - cursor_pos -= offset; - if (auto* group = it->second.target()) - return offset_pos(group->complete_group_id({sep_it+1, path.end()}, cursor_pos), offset); - if (auto* hier_group = it->second.target()) - return offset_pos(hier_group->complete_group_id({sep_it+1, path.end()}, cursor_pos), offset); - } - return {}; + return complete_impl< + &HighlighterGroup::complete_group_id, + &HierachicalHighlighter::complete_group_id + >(m_highlighters, [](const HighlighterAndId& func) { + return func.second.target() or + func.second.target(); + }, path, cursor_pos); } HighlighterGroup& HierachicalHighlighter::get_group(StringView path) @@ -134,40 +133,34 @@ HighlighterFunc HierachicalHighlighter::get_highlighter(StringView path) const return HighlighterFunc(std::ref(it->second)); } -Completions HierachicalHighlighter::complete_id(StringView path, ByteCount cursor_pos) const +template +Completions complete_impl(const HierachicalHighlighter::GroupMap& groups, + StringView path, ByteCount cursor_pos) { auto sep_it = find(path, path_separator); StringView id(path.begin(), sep_it); - auto it = m_groups.find(id); + auto it = groups.find(id); if (sep_it == path.end()) - return { 0_byte, id.length(), m_groups.complete_id(id, cursor_pos) }; + return { 0_byte, id.length(), groups.complete_id(id, cursor_pos) }; - if (it != m_groups.end()) + if (it != groups.end()) { - const ByteCount offset = (int)(sep_it + 1 - path.begin()); + const ByteCount offset = (int)(sep_it + 1- path.begin()); return offset_pos( - it->second.complete_id({sep_it+1, path.end()}, + (it->second.*complete)({sep_it+1, path.end()}, cursor_pos - offset), offset); } return {}; } +Completions HierachicalHighlighter::complete_id(StringView path, ByteCount cursor_pos) const +{ + return complete_impl<&HighlighterGroup::complete_id>(m_groups, path, cursor_pos); +} + Completions HierachicalHighlighter::complete_group_id(StringView path, ByteCount cursor_pos) const { - auto sep_it = find(path, path_separator); - StringView id(path.begin(), sep_it); - auto it = m_groups.find(id); - if (sep_it == path.end()) - return { 0_byte, id.length(), m_groups.complete_id(id, cursor_pos) }; - - if (it != m_groups.end()) - { - const ByteCount offset = (int)(sep_it + 1- path.begin()); - return offset_pos( - it->second.complete_group_id({sep_it+1, path.end()}, - cursor_pos - offset), offset); - } - return {}; + return complete_impl<&HighlighterGroup::complete_group_id>(m_groups, path, cursor_pos); } }