summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-06-10 19:58:02 +0100
committerMaxime Coste <frrrwww@gmail.com>2014-06-10 19:58:02 +0100
commit479c31b571513b068aaa5881eec3624d3ac8cd1b (patch)
tree1ad6038aeda8daa04db24e638984327f86a140ef /src
parent3791e74743c65f384893207d91f693a5c33343cd (diff)
Replace FunctionGroup template with HighlighterGroup class
Diffstat (limited to 'src')
-rw-r--r--src/function_group.hh76
-rw-r--r--src/function_registry.hh1
-rw-r--r--src/highlighter.hh7
-rw-r--r--src/highlighter_group.cc53
-rw-r--r--src/highlighter_group.hh41
-rw-r--r--src/highlighters.cc1
-rw-r--r--src/window.hh2
7 files changed, 97 insertions, 84 deletions
diff --git a/src/function_group.hh b/src/function_group.hh
deleted file mode 100644
index 025a8f92..00000000
--- a/src/function_group.hh
+++ /dev/null
@@ -1,76 +0,0 @@
-#ifndef function_group_hh_INCLUDED
-#define function_group_hh_INCLUDED
-
-#include "exception.hh"
-#include "id_map.hh"
-#include "string.hh"
-
-namespace Kakoune
-{
-
-struct group_not_found : public runtime_error
-{
- using runtime_error::runtime_error;
-};
-
-template<typename... Args>
-class FunctionGroup
-{
-public:
- using Function = std::function<void (Args...)>;
- using FunctionAndId = std::pair<String, std::function<void (Args...)>>;
-
- void operator()(Args... args)
- {
- for (auto& func : m_functions)
- func.second(std::forward<Args>(args)...);
- }
-
- void append(FunctionAndId&& function)
- {
- if (m_functions.contains(function.first))
- throw runtime_error("duplicate id: " + function.first);
-
- m_functions.append(std::forward<FunctionAndId>(function));
- }
- void remove(StringView id)
- {
- m_functions.remove(id);
- }
-
- FunctionGroup& get_group(StringView path, Codepoint path_separator = 0)
- {
- auto sep_it = std::find(path.begin(), path.end(), path_separator);
- StringView id(path.begin(), sep_it);
- auto it = m_functions.find(id);
- if (it == m_functions.end())
- throw group_not_found("no such id: "_str + id);
- FunctionGroup* group = it->second.template target<FunctionGroup>();
- if (not group)
- throw group_not_found("not a group: "_str + id);
- if (sep_it != path.end())
- return group->get_group(StringView(sep_it+1, path.end()), path_separator);
- else
- return *group;
- }
-
- CandidateList complete_id(StringView prefix, ByteCount cursor_pos) const
- {
- return m_functions.complete_id(prefix, cursor_pos);
- }
-
- CandidateList complete_group_id(StringView prefix, ByteCount cursor_pos) const
- {
- return m_functions.complete_id_if(
- prefix, cursor_pos, [](const FunctionAndId& func) {
- return func.second.template target<FunctionGroup>() != nullptr;
- });
- }
-
-private:
- id_map<Function> m_functions;
-};
-
-}
-
-#endif // function_group_hh_INCLUDED
diff --git a/src/function_registry.hh b/src/function_registry.hh
index 1565c36a..b7296eac 100644
--- a/src/function_registry.hh
+++ b/src/function_registry.hh
@@ -4,6 +4,7 @@
#include "completion.hh"
#include "id_map.hh"
#include "string.hh"
+#include "exception.hh"
namespace Kakoune
{
diff --git a/src/highlighter.hh b/src/highlighter.hh
index 2c7bb720..8060675d 100644
--- a/src/highlighter.hh
+++ b/src/highlighter.hh
@@ -1,7 +1,6 @@
#ifndef highlighter_hh_INCLUDED
#define highlighter_hh_INCLUDED
-#include "function_group.hh"
#include "function_registry.hh"
#include "memoryview.hh"
#include "string.hh"
@@ -30,17 +29,11 @@ using HighlighterFunc = std::function<void (const Context& context, HighlightFla
using HighlighterAndId = std::pair<String, HighlighterFunc>;
using HighlighterParameters = memoryview<String>;
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;
-using HighlighterGroup = FunctionGroup<const Context&, HighlightFlags, DisplayBuffer&>;
struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>,
Singleton<HighlighterRegistry>
{};
-struct DefinedHighlighters : public HighlighterGroup,
- public Singleton<DefinedHighlighters>
-{
-};
-
}
#endif // highlighter_hh_INCLUDED
diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc
new file mode 100644
index 00000000..c86e51cc
--- /dev/null
+++ b/src/highlighter_group.cc
@@ -0,0 +1,53 @@
+#include "highlighter_group.hh"
+
+namespace Kakoune
+{
+
+void HighlighterGroup::operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer)
+{
+ for (auto& hl : m_highlighters)
+ hl.second(context, flags, display_buffer);
+}
+
+void HighlighterGroup::append(HighlighterAndId&& hl)
+{
+ if (m_highlighters.contains(hl.first))
+ throw runtime_error("duplicate id: " + hl.first);
+
+ m_highlighters.append(std::move(hl));
+}
+void HighlighterGroup::remove(StringView id)
+{
+ m_highlighters.remove(id);
+}
+
+HighlighterGroup& HighlighterGroup::get_group(StringView path, Codepoint path_separator)
+{
+ auto sep_it = std::find(path.begin(), path.end(), path_separator);
+ StringView id(path.begin(), sep_it);
+ auto it = m_highlighters.find(id);
+ if (it == m_highlighters.end())
+ throw group_not_found("no such id: "_str + id);
+ HighlighterGroup* group = it->second.template target<HighlighterGroup>();
+ if (not group)
+ throw group_not_found("not a group: "_str + id);
+ if (sep_it != path.end())
+ return group->get_group(StringView(sep_it+1, path.end()), path_separator);
+ else
+ return *group;
+}
+
+CandidateList HighlighterGroup::complete_id(StringView prefix, ByteCount cursor_pos) const
+{
+ return m_highlighters.complete_id(prefix, cursor_pos);
+}
+
+CandidateList HighlighterGroup::complete_group_id(StringView prefix, ByteCount cursor_pos) const
+{
+ return m_highlighters.complete_id_if(
+ prefix, cursor_pos, [](const HighlighterAndId& func) {
+ return func.second.template target<HighlighterGroup>() != nullptr;
+ });
+}
+
+}
diff --git a/src/highlighter_group.hh b/src/highlighter_group.hh
new file mode 100644
index 00000000..8445e445
--- /dev/null
+++ b/src/highlighter_group.hh
@@ -0,0 +1,41 @@
+#ifndef highlighter_group_hh_INCLUDED
+#define highlighter_group_hh_INCLUDED
+
+#include "exception.hh"
+#include "id_map.hh"
+#include "highlighter.hh"
+#include "utils.hh"
+
+namespace Kakoune
+{
+
+struct group_not_found : public runtime_error
+{
+ using runtime_error::runtime_error;
+};
+
+class HighlighterGroup
+{
+public:
+ void operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer);
+
+ void append(HighlighterAndId&& hl);
+ void remove(StringView id);
+
+ HighlighterGroup& get_group(StringView path, Codepoint path_separator = 0);
+
+ CandidateList complete_id(StringView prefix, ByteCount cursor_pos) const;
+ CandidateList complete_group_id(StringView prefix, ByteCount cursor_pos) const;
+
+private:
+ id_map<HighlighterFunc> m_highlighters;
+};
+
+struct DefinedHighlighters : public HighlighterGroup,
+ public Singleton<DefinedHighlighters>
+{
+};
+
+}
+
+#endif // highlighter_group_hh_INCLUDED
diff --git a/src/highlighters.cc b/src/highlighters.cc
index ab765bb2..9cb66a05 100644
--- a/src/highlighters.cc
+++ b/src/highlighters.cc
@@ -1,5 +1,6 @@
#include "highlighters.hh"
+#include "highlighter_group.hh"
#include "assert.hh"
#include "buffer_utils.hh"
#include "color_registry.hh"
diff --git a/src/window.hh b/src/window.hh
index 6115f42d..74d4fa37 100644
--- a/src/window.hh
+++ b/src/window.hh
@@ -3,7 +3,7 @@
#include "completion.hh"
#include "display_buffer.hh"
-#include "highlighter.hh"
+#include "highlighter_group.hh"
#include "selection.hh"
#include "hook_manager.hh"
#include "option_manager.hh"