1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 12:36:11 +03:00

Merge {Filter,Highlighter}Group in a FunctionGroup template

This commit is contained in:
Maxime Coste 2013-03-27 13:41:41 +01:00
parent ee0e640384
commit 251f09ff89
11 changed files with 72 additions and 183 deletions

View File

@ -6,7 +6,6 @@
#include "filter.hh"
#include "idvaluemap.hh"
#include "memoryview.hh"
#include "filter_group.hh"
namespace Kakoune
{

View File

@ -7,6 +7,7 @@
#include "utils.hh"
#include "memoryview.hh"
#include "function_registry.hh"
#include "function_group.hh"
namespace Kakoune
{
@ -20,6 +21,7 @@ struct Selection;
using FilterFunc = std::function<void (Buffer& buffer, Selection& selection, String& content)>;
using FilterAndId = std::pair<String, FilterFunc>;
using FilterGroup = FunctionGroup<Buffer&, Selection&, String&>;
using FilterParameters = memoryview<String>;
using FilterFactory = std::function<FilterAndId (const FilterParameters& params)>;

View File

@ -1,57 +0,0 @@
#include "filter_group.hh"
#include "exception.hh"
#include "utils.hh"
namespace Kakoune
{
void FilterGroup::operator()(Buffer& buffer,
Selection& selection, String& content)
{
for (auto& filter : m_filters)
filter.second(buffer, selection, content);
}
void FilterGroup::append(FilterAndId&& filter)
{
if (m_filters.contains(filter.first))
throw runtime_error("duplicate filter id: " + filter.first);
m_filters.append(std::forward<FilterAndId>(filter));
}
void FilterGroup::remove(const String& id)
{
m_filters.remove(id);
}
FilterGroup& FilterGroup::get_group(const String& id)
{
auto it = m_filters.find(id);
if (it == m_filters.end())
throw runtime_error("no such id: " + id);
FilterGroup* group = it->second.target<FilterGroup>();
if (not group)
throw runtime_error("not a group: " + id);
return *group;
}
CandidateList FilterGroup::complete_id(const String& prefix,
ByteCount cursor_pos)
{
return m_filters.complete_id(prefix, cursor_pos);
}
CandidateList FilterGroup::complete_group_id(const String& prefix,
ByteCount cursor_pos)
{
return m_filters.complete_id_if(
prefix, cursor_pos,
[](std::pair<String, FilterFunc>& func)
{ return func.second.target<FilterGroup>() != nullptr; });
}
}

View File

@ -1,31 +0,0 @@
#ifndef filter_group_hh_INCLUDED
#define filter_group_hh_INCLUDED
#include "filter.hh"
#include "idvaluemap.hh"
namespace Kakoune
{
// FilterGroup is an filter which delegate to multiple
// other filters in order of insertion.
class FilterGroup
{
public:
void operator()(Buffer& buffer, Selection& selection, String& content);
void append(FilterAndId&& filter);
void remove(const String& id);
FilterGroup& get_group(const String& id);
CandidateList complete_id(const String& prefix, ByteCount cursor_pos);
CandidateList complete_group_id(const String& prefix, ByteCount cursor_pos);
private:
idvaluemap<String, FilterFunc> m_filters;
};
}
#endif // filter_group_hh_INCLUDED

View File

@ -1,6 +1,5 @@
#include "filters.hh"
#include "buffer.hh"
#include "filter_group.hh"
#include "selection.hh"
namespace Kakoune

66
src/function_group.hh Normal file
View File

@ -0,0 +1,66 @@
#ifndef function_group_hh_INCLUDED
#define function_group_hh_INCLUDED
#include "string.hh"
#include "idvaluemap.hh"
#include "exception.hh"
namespace Kakoune
{
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(const String& id)
{
m_functions.remove(id);
}
FunctionGroup& get_group(const String& id)
{
auto it = m_functions.find(id);
if (it == m_functions.end())
throw runtime_error("no such id: " + id);
FunctionGroup* group = it->second.template target<FunctionGroup>();
if (not group)
throw runtime_error("not a group: " + id);
return *group;
}
CandidateList complete_id(const String& prefix, ByteCount cursor_pos)
{
return m_functions.complete_id(prefix, cursor_pos);
}
CandidateList complete_group_id(const String& prefix, ByteCount cursor_pos)
{
return m_functions.complete_id_if(
prefix, cursor_pos, [](FunctionAndId& func) {
return func.second.template target<FunctionGroup>() != nullptr;
});
}
private:
idvaluemap<String, Function> m_functions;
};
}
#endif // function_group_hh_INCLUDED

View File

@ -7,6 +7,7 @@
#include "utils.hh"
#include "memoryview.hh"
#include "function_registry.hh"
#include "function_group.hh"
namespace Kakoune
{
@ -26,6 +27,8 @@ typedef memoryview<String> HighlighterParameters;
using HighlighterFactory = std::function<HighlighterAndId (const HighlighterParameters& params,
const Window& window)>;
using HighlighterGroup = FunctionGroup<DisplayBuffer&>;
struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>,
Singleton<HighlighterRegistry>
{};

View File

@ -1,56 +0,0 @@
#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("duplicate highlighter id: " + highlighter.first);
m_highlighters.append(std::forward<HighlighterAndId>(highlighter));
}
void HighlighterGroup::remove(const String& id)
{
m_highlighters.remove(id);
}
HighlighterGroup& HighlighterGroup::get_group(const 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 String& prefix,
ByteCount cursor_pos)
{
return m_highlighters.complete_id(prefix, cursor_pos);
}
CandidateList HighlighterGroup::complete_group_id(const String& prefix,
ByteCount cursor_pos)
{
return m_highlighters.complete_id_if(
prefix, cursor_pos,
[](std::pair<String, HighlighterFunc>& func)
{ return func.second.target<HighlighterGroup>() != nullptr; });
}
}

View File

@ -1,34 +0,0 @@
#ifndef highlighter_group_hh_INCLUDED
#define highlighter_group_hh_INCLUDED
#include "highlighter.hh"
#include "idvaluemap.hh"
namespace Kakoune
{
class DisplayBuffer;
class Window;
// HighlighterGroup is an highlighter which delegate to multiple
// other highlighters in order of insertion.
class HighlighterGroup
{
public:
void operator()(DisplayBuffer& display_buffer);
void append(HighlighterAndId&& highlighter);
void remove(const String& id);
HighlighterGroup& get_group(const String& id);
CandidateList complete_id(const String& prefix, ByteCount cursor_pos);
CandidateList complete_group_id(const String& prefix, ByteCount cursor_pos);
private:
idvaluemap<String, HighlighterFunc> m_highlighters;
};
}
#endif // highlighter_group_hh_INCLUDED

View File

@ -1,7 +1,6 @@
#include "highlighters.hh"
#include "assert.hh"
#include "color_registry.hh"
#include "highlighter_group.hh"
#include "register_manager.hh"
#include "context.hh"
#include "string.hh"

View File

@ -5,13 +5,12 @@
#include "display_buffer.hh"
#include "completion.hh"
#include "highlighter.hh"
#include "highlighter_group.hh"
#include "highlighter.hh"
#include "hook_manager.hh"
#include "option_manager.hh"
namespace Kakoune
{
class HighlighterGroup;
// A Window is an editing view onto a Buffer
//