HighlighterGroup: move to it's own file, Window uses them directly

This commit is contained in:
Maxime Coste 2012-01-19 20:37:29 +00:00
parent 2fe07d2dad
commit 2824bd9a46
8 changed files with 109 additions and 123 deletions

47
src/highlighter_group.cc Normal file
View File

@ -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<HighlighterAndId>(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<HighlighterGroup>();
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<str_to_str>(prefix, cursor_pos);
}
}

43
src/highlighter_group.hh Normal file
View File

@ -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<HighlighterGroup>())
res.push_back(id);
}
return res;
}
private:
idvaluemap<std::string, HighlighterFunc> m_highlighters;
};
}
#endif // highlighter_group_hh_INCLUDED

View File

@ -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,

View File

@ -4,6 +4,7 @@
#include "window.hh"
#include "display_buffer.hh"
#include "highlighter_registry.hh"
#include "highlighter_group.hh"
#include <boost/regex.hpp>
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<HighlighterAndId>(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<str_to_str>(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_tabulations>("expand_tabs"));
registry.register_factory("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
registry.register_factory("regex", colorize_regex_factory);
registry.register_factory("group", HighlighterGroup::create);
registry.register_factory("group", highlighter_group_factory);
}
}

View File

@ -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<std::string, HighlighterFunc> m_highlighters;
};
}
#endif // highlighters_hh_INCLUDED

View File

@ -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<std::string>{ "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<std::string>{ "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<std::string>{ "af", "addfilter" }, add_filter,
CommandManager::None,

View File

@ -2,7 +2,6 @@
#include "assert.hh"
#include "highlighter_registry.hh"
#include "highlighters.hh"
#include "hooks_manager.hh"
#include <algorithm>
@ -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<HighlighterAndId>(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<HighlighterGroup>();
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<str_to_str>(prefix, cursor_pos);
}
CandidateList Window::complete_highlighter_groupid(const std::string& prefix,
size_t cursor_pos)
{
CandidateList all = m_highlighters.complete_id<str_to_str>(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<HighlighterGroup>())
result.push_back(id);
}
return result;
}
void Window::add_filter(FilterAndId&& filter)
{
if (m_filters.contains(filter.first))

View File

@ -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<SelectionList> m_selections;
DisplayBuffer m_display_buffer;
idvaluemap<std::string, HighlighterFunc> m_highlighters;
HighlighterGroup m_highlighters;
idvaluemap<std::string, FilterFunc> m_filters;
};