From da9d099f3bdf1806b647fd8deabac60552bb04b2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 29 Mar 2014 08:55:45 +0000 Subject: [PATCH] Remove Range struct, merge it back in Selection --- src/main.cc | 2 +- src/normal.cc | 4 ++-- src/selection.cc | 2 +- src/selection.hh | 56 +++++++++++++++++++----------------------------- src/selectors.cc | 12 +++++------ src/selectors.hh | 2 +- 6 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/main.cc b/src/main.cc index 681ffb0b..1be2212b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -75,7 +75,7 @@ void register_env_vars() }, { "selection", [](const String& name, const Context& context) - { const Range& sel = context.selections().main(); + { const Selection& sel = context.selections().main(); return content(context.buffer(), sel); } }, { "selections", diff --git a/src/normal.cc b/src/normal.cc index 4297e7bb..75b04e59 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -296,7 +296,7 @@ void goto_commands(Context& context, int line) } case 'f': { - const Range& sel = context.selections().main(); + const Selection& sel = context.selections().main(); String filename = content(buffer, sel); static constexpr char forbidden[] = { '\'', '\\', '\0' }; for (auto c : forbidden) @@ -1211,7 +1211,7 @@ private: SelectionList m_ranges; }; -inline bool touches(const Buffer& buffer, const Range& lhs, const Range& rhs) +inline bool touches(const Buffer& buffer, const Selection& lhs, const Selection& rhs) { return lhs.min() <= rhs.min() ? buffer.char_next(lhs.max()) >= rhs.min() : lhs.min() <= buffer.char_next(rhs.max()); diff --git a/src/selection.cc b/src/selection.cc index b65128ee..52d8bc16 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -5,7 +5,7 @@ namespace Kakoune { -void Range::merge_with(const Range& range) +void Selection::merge_with(const Selection& range) { m_cursor = range.m_cursor; if (m_anchor < m_cursor) diff --git a/src/selection.hh b/src/selection.hh index c386994a..21cc73c2 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -6,14 +6,18 @@ namespace Kakoune { -// An oriented, inclusive buffer range -struct Range -{ -public: - Range(BufferCoord anchor, BufferCoord cursor) - : m_anchor{anchor}, m_cursor{cursor} {} +using CaptureList = std::vector; - void merge_with(const Range& range); +// A selection is a Selection, associated with a CaptureList +struct Selection +{ + explicit Selection(BufferCoord pos) : Selection(pos,pos) {} + Selection(BufferCoord anchor, BufferCoord cursor, + CaptureList captures = {}) + : m_anchor{anchor}, m_cursor{cursor}, + m_captures(std::move(captures)) {} + + void merge_with(const Selection& range); BufferCoord& anchor() { return m_anchor; } BufferCoord& cursor() { return m_cursor; } @@ -21,7 +25,10 @@ public: const BufferCoord& anchor() const { return m_anchor; } const BufferCoord& cursor() const { return m_cursor; } - bool operator== (const Range& other) const + CaptureList& captures() { return m_captures; } + const CaptureList& captures() const { return m_captures; } + + bool operator== (const Selection& other) const { return m_anchor == other.m_anchor and m_cursor == other.m_cursor; } @@ -32,26 +39,28 @@ public: private: BufferCoord m_anchor; BufferCoord m_cursor; + + CaptureList m_captures; }; -inline bool overlaps(const Range& lhs, const Range& rhs) +inline bool overlaps(const Selection& lhs, const Selection& rhs) { return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min() : lhs.min() <= rhs.max(); } -inline String content(const Buffer& buffer, const Range& range) +inline String content(const Buffer& buffer, const Selection& range) { return buffer.string(range.min(), buffer.char_next(range.max())); } -inline BufferIterator erase(Buffer& buffer, const Range& range) +inline BufferIterator erase(Buffer& buffer, const Selection& range) { return buffer.erase(buffer.iterator_at(range.min()), utf8::next(buffer.iterator_at(range.max()))); } -inline CharCount char_length(const Buffer& buffer, const Range& range) +inline CharCount char_length(const Buffer& buffer, const Selection& range) { return utf8::distance(buffer.iterator_at(range.min()), utf8::next(buffer.iterator_at(range.max()))); @@ -66,33 +75,12 @@ inline void avoid_eol(const Buffer& buffer, BufferCoord& coord) coord.column = line.byte_count_to(line.char_length() - 2); } -inline void avoid_eol(const Buffer& buffer, Range& sel) +inline void avoid_eol(const Buffer& buffer, Selection& sel) { avoid_eol(buffer, sel.anchor()); avoid_eol(buffer, sel.cursor()); } - -using CaptureList = std::vector; - -// A selection is a Range, associated with a CaptureList -struct Selection : public Range -{ - explicit Selection(BufferCoord pos) : Range(pos,pos) {} - Selection(BufferCoord anchor, BufferCoord cursor, - CaptureList captures = {}) - : Range(anchor, cursor), m_captures(std::move(captures)) {} - - Selection(const Range& range) - : Range(range) {} - - CaptureList& captures() { return m_captures; } - const CaptureList& captures() const { return m_captures; } - -private: - CaptureList m_captures; -}; - static bool compare_selections(const Selection& lhs, const Selection& rhs) { return lhs.min() < rhs.min(); diff --git a/src/selectors.cc b/src/selectors.cc index e800643d..33e9216f 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -76,10 +76,10 @@ Selection select_matching(const Buffer& buffer, const Selection& selection) // c++14 will add std::optional, so we use boost::optional until then using boost::optional; -static optional find_surrounding(const Buffer& buffer, - BufferCoord coord, - CodepointPair matching, - ObjectFlags flags, int init_level) +static optional find_surrounding(const Buffer& buffer, + BufferCoord coord, + CodepointPair matching, + ObjectFlags flags, int init_level) { const bool to_begin = flags & ObjectFlags::ToBegin; const bool to_end = flags & ObjectFlags::ToEnd; @@ -103,7 +103,7 @@ static optional find_surrounding(const Buffer& buffer, --first; } if (level != 0 or *first != matching.first) - return optional{}; + return optional{}; } Utf8Iterator last = pos; @@ -124,7 +124,7 @@ static optional find_surrounding(const Buffer& buffer, ++last; } if (level != 0 or last == buffer.end()) - return optional{}; + return optional{}; } if (flags & ObjectFlags::Inner) diff --git a/src/selectors.hh b/src/selectors.hh index b01c53eb..51a61291 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -51,7 +51,7 @@ inline void remove_selection(SelectionList& selections, int index) using Utf8Iterator = utf8::utf8_iterator; -inline Range utf8_range(const Utf8Iterator& first, const Utf8Iterator& last) +inline Selection utf8_range(const Utf8Iterator& first, const Utf8Iterator& last) { return {first.base().coord(), last.base().coord()}; }