Pass the selection instead of only point of insertion to filters

This commit is contained in:
Maxime Coste 2012-11-19 13:40:23 +01:00
parent fd50046f3a
commit 45bd3dbe5a
6 changed files with 17 additions and 19 deletions

View File

@ -408,15 +408,13 @@ IncrementalInserter::~IncrementalInserter()
m_editor.on_incremental_insertion_end();
}
void IncrementalInserter::insert(const String& string)
void IncrementalInserter::insert(String content)
{
Buffer& buffer = m_editor.buffer();
for (auto& sel : m_editor.m_selections)
{
BufferIterator position = sel.last();
String content = string;
m_editor.filters()(buffer, position, content);
buffer.insert(position, content);
m_editor.filters()(buffer, sel.selection, content);
buffer.insert(sel.last(), content);
}
}

View File

@ -117,7 +117,7 @@ public:
IncrementalInserter(Editor& editor, InsertMode mode = InsertMode::Insert);
~IncrementalInserter();
void insert(const String& string);
void insert(String content);
void insert(const memoryview<String>& strings);
void erase();
void move_cursors(const BufferCoord& offset);

View File

@ -2,6 +2,7 @@
#define filter_hh_INCLUDED
#include "string.hh"
#include "selection.hh"
#include <functional>
namespace Kakoune
@ -14,8 +15,8 @@ class BufferIterator;
// Modification in order to mutate the Buffer or the Modification
// prior to it's application.
typedef std::function<void (Buffer& buffer, BufferIterator& position, String& content)> FilterFunc;
typedef std::pair<String, FilterFunc> FilterAndId;
using FilterFunc = std::function<void (Buffer& buffer, Selection& selection, String& content)>;
using FilterAndId = std::pair<String, FilterFunc>;
}

View File

@ -7,10 +7,10 @@ namespace Kakoune
{
void FilterGroup::operator()(Buffer& buffer,
BufferIterator& position, String& content)
Selection& selection, String& content)
{
for (auto& filter : m_filters)
filter.second(buffer, position, content);
filter.second(buffer, selection, content);
}
void FilterGroup::append(FilterAndId&& filter)

View File

@ -12,7 +12,7 @@ namespace Kakoune
class FilterGroup
{
public:
void operator()(Buffer& buffer, BufferIterator& position, String& content);
void operator()(Buffer& buffer, Selection& selection, String& content);
void append(FilterAndId&& filter);
void remove(const String& id);

View File

@ -6,11 +6,11 @@
namespace Kakoune
{
void preserve_indent(Buffer& buffer, BufferIterator& position, String& content)
void preserve_indent(Buffer& buffer, Selection& selection, String& content)
{
if (content == "\n")
{
BufferIterator line_begin = buffer.iterator_at_line_begin(position - 1);
BufferIterator line_begin = buffer.iterator_at_line_begin(selection.last() - 1);
BufferIterator first_non_white = line_begin;
while ((*first_non_white == '\t' or *first_non_white == ' ') and
not first_non_white.is_end())
@ -20,8 +20,9 @@ void preserve_indent(Buffer& buffer, BufferIterator& position, String& content)
}
}
void cleanup_whitespaces(Buffer& buffer, BufferIterator& position, String& content)
void cleanup_whitespaces(Buffer& buffer, Selection& selection, String& content)
{
const BufferIterator& position = selection.last();
if (content[0] == '\n' and not position.is_begin())
{
BufferIterator whitespace_start = position-1;
@ -30,19 +31,17 @@ void cleanup_whitespaces(Buffer& buffer, BufferIterator& position, String& conte
--whitespace_start;
++whitespace_start;
if (whitespace_start!= position)
{
buffer.erase(whitespace_start, position);
position = whitespace_start;
}
}
}
void expand_tabulations(Buffer& buffer, BufferIterator& position, String& content)
void expand_tabulations(Buffer& buffer, Selection& selection, String& content)
{
const int tabstop = buffer.option_manager()["tabstop"].as_int();
if (content == "\t")
{
int column = 0;
const BufferIterator& position = selection.last();
for (auto line_it = buffer.iterator_at_line_begin(position);
line_it != position; ++line_it)
{
@ -60,7 +59,7 @@ void expand_tabulations(Buffer& buffer, BufferIterator& position, String& conten
}
}
template<void (*filter_func)(Buffer&, BufferIterator&, String&)>
template<void (*filter_func)(Buffer&, Selection&, String&)>
class SimpleFilterFactory
{
public: