Buffer: insert and erase go back to taking iterators, and return iterators

This commit is contained in:
Maxime Coste 2013-06-06 19:39:53 +02:00
parent cf454ef904
commit 2a74b0e9e2
9 changed files with 96 additions and 104 deletions

View File

@ -412,12 +412,12 @@ void Buffer::check_invariant() const
#endif #endif
} }
void Buffer::do_insert(const BufferCoord& pos, const String& content) BufferCoord Buffer::do_insert(const BufferCoord& pos, const String& content)
{ {
kak_assert(is_valid(pos)); kak_assert(is_valid(pos));
if (content.empty()) if (content.empty())
return; return pos;
++m_timestamp; ++m_timestamp;
ByteCount offset = this->offset(pos); ByteCount offset = this->offset(pos);
@ -489,9 +489,10 @@ void Buffer::do_insert(const BufferCoord& pos, const String& content)
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_insert(*this, begin, end); listener->on_insert(*this, begin, end);
return begin;
} }
void Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end) BufferCoord Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end)
{ {
kak_assert(is_valid(begin)); kak_assert(is_valid(begin));
kak_assert(is_valid(end)); kak_assert(is_valid(end));
@ -501,19 +502,25 @@ void Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end)
String suffix = m_lines[end.line].content.substr(end.column); String suffix = m_lines[end.line].content.substr(end.column);
Line new_line = { m_lines[begin.line].start, prefix + suffix }; Line new_line = { m_lines[begin.line].start, prefix + suffix };
BufferCoord next;
if (new_line.length() != 0) if (new_line.length() != 0)
{ {
m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line); m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line);
m_lines[begin.line] = std::move(new_line); m_lines[begin.line] = std::move(new_line);
next = begin;
} }
else else
{
m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line + 1); m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line + 1);
next = is_end(begin) ? end_coord() : BufferCoord{begin.line, 0};
}
for (LineCount i = begin.line+1; i < line_count(); ++i) for (LineCount i = begin.line+1; i < line_count(); ++i)
m_lines[i].start -= length; m_lines[i].start -= length;
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_erase(*this, begin, end); listener->on_erase(*this, begin, end);
return next;
} }
void Buffer::apply_modification(const Modification& modification) void Buffer::apply_modification(const Modification& modification)
@ -547,32 +554,33 @@ void Buffer::apply_modification(const Modification& modification)
} }
} }
void Buffer::insert(BufferCoord pos, String content) BufferIterator Buffer::insert(const BufferIterator& pos, String content)
{ {
kak_assert(is_valid(pos)); kak_assert(is_valid(pos.coord()));
if (content.empty()) if (content.empty())
return; return pos;
if (is_end(pos) and content.back() != '\n') if (pos == end() and content.back() != '\n')
content += '\n'; content += '\n';
if (not (m_flags & Flags::NoUndo)) if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Insert, pos, content); m_current_undo_group.emplace_back(Modification::Insert, pos.coord(), content);
do_insert(pos, content); return {*this, do_insert(pos.coord(), content)};
} }
void Buffer::erase(BufferCoord begin, BufferCoord end) BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end)
{ {
if (is_end(end) and (begin.column != 0 or begin == BufferCoord{0,0})) // do not erase last \n except if we erase from the start of a line
end = { line_count() - 1, m_lines.back().length() - 1}; if (end == this->end() and (begin.coord().column != 0 or begin == this->begin()))
--end;
if (begin == end) if (begin == end)
return; return begin;
if (not (m_flags & Flags::NoUndo)) if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Erase, begin, m_current_undo_group.emplace_back(Modification::Erase, begin.coord(),
string(begin, end)); string(begin.coord(), end.coord()));
do_erase(begin, end); return {*this, do_erase(begin.coord(), end.coord())};
} }
bool Buffer::is_modified() const bool Buffer::is_modified() const

View File

@ -100,8 +100,8 @@ public:
bool set_name(String name); bool set_name(String name);
void insert(BufferCoord pos, String content); BufferIterator insert(const BufferIterator& pos, String content);
void erase(BufferCoord begin, BufferCoord end); BufferIterator erase(BufferIterator begin, BufferIterator end);
size_t timestamp() const { return m_timestamp; } size_t timestamp() const { return m_timestamp; }
@ -160,8 +160,6 @@ public:
void check_invariant() const; void check_invariant() const;
private: private:
friend class BufferIterator;
struct Line struct Line
{ {
ByteCount start; ByteCount start;
@ -179,8 +177,8 @@ private:
}; };
LineList m_lines; LineList m_lines;
void do_insert(const BufferCoord& pos, const String& content); BufferCoord do_insert(const BufferCoord& pos, const String& content);
void do_erase(const BufferCoord& begin, const BufferCoord& end); BufferCoord do_erase(const BufferCoord& begin, const BufferCoord& end);
String m_name; String m_name;
Flags m_flags; Flags m_flags;

View File

@ -60,8 +60,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
constexpr size_t buffer_size = 1024 * 16; constexpr size_t buffer_size = 1024 * 16;
char data[buffer_size]; char data[buffer_size];
ssize_t count = read(watcher.fd(), data, buffer_size); ssize_t count = read(watcher.fd(), data, buffer_size);
buffer->insert(buffer->back_coord(), buffer->insert(buffer->end()-1, count > 0 ? String(data, data+count)
count > 0 ? String(data, data+count)
: "*** kak: fifo closed ***\n"); : "*** kak: fifo closed ***\n");
ClientManager::instance().redraw_clients(); ClientManager::instance().redraw_clients();
if (count <= 0) if (count <= 0)

View File

@ -44,43 +44,31 @@ void Editor::erase()
} }
} }
static BufferCoord prepare_insert(Buffer& buffer, const Selection& sel, static BufferIterator prepare_insert(Buffer& buffer, const Selection& sel,
InsertMode mode) InsertMode mode)
{ {
switch (mode) switch (mode)
{ {
case InsertMode::Insert: case InsertMode::Insert:
return sel.min(); return buffer.iterator_at(sel.min());
case InsertMode::Replace: case InsertMode::Replace:
{ return Kakoune::erase(buffer, sel);
BufferCoord pos = sel.min();
Kakoune::erase(buffer, sel);
return pos;
}
case InsertMode::Append: case InsertMode::Append:
{ {
// special case for end of lines, append to current line instead // special case for end of lines, append to current line instead
auto& pos = std::max(sel.first(), sel.last()); auto pos = buffer.iterator_at(sel.max());
if (pos.column == buffer[pos.line].length() - 1) return *pos == '\n' ? pos : utf8::next(pos);
return pos;
else
return buffer.char_next(pos);
} }
case InsertMode::InsertAtLineBegin: case InsertMode::InsertAtLineBegin:
return sel.min().line; return buffer.iterator_at(sel.min().line);
case InsertMode::AppendAtLineEnd: case InsertMode::AppendAtLineEnd:
return {sel.max().line, buffer[sel.max().line].length() - 1}; return buffer.iterator_at({sel.max().line, buffer[sel.max().line].length() - 1});
case InsertMode::InsertAtNextLineBegin: case InsertMode::InsertAtNextLineBegin:
return sel.max().line+1; return buffer.iterator_at(sel.max().line+1);
case InsertMode::OpenLineBelow: case InsertMode::OpenLineBelow:
return buffer.insert(buffer.iterator_at(sel.max().line + 1), "\n");
case InsertMode::OpenLineAbove: case InsertMode::OpenLineAbove:
{ return buffer.insert(buffer.iterator_at(sel.min().line), "\n");
auto line = mode == InsertMode::OpenLineAbove ?
sel.min().line : sel.max().line + 1;
buffer.insert(line, "\n");
return line;
}
} }
kak_assert(false); kak_assert(false);
return {}; return {};
@ -92,13 +80,13 @@ void Editor::insert(const String& str, InsertMode mode)
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
BufferCoord pos = prepare_insert(*m_buffer, sel, mode); auto pos = prepare_insert(*m_buffer, sel, mode);
m_buffer->insert(pos, str); pos = m_buffer->insert(pos, str);
if (mode == InsertMode::Replace and not m_buffer->is_end(pos)) if (mode == InsertMode::Replace and pos != m_buffer->end())
{ {
sel.first() = pos; sel.first() = pos.coord();
sel.last() = str.empty() ? sel.last() = str.empty() ?
pos : m_buffer->advance(pos, str.byte_count_to(str.char_length() - 1)); pos.coord() : (pos + str.byte_count_to(str.char_length() - 1)).coord();
} }
avoid_eol(*m_buffer, sel); avoid_eol(*m_buffer, sel);
} }
@ -114,14 +102,14 @@ void Editor::insert(const memoryview<String>& strings, InsertMode mode)
for (size_t i = 0; i < selections().size(); ++i) for (size_t i = 0; i < selections().size(); ++i)
{ {
auto& sel = m_selections[i]; auto& sel = m_selections[i];
BufferCoord pos = prepare_insert(*m_buffer, sel, mode); auto pos = prepare_insert(*m_buffer, sel, mode);
const String& str = strings[std::min(i, strings.size()-1)]; const String& str = strings[std::min(i, strings.size()-1)];
m_buffer->insert(pos, str); pos = m_buffer->insert(pos, str);
if (mode == InsertMode::Replace and not m_buffer->is_end(pos)) if (mode == InsertMode::Replace and pos != m_buffer->end())
{ {
sel.first() = pos; sel.first() = pos.coord();
sel.last() = str.empty() ? sel.last() = (str.empty() ?
pos : m_buffer->advance(pos, str.byte_count_to(str.char_length() - 1)); pos : pos + str.byte_count_to(str.char_length() - 1)).coord();
} }
avoid_eol(*m_buffer, sel); avoid_eol(*m_buffer, sel);
} }
@ -462,27 +450,24 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
BufferCoord first, last; BufferCoord first, last;
switch (mode) switch (mode)
{ {
case InsertMode::Insert: first = sel.max(); last = sel.min(); break; case InsertMode::Insert:
case InsertMode::Replace: first = sel.max();
{ last = sel.min();
Kakoune::erase(buffer, sel); break;
first = last = sel.min(); case InsertMode::Replace:
first = last = Kakoune::erase(buffer, sel).coord();
break; break;
}
case InsertMode::Append: case InsertMode::Append:
{
first = sel.min(); first = sel.min();
last = sel.max(); last = sel.max();
// special case for end of lines, append to current line instead // special case for end of lines, append to current line instead
if (last.column != buffer[last.line].length() - 1) if (last.column != buffer[last.line].length() - 1)
last = buffer.char_next(last); last = buffer.char_next(last);
break; break;
}
case InsertMode::OpenLineBelow: case InsertMode::OpenLineBelow:
case InsertMode::AppendAtLineEnd: case InsertMode::AppendAtLineEnd:
first = BufferCoord{sel.max().line, buffer[sel.max().line].length() - 1}; first = last = BufferCoord{sel.max().line, buffer[sel.max().line].length() - 1};
last = first;
break; break;
case InsertMode::OpenLineAbove: case InsertMode::OpenLineAbove:
@ -540,30 +525,32 @@ IncrementalInserter::~IncrementalInserter()
void IncrementalInserter::insert(String content) void IncrementalInserter::insert(String content)
{ {
Buffer& buffer = m_editor.buffer(); auto& buffer = m_editor.buffer();
for (auto& sel : m_editor.m_selections) for (auto& sel : m_editor.m_selections)
{ {
m_editor.filters()(buffer, sel, content); m_editor.filters()(buffer, sel, content);
buffer.insert(sel.last(), content); buffer.insert(buffer.iterator_at(sel.last()), content);
} }
} }
void IncrementalInserter::insert(const memoryview<String>& strings) void IncrementalInserter::insert(const memoryview<String>& strings)
{ {
auto& buffer = m_editor.buffer();
for (size_t i = 0; i < m_editor.m_selections.size(); ++i) for (size_t i = 0; i < m_editor.m_selections.size(); ++i)
{ {
size_t index = std::min(i, strings.size()-1); size_t index = std::min(i, strings.size()-1);
m_editor.buffer().insert(m_editor.m_selections[i].last(), buffer.insert(buffer.iterator_at(m_editor.m_selections[i].last()),
strings[index]); strings[index]);
} }
} }
void IncrementalInserter::erase() void IncrementalInserter::erase()
{ {
auto& buffer = m_editor.buffer();
for (auto& sel : m_editor.m_selections) for (auto& sel : m_editor.m_selections)
{ {
BufferCoord pos = sel.last(); auto pos = buffer.iterator_at(sel.last());
m_editor.buffer().erase(m_editor.buffer().char_prev(pos), pos); buffer.erase(pos-1, pos);
} }
} }

View File

@ -31,7 +31,7 @@ void cleanup_whitespaces(Buffer& buffer, Selection& selection, String& content)
--whitespace_start; --whitespace_start;
++whitespace_start; ++whitespace_start;
if (whitespace_start != position) if (whitespace_start != position)
buffer.erase(whitespace_start.coord(), position.coord()); buffer.erase(whitespace_start, position);
} }
} }
@ -81,10 +81,10 @@ struct RegexFilter
{ {
String suffix(it+1, content.end()); String suffix(it+1, content.end());
content = String(content.begin(), it-1); content = String(content.begin(), it-1);
buffer.insert(position, suffix);
auto& first = selection.first(); auto& first = selection.first();
auto& last = selection.last(); auto& last = selection.last();
buffer.insert(last, suffix);
if (first == last) if (first == last)
first = buffer.advance(first, -suffix.length()); first = buffer.advance(first, -suffix.length());
last = buffer.advance(last, -suffix.length()); last = buffer.advance(last, -suffix.length());

View File

@ -613,9 +613,8 @@ public:
if (offset >= beg_offset and offset + end_offset < buffer_len and if (offset >= beg_offset and offset + end_offset < buffer_len and
std::equal(pos - beg_offset, pos, begin)) std::equal(pos - beg_offset, pos, begin))
{ {
auto beg = buffer.advance(sel.last(), -beg_offset); pos = buffer.erase(pos - beg_offset, pos + end_offset);
buffer.erase(beg, buffer.advance(sel.last(), end_offset)); buffer.insert(pos, candidate);
buffer.insert(beg, candidate);
} }
} }

View File

@ -476,7 +476,7 @@ void join_select_spaces(Context& context)
kak_assert(std::is_sorted(res.begin(), res.end(), kak_assert(std::is_sorted(res.begin(), res.end(),
[](const Selection& lhs, const Selection& rhs) [](const Selection& lhs, const Selection& rhs)
{ return lhs.min() < rhs.min(); })); { return lhs.min() < rhs.min(); }));
if (not res.empty() and buffer.is_end(buffer.char_next(res.back().max()))) if (not res.empty() and res.back().max() == buffer.back_coord())
res.pop_back(); res.pop_back();
return res; return res;
}); });
@ -713,7 +713,7 @@ void align(Context& context)
for (auto& sel : selections) for (auto& sel : selections)
{ {
CharCount padding = max_col - get_column(sel.last()); CharCount padding = max_col - get_column(sel.last());
buffer.insert(sel.last(), String{ ' ', padding }); buffer.insert(buffer.iterator_at(sel.last()), String{ ' ', padding });
} }
} }

View File

@ -45,9 +45,10 @@ inline String content(const Buffer& buffer, const Range& range)
return buffer.string(range.min(), buffer.char_next(range.max())); return buffer.string(range.min(), buffer.char_next(range.max()));
} }
inline void erase(Buffer& buffer, const Range& range) inline BufferIterator erase(Buffer& buffer, const Range& range)
{ {
return buffer.erase(range.min(), buffer.char_next(range.max())); return buffer.erase(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max())));
} }
using CaptureList = std::vector<String>; using CaptureList = std::vector<String>;

View File

@ -13,16 +13,16 @@ void test_buffer()
Buffer buffer("test", Buffer::Flags::None, { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" }); Buffer buffer("test", Buffer::Flags::None, { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" });
kak_assert(buffer.line_count() == 4); kak_assert(buffer.line_count() == 4);
BufferCoord pos = {0,0}; BufferIterator pos = buffer.begin();
kak_assert(buffer.byte_at(pos) == 'a'); kak_assert(*pos == 'a');
pos = buffer.advance(pos, 6); pos += 6;
kak_assert(pos == BufferCoord{0 COMMA 6}); kak_assert(pos.coord() == BufferCoord{0 COMMA 6});
pos = buffer.next(pos); ++pos;
kak_assert(pos == BufferCoord{1 COMMA 0}); kak_assert(pos.coord() == BufferCoord{1 COMMA 0});
pos = buffer.prev(pos); --pos;
kak_assert(pos == BufferCoord{0 COMMA 6}); kak_assert(pos.coord() == BufferCoord{0 COMMA 6});
pos = buffer.advance(pos, 1); pos += 1;
kak_assert(pos == BufferCoord{1 COMMA 0}); kak_assert(pos.coord() == BufferCoord{1 COMMA 0});
buffer.insert(pos, "tchou kanaky\n"); buffer.insert(pos, "tchou kanaky\n");
kak_assert(buffer.line_count() == 5); kak_assert(buffer.line_count() == 5);
@ -30,17 +30,17 @@ void test_buffer()
kak_assert(str == "youpi"); kak_assert(str == "youpi");
// check insert at end behaviour: auto add end of line if necessary // check insert at end behaviour: auto add end of line if necessary
pos = buffer.back_coord(); pos = buffer.end()-1;
buffer.insert(pos, "tchou"); buffer.insert(pos, "tchou");
kak_assert(buffer.string(pos, buffer.end_coord()) == "tchou\n"); kak_assert(buffer.string(pos.coord(), buffer.end_coord()) == "tchou\n");
pos = buffer.back_coord(); pos = buffer.end()-1;
buffer.insert(buffer.end_coord(), "kanaky\n"); buffer.insert(buffer.end(), "kanaky\n");
kak_assert(buffer.string(buffer.next(pos), buffer.end_coord()) == "kanaky\n"); kak_assert(buffer.string((pos+1).coord(), buffer.end_coord()) == "kanaky\n");
buffer.commit_undo_group(); buffer.commit_undo_group();
buffer.erase(buffer.next(pos), buffer.end_coord()); buffer.erase(pos+1, buffer.end());
buffer.insert(buffer.end_coord(), "mutch\n"); buffer.insert(buffer.end(), "mutch\n");
buffer.commit_undo_group(); buffer.commit_undo_group();
buffer.undo(); buffer.undo();
kak_assert(buffer.string(buffer.advance(buffer.end_coord(), -7), buffer.end_coord()) == "kanaky\n"); kak_assert(buffer.string(buffer.advance(buffer.end_coord(), -7), buffer.end_coord()) == "kanaky\n");