edbee - Qt Editor Library v0.11.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
textdocumentscopes.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 <QHash>
9#include <QObject>
10#include <QStringList>
11#include <QVector>
12
16
17namespace edbee {
18
20class RegExp;
21class ScopedTextRange;
23class TextGrammarRule;
24class TextScope;
25
27typedef short TextScopeAtomId;
28
29/*
30 ScopeElement
31 FullScope = ScopeElement.ScopeElement.ScopeElement
32*/
33
36class EDBEE_EXPORT TextScope {
37public:
38 const QString name();
39 int atomCount();
40 TextScopeAtomId atomAt(int idx) const;
41
42 bool startsWith( TextScope* scope );
43 int rindexOf( TextScope* scope );
44
45private:
46 TextScope( const QString& fullScope );
47 TextScope();
48 ~TextScope();
49
50 char scopeAtomCount_;
51 TextScopeAtomId* scopeAtoms_;
52
53 friend class TextScopeManager;
54};
55
56//===========================================
57
60class EDBEE_EXPORT TextScopeList : public QVector<TextScope*>
61{
62public:
64 TextScopeList( int initialSize );
65 TextScopeList( QVector<ScopedTextRange*>& ranges );
66
67 int atomCount() const;
68
69 QString toString();
70};
71
72
73//===========================================
74
113public:
114 TextScopeSelector( const QString& selector );
115 virtual ~TextScopeSelector();
116
117 double calculateMatchScore(const TextScopeList* scopeList );
118 QString toString();
119
120private:
121 double calculateMatchScoreForSelector( TextScopeList* selector, const TextScopeList* scopeList );
122
123
124private:
125 QVector<TextScopeList*> selectorList_;
126};
127
128
129//===========================================
130
140public:
142 virtual ~TextScopeManager();
143
144public:
145
146 void reset();
147
149
150 TextScopeAtomId findOrRegisterScopeAtom( const QString& atom );
151 TextScope* refTextScope( const QString& scopeString );
153
154 TextScopeList* createTextScopeList(const QString &scopeListString );
155
156 const QString& atomName( TextScopeAtomId id );
157
158private:
159 TextScopeAtomId wildCardId_;
160
161 // scope atoms
162 QList<QString> atomNameList_;
163 QHash<QString,TextScopeAtomId> atomNameMap_;
164
165 // full scopes
166 QList<TextScope*> textScopeList_;
167 QHash<QString,TextScope*> textScopeRefMap_;
168};
169
170
171//===========================================
172
173
176{
177public:
179// ScopedTextRange( const MultiLineScopedTextRange& range );
180 virtual ~ScopedTextRange();
181
182 void setScope( TextScope* scope );
183 TextScope* scope() const;
184 QString toString() const;
185
188
189
190private:
191 TextScope* scopeRef_;
192
193};
194
195
196//===========================================
197
198
212
213
214
215//===========================================
216
221 Q_DISABLE_COPY(ScopedTextRangeList)
222public:
223
224 explicit ScopedTextRangeList();
225 virtual ~ScopedTextRangeList();
226
227 int size() const;
228 ScopedTextRange* at( int idx );
231
232 void squeeze();
233 void setIndependent(bool enable=true);
234 bool isIndependent() const;
235
236 QString toString();
237
238
239private:
240
241 QVector<ScopedTextRange*> ranges_;
242 bool independent_;
243
244// int size_; /// The number of ranges
245// ScopedTextRange* ranges_; /// The list of ranges
246};
247
248
249//===========================================
250
251
254{
255public:
258
259 void setGrammarRule( TextGrammarRule* rule );
261
262 void giveEndRegExp( RegExp* regExp );
263 RegExp* endRegExp();
264
266
267private:
268 TextGrammarRule* ruleRef_;
269 RegExp* endRegExp_;
270};
271
272
273//===========================================
274
275
279{
280public:
283 void reset();
284
285 // text range functionality
286 virtual int rangeCount() const;
287 virtual TextRange& range(int idx);
288 virtual const TextRange& constRange(int idx) const;
289 virtual void addRange( int anchor, int caret );
290 virtual void addRange(const TextRange& range);
291
292 virtual void removeRange( int idx );
293 virtual void clear();
294 virtual void toSingleRange();
295 virtual void sortRanges();
296 virtual MultiLineScopedTextRange& scopedRange(int idx);
297 virtual MultiLineScopedTextRange& addRange(int anchor, int caret, const QString& name , TextGrammarRule *rule);
298
299 void removeAndInvalidateRangesAfterOffset( int offset );
300
301 // adds a text scope
303 void processChangesIfRequired( bool joinBorders );
304
305 QString toString();
306
308
309private:
310
311 TextDocumentScopes* textDocumentScopesRef_;
312 QList<MultiLineScopedTextRange*> scopedRangeList_;
313};
314
315
316
317//===========================================
318
319
321class EDBEE_EXPORT TextDocumentScopes : public QObject
322{
323Q_OBJECT
324
325public:
327 virtual ~TextDocumentScopes();
328
329 int lastScopedOffset();
330 void setLastScopedOffset( int offset );
331
332
333 // scope management
334 void setDefaultScope(const QString& name, TextGrammarRule *rule);
335
336 void giveLineScopedRangeList( int line, ScopedTextRangeList* list);
338 int scopedLineCount();
339
341 void removeScopesAfterOffset( int offset );
343
344 QVector<MultiLineScopedTextRange*> multiLineScopedRangesBetweenOffsets( int offsetBegin, int offsetEnd );
345 TextScopeList scopesAtOffset(int offset , bool includeEnd=false );
346 QVector<ScopedTextRange*> createScopedRangesAtOffsetList( int offset );
347
348 QString toString();
349 QStringList scopesAsStringList();
350
351 void dumpScopedLineAddresses( const QString& text = QString() );
352
353 // getters
355
356protected slots:
357
358 void grammarChanged();
359
360signals:
361 void lastScopedOffsetChanged(int previousOffset, int lastScopedOffset );
362
363private:
364
365 TextDocument* textDocumentRef_;
366
367// TextScope* defaultScope_; ///< The default scope
368 MultiLineScopedTextRange defaultScopedRange_;
369// QHash<QString,TextScope*> scopeMap_; ///< A list of all defined/used scopes (pointers to these scopes are smaller then full strings)
370 MultiLineScopedTextRangeSet scopedRanges_;
371 GapVector<ScopedTextRangeList*> lineRangeList_;
372
381 int lastScopedOffset_;
382
383};
384
385
386} // edbee
This class is used to define a gap vector. A Gapvector is split in 2 parts. where the gap is moved to...
Definition gapvector.h:19
This class 'defines' a single document scope.
Definition textdocumentscopes.h:254
void giveEndRegExp(RegExp *regExp)
Gives the end regular expression.
Definition textdocumentscopes.cpp:201
static bool lessThan(MultiLineScopedTextRange *r1, MultiLineScopedTextRange *r2)
This method compares selection ranges.
Definition textdocumentscopes.cpp:215
TextGrammarRule * grammarRule() const
Returns the active grammar rule.
Definition textdocumentscopes.cpp:194
MultiLineScopedTextRange(int anchor, int caret, TextScope *scope)
The multiline scoped textrange.
Definition textdocumentscopes.cpp:171
void setGrammarRule(TextGrammarRule *rule)
Sets the rule (we need the rule to perform end-of-line matching)
Definition textdocumentscopes.cpp:187
RegExp * endRegExp()
returns the end-regular expression
Definition textdocumentscopes.cpp:208
virtual MultiLineScopedTextRange * multiLineScopedTextRange()
returns the multi-line scoped text range
Definition textdocumentscopes.cpp:76
MultiLineScopedTextRangeReference(MultiLineScopedTextRange &range)
Creates a scoped text range based on a scope textrange.
Definition textdocumentscopes.cpp:62
This is a set of scoped textranges. This set is used to remember parsed language ranges.
Definition textdocumentscopes.h:279
virtual MultiLineScopedTextRange & scopedRange(int idx)
this method returns the scoped range
Definition textdocumentscopes.cpp:693
void processChangesIfRequired(bool joinBorders)
This method process the changes if required.
Definition textdocumentscopes.cpp:738
virtual TextRange & range(int idx)
returns the given textrange
Definition textdocumentscopes.cpp:633
virtual const TextRange & constRange(int idx) const
returns the cont range
Definition textdocumentscopes.cpp:641
virtual void removeRange(int idx)
' removes the given scope
Definition textdocumentscopes.cpp:663
void removeAndInvalidateRangesAfterOffset(int offset)
This method removes all ranges after a given offset. This means it will remove all complete ranges af...
Definition textdocumentscopes.cpp:713
virtual int rangeCount() const
Returns the number of ranges.
Definition textdocumentscopes.cpp:626
void reset()
Completely empties the scope list.
Definition textdocumentscopes.cpp:618
virtual void clear()
removes all scopes
Definition textdocumentscopes.cpp:671
QString toString()
convert the found ranges to strings
Definition textdocumentscopes.cpp:751
virtual void toSingleRange()
Definition textdocumentscopes.cpp:678
TextDocumentScopes * textDocumentScopes()
Returns a reference to the textdocument scopes.
Definition textdocumentscopes.cpp:226
MultiLineScopedTextRangeSet(TextDocument *textDocument, TextDocumentScopes *textDocumentScopes)
A multilinescoped textrange set.
Definition textdocumentscopes.cpp:604
virtual void sortRanges()
This method sorts all ranges.
Definition textdocumentscopes.cpp:686
void giveScopedTextRange(MultiLineScopedTextRange *textScope)
This method gives the scoped text range to this object.
Definition textdocumentscopes.cpp:731
virtual void addRange(int anchor, int caret)
This method adds a range with the default scope.
Definition textdocumentscopes.cpp:649
A class for matching QStrings with the Oniguruma API We need this Regular Expression library to be ab...
Definition regexp.h:35
A base scoped text range.
Definition textdocumentscopes.h:176
QString toString() const
Converts the scoped textrange to a string.
Definition textdocumentscopes.cpp:51
ScopedTextRange(int anchor, int caret, TextScope *scope)
A scoped text range.
Definition textdocumentscopes.cpp:21
virtual MultiLineScopedTextRange * multiLineScopedTextRange()
returns the multi-line scoped text range
Definition textdocumentscopes.h:187
void setScope(TextScope *scope)
sets the scope of this textrange
Definition textdocumentscopes.cpp:36
TextScope * scope() const
returns the scope
Definition textdocumentscopes.cpp:44
a list of textscopes This class is used for single-line scopes (Todo, this needs to be optimized)
Definition textdocumentscopes.h:220
ScopedTextRangeList()
A scoped textrange lsit.
Definition textdocumentscopes.cpp:86
void squeeze()
Squeezes the ranges (reduces the memory usage)
Definition textdocumentscopes.cpp:133
QString toString()
Converts the scoped textrange list to a strubg.
Definition textdocumentscopes.cpp:154
ScopedTextRange * at(int idx)
Returns the scoped textrange at the given list.
Definition textdocumentscopes.cpp:109
void giveAndPrependRange(ScopedTextRange *range)
Prepends a range.
Definition textdocumentscopes.cpp:126
int size() const
Retursn the number of scoped textranges in the list.
Definition textdocumentscopes.cpp:102
bool isIndependent() const
returns the independent flag
Definition textdocumentscopes.cpp:147
void setIndependent(bool enable=true)
set the independent flag. The independent flag means it's not dependent on other lines
Definition textdocumentscopes.cpp:140
void giveRange(ScopedTextRange *at)
give a rnage to the range set
Definition textdocumentscopes.cpp:118
This is the base and abstract class of a text document A TextDocument is the model part of the editor...
Definition textdocument.h:42
This class is used to 'contain' all document scope information.
Definition textdocumentscopes.h:322
QStringList scopesAsStringList()
Returns a string-list will all scopes. This list is mainly used for debugging and testing.
Definition textdocumentscopes.cpp:988
QVector< MultiLineScopedTextRange * > multiLineScopedRangesBetweenOffsets(int offsetBegin, int offsetEnd)
This method returns all scope-ranges at the given offset-ranges.
Definition textdocumentscopes.cpp:893
void giveLineScopedRangeList(int line, ScopedTextRangeList *list)
This method sets the scoped line list.
Definition textdocumentscopes.cpp:820
void grammarChanged()
the grammar has been changed
Definition textdocumentscopes.cpp:1033
TextScopeList scopesAtOffset(int offset, bool includeEnd=false)
returns all scopes between the given offsets
Definition textdocumentscopes.cpp:911
void giveMultiLineScopedTextRange(MultiLineScopedTextRange *range)
gives the multi-lined textrange to the scopedranges
Definition textdocumentscopes.cpp:850
void setDefaultScope(const QString &name, TextGrammarRule *rule)
This method sets the default scope.
Definition textdocumentscopes.cpp:810
ScopedTextRangeList * scopedRangesAtLine(int line)
This method returns all scoped ranges on the given line.
Definition textdocumentscopes.cpp:835
MultiLineScopedTextRange & defaultScopedRange()
This method retursn the default scoped textrange Currently this is done very dirty,...
Definition textdocumentscopes.cpp:885
TextDocument * textDocument()
returns the current textdocument scope
Definition textdocumentscopes.cpp:1026
void removeScopesAfterOffset(int offset)
This method invalidates all scopes after the given offset.
Definition textdocumentscopes.cpp:858
void lastScopedOffsetChanged(int previousOffset, int lastScopedOffset)
Definition moc_textdocumentscopes.cpp:180
void setLastScopedOffset(int offset)
Sets the last scoped offset.
Definition textdocumentscopes.cpp:797
int scopedLineCount()
Returns the number of scopes lines in the lineRangeList_.
Definition textdocumentscopes.cpp:843
QString toString()
Converts the textdocument scoped to a string.
Definition textdocumentscopes.cpp:981
int lastScopedOffset()
returns the last scoped offset
Definition textdocumentscopes.cpp:789
QVector< ScopedTextRange * > createScopedRangesAtOffsetList(int offset)
This method returns all scoped ranges at the given offset Hmmm this is almost exactly the same implem...
Definition textdocumentscopes.cpp:948
void dumpScopedLineAddresses(const QString &text=QString())
add all dumped line scopes
Definition textdocumentscopes.cpp:1015
TextDocumentScopes(TextDocument *textDocument)
The constructor for storing/managing the textdocument scopes.
Definition textdocumentscopes.cpp:768
defines a single grammar rule
Definition textgrammar.h:24
A single text region A region constists of an anchor and a caret: The anchor defines the 'start' of t...
Definition textrange.h:29
TextRange(int anchor=0, int caret=0)
Definition textrange.h:31
int anchor() const
Definition textrange.h:33
int caret() const
Definition textrange.h:34
TextRangeSetBase(TextDocument *doc)
Definition textrange.cpp:434
TextDocument * textDocument() const
Returns the associated textdocument.
Definition textrange.cpp:972
This class defines a full text-scope. A full textscope is textscope with one ore more scoped-elements...
Definition textdocumentscopes.h:36
friend class TextScopeManager
Definition textdocumentscopes.h:53
TextScopeAtomId atomAt(int idx) const
This method returns the atom at the given index.
Definition textdocumentscopes.cpp:294
int atomCount()
returns the number of atom items
Definition textdocumentscopes.cpp:287
const QString name()
This method returns the name of this scope.
Definition textdocumentscopes.cpp:273
int rindexOf(TextScope *scope)
This method returns the 'index' of the given full-scope. This is a kind of substring search.
Definition textdocumentscopes.cpp:324
bool startsWith(TextScope *scope)
Checks if the current scope starts with the given scope wild-card atoms will always match.
Definition textdocumentscopes.cpp:304
A list of text-scopes. on a certian location, usually more then one scope is available on a given loc...
Definition textdocumentscopes.h:61
TextScopeList()
A list of text scopes.
Definition textdocumentscopes.cpp:352
int atomCount() const
This method returns the total number of atoms.
Definition textdocumentscopes.cpp:375
QString toString()
Converts the scopelist to a string.
Definition textdocumentscopes.cpp:386
TextScope * refTextScope(const QString &scopeString)
This method finds or creates a full-scope.
Definition textdocumentscopes.cpp:561
TextScopeAtomId findOrRegisterScopeAtom(const QString &atom)
This method registers the scope element.
Definition textdocumentscopes.cpp:548
TextScopeAtomId wildcardId()
returns the wildcard reference id
Definition textdocumentscopes.cpp:541
const QString & atomName(TextScopeAtomId id)
Returns the name of the given atom id.
Definition textdocumentscopes.cpp:593
TextScopeList * createTextScopeList(const QString &scopeListString)
This method creates a text-scope list from the given scope string.
Definition textdocumentscopes.cpp:581
TextScopeManager()
The scopemanager constructor.
Definition textdocumentscopes.cpp:498
void reset()
This method clears and delets all scopes. WARNING this destroys all registered text-scopes This metho...
Definition textdocumentscopes.cpp:515
TextScope * refEmptyScope()
Returns the empty scope.
Definition textdocumentscopes.cpp:574
TextScopeSelector(const QString &selector)
The textscope selector.
Definition textdocumentscopes.cpp:401
QString toString()
Converts the scope to a string.
Definition textdocumentscopes.cpp:439
double calculateMatchScore(const TextScopeList *scopeList)
This method calculates the matching score of the scope with this selector Currently the scope-calcula...
Definition textdocumentscopes.cpp:428
#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
short TextScopeAtomId
This type defines a single scope atom.
Definition textdocumentscopes.h:27