edbee - Qt Editor Library
textdocumentscopes.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <QHash>
9 #include <QObject>
10 #include <QStringList>
11 #include <QVector>
12 
13 #include "edbee/models/textrange.h"
15 #include "edbee/util/gapvector.h"
16 
17 namespace edbee {
18 
19 class MultiLineScopedTextRange;
20 class RegExp;
21 class ScopedTextRange;
22 class TextDocumentScopes;
23 class TextGrammarRule;
24 class TextScope;
25 
27 typedef short TextScopeAtomId;
28 
29 /*
30  ScopeElement
31  FullScope = ScopeElement.ScopeElement.ScopeElement
32 */
33 
36 class TextScope
37 {
38 public:
39  const QString name();
40  int atomCount();
41  TextScopeAtomId atomAt(int idx) const;
42 
43  bool startsWith( TextScope* scope );
44  int rindexOf( TextScope* scope );
45 
46 private:
47  TextScope( const QString& fullScope );
48  TextScope();
49  ~TextScope();
50 
51  char scopeAtomCount_;
52  TextScopeAtomId* scopeAtoms_;
53 
54  friend class TextScopeManager;
55 };
56 
57 //===========================================
58 
61 class TextScopeList : public QVector<TextScope*>
62 {
63 public:
64  TextScopeList();
65  TextScopeList( int initialSize );
66  TextScopeList( QVector<ScopedTextRange*>& ranges );
67 
68  int atomCount() const;
69 
70  QString toString();
71 };
72 
73 
74 //===========================================
75 
114 {
115 public:
116  TextScopeSelector( const QString& selector );
117  virtual ~TextScopeSelector();
118 
119  double calculateMatchScore(const TextScopeList* scopeList );
120  QString toString();
121 
122 private:
123  double calculateMatchScoreForSelector( TextScopeList* selector, const TextScopeList* scopeList );
124 
125 
126 private:
127  QVector<TextScopeList*> selectorList_;
128 };
129 
130 
131 //===========================================
132 
142 {
143 public:
145  virtual ~TextScopeManager();
146 
147 public:
148 
149  void reset();
150 
151  TextScopeAtomId wildcardId();
152 
153  TextScopeAtomId findOrRegisterScopeAtom( const QString& atom );
154  TextScope* refTextScope( const QString& scopeString );
155  TextScope* refEmptyScope();
156 
157  TextScopeList* createTextScopeList(const QString &scopeListString );
158 
159  const QString& atomName( TextScopeAtomId id );
160 
161 private:
162  TextScopeAtomId wildCardId_;
163 
164  // scope atoms
165  QList<QString> atomNameList_;
166  QHash<QString,TextScopeAtomId> atomNameMap_;
167 
168  // full scopes
169  QList<TextScope*> textScopeList_;
170  QHash<QString,TextScope*> textScopeRefMap_;
171 };
172 
173 
174 //===========================================
175 
176 
179 {
180 public:
181  ScopedTextRange( int anchor, int caret, TextScope* scope );
182 // ScopedTextRange( const MultiLineScopedTextRange& range );
183  virtual ~ScopedTextRange();
184 
185  void setScope( TextScope* scope );
186  TextScope* scope() const;
187  QString toString() const;
188 
191 
192 
193 private:
194  TextScope* scopeRef_;
195 
196 };
197 
198 
199 //===========================================
200 
201 
204 {
205 public:
208 
210  virtual MultiLineScopedTextRange* multiLineScopedTextRange();
211 
212 private:
213  MultiLineScopedTextRange* multiScopeRef_;
214 };
215 
216 
217 
218 //===========================================
219 
224 {
225  Q_DISABLE_COPY(ScopedTextRangeList)
226 public:
227 
228  explicit ScopedTextRangeList();
229  virtual ~ScopedTextRangeList();
230 
231  int size() const;
232  ScopedTextRange* at( int idx );
233  void giveRange( ScopedTextRange* at );
234  void giveAndPrependRange(ScopedTextRange* range );
235 
236  void squeeze();
237  void setIndependent(bool enable=true);
238  bool isIndependent() const;
239 
240  QString toString();
241 
242 
243 private:
244 
245  QVector<ScopedTextRange*> ranges_;
246  bool independent_;
247 
248 // int size_; /// The number of ranges
249 // ScopedTextRange* ranges_; /// The list of ranges
250 };
251 
252 
253 //===========================================
254 
255 
258 {
259 public:
260  MultiLineScopedTextRange(int anchor, int caret, TextScope* scope);
261  virtual ~MultiLineScopedTextRange();
262 
263  void setGrammarRule( TextGrammarRule* rule );
264  TextGrammarRule* grammarRule() const;
265 
266  void giveEndRegExp( RegExp* regExp );
267  RegExp* endRegExp();
268 
269  static bool lessThan( MultiLineScopedTextRange* r1, MultiLineScopedTextRange* r2);
270 
271 private:
272  TextGrammarRule* ruleRef_;
273  RegExp* endRegExp_;
274 };
275 
276 
277 //===========================================
278 
279 
283 {
284 public:
285  MultiLineScopedTextRangeSet( TextDocument* textDocument, TextDocumentScopes* textDocumentScopes );
286  virtual ~MultiLineScopedTextRangeSet();
287  void reset();
288 
289  // text range functionality
290  virtual int rangeCount() const;
291  virtual TextRange& range(int idx);
292  virtual const TextRange& constRange(int idx) const;
293  virtual void addRange( int anchor, int caret );
294  virtual void addRange(const TextRange& range);
295 
296  virtual void removeRange( int idx );
297  virtual void clear();
298  virtual void toSingleRange();
299  virtual void sortRanges();
300  virtual MultiLineScopedTextRange& scopedRange(int idx);
301  virtual MultiLineScopedTextRange& addRange(int anchor, int caret, const QString& name , TextGrammarRule *rule);
302 
303  void removeAndInvalidateRangesAfterOffset( int offset );
304 
305  // adds a text scope
306  void giveScopedTextRange( MultiLineScopedTextRange* textScope );
307  void processChangesIfRequired( bool joinBorders );
308 
309  QString toString();
310 
311  TextDocumentScopes* textDocumentScopes();
312 
313 private:
314 
315  TextDocumentScopes* textDocumentScopesRef_;
316  QList<MultiLineScopedTextRange*> scopedRangeList_;
317 };
318 
319 
320 
321 //===========================================
322 
323 
325 class TextDocumentScopes : public QObject
326 {
327 Q_OBJECT
328 
329 public:
330  TextDocumentScopes( TextDocument* textDocument);
331  virtual ~TextDocumentScopes();
332 
333  int lastScopedOffset();
334  void setLastScopedOffset( int offset );
335 
336 
337  // scope management
338  void setDefaultScope(const QString& name, TextGrammarRule *rule);
339 
340  void giveLineScopedRangeList( int line, ScopedTextRangeList* list);
341  ScopedTextRangeList* scopedRangesAtLine( int line );
342  int scopedLineCount();
343 
344  void giveMultiLineScopedTextRange( MultiLineScopedTextRange* range );
345  void removeScopesAfterOffset( int offset );
346  MultiLineScopedTextRange& defaultScopedRange();
347 
348  QVector<MultiLineScopedTextRange*> multiLineScopedRangesBetweenOffsets( int offsetBegin, int offsetEnd );
349  TextScopeList scopesAtOffset( int offset );
350  QVector<ScopedTextRange*> createScopedRangesAtOffsetList( int offset );
351 
352  QString toString();
353  QStringList scopesAsStringList();
354 
355  void dumpScopedLineAddresses( const QString& text = QString() );
356 
357  // getters
358  TextDocument* textDocument();
359 
360 protected slots:
361 
362  void grammarChanged();
363 
364 signals:
365  void lastScopedOffsetChanged(int previousOffset, int lastScopedOffset );
366 
367 private:
368 
369  TextDocument* textDocumentRef_;
370 
371 // TextScope* defaultScope_; ///< The default scope
372  MultiLineScopedTextRange defaultScopedRange_;
373 // QHash<QString,TextScope*> scopeMap_; ///< A list of all defined/used scopes (pointers to these scopes are smaller then full strings)
374  MultiLineScopedTextRangeSet scopedRanges_;
375  GapVector<ScopedTextRangeList*> lineRangeList_;
376 
385  int lastScopedOffset_;
386 
387 };
388 
389 
390 } // edbee
This is the base and abstract class of a text document A TextDocument is the model part of the editor...
Definition: textdocument.h:40
This abstract class represents a set of textranges The ranges are kept ordered and will not contain o...
Definition: textrange.h:105
The scope manager is used to manage the scopes... A scope consist out of several scope-parts: ...
Definition: textdocumentscopes.h:141
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
int atomCount()
returns the number of atom items
Definition: textdocumentscopes.cpp:289
short TextScopeAtomId
This type defines a single scope atom.
Definition: textdocumentscopes.h:24
This class &#39;defines&#39; a single document scope.
Definition: textdocumentscopes.h:257
defines a single grammar rule
Definition: textgrammar.h:24
Our first version of scope-selector only support a list of scopes Thus the Descendant (space) selecto...
Definition: textdocumentscopes.h:113
This class defines a full text-scope. A full textscope is textscope with one ore more scoped-elements...
Definition: textdocumentscopes.h:36
virtual MultiLineScopedTextRange * multiLineScopedTextRange()
returns the multi-line scoped text range
Definition: textdocumentscopes.h:190
This is a set of scoped textranges. This set is used to remember parsed language ranges.
Definition: textdocumentscopes.h:282
friend class TextScopeManager
Definition: textdocumentscopes.h:54
A list of text-scopes. on a certian location, usually more then one scope is available on a given loc...
Definition: textdocumentscopes.h:61
Copyright 2011-2013 - Reliable Bits Software by Blommers IT.
Definition: commentcommand.cpp:22
A class for matching QStrings with the Oniguruma API We need this Regular Expression library to be ab...
Definition: regexp.h:36
a list of textscopes This class is used for single-line scopes (Todo, this needs to be optimized) ...
Definition: textdocumentscopes.h:223
TextScopeAtomId atomAt(int idx) const
This method returns the atom at the given index.
Definition: textdocumentscopes.cpp:296
A base scoped text range.
Definition: textdocumentscopes.h:178
bool startsWith(TextScope *scope)
Checks if the current scope starts with the given scope wild-card atoms will always match...
Definition: textdocumentscopes.cpp:306
This class is used to &#39;contain&#39; all document scope information.
Definition: textdocumentscopes.h:325
A single text region A region constists of an anchor and a caret: The anchor defines the &#39;start&#39; of t...
Definition: textrange.h:29
const QString name()
This method returns the name of this scope.
Definition: textdocumentscopes.cpp:275
A line based ScopedText range, that referenes a multi-line text-reference.
Definition: textdocumentscopes.h:203
int rindexOf(TextScope *scope)
This method returns the &#39;index&#39; of the given full-scope. This is a kind of substring search...
Definition: textdocumentscopes.cpp:326