From 2824bd9a464521999f7f68f4f08356ac9e69538e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 19 Jan 2012 20:37:29 +0000 Subject: [PATCH] HighlighterGroup: move to it's own file, Window uses them directly --- src/highlighter_group.cc | 47 +++++++++++++++++++++++++++++++ src/highlighter_group.hh | 43 ++++++++++++++++++++++++++++ src/highlighter_registry.cc | 4 +-- src/highlighters.cc | 31 +++----------------- src/highlighters.hh | 23 --------------- src/main.cc | 16 +++++------ src/window.cc | 56 ++----------------------------------- src/window.hh | 12 ++------ 8 files changed, 109 insertions(+), 123 deletions(-) create mode 100644 src/highlighter_group.cc create mode 100644 src/highlighter_group.hh diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc new file mode 100644 index 00000000..831c9b47 --- /dev/null +++ b/src/highlighter_group.cc @@ -0,0 +1,47 @@ +#include "highlighter_group.hh" + +#include "exception.hh" +#include "utils.hh" + +namespace Kakoune +{ + +void HighlighterGroup::operator()(DisplayBuffer& display_buffer) +{ + for (auto& highlighter : m_highlighters) + highlighter.second(display_buffer); +} + +void HighlighterGroup::append(HighlighterAndId&& highlighter) +{ + if (m_highlighters.contains(highlighter.first)) + throw runtime_error("highlighter id not found " + highlighter.first); + + m_highlighters.append(std::forward(highlighter)); +} + +void HighlighterGroup::remove(const std::string& id) +{ + m_highlighters.remove(id); +} + +HighlighterGroup& HighlighterGroup::get_group(const std::string& id) +{ + auto it = m_highlighters.find(id); + if (it == m_highlighters.end()) + throw runtime_error("no such id: " + id); + HighlighterGroup* group = it->second.target(); + if (not group) + throw runtime_error("not a group: " + id); + + return *group; +} + + +CandidateList HighlighterGroup::complete_id(const std::string& prefix, + size_t cursor_pos) +{ + return m_highlighters.complete_id(prefix, cursor_pos); +} + +} diff --git a/src/highlighter_group.hh b/src/highlighter_group.hh new file mode 100644 index 00000000..07379a63 --- /dev/null +++ b/src/highlighter_group.hh @@ -0,0 +1,43 @@ +#ifndef highlighter_group_hh_INCLUDED +#define highlighter_group_hh_INCLUDED + +#include "highlighter.hh" +#include "idvaluemap.hh" + +namespace Kakoune +{ + +class DisplayBuffer; +class Window; + +class HighlighterGroup +{ +public: + void operator()(DisplayBuffer& display_buffer); + + void append(HighlighterAndId&& highlighter); + void remove(const std::string& id); + + HighlighterGroup& get_group(const std::string& id); + + CandidateList complete_id(const std::string& prefix, size_t cursor_pos); + + CandidateList complete_group_id(const std::string& prefix, size_t cursor_pos) + { + CandidateList all = complete_id(prefix, cursor_pos); + CandidateList res; + for (auto& id : all) + { + if (m_highlighters.find(id)->second.target()) + res.push_back(id); + } + return res; + } + +private: + idvaluemap m_highlighters; +}; + +} + +#endif // highlighter_group_hh_INCLUDED diff --git a/src/highlighter_registry.cc b/src/highlighter_registry.cc index 42de13cc..9eb0b3b1 100644 --- a/src/highlighter_registry.cc +++ b/src/highlighter_registry.cc @@ -28,7 +28,7 @@ void HighlighterRegistry::add_highlighter_to_window(Window& window, if (it == m_factories.end()) throw factory_not_found(name); - window.add_highlighter(it->second(window, parameters)); + window.highlighters().append(it->second(window, parameters)); } void HighlighterRegistry::add_highlighter_to_group(Window& window, @@ -40,7 +40,7 @@ void HighlighterRegistry::add_highlighter_to_group(Window& window, if (it == m_factories.end()) throw factory_not_found(name); - group.add_highlighter(it->second(window, parameters)); + group.append(it->second(window, parameters)); } CandidateList HighlighterRegistry::complete_highlighter(const std::string& prefix, diff --git a/src/highlighters.cc b/src/highlighters.cc index dd5ddf1b..6d3700c7 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -4,6 +4,7 @@ #include "window.hh" #include "display_buffer.hh" #include "highlighter_registry.hh" +#include "highlighter_group.hh" #include namespace Kakoune @@ -264,32 +265,8 @@ private: const Window& m_window; }; -void HighlighterGroup::operator()(DisplayBuffer& display_buffer) -{ - for (auto& highlighter : m_highlighters) - highlighter.second(display_buffer); -} - -void HighlighterGroup::add_highlighter(HighlighterAndId&& highlighter) -{ - if (m_highlighters.contains(highlighter.first)) - throw runtime_error("highlighter id not found " + highlighter.first); - m_highlighters.append(std::forward(highlighter)); -} - -void HighlighterGroup::remove_highlighter(const std::string& id) -{ - m_highlighters.remove(id); -} - -CandidateList HighlighterGroup::complete_highlighterid(const std::string& prefix, - size_t cursor_pos) -{ - return m_highlighters.complete_id(prefix, cursor_pos); -} - -HighlighterAndId HighlighterGroup::create(Window& window, - const HighlighterParameters& params) +HighlighterAndId highlighter_group_factory(Window& window, + const HighlighterParameters& params) { if (params.size() != 1) throw runtime_error("wrong parameter count"); @@ -305,7 +282,7 @@ void register_highlighters() registry.register_factory("expand_tabs", SimpleHighlighterFactory("expand_tabs")); registry.register_factory("number_lines", SimpleHighlighterFactory("number_lines")); registry.register_factory("regex", colorize_regex_factory); - registry.register_factory("group", HighlighterGroup::create); + registry.register_factory("group", highlighter_group_factory); } } diff --git a/src/highlighters.hh b/src/highlighters.hh index 10903b17..53738e94 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -1,34 +1,11 @@ #ifndef highlighters_hh_INCLUDED #define highlighters_hh_INCLUDED -#include "highlighter.hh" -#include "idvaluemap.hh" - namespace Kakoune { void register_highlighters(); -class DisplayBuffer; -class Window; - -class HighlighterGroup -{ -public: - void operator()(DisplayBuffer& display_buffer); - - void add_highlighter(HighlighterAndId&& highlighter); - void remove_highlighter(const std::string& id); - - CandidateList complete_highlighterid(const std::string& prefix, - size_t cursor_pos); - - static HighlighterAndId create(Window& window, - const HighlighterParameters& params); -private: - idvaluemap m_highlighters; -}; - } #endif // highlighters_hh_INCLUDED diff --git a/src/main.cc b/src/main.cc index 8ae523a7..cd26d3d4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -504,7 +504,7 @@ void add_group_highlighter(const CommandParameters& params, const Context& conte { HighlighterRegistry& registry = HighlighterRegistry::instance(); - HighlighterGroup& group = context.window().get_highlighter_group(params[0]); + HighlighterGroup& group = context.window().highlighters().get_group(params[0]); HighlighterParameters highlighter_params(params.begin()+2, params.end()); registry.add_highlighter_to_group(context.window(), group, params[1], highlighter_params); @@ -520,7 +520,7 @@ void rm_highlighter(const CommandParameters& params, const Context& context) if (params.size() != 1) throw wrong_argument_count(); - context.window().remove_highlighter(params[0]); + context.window().highlighters().remove(params[0]); } void rm_group_highlighter(const CommandParameters& params, const Context& context) @@ -530,8 +530,8 @@ void rm_group_highlighter(const CommandParameters& params, const Context& contex try { - HighlighterGroup& group = context.window().get_highlighter_group(params[0]); - group.remove_highlighter(params[1]); + HighlighterGroup& group = context.window().highlighters().get_group(params[0]); + group.remove(params[1]); } catch (runtime_error& err) { @@ -970,14 +970,14 @@ int main(int argc, char* argv[]) CommandManager::None, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) - { return main_context.window().complete_highlighter_groupid(prefix, cursor_pos); }, + { return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); }, std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) }); command_manager.register_command(std::vector{ "rh", "rmhl" }, rm_highlighter, CommandManager::None, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) - { return main_context.window().complete_highlighterid(prefix, cursor_pos); } + { return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); } }); command_manager.register_command(std::vector{ "rgh", "rmgrouphl" }, rm_group_highlighter, CommandManager::None, @@ -987,9 +987,9 @@ int main(int argc, char* argv[]) const std::string& arg = token_to_complete < params.size() ? params[token_to_complete] : std::string(); if (token_to_complete == 0) - return w.complete_highlighter_groupid(arg, pos_in_token); + return w.highlighters().complete_group_id(arg, pos_in_token); else if (token_to_complete == 1) - return w.get_highlighter_group(params[0]).complete_highlighterid(arg, pos_in_token); + return w.highlighters().get_group(params[0]).complete_id(arg, pos_in_token); }); command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter, CommandManager::None, diff --git a/src/window.cc b/src/window.cc index e0033a78..e220d0b3 100644 --- a/src/window.cc +++ b/src/window.cc @@ -2,7 +2,6 @@ #include "assert.hh" #include "highlighter_registry.hh" -#include "highlighters.hh" #include "hooks_manager.hh" #include @@ -341,11 +340,8 @@ void Window::update_display_buffer() m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end)); - for (auto& highlighter : m_highlighters) - { - highlighter.second(m_display_buffer); - m_display_buffer.check_invariant(); - } + m_highlighters(m_display_buffer); + m_display_buffer.check_invariant(); } void Window::set_dimensions(const DisplayCoord& dimensions) @@ -391,54 +387,6 @@ std::string Window::status_line() const return oss.str(); } -void Window::add_highlighter(HighlighterAndId&& highlighter) -{ - if (m_highlighters.contains(highlighter.first)) - throw id_not_unique(highlighter.first); - m_highlighters.append(std::forward(highlighter)); -} - -void Window::remove_highlighter(const std::string& id) -{ - m_highlighters.remove(id); -} - -HighlighterGroup& Window::get_highlighter_group(const std::string& id) -{ - auto group_it = m_highlighters.find(id); - - if (group_it == m_highlighters.end()) - throw runtime_error("no such group id " + id); - - HighlighterGroup* group = group_it->second.target(); - - if (not group) - throw runtime_error("not a group " + id); - - return *group; -} - -CandidateList Window::complete_highlighterid(const std::string& prefix, - size_t cursor_pos) -{ - return m_highlighters.complete_id(prefix, cursor_pos); -} - -CandidateList Window::complete_highlighter_groupid(const std::string& prefix, - size_t cursor_pos) -{ - CandidateList all = m_highlighters.complete_id(prefix, cursor_pos); - CandidateList result; - for (auto& id : all) - { - auto group_it = m_highlighters.find(id); - if (group_it != m_highlighters.end() and - group_it->second.target()) - result.push_back(id); - } - return result; -} - void Window::add_filter(FilterAndId&& filter) { if (m_filters.contains(filter.first)) diff --git a/src/window.hh b/src/window.hh index fe7b458d..c75dc2f2 100644 --- a/src/window.hh +++ b/src/window.hh @@ -8,6 +8,7 @@ #include "display_buffer.hh" #include "completion.hh" #include "highlighter.hh" +#include "highlighter_group.hh" #include "filter.hh" #include "idvaluemap.hh" @@ -102,14 +103,7 @@ public: : runtime_error("id not unique: " + id) {} }; - void add_highlighter(HighlighterAndId&& highlighter); - void remove_highlighter(const std::string& id); - HighlighterGroup& get_highlighter_group(const std::string& id); - - CandidateList complete_highlighterid(const std::string& prefix, - size_t cursor_pos); - CandidateList complete_highlighter_groupid(const std::string& prefix, - size_t cursor_pos); + HighlighterGroup& highlighters() { return m_highlighters; } void add_filter(FilterAndId&& filter); void remove_filter(const std::string& id); @@ -144,7 +138,7 @@ private: std::vector m_selections; DisplayBuffer m_display_buffer; - idvaluemap m_highlighters; + HighlighterGroup m_highlighters; idvaluemap m_filters; };