2012-01-31 20:12:06 +01:00
|
|
|
#ifndef selection_hh_INCLUDED
|
|
|
|
#define selection_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-02-27 21:01:59 +01:00
|
|
|
// A Selection holds a buffer range
|
|
|
|
//
|
|
|
|
// The Selection class manage a (first, last) buffer iterators pair.
|
|
|
|
// Selections are oriented, first may be > last, and inclusive.
|
|
|
|
// Selection updates it's iterators according to modifications made
|
|
|
|
// in the buffer.
|
2012-07-16 21:51:37 +02:00
|
|
|
struct Selection : public BufferChangeListener
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-02-10 00:47:55 +01:00
|
|
|
Selection(const BufferIterator& first, const BufferIterator& last);
|
2012-01-31 20:12:06 +01:00
|
|
|
Selection(const Selection& other);
|
|
|
|
~Selection();
|
|
|
|
|
|
|
|
Selection& operator=(const Selection& other);
|
|
|
|
|
2012-02-27 21:01:59 +01:00
|
|
|
// returns min(first, last)
|
2012-01-31 20:12:06 +01:00
|
|
|
BufferIterator begin() const;
|
2012-02-27 21:01:59 +01:00
|
|
|
// returns max(first, last) + 1
|
2012-01-31 20:12:06 +01:00
|
|
|
BufferIterator end() const;
|
|
|
|
|
|
|
|
const BufferIterator& first() const { return m_first; }
|
|
|
|
const BufferIterator& last() const { return m_last; }
|
|
|
|
|
2012-11-19 14:19:31 +01:00
|
|
|
BufferIterator& first() { return m_first; }
|
|
|
|
BufferIterator& last() { return m_last; }
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
void merge_with(const Selection& selection);
|
2012-08-15 18:20:02 +02:00
|
|
|
void avoid_eol();
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-09-26 20:33:47 +02:00
|
|
|
private:
|
2012-09-04 00:17:41 +02:00
|
|
|
void on_insert(const BufferIterator& begin,
|
|
|
|
const BufferIterator& end) override;
|
|
|
|
void on_erase(const BufferIterator& begin,
|
|
|
|
const BufferIterator& end) override;
|
2012-07-16 21:51:37 +02:00
|
|
|
|
2012-10-08 14:26:57 +02:00
|
|
|
void check_invariant() const;
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
BufferIterator m_first;
|
|
|
|
BufferIterator m_last;
|
|
|
|
|
|
|
|
void register_with_buffer();
|
|
|
|
void unregister_with_buffer();
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<Selection> SelectionList;
|
2012-03-08 22:23:29 +01:00
|
|
|
typedef std::vector<String> CaptureList;
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2012-02-27 21:01:59 +01:00
|
|
|
// Selections are often associated with a capture list
|
|
|
|
// like when they are created from a regex match with
|
|
|
|
// capture groups.
|
2012-02-10 00:47:55 +01:00
|
|
|
struct SelectionAndCaptures
|
|
|
|
{
|
|
|
|
Selection selection;
|
|
|
|
CaptureList captures;
|
|
|
|
|
|
|
|
SelectionAndCaptures(const BufferIterator& first,
|
|
|
|
const BufferIterator& last,
|
2012-09-26 20:43:42 +02:00
|
|
|
CaptureList captures_list)
|
2012-09-26 20:33:47 +02:00
|
|
|
: selection(first, last), captures(std::move(captures_list)) {}
|
2012-09-26 20:43:42 +02:00
|
|
|
SelectionAndCaptures(const Selection& sel,
|
|
|
|
CaptureList captures_list)
|
|
|
|
: selection(sel), captures(std::move(captures_list)) {}
|
2012-02-10 00:47:55 +01:00
|
|
|
SelectionAndCaptures(const Selection& sel)
|
|
|
|
: selection(sel) {}
|
2012-09-26 20:33:47 +02:00
|
|
|
|
|
|
|
// helper to access the selection
|
|
|
|
BufferIterator begin() const { return selection.begin(); }
|
|
|
|
BufferIterator end() const { return selection.end(); }
|
|
|
|
|
|
|
|
const BufferIterator& first() const { return selection.first(); }
|
|
|
|
const BufferIterator& last() const { return selection.last(); }
|
2012-02-10 00:47:55 +01:00
|
|
|
};
|
|
|
|
|
2012-11-12 20:27:56 +01:00
|
|
|
inline bool overlaps(const SelectionAndCaptures& lhs,
|
|
|
|
const SelectionAndCaptures& rhs)
|
|
|
|
{
|
|
|
|
return (lhs.first() <= rhs.first() and lhs.last() >= rhs.first()) or
|
|
|
|
(lhs.first() <= rhs.last() and lhs.last() >= rhs.last());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-10 00:47:55 +01:00
|
|
|
typedef std::vector<SelectionAndCaptures> SelectionAndCapturesList;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selection_hh_INCLUDED
|
|
|
|
|