Add Buffer::erase_range()
This commit is contained in:
parent
438a3e6960
commit
c3852e950a
@ -212,6 +212,19 @@ void Buffer::erase_code_point(const Buffer::Iterator & position)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Buffer::erase_range(const Buffer::Iterator & start,
|
||||||
|
const Buffer::Iterator & end)
|
||||||
|
{
|
||||||
|
if (start.valid() && (start < end))
|
||||||
|
{
|
||||||
|
push_operation();
|
||||||
|
size_t start_offset = start.offset();
|
||||||
|
size_t end_offset = end.valid() ? end.offset() : m_gap_buffer->size();
|
||||||
|
erase_data(start_offset, end_offset - start_offset, true);
|
||||||
|
pop_operation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Buffer::erase_data(size_t offset, size_t length, bool do_record_change)
|
void Buffer::erase_data(size_t offset, size_t length, bool do_record_change)
|
||||||
{
|
{
|
||||||
warp_iterators_before_delete(offset, length);
|
warp_iterators_before_delete(offset, length);
|
||||||
|
@ -131,6 +131,8 @@ public:
|
|||||||
bool insert_mode() const { return m_insert_mode; }
|
bool insert_mode() const { return m_insert_mode; }
|
||||||
void insert_code_point(const Buffer::Iterator & position, uint32_t code_point);
|
void insert_code_point(const Buffer::Iterator & position, uint32_t code_point);
|
||||||
void erase_code_point(const Buffer::Iterator & position);
|
void erase_code_point(const Buffer::Iterator & position);
|
||||||
|
void erase_range(const Buffer::Iterator & start,
|
||||||
|
const Buffer::Iterator & end);
|
||||||
std::shared_ptr<std::string> filename() const { return m_filename; }
|
std::shared_ptr<std::string> filename() const { return m_filename; }
|
||||||
Iterator begin() const { return Iterator(this); }
|
Iterator begin() const { return Iterator(this); }
|
||||||
Iterator end() const { return *m_eof_iterator; }
|
Iterator end() const { return *m_eof_iterator; }
|
||||||
|
@ -133,6 +133,37 @@ TEST(BufferTest, allows_inserting_and_erasing_characters)
|
|||||||
b.exit_insert_mode();
|
b.exit_insert_mode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, allows_erasing_ranges)
|
||||||
|
{
|
||||||
|
std::string s("abc\ndef\n");
|
||||||
|
Buffer b((const uint8_t *)&s[0], s.size());
|
||||||
|
auto it = b.add_iterator();
|
||||||
|
auto it2 = b.add_iterator();
|
||||||
|
it2->go_next_line();
|
||||||
|
|
||||||
|
/* Invalid tests. */
|
||||||
|
b.erase_range(b.end(), b.end());
|
||||||
|
EXPECT_EQ(ss("abc\ndef\n"), b.get_string());
|
||||||
|
b.erase_range(*it, b.begin());
|
||||||
|
EXPECT_EQ(ss("abc\ndef\n"), b.get_string());
|
||||||
|
|
||||||
|
EXPECT_EQ(C('a'), **it);
|
||||||
|
EXPECT_EQ(C('d'), **it2);
|
||||||
|
b.erase_range(*it, *it2);
|
||||||
|
EXPECT_EQ(ss("def\n"), b.get_string());
|
||||||
|
EXPECT_EQ(*it, *it2);
|
||||||
|
EXPECT_EQ(C('d'), **it);
|
||||||
|
while (it2->valid())
|
||||||
|
{
|
||||||
|
it2->go_forward();
|
||||||
|
}
|
||||||
|
EXPECT_EQ(C('d'), **it);
|
||||||
|
b.erase_range(*it, *it2);
|
||||||
|
EXPECT_EQ(ss(""), b.get_string());
|
||||||
|
EXPECT_FALSE(it->valid());
|
||||||
|
EXPECT_FALSE(it2->valid());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(BufferTest, adds_a_newline_after_inserted_character_when_inserting_in_an_empty_buffer)
|
TEST(BufferTest, adds_a_newline_after_inserted_character_when_inserting_in_an_empty_buffer)
|
||||||
{
|
{
|
||||||
Buffer b;
|
Buffer b;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user