2012-01-31 20:12:06 +01:00
|
|
|
#ifndef selection_hh_INCLUDED
|
|
|
|
#define selection_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
// An oriented, inclusive buffer range
|
|
|
|
struct Range
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-11-30 18:32:49 +01:00
|
|
|
public:
|
2013-07-26 00:44:00 +02:00
|
|
|
Range(BufferCoord first, BufferCoord last)
|
2012-11-30 18:32:49 +01:00
|
|
|
: m_first{first}, m_last{last} {}
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
void merge_with(const Range& range);
|
|
|
|
|
2013-06-03 18:58:09 +02:00
|
|
|
BufferCoord& first() { return m_first; }
|
|
|
|
BufferCoord& last() { return m_last; }
|
2012-11-30 18:32:49 +01:00
|
|
|
|
2013-06-03 18:58:09 +02:00
|
|
|
const BufferCoord& first() const { return m_first; }
|
|
|
|
const BufferCoord& last() const { return m_last; }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-12-11 19:51:59 +01:00
|
|
|
bool operator== (const Range& other) const
|
|
|
|
{
|
|
|
|
return m_first == other.m_first and m_last == other.m_last;
|
|
|
|
}
|
|
|
|
|
2013-06-03 18:58:09 +02:00
|
|
|
const BufferCoord& min() const { return std::min(m_first, m_last); }
|
|
|
|
const BufferCoord& max() const { return std::max(m_first, m_last); }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
private:
|
2013-06-03 18:58:09 +02:00
|
|
|
BufferCoord m_first;
|
|
|
|
BufferCoord m_last;
|
2012-11-30 18:32:49 +01:00
|
|
|
};
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-12-11 14:11:33 +01:00
|
|
|
inline bool overlaps(const Range& lhs, const Range& rhs)
|
|
|
|
{
|
2013-05-24 14:25:50 +02:00
|
|
|
return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min()
|
|
|
|
: lhs.min() <= rhs.max();
|
2013-05-03 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2013-06-03 18:58:09 +02:00
|
|
|
inline String content(const Buffer& buffer, const Range& range)
|
|
|
|
{
|
|
|
|
return buffer.string(range.min(), buffer.char_next(range.max()));
|
|
|
|
}
|
|
|
|
|
2013-06-06 19:39:53 +02:00
|
|
|
inline BufferIterator erase(Buffer& buffer, const Range& range)
|
2013-06-03 18:58:09 +02:00
|
|
|
{
|
2013-06-06 19:39:53 +02:00
|
|
|
return buffer.erase(buffer.iterator_at(range.min()),
|
|
|
|
utf8::next(buffer.iterator_at(range.max())));
|
2013-06-03 18:58:09 +02:00
|
|
|
}
|
|
|
|
|
2013-12-15 21:52:57 +01:00
|
|
|
inline CharCount char_length(const Buffer& buffer, const Range& range)
|
|
|
|
{
|
|
|
|
return utf8::distance(buffer.iterator_at(range.min()),
|
|
|
|
utf8::next(buffer.iterator_at(range.max())));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-14 15:08:26 +01:00
|
|
|
inline void avoid_eol(const Buffer& buffer, BufferCoord& coord)
|
|
|
|
{
|
|
|
|
const auto column = coord.column;
|
|
|
|
const auto& line = buffer[coord.line];
|
|
|
|
if (column != 0 and column == line.length() - 1)
|
|
|
|
coord.column = line.byte_count_to(line.char_length() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void avoid_eol(const Buffer& buffer, Range& sel)
|
|
|
|
{
|
|
|
|
avoid_eol(buffer, sel.first());
|
|
|
|
avoid_eol(buffer, sel.last());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
using CaptureList = std::vector<String>;
|
|
|
|
|
|
|
|
// A selection is a Range, associated with a CaptureList
|
2012-12-11 19:51:59 +01:00
|
|
|
struct Selection : public Range
|
2012-11-30 18:32:49 +01:00
|
|
|
{
|
2013-07-26 00:44:00 +02:00
|
|
|
explicit Selection(BufferCoord pos) : Range(pos,pos) {}
|
|
|
|
Selection(BufferCoord first, BufferCoord last,
|
2012-12-11 19:51:59 +01:00
|
|
|
CaptureList captures = {})
|
2013-01-04 18:39:13 +01:00
|
|
|
: Range(first, last), m_captures(std::move(captures)) {}
|
2012-11-19 14:19:31 +01:00
|
|
|
|
2013-04-22 14:18:49 +02:00
|
|
|
Selection(const Range& range)
|
|
|
|
: Range(range) {}
|
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
CaptureList& captures() { return m_captures; }
|
|
|
|
const CaptureList& captures() const { return m_captures; }
|
|
|
|
|
2012-09-26 20:33:47 +02:00
|
|
|
private:
|
2012-11-30 18:32:49 +01:00
|
|
|
CaptureList m_captures;
|
2012-02-10 00:47:55 +01:00
|
|
|
};
|
2013-05-02 19:04:59 +02:00
|
|
|
|
2013-12-13 00:17:06 +01:00
|
|
|
static bool compare_selections(const Selection& lhs, const Selection& rhs)
|
|
|
|
{
|
|
|
|
return lhs.min() < rhs.min();
|
|
|
|
}
|
|
|
|
|
2013-05-02 19:04:59 +02:00
|
|
|
struct SelectionList : std::vector<Selection>
|
|
|
|
{
|
2013-06-18 21:43:44 +02:00
|
|
|
SelectionList() = default;
|
2013-12-14 19:38:14 +01:00
|
|
|
SelectionList(BufferCoord c) : std::vector<Selection>{Selection{c,c}} {}
|
2013-06-18 21:43:44 +02:00
|
|
|
SelectionList(Selection s) : std::vector<Selection>{s} {}
|
2013-05-02 19:04:59 +02:00
|
|
|
|
2013-07-26 00:44:00 +02:00
|
|
|
void update_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end);
|
|
|
|
void update_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end);
|
2013-05-03 13:38:48 +02:00
|
|
|
|
|
|
|
void check_invariant() const;
|
2013-12-13 00:17:06 +01:00
|
|
|
|
|
|
|
const Selection& main() const { return (*this)[m_main]; }
|
|
|
|
Selection& main() { return (*this)[m_main]; }
|
|
|
|
size_t main_index() const { return m_main; }
|
|
|
|
void set_main_index(size_t main) { kak_assert(main < size()); m_main = main; }
|
|
|
|
|
|
|
|
void rotate_main(int count) { m_main = (m_main + count) % size(); }
|
|
|
|
|
|
|
|
template<typename OverlapsFunc>
|
|
|
|
void merge_overlapping(OverlapsFunc overlaps)
|
|
|
|
{
|
|
|
|
kak_assert(std::is_sorted(begin(), end(), compare_selections));
|
|
|
|
for (size_t i = 0; i+1 < size() and size() > 1;)
|
|
|
|
{
|
|
|
|
if (overlaps((*this)[i], (*this)[i+1]))
|
|
|
|
{
|
|
|
|
(*this)[i].merge_with((*this)[i+1]);
|
|
|
|
erase(begin() + i + 1);
|
|
|
|
if (i + 1 <= m_main)
|
|
|
|
--m_main;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sort_and_merge_overlapping()
|
|
|
|
{
|
|
|
|
if (size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& main = this->main();
|
|
|
|
const auto main_begin = main.min();
|
|
|
|
m_main = std::count_if(begin(), end(), [&](const Selection& sel) {
|
|
|
|
auto begin = sel.min();
|
|
|
|
if (begin == main_begin)
|
|
|
|
return &sel < &main;
|
|
|
|
else
|
|
|
|
return begin < main_begin;
|
|
|
|
});
|
|
|
|
std::stable_sort(begin(), end(), compare_selections);
|
|
|
|
merge_overlapping(overlaps);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_main = 0;
|
2013-05-02 19:04:59 +02:00
|
|
|
};
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selection_hh_INCLUDED
|