Remove Range struct, merge it back in Selection

This commit is contained in:
Maxime Coste 2014-03-29 08:55:45 +00:00
parent c38e14958f
commit da9d099f3b
6 changed files with 33 additions and 45 deletions

View File

@ -75,7 +75,7 @@ void register_env_vars()
}, { }, {
"selection", "selection",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ const Range& sel = context.selections().main(); { const Selection& sel = context.selections().main();
return content(context.buffer(), sel); } return content(context.buffer(), sel); }
}, { }, {
"selections", "selections",

View File

@ -296,7 +296,7 @@ void goto_commands(Context& context, int line)
} }
case 'f': case 'f':
{ {
const Range& sel = context.selections().main(); const Selection& sel = context.selections().main();
String filename = content(buffer, sel); String filename = content(buffer, sel);
static constexpr char forbidden[] = { '\'', '\\', '\0' }; static constexpr char forbidden[] = { '\'', '\\', '\0' };
for (auto c : forbidden) for (auto c : forbidden)
@ -1211,7 +1211,7 @@ private:
SelectionList m_ranges; 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() return lhs.min() <= rhs.min() ? buffer.char_next(lhs.max()) >= rhs.min()
: lhs.min() <= buffer.char_next(rhs.max()); : lhs.min() <= buffer.char_next(rhs.max());

View File

@ -5,7 +5,7 @@
namespace Kakoune namespace Kakoune
{ {
void Range::merge_with(const Range& range) void Selection::merge_with(const Selection& range)
{ {
m_cursor = range.m_cursor; m_cursor = range.m_cursor;
if (m_anchor < m_cursor) if (m_anchor < m_cursor)

View File

@ -6,14 +6,18 @@
namespace Kakoune namespace Kakoune
{ {
// An oriented, inclusive buffer range using CaptureList = std::vector<String>;
struct Range
{
public:
Range(BufferCoord anchor, BufferCoord cursor)
: m_anchor{anchor}, m_cursor{cursor} {}
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& anchor() { return m_anchor; }
BufferCoord& cursor() { return m_cursor; } BufferCoord& cursor() { return m_cursor; }
@ -21,7 +25,10 @@ public:
const BufferCoord& anchor() const { return m_anchor; } const BufferCoord& anchor() const { return m_anchor; }
const BufferCoord& cursor() const { return m_cursor; } 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; return m_anchor == other.m_anchor and m_cursor == other.m_cursor;
} }
@ -32,26 +39,28 @@ public:
private: private:
BufferCoord m_anchor; BufferCoord m_anchor;
BufferCoord m_cursor; 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() return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min()
: lhs.min() <= rhs.max(); : 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())); 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()), return buffer.erase(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max()))); 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()), return utf8::distance(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max()))); 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); 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.anchor());
avoid_eol(buffer, sel.cursor()); avoid_eol(buffer, sel.cursor());
} }
using CaptureList = std::vector<String>;
// 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) static bool compare_selections(const Selection& lhs, const Selection& rhs)
{ {
return lhs.min() < rhs.min(); return lhs.min() < rhs.min();

View File

@ -76,7 +76,7 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
// c++14 will add std::optional, so we use boost::optional until then // c++14 will add std::optional, so we use boost::optional until then
using boost::optional; using boost::optional;
static optional<Range> find_surrounding(const Buffer& buffer, static optional<Selection> find_surrounding(const Buffer& buffer,
BufferCoord coord, BufferCoord coord,
CodepointPair matching, CodepointPair matching,
ObjectFlags flags, int init_level) ObjectFlags flags, int init_level)
@ -103,7 +103,7 @@ static optional<Range> find_surrounding(const Buffer& buffer,
--first; --first;
} }
if (level != 0 or *first != matching.first) if (level != 0 or *first != matching.first)
return optional<Range>{}; return optional<Selection>{};
} }
Utf8Iterator last = pos; Utf8Iterator last = pos;
@ -124,7 +124,7 @@ static optional<Range> find_surrounding(const Buffer& buffer,
++last; ++last;
} }
if (level != 0 or last == buffer.end()) if (level != 0 or last == buffer.end())
return optional<Range>{}; return optional<Selection>{};
} }
if (flags & ObjectFlags::Inner) if (flags & ObjectFlags::Inner)

View File

@ -51,7 +51,7 @@ inline void remove_selection(SelectionList& selections, int index)
using Utf8Iterator = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pass>; using Utf8Iterator = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pass>;
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()}; return {first.base().coord(), last.base().coord()};
} }