1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-22 02:51:32 +03:00

Refactor Buffer::do_insert

This commit is contained in:
Maxime Coste 2016-03-15 23:28:10 +00:00
parent d2dfb9ecb1
commit 338462e94f

View File

@ -345,32 +345,15 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
ByteCoord begin; ByteCoord begin;
ByteCoord end; ByteCoord end;
const bool at_end = is_end(pos); const bool at_end = is_end(pos);
// if we inserted at the end of the buffer, we have created a new
// line without inserting a '\n'
if (at_end) if (at_end)
{ pos = line_count();
ByteCount start = 0;
for (ByteCount i = 0; i < content.length(); ++i)
{
if (content[i] == '\n')
{
m_lines.push_back(StringData::create(content.substr(start, i + 1 - start)));
start = i + 1;
}
}
if (start != content.length())
m_lines.push_back(StringData::create(content.substr(start)));
begin = pos.column == 0 ? pos : ByteCoord{ pos.line + 1, 0 }; const StringView prefix = at_end ?
end = ByteCoord{ line_count(), 0 }; StringView{} : m_lines[pos.line].substr(0, pos.column);
} const StringView suffix = at_end ?
else StringView{} : m_lines[pos.line].substr(pos.column);
{
StringView prefix = m_lines[pos.line].substr(0, pos.column);
StringView suffix = m_lines[pos.line].substr(pos.column);
LineList new_lines; LineList new_lines;
ByteCount start = 0; ByteCount start = 0;
for (ByteCount i = 0; i < content.length(); ++i) for (ByteCount i = 0; i < content.length(); ++i)
{ {
@ -387,15 +370,17 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
new_lines.push_back(StringData::create(content.substr(start) + suffix)); new_lines.push_back(StringData::create(content.substr(start) + suffix));
auto line_it = m_lines.begin() + (int)pos.line; auto line_it = m_lines.begin() + (int)pos.line;
*line_it = std::move(*new_lines.begin()); auto new_lines_it = new_lines.begin();
if (not at_end)
*line_it++ = std::move(*new_lines_it++);
m_lines.insert(line_it+1, std::make_move_iterator(new_lines.begin() + 1), m_lines.insert(line_it,
std::make_move_iterator(new_lines_it),
std::make_move_iterator(new_lines.end())); std::make_move_iterator(new_lines.end()));
begin = pos; begin = pos;
const LineCount last_line = pos.line + new_lines.size() - 1; const LineCount last_line = pos.line + new_lines.size() - 1;
end = ByteCoord{ last_line, m_lines[last_line].length() - suffix.length() }; end = ByteCoord{ last_line, m_lines[last_line].length() - suffix.length() };
}
m_changes.push_back({ Change::Insert, at_end, begin, end }); m_changes.push_back({ Change::Insert, at_end, begin, end });
return begin; return begin;