Buffer: rename BufferModification to Modification
This commit is contained in:
parent
73cebb9072
commit
2edddfe009
|
@ -39,13 +39,13 @@ Buffer::~Buffer()
|
||||||
|
|
||||||
void Buffer::erase(const BufferIterator& begin, const BufferIterator& end)
|
void Buffer::erase(const BufferIterator& begin, const BufferIterator& end)
|
||||||
{
|
{
|
||||||
append_modification(BufferModification(BufferModification::Erase,
|
append_modification(Modification(Modification::Erase, begin,
|
||||||
begin, string(begin, end)));
|
string(begin, end)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::insert(const BufferIterator& position, const BufferString& string)
|
void Buffer::insert(const BufferIterator& position, const BufferString& string)
|
||||||
{
|
{
|
||||||
append_modification(BufferModification(BufferModification::Insert, position, string));
|
append_modification(Modification(Modification::Insert, position, string));
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const
|
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const
|
||||||
|
@ -169,7 +169,7 @@ void Buffer::end_undo_group()
|
||||||
m_current_undo_group.clear();
|
m_current_undo_group.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferModification BufferModification::inverse() const
|
Modification Modification::inverse() const
|
||||||
{
|
{
|
||||||
Type inverse_type;
|
Type inverse_type;
|
||||||
switch (type)
|
switch (type)
|
||||||
|
@ -178,7 +178,7 @@ BufferModification BufferModification::inverse() const
|
||||||
case Erase: inverse_type = Insert; break;
|
case Erase: inverse_type = Insert; break;
|
||||||
default: assert(false);
|
default: assert(false);
|
||||||
}
|
}
|
||||||
return BufferModification(inverse_type, position, content);
|
return Modification(inverse_type, position, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Buffer::undo()
|
bool Buffer::undo()
|
||||||
|
@ -188,7 +188,7 @@ bool Buffer::undo()
|
||||||
|
|
||||||
--m_history_cursor;
|
--m_history_cursor;
|
||||||
|
|
||||||
for (const BufferModification& modification : reversed(*m_history_cursor))
|
for (const Modification& modification : reversed(*m_history_cursor))
|
||||||
apply_modification(modification.inverse());
|
apply_modification(modification.inverse());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,21 +197,21 @@ bool Buffer::redo()
|
||||||
if (m_history_cursor == m_history.end())
|
if (m_history_cursor == m_history.end())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (const BufferModification& modification : *m_history_cursor)
|
for (const Modification& modification : *m_history_cursor)
|
||||||
apply_modification(modification);
|
apply_modification(modification);
|
||||||
|
|
||||||
++m_history_cursor;
|
++m_history_cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::apply_modification(const BufferModification& modification)
|
void Buffer::apply_modification(const Modification& modification)
|
||||||
{
|
{
|
||||||
switch (modification.type)
|
switch (modification.type)
|
||||||
{
|
{
|
||||||
case BufferModification::Insert:
|
case Modification::Insert:
|
||||||
m_content.insert(modification.position.m_position,
|
m_content.insert(modification.position.m_position,
|
||||||
modification.content);
|
modification.content);
|
||||||
break;
|
break;
|
||||||
case BufferModification::Erase:
|
case Modification::Erase:
|
||||||
{
|
{
|
||||||
size_t size = modification.content.size();
|
size_t size = modification.content.size();
|
||||||
assert(string(modification.position, modification.position + size)
|
assert(string(modification.position, modification.position + size)
|
||||||
|
@ -227,7 +227,7 @@ void Buffer::apply_modification(const BufferModification& modification)
|
||||||
listener->on_modification(modification);
|
listener->on_modification(modification);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::append_modification(BufferModification&& modification)
|
void Buffer::append_modification(Modification&& modification)
|
||||||
{
|
{
|
||||||
for (auto filter : m_filters)
|
for (auto filter : m_filters)
|
||||||
filter.second(*this, modification);
|
filter.second(*this, modification);
|
||||||
|
@ -265,14 +265,14 @@ void Buffer::notify_saved()
|
||||||
m_last_save_undo_index = history_cursor_index;
|
m_last_save_undo_index = history_cursor_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::register_modification_listener(BufferModificationListener* listener)
|
void Buffer::register_modification_listener(ModificationListener* listener)
|
||||||
{
|
{
|
||||||
assert(listener);
|
assert(listener);
|
||||||
assert(not contains(m_modification_listeners, listener));
|
assert(not contains(m_modification_listeners, listener));
|
||||||
m_modification_listeners.push_back(listener);
|
m_modification_listeners.push_back(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::unregister_modification_listener(BufferModificationListener* listener)
|
void Buffer::unregister_modification_listener(ModificationListener* listener)
|
||||||
{
|
{
|
||||||
assert(listener);
|
assert(listener);
|
||||||
auto it = std::find(m_modification_listeners.begin(),
|
auto it = std::find(m_modification_listeners.begin(),
|
||||||
|
|
|
@ -77,7 +77,7 @@ private:
|
||||||
friend class Buffer;
|
friend class Buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BufferModification
|
struct Modification
|
||||||
{
|
{
|
||||||
enum Type { Insert, Erase };
|
enum Type { Insert, Erase };
|
||||||
|
|
||||||
|
@ -85,17 +85,17 @@ struct BufferModification
|
||||||
BufferIterator position;
|
BufferIterator position;
|
||||||
BufferString content;
|
BufferString content;
|
||||||
|
|
||||||
BufferModification(Type type, BufferIterator position,
|
Modification(Type type, BufferIterator position,
|
||||||
BufferString content)
|
BufferString content)
|
||||||
: type(type), position(position), content(content) {}
|
: type(type), position(position), content(content) {}
|
||||||
|
|
||||||
BufferModification inverse() const;
|
Modification inverse() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class BufferModificationListener
|
class ModificationListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void on_modification(const BufferModification& modification) = 0;
|
virtual void on_modification(const Modification& modification) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Buffer
|
class Buffer
|
||||||
|
@ -151,8 +151,8 @@ public:
|
||||||
Type type() const { return m_type; }
|
Type type() const { return m_type; }
|
||||||
void notify_saved();
|
void notify_saved();
|
||||||
|
|
||||||
void register_modification_listener(BufferModificationListener* listener);
|
void register_modification_listener(ModificationListener* listener);
|
||||||
void unregister_modification_listener(BufferModificationListener* listener);
|
void unregister_modification_listener(ModificationListener* listener);
|
||||||
|
|
||||||
struct filter_id_not_unique : public runtime_error
|
struct filter_id_not_unique : public runtime_error
|
||||||
{
|
{
|
||||||
|
@ -191,22 +191,22 @@ private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
const Type m_type;
|
const Type m_type;
|
||||||
|
|
||||||
typedef std::vector<BufferModification> UndoGroup;
|
typedef std::vector<Modification> UndoGroup;
|
||||||
|
|
||||||
std::vector<UndoGroup> m_history;
|
std::vector<UndoGroup> m_history;
|
||||||
std::vector<UndoGroup>::iterator m_history_cursor;
|
std::vector<UndoGroup>::iterator m_history_cursor;
|
||||||
UndoGroup m_current_undo_group;
|
UndoGroup m_current_undo_group;
|
||||||
|
|
||||||
void apply_modification(const BufferModification& modification);
|
void apply_modification(const Modification& modification);
|
||||||
void revert_modification(const BufferModification& modification);
|
void revert_modification(const Modification& modification);
|
||||||
|
|
||||||
void append_modification(BufferModification&& modification);
|
void append_modification(Modification&& modification);
|
||||||
|
|
||||||
std::list<std::unique_ptr<Window>> m_windows;
|
std::list<std::unique_ptr<Window>> m_windows;
|
||||||
|
|
||||||
size_t m_last_save_undo_index;
|
size_t m_last_save_undo_index;
|
||||||
|
|
||||||
std::vector<BufferModificationListener*> m_modification_listeners;
|
std::vector<ModificationListener*> m_modification_listeners;
|
||||||
|
|
||||||
idvaluemap<std::string, FilterFunc> m_filters;
|
idvaluemap<std::string, FilterFunc> m_filters;
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,13 +36,13 @@ DynamicBufferIterator::~DynamicBufferIterator()
|
||||||
unregister_ifn();
|
unregister_ifn();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicBufferIterator::on_modification(const BufferModification& modification)
|
void DynamicBufferIterator::on_modification(const Modification& modification)
|
||||||
{
|
{
|
||||||
if (*this < modification.position)
|
if (*this < modification.position)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
size_t length = modification.content.length();
|
size_t length = modification.content.length();
|
||||||
if (modification.type == BufferModification::Erase)
|
if (modification.type == Modification::Erase)
|
||||||
{
|
{
|
||||||
// do not move length on the other side of the inequality,
|
// do not move length on the other side of the inequality,
|
||||||
// as modification.position + length may be after buffer end
|
// as modification.position + length may be after buffer end
|
||||||
|
@ -53,7 +53,7 @@ void DynamicBufferIterator::on_modification(const BufferModification& modificati
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assert(modification.type == BufferModification::Insert);
|
assert(modification.type == Modification::Insert);
|
||||||
*this += length;
|
*this += length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
class DynamicBufferIterator : public BufferIterator,
|
class DynamicBufferIterator : public BufferIterator,
|
||||||
public BufferModificationListener
|
public ModificationListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DynamicBufferIterator() : BufferIterator() {}
|
DynamicBufferIterator() : BufferIterator() {}
|
||||||
|
@ -22,7 +22,7 @@ public:
|
||||||
{ return this->operator= (static_cast<const BufferIterator&>(other)); }
|
{ return this->operator= (static_cast<const BufferIterator&>(other)); }
|
||||||
~DynamicBufferIterator();
|
~DynamicBufferIterator();
|
||||||
|
|
||||||
void on_modification(const BufferModification& modification);
|
void on_modification(const Modification& modification);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void register_ifp();
|
void register_ifp();
|
||||||
|
|
|
@ -8,9 +8,9 @@ namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
class Buffer;
|
class Buffer;
|
||||||
class BufferModification;
|
class Modification;
|
||||||
|
|
||||||
typedef std::function<void (Buffer& buffer, BufferModification& modification)> FilterFunc;
|
typedef std::function<void (Buffer& buffer, Modification& modification)> FilterFunc;
|
||||||
typedef std::pair<std::string, FilterFunc> FilterAndId;
|
typedef std::pair<std::string, FilterFunc> FilterAndId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
void preserve_indent(Buffer& buffer, BufferModification& modification)
|
void preserve_indent(Buffer& buffer, Modification& modification)
|
||||||
{
|
{
|
||||||
if (modification.type == BufferModification::Insert and
|
if (modification.type == Modification::Insert and
|
||||||
modification.content == "\n")
|
modification.content == "\n")
|
||||||
{
|
{
|
||||||
BufferIterator line_begin = buffer.iterator_at_line_begin(modification.position - 1);
|
BufferIterator line_begin = buffer.iterator_at_line_begin(modification.position - 1);
|
||||||
|
@ -20,10 +20,10 @@ void preserve_indent(Buffer& buffer, BufferModification& modification)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void expand_tabulations(Buffer& buffer, BufferModification& modification)
|
void expand_tabulations(Buffer& buffer, Modification& modification)
|
||||||
{
|
{
|
||||||
const int tabstop = 8;
|
const int tabstop = 8;
|
||||||
if (modification.type == BufferModification::Insert and
|
if (modification.type == Modification::Insert and
|
||||||
modification.content == "\t")
|
modification.content == "\t")
|
||||||
{
|
{
|
||||||
int column = 0;
|
int column = 0;
|
||||||
|
@ -43,7 +43,7 @@ void expand_tabulations(Buffer& buffer, BufferModification& modification)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<void (*filter_func)(Buffer&, BufferModification&)>
|
template<void (*filter_func)(Buffer&, Modification&)>
|
||||||
class SimpleFilterFactory
|
class SimpleFilterFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user