Restores the piece-table text editor from agent-work on the merged Containers AVL tree, with the test build no longer gated behind the undefined MODULES_BUILD_TESTS option. The module is registered in the root build.
208 lines
4.6 KiB
C++
208 lines
4.6 KiB
C++
#pragma once
|
|
|
|
#include "Buffer.hpp"
|
|
#include "Tree.hpp"
|
|
|
|
namespace tp {
|
|
|
|
typedef char Char;
|
|
typedef halni Index;
|
|
typedef halni LineId;
|
|
typedef halni ColumnId;
|
|
|
|
bool isNewLineChar(Char character);
|
|
|
|
struct Input {
|
|
const Char* mInput = nullptr;
|
|
Index mLen = 0;
|
|
|
|
void getLineOffsets(Buffer<Index>& out) const;
|
|
};
|
|
|
|
struct Pointer {
|
|
LineId mLine;
|
|
ColumnId mCol;
|
|
|
|
Pointer();
|
|
Pointer(LineId aLine, ColumnId aCol);
|
|
explicit Pointer(const Input& aInput);
|
|
|
|
void operator+=(Char achar);
|
|
void operator+=(const Pointer& ap);
|
|
Pointer operator-(const Pointer& in) const;
|
|
Pointer operator+(const Pointer& ap) const;
|
|
bool operator==(const Pointer& in) const;
|
|
bool operator!=(const Pointer& in) const;
|
|
bool operator>(const Pointer& in) const;
|
|
bool operator>=(const Pointer& in) const;
|
|
};
|
|
|
|
struct Piece {
|
|
bool mAddedBuff = false;
|
|
Pointer mStart;
|
|
Pointer mEnd;
|
|
|
|
Pointer diffPtr() const;
|
|
LineId nLines() const;
|
|
};
|
|
|
|
class PieceKey {
|
|
public:
|
|
PieceKey() = default;
|
|
|
|
static PieceKey forLookup(Pointer position);
|
|
|
|
bool descentRight(const PieceKey& query) const;
|
|
bool exactNode(const PieceKey& query) const;
|
|
PieceKey keyInRightSubtree(const PieceKey& query) const;
|
|
PieceKey keyInLeftSubtree(const PieceKey& query) const;
|
|
|
|
template <typename NodeType>
|
|
void updateNodeCache(const NodeType* node) {
|
|
mSpan = node->data.diffPtr();
|
|
mLeftSpan = node->mLeft->key.subtreeSpan();
|
|
mRightSpan = node->mRight->key.subtreeSpan();
|
|
}
|
|
|
|
Pointer span() const;
|
|
Pointer leftSpan() const;
|
|
Pointer subtreeSpan() const;
|
|
|
|
private:
|
|
Pointer mSpan;
|
|
Pointer mLeftSpan;
|
|
Pointer mRightSpan;
|
|
Pointer mLookupPosition;
|
|
};
|
|
|
|
class PiecesDS {
|
|
typedef AvlTree<PieceKey, Piece> Tree;
|
|
|
|
public:
|
|
typedef Tree::Node Node;
|
|
|
|
PiecesDS() = default;
|
|
|
|
Node* find(Pointer position) const;
|
|
Node* first() const;
|
|
Node* last() const;
|
|
Node* next(Node* node) const;
|
|
Node* prev(Node* node) const;
|
|
|
|
Pointer position(const Node* node) const;
|
|
Pointer ptrSum() const;
|
|
ualni nPieces() const;
|
|
|
|
Node* insertAfter(Node* predecessor, const Piece& piece);
|
|
void remove(Node* node);
|
|
void resize(Node* node, Pointer newEnd);
|
|
|
|
void undo() {}
|
|
void redo() {}
|
|
void pushUndoPoint() {}
|
|
|
|
private:
|
|
Tree mTree;
|
|
};
|
|
|
|
struct AddBuff {
|
|
Buffer<Char> mBuff;
|
|
Buffer<Index> mLineOffsets;
|
|
|
|
AddBuff();
|
|
void append(const Input& aInput);
|
|
Pointer endPtr() const;
|
|
};
|
|
|
|
struct OrigBuff {
|
|
bool mOwnsInput = false;
|
|
Input mOriginal;
|
|
Buffer<Index> mLineOffsets;
|
|
|
|
Pointer mPtrStart;
|
|
Pointer mPtrEnd;
|
|
|
|
explicit OrigBuff(Input aOriginal);
|
|
OrigBuff(const OrigBuff&) = delete;
|
|
OrigBuff& operator=(const OrigBuff&) = delete;
|
|
~OrigBuff();
|
|
|
|
void makeOwnOriginalInput();
|
|
};
|
|
|
|
struct PieceCrs {
|
|
Pointer piece_ptr;
|
|
Pointer offset;
|
|
Pointer ptr;
|
|
PiecesDS::Node* piece = nullptr;
|
|
};
|
|
|
|
class TextEditor {
|
|
public:
|
|
struct IteratorEnd {};
|
|
|
|
class Iterator {
|
|
friend TextEditor;
|
|
|
|
public:
|
|
Char character() const;
|
|
void operator++();
|
|
bool operator!=(const IteratorEnd&) const;
|
|
bool operator==(const IteratorEnd&) const;
|
|
const Iterator& operator*() const;
|
|
|
|
private:
|
|
explicit Iterator(const TextEditor* self);
|
|
void loadCharacter();
|
|
|
|
const TextEditor* mSelf;
|
|
PiecesDS::Node* mNode = nullptr;
|
|
Pointer mOffset = { 0, 0 };
|
|
Char mVal = 0;
|
|
};
|
|
|
|
enum Dir : uint1 { LEFT, RIGHT, UP, DOWN };
|
|
enum DirUnit : uint1 { CHAR, WORD, TOK, LINE_END, LINE_START };
|
|
|
|
public:
|
|
TextEditor();
|
|
explicit TextEditor(Input original);
|
|
|
|
[[nodiscard]] Index mNLines() const;
|
|
void clampPtr(Pointer& ptr) const;
|
|
[[nodiscard]] Pointer getCursor() const;
|
|
[[nodiscard]] Pointer getCursorEnd() const;
|
|
void setCursor(Pointer ptr);
|
|
void setCursorEnd(Pointer ptr);
|
|
|
|
void insert(Input aInput);
|
|
void remove(bool only_selection = false, Dir dir = LEFT, DirUnit unit = CHAR);
|
|
|
|
void undo();
|
|
void redo();
|
|
|
|
Iterator begin() const;
|
|
IteratorEnd end() const;
|
|
|
|
private:
|
|
[[nodiscard]] PieceCrs findPiece(Pointer ptr) const;
|
|
[[nodiscard]] Pointer prevPiecePtr(PiecesDS::Node* piece_node) const;
|
|
[[nodiscard]] Pointer nextPiecePtr(PiecesDS::Node* piece_node) const;
|
|
|
|
[[nodiscard]] const Char* getBuffLine(const Piece& piece, LineId line) const;
|
|
[[nodiscard]] const Char* getPieceLine(const Piece& piece, LineId line) const;
|
|
[[nodiscard]] LineId getPieceLineLen(const Piece& piece, LineId line) const;
|
|
[[nodiscard]] PiecesDS::Node* splitPiece(PiecesDS::Node* piece, Pointer split_ptr);
|
|
|
|
void removeRange(PiecesDS::Node* first, PiecesDS::Node* last);
|
|
|
|
private:
|
|
PiecesDS mPieces;
|
|
OrigBuff mOrigBuff;
|
|
AddBuff mAddBuff;
|
|
|
|
Pointer mCursor;
|
|
Pointer mCursorEnd;
|
|
Pointer mLastInsertionCrs;
|
|
};
|
|
}
|