Filters: move from buffer to window

This commit is contained in:
Maxime Coste 2011-12-07 14:29:10 +00:00
parent c40eb2b03a
commit 2239fb6e49
9 changed files with 60 additions and 58 deletions

View File

@ -218,9 +218,6 @@ void Buffer::apply_modification(const Modification& modification)
void Buffer::modify(Modification&& modification) void Buffer::modify(Modification&& modification)
{ {
for (auto filter : m_filters)
filter.second(*this, modification);
apply_modification(modification); apply_modification(modification);
m_current_undo_group.push_back(std::move(modification)); m_current_undo_group.push_back(std::move(modification));
} }
@ -271,22 +268,4 @@ void Buffer::unregister_modification_listener(ModificationListener* listener)
m_modification_listeners.erase(it); m_modification_listeners.erase(it);
} }
void Buffer::add_filter(FilterAndId&& filter)
{
if (m_filters.contains(filter.first))
throw filter_id_not_unique(filter.first);
m_filters.append(filter);
}
void Buffer::remove_filter(const std::string& id)
{
m_filters.remove(id);
}
CandidateList Buffer::complete_filterid(const std::string& prefix,
size_t cursor_pos)
{
return m_filters.complete_id<str_to_str>(prefix, cursor_pos);
}
} }

View File

@ -7,10 +7,6 @@
#include <memory> #include <memory>
#include "line_and_column.hh" #include "line_and_column.hh"
#include "filter.hh"
#include "exception.hh"
#include "completion.hh"
#include "idvaluemap.hh"
namespace Kakoune namespace Kakoune
{ {
@ -154,18 +150,6 @@ public:
void register_modification_listener(ModificationListener* listener); void register_modification_listener(ModificationListener* listener);
void unregister_modification_listener(ModificationListener* listener); void unregister_modification_listener(ModificationListener* listener);
struct filter_id_not_unique : public runtime_error
{
filter_id_not_unique(const std::string& id)
: runtime_error("filter id not unique: " + id) {}
};
void add_filter(FilterAndId&& filter);
void remove_filter(const std::string& id);
CandidateList complete_filterid(const std::string& prefix,
size_t cursor_pos = std::string::npos);
// returns an iterator pointing to the first character of the line // returns an iterator pointing to the first character of the line
// iterator is on // iterator is on
BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const; BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const;
@ -205,8 +189,6 @@ private:
size_t m_last_save_undo_index; size_t m_last_save_undo_index;
std::vector<ModificationListener*> m_modification_listeners; std::vector<ModificationListener*> m_modification_listeners;
idvaluemap<std::string, FilterFunc> m_filters;
}; };
inline Modification Modification::make_erase(BufferIterator begin, inline Modification Modification::make_erase(BufferIterator begin,

View File

@ -1,7 +1,7 @@
#include "filter_registry.hh" #include "filter_registry.hh"
#include "exception.hh" #include "exception.hh"
#include "buffer.hh" #include "window.hh"
namespace Kakoune namespace Kakoune
{ {
@ -19,7 +19,7 @@ void FilterRegistry::register_factory(const std::string& name,
m_factories.append(std::make_pair(name, factory)); m_factories.append(std::make_pair(name, factory));
} }
void FilterRegistry::add_filter_to_buffer(Buffer& buffer, void FilterRegistry::add_filter_to_window(Window& window,
const std::string& name, const std::string& name,
const FilterParameters& parameters) const FilterParameters& parameters)
{ {
@ -27,7 +27,7 @@ void FilterRegistry::add_filter_to_buffer(Buffer& buffer,
if (it == m_factories.end()) if (it == m_factories.end())
throw factory_not_found(name); throw factory_not_found(name);
buffer.add_filter(it->second(buffer, parameters)); window.add_filter(it->second(window, parameters));
} }
CandidateList FilterRegistry::complete_filter(const std::string& prefix, CandidateList FilterRegistry::complete_filter(const std::string& prefix,

View File

@ -16,7 +16,7 @@ class Window;
typedef std::vector<std::string> FilterParameters; typedef std::vector<std::string> FilterParameters;
typedef std::function<FilterAndId (Buffer& buffer, typedef std::function<FilterAndId (Window& window,
const FilterParameters& params)> FilterFactory; const FilterParameters& params)> FilterFactory;
class FilterRegistry : public Singleton<FilterRegistry> class FilterRegistry : public Singleton<FilterRegistry>
@ -25,7 +25,7 @@ public:
void register_factory(const std::string& name, void register_factory(const std::string& name,
const FilterFactory& factory); const FilterFactory& factory);
void add_filter_to_buffer(Buffer& window, void add_filter_to_window(Window& window,
const std::string& factory_name, const std::string& factory_name,
const FilterParameters& parameters); const FilterParameters& parameters);

View File

@ -49,7 +49,7 @@ class SimpleFilterFactory
public: public:
SimpleFilterFactory(const std::string& id) : m_id(id) {} SimpleFilterFactory(const std::string& id) : m_id(id) {}
FilterAndId operator()(Buffer& buffer, FilterAndId operator()(Window& window,
const FilterParameters& params) const const FilterParameters& params) const
{ {
return FilterAndId(m_id, FilterFunc(filter_func)); return FilterAndId(m_id, FilterFunc(filter_func));

View File

@ -1,3 +1,3 @@
hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addhl hlcpp hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addhl hlcpp
hook BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter preserve_indent hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter preserve_indent
hook WinCreate .* addhl regex \h+(?=\n) default red hook WinCreate .* addhl regex \h+(?=\n) default red

View File

@ -499,7 +499,7 @@ void add_filter(const CommandParameters& params, const Context& context)
{ {
FilterRegistry& registry = FilterRegistry::instance(); FilterRegistry& registry = FilterRegistry::instance();
FilterParameters filter_params(params.begin()+1, params.end()); FilterParameters filter_params(params.begin()+1, params.end());
registry.add_filter_to_buffer(*context.buffer, params[0], registry.add_filter_to_window(*context.window, params[0],
filter_params); filter_params);
} }
catch (runtime_error& err) catch (runtime_error& err)
@ -513,7 +513,7 @@ void rm_filter(const CommandParameters& params, const Context& context)
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
context.buffer->remove_filter(params[0]); context.window->remove_filter(params[0]);
} }
void add_hook(const CommandParameters& params, const Context& context) void add_hook(const CommandParameters& params, const Context& context)
@ -770,7 +770,7 @@ int main(int argc, char* argv[])
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter, command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.buffer->complete_filterid(prefix, cursor_pos); } { return main_context.window->complete_filterid(prefix, cursor_pos); }
}); });
command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook); command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook);

View File

@ -353,7 +353,7 @@ std::string Window::status_line() const
void Window::add_highlighter(HighlighterAndId&& highlighter) void Window::add_highlighter(HighlighterAndId&& highlighter)
{ {
if (m_highlighters.contains(highlighter.first)) if (m_highlighters.contains(highlighter.first))
throw highlighter_id_not_unique(highlighter.first); throw id_not_unique(highlighter.first);
m_highlighters.append(highlighter); m_highlighters.append(highlighter);
} }
@ -368,6 +368,24 @@ CandidateList Window::complete_highlighterid(const std::string& prefix,
return m_highlighters.complete_id<str_to_str>(prefix, cursor_pos); return m_highlighters.complete_id<str_to_str>(prefix, cursor_pos);
} }
void Window::add_filter(FilterAndId&& filter)
{
if (m_filters.contains(filter.first))
throw id_not_unique(filter.first);
m_filters.append(filter);
}
void Window::remove_filter(const std::string& id)
{
m_filters.remove(id);
}
CandidateList Window::complete_filterid(const std::string& prefix,
size_t cursor_pos)
{
return m_filters.complete_id<str_to_str>(prefix, cursor_pos);
}
IncrementalInserter::IncrementalInserter(Window& window, Mode mode) IncrementalInserter::IncrementalInserter(Window& window, Mode mode)
: m_window(window) : m_window(window)
@ -394,14 +412,14 @@ IncrementalInserter::IncrementalInserter(Window& window, Mode mode)
case Mode::AppendAtLineEnd: case Mode::AppendAtLineEnd:
pos = m_window.m_buffer.iterator_at_line_end(sel.end() - 1) - 1; pos = m_window.m_buffer.iterator_at_line_end(sel.end() - 1) - 1;
if (mode == Mode::OpenLineBelow) if (mode == Mode::OpenLineBelow)
window.m_buffer.modify(Modification::make_insert(pos, "\n")); apply(Modification::make_insert(pos, "\n"));
break; break;
case Mode::OpenLineAbove: case Mode::OpenLineAbove:
case Mode::InsertAtLineBegin: case Mode::InsertAtLineBegin:
pos = m_window.m_buffer.iterator_at_line_begin(sel.begin()); pos = m_window.m_buffer.iterator_at_line_begin(sel.begin());
if (mode == Mode::OpenLineAbove) if (mode == Mode::OpenLineAbove)
window.m_buffer.modify(Modification::make_insert(--pos, "\n")); apply(Modification::make_insert(--pos, "\n"));
break; break;
} }
sel = Selection(pos, pos, sel.captures()); sel = Selection(pos, pos, sel.captures());
@ -417,9 +435,18 @@ IncrementalInserter::~IncrementalInserter()
m_window.m_buffer.end_undo_group(); m_window.m_buffer.end_undo_group();
} }
void IncrementalInserter::apply(Modification&& modification) const
{
for (auto filter : m_window.m_filters)
filter.second(m_window.buffer(), modification);
m_window.buffer().modify(std::move(modification));
}
void IncrementalInserter::insert(const Window::String& string) void IncrementalInserter::insert(const Window::String& string)
{ {
m_window.insert_noundo(string); for (auto& sel : m_window.m_selections)
apply(Modification::make_insert(sel.begin(), string));
} }
void IncrementalInserter::insert_capture(size_t index) void IncrementalInserter::insert_capture(size_t index)
@ -433,8 +460,11 @@ void IncrementalInserter::insert_capture(size_t index)
void IncrementalInserter::erase() void IncrementalInserter::erase()
{ {
for (auto& sel : m_window.m_selections) for (auto& sel : m_window.m_selections)
{
sel = Selection(sel.first() - 1, sel.last() - 1); sel = Selection(sel.first() - 1, sel.last() - 1);
m_window.erase_noundo(); apply(Modification::make_erase(sel.begin(), sel.end()));
}
m_window.scroll_to_keep_cursor_visible_ifn(); m_window.scroll_to_keep_cursor_visible_ifn();
} }

View File

@ -8,6 +8,7 @@
#include "display_buffer.hh" #include "display_buffer.hh"
#include "completion.hh" #include "completion.hh"
#include "highlighter.hh" #include "highlighter.hh"
#include "filter.hh"
#include "idvaluemap.hh" #include "idvaluemap.hh"
namespace Kakoune namespace Kakoune
@ -88,10 +89,10 @@ public:
std::string status_line() const; std::string status_line() const;
struct highlighter_id_not_unique : public runtime_error struct id_not_unique : public runtime_error
{ {
highlighter_id_not_unique(const std::string& id) id_not_unique(const std::string& id)
: runtime_error("highlighter id not unique: " + id) {} : runtime_error("id not unique: " + id) {}
}; };
void add_highlighter(HighlighterAndId&& highlighter); void add_highlighter(HighlighterAndId&& highlighter);
@ -100,6 +101,13 @@ public:
CandidateList complete_highlighterid(const std::string& prefix, CandidateList complete_highlighterid(const std::string& prefix,
size_t cursor_pos = std::string::npos); size_t cursor_pos = std::string::npos);
void add_filter(FilterAndId&& filter);
void remove_filter(const std::string& id);
CandidateList complete_filterid(const std::string& prefix,
size_t cursor_pos = std::string::npos);
private: private:
friend class Buffer; friend class Buffer;
@ -123,6 +131,7 @@ private:
DisplayBuffer m_display_buffer; DisplayBuffer m_display_buffer;
idvaluemap<std::string, HighlighterFunc> m_highlighters; idvaluemap<std::string, HighlighterFunc> m_highlighters;
idvaluemap<std::string, FilterFunc> m_filters;
}; };
class IncrementalInserter class IncrementalInserter
@ -148,7 +157,9 @@ public:
void move_cursor(const DisplayCoord& offset); void move_cursor(const DisplayCoord& offset);
private: private:
Window& m_window; void apply(Modification&& modification) const;
Window& m_window;
}; };
} }