selections should always point to an utf8 character sequence start byte

This commit is contained in:
Maxime Coste 2012-10-08 14:26:57 +02:00
parent f2e98f700e
commit 5a267ab627
3 changed files with 25 additions and 6 deletions

View File

@ -132,6 +132,7 @@ public:
BufferIterator end() const; BufferIterator end() const;
CharCount character_count() const; CharCount character_count() const;
LineCount line_count() const; LineCount line_count() const;
CharCount line_length(LineCount line) const;
// returns an iterator at given coordinates. line_and_column is // returns an iterator at given coordinates. line_and_column is
// clamped according to avoid_eol. // clamped according to avoid_eol.
@ -206,8 +207,6 @@ private:
void do_insert(const BufferIterator& pos, const String& content); void do_insert(const BufferIterator& pos, const String& content);
void do_erase(const BufferIterator& pos, CharCount length); void do_erase(const BufferIterator& pos, CharCount length);
CharCount line_length(LineCount line) const;
String m_name; String m_name;
const Type m_type; const Type m_type;

View File

@ -1,11 +1,14 @@
#include "selection.hh" #include "selection.hh"
#include "utf8.hh"
namespace Kakoune namespace Kakoune
{ {
Selection::Selection(const BufferIterator& first, const BufferIterator& last) Selection::Selection(const BufferIterator& first, const BufferIterator& last)
: m_first(first), m_last(last) : m_first(first), m_last(last)
{ {
check_invariant();
register_with_buffer(); register_with_buffer();
} }
@ -42,7 +45,7 @@ BufferIterator Selection::begin() const
BufferIterator Selection::end() const BufferIterator Selection::end() const
{ {
return std::max(m_first, m_last) + 1; return utf8::next(std::max(m_first, m_last));
} }
void Selection::merge_with(const Selection& selection) void Selection::merge_with(const Selection& selection)
@ -54,22 +57,31 @@ void Selection::merge_with(const Selection& selection)
m_last = selection.m_last; m_last = selection.m_last;
} }
static void avoid_eol(BufferIterator& it)
{
const auto column = it.column();
if (column != 0 and column == it.buffer().line_length(it.line()) - 1)
it = utf8::previous(it);
}
void Selection::avoid_eol() void Selection::avoid_eol()
{ {
m_first.clamp(true); Kakoune::avoid_eol(m_first);
m_last.clamp(true); Kakoune::avoid_eol(m_last);
} }
void Selection::on_insert(const BufferIterator& begin, const BufferIterator& end) void Selection::on_insert(const BufferIterator& begin, const BufferIterator& end)
{ {
m_first.on_insert(begin.coord(), end.coord()); m_first.on_insert(begin.coord(), end.coord());
m_last.on_insert(begin.coord(), end.coord()); m_last.on_insert(begin.coord(), end.coord());
check_invariant();
} }
void Selection::on_erase(const BufferIterator& begin, const BufferIterator& end) void Selection::on_erase(const BufferIterator& begin, const BufferIterator& end)
{ {
m_first.on_erase(begin.coord(), end.coord()); m_first.on_erase(begin.coord(), end.coord());
m_last.on_erase(begin.coord(), end.coord()); m_last.on_erase(begin.coord(), end.coord());
check_invariant();
} }
void Selection::register_with_buffer() void Selection::register_with_buffer()
@ -84,4 +96,10 @@ void Selection::unregister_with_buffer()
buffer.remove_change_listener(*this); buffer.remove_change_listener(*this);
} }
void Selection::check_invariant() const
{
assert(utf8::is_character_start(m_first));
assert(utf8::is_character_start(m_last));
}
} }

View File

@ -37,6 +37,8 @@ private:
void on_erase(const BufferIterator& begin, void on_erase(const BufferIterator& begin,
const BufferIterator& end) override; const BufferIterator& end) override;
void check_invariant() const;
BufferIterator m_first; BufferIterator m_first;
BufferIterator m_last; BufferIterator m_last;