buffer.cc (26011B)
1 #include "buffer.hh" 2 3 #include "assert.hh" 4 #include "buffer_manager.hh" 5 #include "buffer_utils.hh" 6 #include "hook_manager.hh" 7 #include "option_manager.hh" 8 #include "client.hh" 9 #include "context.hh" 10 #include "diff.hh" 11 #include "file.hh" 12 #include "flags.hh" 13 #include "option_types.hh" 14 #include "ranges.hh" 15 #include "shared_string.hh" 16 #include "unit_tests.hh" 17 #include "utils.hh" 18 #include "window.hh" 19 20 #include <algorithm> 21 22 namespace Kakoune 23 { 24 25 Buffer::HistoryNode::HistoryNode(HistoryId parent) 26 : parent{parent}, committed{Clock::now()} 27 {} 28 29 Buffer::Buffer(String name, Flags flags, BufferLines lines, 30 ByteOrderMark bom, EolFormat eolformat, FinalEol finaleol, 31 FsStatus fs_status) 32 : Scope{GlobalScope::instance()}, 33 m_filename{(flags & Flags::File) ? real_path(parse_filename(name)) : String{}}, 34 m_display_name{(flags & Flags::File) ? compact_path(m_filename) : std::move(name)}, 35 m_flags{flags | Flags::NoUndo}, 36 m_history{{HistoryId::Invalid}}, 37 m_history_id{HistoryId::First}, 38 m_last_save_history_id{HistoryId::First}, 39 m_fs_status{fs_status} 40 { 41 #ifdef KAK_DEBUG 42 for (auto& line : lines) 43 kak_assert(not (line->length == 0) and 44 line->data()[line->length-1] == '\n'); 45 #endif 46 static_cast<BufferLines&>(m_lines) = std::move(lines); 47 48 m_changes.push_back({ Change::Insert, {0,0}, line_count() }); 49 50 options().get_local_option("eolformat").set(eolformat); 51 options().get_local_option("finaleol").set(finaleol); 52 options().get_local_option("BOM").set(bom); 53 options().get_local_option("readonly").set((bool)(flags & Flags::ReadOnly)); 54 55 // now we may begin to record undo data 56 if (not (flags & Flags::NoUndo)) 57 m_flags &= ~Flags::NoUndo; 58 } 59 60 void Buffer::on_registered() 61 { 62 // Ignore debug buffer, as it can be created in many 63 // corner cases (including while destroying the BufferManager 64 // if a BufClose hooks triggers writing to it). 65 if (m_flags & Flags::Debug) 66 return; 67 68 options().register_watcher(*this); 69 70 if (m_flags & Buffer::Flags::NoHooks) 71 { 72 on_option_changed(options()["readonly"]); 73 return; 74 } 75 76 m_flags |= Flags::NoBufSetOption; 77 78 run_hook_in_own_context(Hook::BufCreate, (m_flags & Flags::File) ? m_filename : m_display_name); 79 80 if (m_flags & Flags::File) 81 { 82 if (m_flags & Buffer::Flags::New) 83 run_hook_in_own_context(Hook::BufNewFile, (m_flags & Flags::File) ? m_filename : m_display_name); 84 else 85 { 86 kak_assert(m_fs_status.timestamp != InvalidTime); 87 run_hook_in_own_context(Hook::BufOpenFile, (m_flags & Flags::File) ? m_filename : m_display_name); 88 } 89 } 90 m_flags &= ~Flags::NoBufSetOption; 91 92 for (auto& option : options().flatten_options() 93 | transform(&UniquePtr<Option>::get) 94 | gather<Vector<Option*>>()) 95 on_option_changed(*option); 96 } 97 98 void Buffer::on_unregistered() 99 { 100 if (m_flags & Flags::Debug) 101 return; 102 103 options().unregister_watcher(*this); 104 run_hook_in_own_context(Hook::BufClose, (m_flags & Flags::File) ? m_filename : m_display_name); 105 } 106 107 Buffer::~Buffer() 108 { 109 m_values.clear(); 110 } 111 112 bool Buffer::set_name(String name) 113 { 114 Buffer* other = BufferManager::instance().get_buffer_ifp(name); 115 if (other == nullptr or other == this) 116 { 117 if (m_flags & Flags::File) 118 { 119 m_filename = real_path(name); 120 m_display_name = compact_path(m_filename); 121 if (m_flags & Buffer::Flags::File and not file_exists(m_filename)) 122 { 123 m_flags |= Buffer::Flags::New; 124 m_last_save_history_id = HistoryId::Invalid; 125 } 126 } 127 else 128 { 129 m_filename = String{}; 130 m_display_name = std::move(name); 131 } 132 return true; 133 } 134 return false; 135 } 136 137 void Buffer::throw_if_read_only() const 138 { 139 if (m_flags & Flags::ReadOnly) 140 throw runtime_error("buffer is read-only"); 141 } 142 143 void Buffer::update_display_name() 144 { 145 if (m_flags & Flags::File) 146 m_display_name = compact_path(m_filename); 147 } 148 149 BufferIterator Buffer::iterator_at(BufferCoord coord) const 150 { 151 kak_assert(is_valid(coord)); 152 return {*this, coord}; 153 } 154 155 BufferCoord Buffer::clamp(BufferCoord coord) const 156 { 157 if (coord > back_coord()) 158 coord = back_coord(); 159 kak_assert(coord.line >= 0 and coord.line < line_count()); 160 ByteCount max_col = std::max(0_byte, m_lines[coord.line].length() - 1); 161 coord.column = Kakoune::clamp(coord.column, 0_byte, max_col); 162 return coord; 163 } 164 165 BufferCoord Buffer::offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const 166 { 167 return utf8::advance(iterator_at(coord), offset < 0 ? begin() : end()-1, offset).coord(); 168 } 169 170 BufferCoordAndTarget Buffer::offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const 171 { 172 const auto column = coord.target == -1 ? get_column(*this, tabstop, coord) : coord.target; 173 const bool avoid_eol = coord.target < max_column; 174 const auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1); 175 const auto max_byte = m_lines[line].length()-1; 176 const auto max_column = get_column(*this, tabstop, {line, max_byte}); 177 const auto final_column = std::max(0_col, std::min(column, max_column - (avoid_eol ? 1 : 0))); 178 return {line, std::min(max_byte, get_byte_to_column(*this, tabstop, {line, final_column})), column}; 179 } 180 181 String Buffer::string(BufferCoord begin, BufferCoord end) const 182 { 183 String res; 184 const auto last_line = std::min(end.line, line_count()-1); 185 for (auto line = begin.line; line <= last_line; ++line) 186 { 187 ByteCount start = 0; 188 if (line == begin.line) 189 start = begin.column; 190 ByteCount count = -1; 191 if (line == end.line) 192 count = end.column - start; 193 res += m_lines[line].substr(start, count); 194 } 195 return res; 196 } 197 198 Buffer::Modification Buffer::Modification::inverse() const 199 { 200 return {type == Insert ? Erase : Insert, coord, content}; 201 } 202 203 void Buffer::reload(BufferLines lines, ByteOrderMark bom, EolFormat eolformat, FinalEol finaleol, FsStatus fs_status) 204 { 205 const bool record_undo = not (m_flags & Flags::NoUndo); 206 207 commit_undo_group(); 208 209 if (not record_undo) 210 { 211 // Erase history about to be invalidated history 212 m_history_id = HistoryId::First; 213 m_last_save_history_id = HistoryId::First; 214 m_history = {HistoryNode{HistoryId::Invalid}}; 215 216 m_changes.push_back({ Change::Erase, {0,0}, line_count() }); 217 static_cast<BufferLines&>(m_lines) = std::move(lines); 218 m_changes.push_back({ Change::Insert, {0,0}, line_count() }); 219 } 220 else 221 { 222 Vector<Diff> diff; 223 for_each_diff(m_lines.begin(), m_lines.size(), 224 lines.begin(), lines.size(), 225 [&diff](DiffOp op, int len) 226 { diff.push_back({op, len}); }, 227 [](const StringDataPtr& lhs, const StringDataPtr& rhs) 228 { return lhs->strview() == rhs->strview(); }); 229 230 auto read_it = m_lines.begin(); 231 auto write_it = m_lines.begin(); 232 auto new_it = lines.begin(); 233 for (auto [op, len] : diff) 234 { 235 kak_assert(read_it >= write_it); 236 if (op == DiffOp::Keep) 237 { 238 if (read_it != write_it) 239 std::move(read_it, read_it + len, write_it); 240 write_it += len; 241 read_it += len; 242 new_it += len; 243 } 244 else if (op == DiffOp::Add) 245 { 246 const LineCount cur_line = (int)(write_it - m_lines.begin()); 247 for (LineCount line = 0; line < len; ++line) 248 m_current_undo_group.push_back({Modification::Insert, cur_line + line, *(new_it + (int)line)}); 249 m_changes.push_back({Change::Insert, cur_line, cur_line + len}); 250 251 if (read_it != write_it) 252 { 253 auto count = std::min(len, static_cast<int>(read_it - write_it)); 254 write_it = std::copy(new_it, new_it + count, write_it); 255 new_it += count; 256 if (len == count) 257 continue; 258 len -= count; 259 } 260 261 auto read_pos = read_it - m_lines.begin(); 262 write_it = m_lines.insert(write_it, new_it, new_it + len) + len; 263 read_it = m_lines.begin() + read_pos + len; 264 new_it += len; 265 } 266 else if (op == DiffOp::Remove) 267 { 268 const LineCount cur_line = (int)(write_it - m_lines.begin()); 269 for (LineCount line = len-1; line >= 0; --line) 270 m_current_undo_group.push_back({ 271 Modification::Erase, cur_line + line, 272 *(read_it + (size_t)line)}); 273 274 read_it += len; 275 m_changes.push_back({ Change::Erase, cur_line, cur_line + len }); 276 } 277 } 278 m_lines.erase(write_it, m_lines.end()); 279 } 280 281 commit_undo_group(); 282 283 options().get_local_option("eolformat").set(eolformat); 284 options().get_local_option("finaleol").set(finaleol); 285 options().get_local_option("BOM").set(bom); 286 287 m_last_save_history_id = m_history_id; 288 m_fs_status = fs_status; 289 } 290 291 void Buffer::commit_undo_group() 292 { 293 if (m_flags & Flags::NoUndo) 294 return; 295 296 if (m_current_undo_group.empty()) 297 return; 298 299 const HistoryId id = next_history_id(); 300 m_history.push_back({m_history_id}); 301 m_history.back().undo_group = std::move(m_current_undo_group); 302 m_current_undo_group.clear(); 303 current_history_node().redo_child = id; 304 m_history_id = id; 305 } 306 307 bool Buffer::undo(size_t count) 308 { 309 throw_if_read_only(); 310 311 commit_undo_group(); 312 313 if (current_history_node().parent == HistoryId::Invalid) 314 return false; 315 316 while (count-- != 0 and current_history_node().parent != HistoryId::Invalid) 317 { 318 for (const Modification& modification : current_history_node().undo_group | reverse()) 319 apply_modification(modification.inverse()); 320 321 m_history_id = current_history_node().parent; 322 } 323 324 return true; 325 } 326 327 bool Buffer::redo(size_t count) 328 { 329 throw_if_read_only(); 330 331 if (current_history_node().redo_child == HistoryId::Invalid or 332 not m_current_undo_group.empty()) 333 return false; 334 335 while (count-- != 0 and current_history_node().redo_child != HistoryId::Invalid) 336 { 337 m_history_id = current_history_node().redo_child; 338 339 for (const Modification& modification : current_history_node().undo_group) 340 apply_modification(modification); 341 } 342 return true; 343 } 344 345 bool Buffer::move_to(HistoryId id) 346 { 347 if (id >= next_history_id()) 348 return false; 349 350 throw_if_read_only(); 351 352 commit_undo_group(); 353 354 auto find_lowest_common_parent = [this](HistoryId a, HistoryId b) { 355 auto depth_of = [this](HistoryId id) { 356 size_t depth = 0; 357 for (; history_node(id).parent != HistoryId::Invalid; id = history_node(id).parent) 358 ++depth; 359 return depth; 360 }; 361 auto depthA = depth_of(a), depthB = depth_of(b); 362 363 for (; depthA > depthB; --depthA) 364 a = history_node(a).parent; 365 for (; depthB > depthA; --depthB) 366 b = history_node(b).parent; 367 368 while (a != b) 369 { 370 a = history_node(a).parent; 371 b = history_node(b).parent; 372 } 373 374 kak_assert(a == b and a != HistoryId::Invalid); 375 return a; 376 }; 377 378 auto parent = find_lowest_common_parent(m_history_id, id); 379 380 // undo up to common parent 381 for (auto id = m_history_id; id != parent; id = history_node(id).parent) 382 { 383 for (const Modification& modification : history_node(id).undo_group | reverse()) 384 apply_modification(modification.inverse()); 385 } 386 387 static void (*apply_from_parent)(Buffer&, HistoryId, HistoryId) = 388 [](Buffer& buffer, HistoryId parent, HistoryId id) { 389 if (id == parent) 390 return; 391 392 auto& node = buffer.history_node(id); 393 apply_from_parent(buffer, parent, node.parent); 394 395 buffer.history_node(node.parent).redo_child = id; 396 397 for (const Modification& modification : node.undo_group) 398 buffer.apply_modification(modification); 399 }; 400 401 apply_from_parent(*this, parent, id); 402 m_history_id = id; 403 return true; 404 } 405 406 void Buffer::check_invariant() const 407 { 408 #ifdef KAK_DEBUG 409 kak_assert(not m_lines.empty()); 410 for (auto& line : m_lines) 411 { 412 kak_assert(line->strview().length() > 0); 413 kak_assert(line->strview().back() == '\n'); 414 } 415 #endif 416 } 417 418 BufferRange Buffer::do_insert(BufferCoord pos, StringView content) 419 { 420 kak_assert(is_valid(pos)); 421 422 if (content.empty()) 423 return {pos, pos}; 424 425 const bool at_end = is_end(pos); 426 const bool append_lines = at_end and (m_lines.empty() or byte_at(back_coord()) == '\n'); 427 428 const StringView prefix = append_lines ? 429 StringView{} : m_lines[pos.line].substr(0, pos.column); 430 const StringView suffix = at_end ? 431 StringView{} : m_lines[pos.line].substr(pos.column); 432 433 LineList new_lines; 434 ByteCount start = 0; 435 for (ByteCount i = 0; i < content.length(); ++i) 436 { 437 if (content[i] == '\n') 438 { 439 StringView line = content.substr(start, i + 1 - start); 440 new_lines.push_back(start == 0 ? StringData::create(prefix, line) : StringData::create(line)); 441 start = i + 1; 442 } 443 } 444 if (start == 0) 445 new_lines.push_back(StringData::create(prefix, content, suffix)); 446 else if (start != content.length() or not suffix.empty()) 447 new_lines.push_back(StringData::create(content.substr(start), suffix)); 448 449 auto line_it = m_lines.begin() + (int)pos.line; 450 auto new_lines_it = new_lines.begin(); 451 if (not append_lines) // replace first line with new first line 452 *line_it++ = std::move(*new_lines_it++); 453 454 m_lines.insert(line_it, 455 std::make_move_iterator(new_lines_it), 456 std::make_move_iterator(new_lines.end())); 457 458 const LineCount last_line = pos.line + new_lines.size() - 1; 459 const auto end = at_end ? line_count() 460 : BufferCoord{ last_line, m_lines[last_line].length() - suffix.length() }; 461 462 m_changes.push_back({ Change::Insert, pos, end }); 463 return {pos, end}; 464 } 465 466 BufferCoord Buffer::do_erase(BufferCoord begin, BufferCoord end) 467 { 468 if (begin == end) 469 return begin; 470 471 kak_assert(is_valid(begin)); 472 kak_assert(is_valid(end)); 473 StringView prefix = m_lines[begin.line].substr(0, begin.column); 474 StringView suffix = end.line == line_count() ? StringView{} : m_lines[end.line].substr(end.column); 475 476 auto new_line = (not prefix.empty() or not suffix.empty()) ? StringData::create(prefix, suffix) : StringDataPtr{}; 477 m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line); 478 479 m_changes.push_back({ Change::Erase, begin, end }); 480 if (new_line) 481 m_lines.get_storage(begin.line) = std::move(new_line); 482 483 return begin; 484 } 485 486 void Buffer::apply_modification(const Modification& modification) 487 { 488 StringView content = modification.content->strview(); 489 BufferCoord coord = modification.coord; 490 491 kak_assert(is_valid(coord)); 492 switch (modification.type) 493 { 494 case Modification::Insert: 495 do_insert(coord, content); 496 break; 497 case Modification::Erase: 498 { 499 auto end = advance(coord, content.length()); 500 kak_assert(string(coord, end) == content); 501 do_erase(coord, end); 502 break; 503 } 504 default: 505 kak_assert(false); 506 } 507 } 508 509 BufferRange Buffer::insert(BufferCoord pos, StringView content) 510 { 511 throw_if_read_only(); 512 513 kak_assert(is_valid(pos)); 514 if (content.empty()) 515 return {pos, pos}; 516 517 StringDataPtr real_content; 518 if (is_end(pos) and content.back() != '\n') 519 real_content = intern(content + "\n"); 520 else 521 real_content = intern(content); 522 523 if (not (m_flags & Flags::NoUndo)) 524 m_current_undo_group.push_back({Modification::Insert, pos, real_content}); 525 return do_insert(pos, real_content->strview()); 526 } 527 528 BufferCoord Buffer::erase(BufferCoord begin, BufferCoord end) 529 { 530 throw_if_read_only(); 531 532 kak_assert(is_valid(begin) and is_valid(end)); 533 // do not erase last \n except if we erase from the start of a line, and normalize 534 // end coord 535 if (is_end(end)) 536 end = (begin.column != 0 or begin == BufferCoord{0,0}) ? prev(end) : end_coord(); 537 538 if (begin >= end) // use >= to handle case where begin is {line_count} 539 return begin; 540 541 if (not (m_flags & Flags::NoUndo)) 542 m_current_undo_group.push_back({Modification::Erase, begin, 543 intern(string(begin, end))}); 544 return do_erase(begin, end); 545 } 546 547 BufferRange Buffer::replace(BufferCoord begin, BufferCoord end, StringView content) 548 { 549 throw_if_read_only(); 550 551 if (std::equal(iterator_at(begin), iterator_at(end), content.begin(), content.end())) 552 return {begin, end}; 553 554 if (is_end(end) and not content.empty() and content.back() == '\n') 555 { 556 auto pos = insert(erase(begin, back_coord()), 557 content.substr(0, content.length() - 1)).begin; 558 return {pos, end_coord()}; 559 } 560 return insert(erase(begin, end), content); 561 } 562 563 bool Buffer::is_modified() const 564 { 565 return m_flags & Flags::File and 566 (m_history_id != m_last_save_history_id or 567 not m_current_undo_group.empty()); 568 } 569 570 void Buffer::notify_saved(FsStatus status) 571 { 572 if (not m_current_undo_group.empty()) 573 commit_undo_group(); 574 575 m_flags &= ~Flags::New; 576 m_last_save_history_id = m_history_id; 577 m_fs_status = status; 578 } 579 580 BufferCoord Buffer::advance(ArrayView<const StringDataPtr> lines, BufferCoord coord, ByteCount count) 581 { 582 if (count > 0) 583 { 584 auto line = coord.line; 585 count += coord.column; 586 while (count >= lines[(size_t)line]->length) 587 { 588 count -= lines[(size_t)line++]->length; 589 if ((size_t)line == lines.size()) 590 return line; 591 } 592 return { line, count }; 593 } 594 else if (count < 0) 595 { 596 auto line = coord.line; 597 count += coord.column; 598 while (count < 0) 599 { 600 if (--line < 0) 601 return {0, 0}; 602 count += lines[(size_t)line]->length; 603 } 604 return { line, count }; 605 } 606 return coord; 607 } 608 609 BufferCoord Buffer::char_next(BufferCoord coord) const 610 { 611 if (coord.column < m_lines[coord.line].length() - 1) 612 { 613 auto line = m_lines[coord.line]; 614 auto column = coord.column + utf8::codepoint_size(line[coord.column]); 615 if (column >= line.length()) // Handle invalid utf-8 616 return { coord.line + 1, 0 }; 617 return { coord.line, column }; 618 } 619 return { coord.line + 1, 0 }; 620 } 621 622 BufferCoord Buffer::char_prev(BufferCoord coord) const 623 { 624 kak_assert(is_valid(coord)); 625 if (coord.column == 0) 626 return { coord.line - 1, m_lines[coord.line - 1].length() - 1 }; 627 628 auto line = m_lines[coord.line]; 629 auto column = (int)(utf8::character_start(line.begin() + coord.column - 1, line.begin()) - line.begin()); 630 return { coord.line, column }; 631 } 632 633 void Buffer::set_fs_status(FsStatus status) 634 { 635 kak_assert(m_flags & Flags::File); 636 m_fs_status = std::move(status); 637 } 638 639 const FsStatus& Buffer::fs_status() const 640 { 641 kak_assert(m_flags & Flags::File); 642 return m_fs_status; 643 } 644 645 void Buffer::on_option_changed(const Option& option) 646 { 647 if (m_flags & Flags::NoBufSetOption) 648 return; 649 650 if (option.name() == "readonly") 651 { 652 if (option.get<bool>()) 653 m_flags |= Flags::ReadOnly; 654 else 655 m_flags &= ~Flags::ReadOnly; 656 } 657 run_hook_in_own_context(Hook::BufSetOption, 658 format("{}={}", option.name(), option.get_desc_string())); 659 } 660 661 void Buffer::run_hook_in_own_context(Hook hook, StringView param, String client_name) 662 { 663 if (m_flags & Buffer::Flags::NoHooks) 664 return; 665 666 InputHandler hook_handler{{ *this, Selection{} }, 667 Context::Flags::Draft, 668 std::move(client_name)}; 669 hooks().run_hook(hook, param, hook_handler.context()); 670 } 671 672 Optional<BufferCoord> Buffer::last_modification_coord() const 673 { 674 if (m_history_id == HistoryId::First) 675 return {}; 676 return current_history_node().undo_group.back().coord; 677 } 678 679 String Buffer::debug_description() const 680 { 681 size_t content_size = 0; 682 for (auto& line : m_lines) 683 content_size += (int)line->strview().length(); 684 685 const size_t additional_size = accumulate(m_history, 0, [](size_t s, auto&& history) { 686 return sizeof(history) + history.undo_group.size() * sizeof(Modification) + s; 687 }) + m_changes.size() * sizeof(Change); 688 689 return format("{}\nFlags: {}{}{}{}{}{}{}{}\nUsed mem: content={} additional={}\n", 690 display_name(), 691 (m_flags & Flags::File) ? "File (" + filename() + ") " : "", 692 (m_flags & Flags::New) ? "New " : "", 693 (m_flags & Flags::Fifo) ? "Fifo " : "", 694 (m_flags & Flags::NoUndo) ? "NoUndo " : "", 695 (m_flags & Flags::NoHooks) ? "NoHooks " : "", 696 (m_flags & Flags::Debug) ? "Debug " : "", 697 (m_flags & Flags::ReadOnly) ? "ReadOnly " : "", 698 is_modified() ? "Modified " : "", 699 content_size, additional_size); 700 } 701 702 UnitTest test_buffer{[]() 703 { 704 auto make_lines = [](auto&&... lines) { return BufferLines{StringData::create(lines)...}; }; 705 706 Buffer empty_buffer("empty", Buffer::Flags::None, make_lines("\n")); 707 708 Buffer buffer("test", Buffer::Flags::None, make_lines("allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n")); 709 kak_assert(buffer.line_count() == 4); 710 711 BufferIterator pos = buffer.begin(); 712 kak_assert(*pos == 'a'); 713 pos += 6; 714 kak_assert(pos.coord() == BufferCoord{0, 6}); 715 ++pos; 716 kak_assert(pos.coord() == BufferCoord{1, 0}); 717 --pos; 718 kak_assert(pos.coord() == BufferCoord{0, 6}); 719 pos += 1; 720 kak_assert(pos.coord() == BufferCoord{1, 0}); 721 buffer.insert(pos.coord(), "tchou kanaky\n"); 722 kak_assert(buffer.line_count() == 5); 723 BufferIterator pos2 = buffer.end(); 724 pos2 -= 9; 725 kak_assert(*pos2 == '?'); 726 727 String str = buffer.string({ 4, 1 }, buffer.next({ 4, 5 })); 728 kak_assert(str == "youpi"); 729 730 // check insert at end behaviour: auto add end of line if necessary 731 pos = buffer.end()-1; 732 buffer.insert(pos.coord(), "tchou"); 733 kak_assert(buffer.string(pos.coord(), buffer.end_coord()) == "tchou\n"_sv); 734 735 pos = buffer.end()-1; 736 buffer.insert(buffer.end_coord(), "kanaky\n"); 737 kak_assert(buffer.string((pos+1).coord(), buffer.end_coord()) == "kanaky\n"_sv); 738 739 buffer.commit_undo_group(); 740 buffer.erase((pos+1).coord(), buffer.end_coord()); 741 buffer.insert(buffer.end_coord(), "mutch\n"); 742 buffer.commit_undo_group(); 743 buffer.undo(); 744 kak_assert(buffer.string(buffer.advance(buffer.end_coord(), -7), buffer.end_coord()) == "kanaky\n"_sv); 745 buffer.redo(); 746 kak_assert(buffer.string(buffer.advance(buffer.end_coord(), -6), buffer.end_coord()) == "mutch\n"_sv); 747 }}; 748 749 UnitTest test_undo{[]() 750 { 751 auto make_lines = [](auto&&... lines) { return BufferLines{StringData::create(lines)...}; }; 752 753 Buffer buffer("test", Buffer::Flags::None, make_lines("allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n")); 754 auto pos = buffer.end_coord(); 755 buffer.insert(pos, "kanaky\n"); // change 1 756 buffer.commit_undo_group(); 757 buffer.erase(pos, buffer.end_coord()); // change 2 758 buffer.commit_undo_group(); 759 buffer.insert(2_line, "tchou\n"); // change 3 760 buffer.commit_undo_group(); 761 buffer.undo(); 762 buffer.insert(2_line, "mutch\n"); // change 4 763 buffer.commit_undo_group(); 764 buffer.erase({2, 1}, {2, 5}); // change 5 765 buffer.commit_undo_group(); 766 buffer.undo(2); 767 buffer.redo(2); 768 buffer.undo(); 769 buffer.replace(2_line, buffer.end_coord(), "foo"); // change 6 770 buffer.commit_undo_group(); 771 772 kak_assert((int)buffer.line_count() == 3); 773 kak_assert(buffer[0_line] == "allo ?\n"); 774 kak_assert(buffer[1_line] == "mais que fais la police\n"); 775 kak_assert(buffer[2_line] == "foo\n"); 776 777 buffer.move_to((Buffer::HistoryId)3); 778 kak_assert((int)buffer.line_count() == 5); 779 kak_assert(buffer[0_line] == "allo ?\n"); 780 kak_assert(buffer[1_line] == "mais que fais la police\n"); 781 kak_assert(buffer[2_line] == "tchou\n"); 782 kak_assert(buffer[3_line] == " hein ?\n"); 783 kak_assert(buffer[4_line] == " youpi\n"); 784 785 buffer.move_to((Buffer::HistoryId)4); 786 kak_assert((int)buffer.line_count() == 5); 787 kak_assert(buffer[0_line] == "allo ?\n"); 788 kak_assert(buffer[1_line] == "mais que fais la police\n"); 789 kak_assert(buffer[2_line] == "mutch\n"); 790 kak_assert(buffer[3_line] == " hein ?\n"); 791 kak_assert(buffer[4_line] == " youpi\n"); 792 793 buffer.move_to(Buffer::HistoryId::First); 794 kak_assert((int)buffer.line_count() == 4); 795 kak_assert(buffer[0_line] == "allo ?\n"); 796 kak_assert(buffer[1_line] == "mais que fais la police\n"); 797 kak_assert(buffer[2_line] == " hein ?\n"); 798 kak_assert(buffer[3_line] == " youpi\n"); 799 kak_assert(not buffer.undo()); 800 801 buffer.move_to((Buffer::HistoryId)5); 802 kak_assert(not buffer.redo()); 803 804 buffer.move_to((Buffer::HistoryId)6); 805 kak_assert(not buffer.redo()); 806 }}; 807 808 }