edbee - Qt Editor Library v0.11.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
textrange.h
Go to the documentation of this file.
1// edbee - Copyright (c) 2012-2025 by Rick Blommers and contributors
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include "edbee/exports.h"
7
8#include <QObject>
9#include <QVector>
10
12
13namespace edbee {
14
15class TextDocument;
16
30public:
31 TextRange( int anchor=0, int caret=0 ) : anchor_(anchor), caret_(caret) {}
32
33 inline int anchor() const { return anchor_; }
34 inline int caret() const { return caret_; }
35
37 inline int min() const { return qMin( caret_, anchor_ ); }
38 inline int max() const { return qMax( caret_, anchor_ ); }
39
41 inline int& minVar() { return caret_ < anchor_ ? caret_ : anchor_; }
42 inline int& maxVar() { return caret_ < anchor_ ? anchor_: caret_; }
43
44 inline int length() const { return qAbs(caret_ - anchor_ ); }
45
46 void fixCaretForUnicode(TextDocument* doc, int direction );
47
48
49 void setAnchor( int anchor ) { anchor_ = anchor; }
50 void setAnchorBounded( TextDocument* doc, int anchor );
51 void setCaret( int caret ) { caret_ = caret; }
52 void setCaretBounded( TextDocument* doc, int caret );
53 void setLength( int newLength );
54
55
56 void set( int anchor, int caret ) { anchor_ = anchor; caret_ = caret; }
57
58 void reset() { anchor_ = caret_; }
59 bool hasSelection() const { return anchor_ != caret_; }
60 bool isEmpty() const { return anchor_==caret_; }
61 void clearSelection() { caret_ = anchor_; }
62
63 QString toString() const;
64
65 void moveCaret( TextDocument* doc, int amount );
66 void moveCaretOrDeselect( TextDocument* doc, int amount );
67 int moveWhileChar( TextDocument* doc, int pos, int amount, const QString& chars );
68 int moveUntilChar( TextDocument* doc, int pos, int amount, const QString& chars );
69 void moveCaretWhileChar( TextDocument* doc, int amount, const QString& chars );
70 void moveCaretUntilChar( TextDocument* doc, int amount, const QString& chars );
71 void moveAnchortWhileChar( TextDocument* doc, int amount, const QString& chars );
72 void moveAnchorUntilChar( TextDocument* doc, int amount, const QString& chars );
73 void moveCaretByCharGroup( TextDocument* doc, int amount, const QString& whitespace, const QStringList& characterGroups );
74 void moveCaretToLineBoundary( TextDocument* doc, int amount, const QString& whitespace );
75 void moveCaretToWordBoundaryAtOffset( TextDocument* doc, int offset );
76 void moveCaretToLineBoundaryAtOffset( TextDocument* doc, int offset );
77
78
79 void expandToFullLine( TextDocument* doc, int amount );
80 void deselectTrailingNewLine( TextDocument* doc );
81 void expandToWord( TextDocument* doc, const QString& whitespace, const QStringList& characterGroups );
82 void expandToIncludeRange( TextRange& range );
83
84 void forceBounds( TextDocument* doc );
85
86 bool equals(const TextRange &range );
87 bool touches( TextRange& range );
88 bool contains( int pos );
89
90 static bool lessThan( TextRange& r1, TextRange& r2 );
91
92private:
93 int anchor_;
94 int caret_;
95};
96
97
98//======================================================================
99
100
109public:
111 virtual ~TextRangeSetBase() {}
112 // TextRangeSet(const TextRangeSet& sel);
113
114 // pure virtual methods
115// virtual TextRangeSetBase* clone() const = 0;
116 // TextRangeSet & operator=( const TextRangeSet& sel );
117
118 virtual int rangeCount() const = 0;
119 virtual TextRange& range(int idx) = 0;
120 virtual const TextRange& constRange(int idx) const = 0;
121 virtual void addRange( int anchor, int caret ) = 0;
122 virtual void addRange( const TextRange& range ) = 0;
123 virtual void removeRange( int idx ) = 0;
124 virtual void clear() = 0;
125 virtual void toSingleRange() = 0;
126 virtual void sortRanges() = 0;
127
130
131 int rangeIndexAtOffset( int offset );
132 bool rangesBetweenOffsets( int offsetBegin, int offsetEnd, int& firstIndex, int& lastIndex );
133 bool rangesBetweenOffsetsExlusiveEnd( int offsetBegin, int offsetEnd, int& firstIndex, int& lastIndex );
134 bool rangesAtLine( int line, int& firstIndex, int& lastIndex );
135 bool rangesAtLineExclusiveEnd( int line, int& firstIndex, int& lastIndex );
136 bool hasSelection();
137 bool equals( TextRangeSetBase& sel );
138 void replaceAll( const TextRangeSetBase& base );
139
140
141 QString getSelectedText();
143
144 QString rangesAsString() const;
145
146 // changing
147 void beginChanges();
148 void endChanges();
150 bool changing() const;
151
152 void resetAnchors();
153 void clearSelection();
154
155 void addTextRanges( const TextRangeSetBase& sel);
156 void substractTextRanges( const TextRangeSetBase& sel );
157 void substractRange( int min, int max );
158
159
160 // selection
161 void expandToFullLines(int amount);
162 void expandToWords( const QString& whitespace, const QStringList& characterGroups);
163 void selectWordAt(int offset , const QString& whitespace, const QStringList& characterGroups);
164 void toggleWordSelectionAt( int offset, const QString& whitespace, const QStringList& characterGroups);
165
166 // movement
167 void moveCarets( int amount );
168 void moveCaretsOrDeselect( int amount );
169 void moveCaretsByCharGroup( int amount, const QString& whitespace, const QStringList& charGroups );
170 void moveCaretsToLineBoundary(int direction, const QString& whitespace );
171 // void moveCaretsByLine( int amount ); //< Impossible to do without view when having a flexible font, guess that's why it wasn't implemented
172
173
174 // changing
175 // void growSelectionAtBegin( int amount );
176 void changeSpatial(int pos, int length, int newLength, bool sticky=false, bool performDelete=false);
177
178 void setRange( int anchor, int caret, int index = 0 );
179 void setRange( const TextRange& range , int index = 0 );
180
181 virtual void processChangesIfRequired(bool joinBorders=false);
182
183 // getters
184 TextDocument* textDocument() const;
185 //TextBuffer* textBuffer() const { return textBufferRef_; }
186
187
188 void mergeOverlappingRanges( bool joinBorders );
189
190protected:
191
194};
195
196
197//======================================================================
198
199
202{
203public:
205 TextRangeSet( const TextRangeSet& sel );
206 TextRangeSet( const TextRangeSet* sel );
207 virtual ~TextRangeSet() {}
208
209 TextRangeSet& operator=(const TextRangeSet& sel);
210 TextRangeSet* clone() const;
211
212 virtual int rangeCount() const { return selectionRanges_.size(); }
213 virtual TextRange& range(int idx);
214 virtual const TextRange& constRange(int idx ) const;
215 virtual void addRange(int anchor, int caret);
216 virtual void addRange( const TextRange& range );
217 virtual void removeRange(int idx);
218 virtual void clear();
219 virtual void toSingleRange();
220 virtual void sortRanges();
221
222private:
223
224 QVector<TextRange> selectionRanges_;
225};
226
227
228//======================================================================
229
230
240class EDBEE_EXPORT DynamicTextRangeSet : public QObject, public TextRangeSet
241{
242Q_OBJECT
243
244public:
245 DynamicTextRangeSet( TextDocument* doc, bool stickyMode=false, bool deleteMode=false, QObject* parent=0 );
246 DynamicTextRangeSet( const TextRangeSet& sel, bool stickyMode=false, bool deleteMode=false, QObject* parent=0 );
247 DynamicTextRangeSet( const TextRangeSet* sel, bool stickyMode=false, bool deleteMode=false, QObject* parent=0 );
248 virtual ~DynamicTextRangeSet();
249
250 void setStickyMode( bool mode );
251 bool stickyMode() const;
252
253 void setDeleteMode( bool mode );
254 bool deleteMode() const;
255
256public slots:
257 void textChanged( edbee::TextBufferChange change, QString oldText = QString() );
258
259private:
260 bool stickyMode_;
261 bool deleteMode_;
262};
263
264
265} // edbee
DynamicTextRangeSet(TextDocument *doc, bool stickyMode=false, bool deleteMode=false, QObject *parent=0)
Constructs the dynamic textrange set with a document.
Definition textrange.cpp:1156
bool deleteMode() const
Returns the current delete mode.
Definition textrange.cpp:1224
void textChanged(edbee::TextBufferChange change, QString oldText=QString())
This method is notified if a change happens to the textbuffer.
Definition textrange.cpp:1231
void setStickyMode(bool mode)
Is the stickymode enabled.
Definition textrange.cpp:1203
bool stickyMode() const
returns the current stickymode
Definition textrange.cpp:1210
void setDeleteMode(bool mode)
Sets the delete mode.
Definition textrange.cpp:1217
This clas represents a text buffer change and is used to pass around between events This is a sharedd...
Definition textbuffer.h:45
This is the base and abstract class of a text document A TextDocument is the model part of the editor...
Definition textdocument.h:42
A single text region A region constists of an anchor and a caret: The anchor defines the 'start' of t...
Definition textrange.h:29
int max() const
Definition textrange.h:38
bool hasSelection() const
Definition textrange.h:59
void clearSelection()
Definition textrange.h:61
TextRange(int anchor=0, int caret=0)
Definition textrange.h:31
int anchor() const
Definition textrange.h:33
int & maxVar()
Definition textrange.h:42
void reset()
Definition textrange.h:58
int length() const
Definition textrange.h:44
int & minVar()
returns the minimal variable reference
Definition textrange.h:41
void setAnchor(int anchor)
Definition textrange.h:49
void set(int anchor, int caret)
Definition textrange.h:56
void setCaret(int caret)
Definition textrange.h:51
int min() const
returns the minimal value
Definition textrange.h:37
int caret() const
Definition textrange.h:34
bool isEmpty() const
Definition textrange.h:60
bool rangesAtLine(int line, int &firstIndex, int &lastIndex)
Returns the range indices that are being used on the given line.
Definition textrange.cpp:521
int rangeIndexAtOffset(int offset)
This method returns the range index at the given offset.
Definition textrange.cpp:456
QString getSelectedTextExpandedToFullLines()
Returns ALL lines that are touched by the selection. This means Full lines are always returned.
Definition textrange.cpp:594
bool rangesBetweenOffsets(int offsetBegin, int offsetEnd, int &firstIndex, int &lastIndex)
returns the range indices that are being overlapped by the given offsetBegin and offsetEnd
Definition textrange.cpp:477
virtual const TextRange & constRange(int idx) const =0
void beginChanges()
This method starts the changes.
Definition textrange.cpp:654
TextRangeSetBase(TextDocument *doc)
Definition textrange.cpp:434
virtual void addRange(int anchor, int caret)=0
void setRange(int anchor, int caret, int index=0)
This method sets the first range item.
Definition textrange.cpp:941
void moveCaretsByCharGroup(int amount, const QString &whitespace, const QStringList &charGroups)
This method moves the carets.
Definition textrange.cpp:840
void endChanges()
Definition textrange.cpp:661
void replaceAll(const TextRangeSetBase &base)
Replaces all ranges with the supplied ranges.
Definition textrange.cpp:562
virtual void toSingleRange()=0
TextDocument * textDocument() const
Returns the associated textdocument.
Definition textrange.cpp:972
virtual void clear()=0
virtual void sortRanges()=0
QString rangesAsString() const
This method converts the selection ranges as a string, in the format: anchor>caret,...
Definition textrange.cpp:620
bool equals(TextRangeSetBase &sel)
This method checks if two selections are equal.
Definition textrange.cpp:551
virtual TextRange & range(int idx)=0
TextRange & lastRange()
this method returns the last range
Definition textrange.cpp:441
void expandToFullLines(int amount)
Expands the selection so it selects full lines.
Definition textrange.cpp:758
virtual void addRange(const TextRange &range)=0
virtual ~TextRangeSetBase()
Definition textrange.h:111
QString getSelectedText()
This method returns all selected text For every filled selection range a line is returned.
Definition textrange.cpp:574
bool rangesBetweenOffsetsExlusiveEnd(int offsetBegin, int offsetEnd, int &firstIndex, int &lastIndex)
returns the range indices that are being overlapped by the given offsetBegin and offsetEnd
Definition textrange.cpp:501
void clearSelection()
This method moves all carets to the anchor positions.
Definition textrange.cpp:644
void toggleWordSelectionAt(int offset, const QString &whitespace, const QStringList &characterGroups)
Toggles a word selection at the given location The idea is the following, double-click an empty place...
Definition textrange.cpp:792
virtual void removeRange(int idx)=0
void moveCaretsOrDeselect(int amount)
This method moves the carets or deslects the given character.
Definition textrange.cpp:830
int changing_
A (integer) boolean for handling changes between beginChagnes and endChanges.
Definition textrange.h:193
bool rangesAtLineExclusiveEnd(int line, int &firstIndex, int &lastIndex)
Returns the range indices that are being used on the given line (Excluding the last offset)
Definition textrange.cpp:530
TextDocument * textDocumentRef_
The reference to the textbuffer.
Definition textrange.h:192
virtual void processChangesIfRequired(bool joinBorders=false)
This method process the changes if required.
Definition textrange.cpp:959
void substractRange(int min, int max)
This method substracts a single range from the ranges list.
Definition textrange.cpp:713
void endChangesWithoutProcessing()
Ends the changes without processing. WARNING, you should ONLY call this method if the operation you p...
Definition textrange.cpp:672
void selectWordAt(int offset, const QString &whitespace, const QStringList &characterGroups)
Selects the word at the given offset.
Definition textrange.cpp:780
virtual int rangeCount() const =0
void moveCaretsToLineBoundary(int direction, const QString &whitespace)
Moves all carets to the given line boundary (line-boundary automatically switches between column 0 an...
Definition textrange.cpp:852
TextRange & firstRange()
the first range
Definition textrange.cpp:447
void moveCarets(int amount)
This method moves the carets by character.
Definition textrange.cpp:820
void changeSpatial(int pos, int length, int newLength, bool sticky=false, bool performDelete=false)
This method adds (or removes) the given spatial length at the given location.
Definition textrange.cpp:987
void addTextRanges(const TextRangeSetBase &sel)
An union operation This method adds all text selection-items. Merges all ranges.
Definition textrange.cpp:689
void expandToWords(const QString &whitespace, const QStringList &characterGroups)
Expands the selection to full words.
Definition textrange.cpp:769
bool changing() const
Checks if the current rangeset is in a changing state.
Definition textrange.cpp:680
void substractTextRanges(const TextRangeSetBase &sel)
The difference operation This method substracts the text-selection from the current selection.
Definition textrange.cpp:700
bool hasSelection()
This method checks if there's a selection available A selection is an range with a different anchor t...
Definition textrange.cpp:541
void resetAnchors()
This method resets all anchors to the positions of the carets.
Definition textrange.cpp:635
void mergeOverlappingRanges(bool joinBorders)
This method merges overlapping ranges.
Definition textrange.cpp:864
The basic textrange class. A simple class of textrange with a simple vector implementation.
Definition textrange.h:202
virtual ~TextRangeSet()
Definition textrange.h:207
virtual int rangeCount() const
Definition textrange.h:212
TextRangeSet(TextDocument *doc)
Constructs a textrange set.
Definition textrange.cpp:1043
#define EDBEE_EXPORT
Definition exports.h:15
QT Acessibility has an issue with reporting blank lines between elements lines. defining 'WINDOWS_EMP...
Definition commentcommand.cpp:20