File: qdocumentline.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (592 lines) | stat: -rw-r--r-- 14,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
** 
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qdocumentline.h"

/*!
	\file qdocumentline.cpp
	\brief Implementation of the QDocumentLine class.
*/

#include "qdocument_p.h"

/*!
	\ingroup document
	@{
*/

/*!
       \class QDocumentLine

       \brief A reference to line objects

       In QCodeEdit, documents are stored as a list of lines. A QDocumentLine holds
       a pointer to the data of one line and gives access to its content.

       It is not meant to be used to iterate over the document, though it is possible
       for conveneience and compatibility reasons. Indeed, QDocumentLine does not now
	   where in the document it is located. It can obtain that information but this is
	   a O(n) operation. Navigation within the document is one of the task devoted to
       QDocumentCursor which can move around in O(1) (or amortized O(1) in some rare
	   cases).

       Lines can be given formatting in various way : "regular" formatting used for
       highlighting, overlays used mainly to display search matches and similar informations
       and marks.
*/

QDocumentLine::QDocumentLine(QDocument *doc)
 : m_handle(doc ? doc->impl()->at(0) : 0)
{
	//m_lines_backing_store << this;
	
	if ( m_handle )
		m_handle->ref();
	
}

QDocumentLine::QDocumentLine(const QDocumentLine& line)
 : m_handle(line.m_handle)
{
	//m_lines_backing_store << this;
	if ( m_handle )
		m_handle->ref();
	
}

QDocumentLine::QDocumentLine(QDocumentLineHandle* handle)
 : m_handle(handle)
{
	//m_lines_backing_store << this;
	if ( m_handle )
		m_handle->ref();
	
}

QDocumentLine::~QDocumentLine()
{
	if ( m_handle )
		m_handle->deref();
	
	//m_lines_backing_store.removeAll(this);
}

/*!
	\brief Comparison operator
*/
bool QDocumentLine::operator == (const QDocumentLine& l) const
{
	return m_handle == l.m_handle;
	//return lineNumber() == l.lineNumber();
}

/*!
	\brief Comparison operator
*/
bool QDocumentLine::operator != (const QDocumentLine& l) const
{
	return m_handle != l.m_handle;
	//return lineNumber() != l.lineNumber();
}

/*!
	\brief copy operator
	
	QDocumentLine objects are just wrappers around the "real" line data.
	Copies of a QDocumentLine all points to the same underlying data and
	modifying one affect them all (no implicit sharing).
*/
QDocumentLine& QDocumentLine::operator = (const QDocumentLine& l)
{
	if ( m_handle )
		m_handle->deref();
	
	m_handle = l.m_handle;
	
	if ( m_handle )
		m_handle->ref();
	
	return *this;
}

/*!
	\return the document to which that line belongs
*/
QDocument* QDocumentLine::document() const
{
	return m_handle ? m_handle->document() : 0;
}

/*!
	\brief Check whether a given flag is set to the line
*/
bool QDocumentLine::hasFlag(State s) const
{
	return m_handle ? m_handle->hasFlag(s) : false;
}

/*!
	\brief Check whether any of the flags in a given combination is set to the line
*/
bool QDocumentLine::hasAnyFlag(int s) const
{
	return m_handle ? m_handle->hasFlag(s) : false;
}

/*!
	\brief Set a flag of the line
	
	\warning Do not mess with flags unless you know what you are doing
*/
void QDocumentLine::setFlag(State s, bool y)
{
	if ( m_handle )
		m_handle->setFlag(s, y);
	
}

/*!
	\return whether the line object is null (i.e invalid)
*/
bool QDocumentLine::isNull() const
{
	return m_handle ? !m_handle->document() : true;
}

/*!
	\return whether the line object is valid
*/
bool QDocumentLine::isValid() const
{
    return m_handle ? m_handle->document()!=NULL : false;
}

/*!
	\return the text of the line
*/
QString QDocumentLine::text() const
{
	return m_handle ? m_handle->text() : QString();
}

/*!
	\return The length of the line, in characters
*/
int QDocumentLine::length() const
{
	return m_handle ? m_handle->text().length() : 0;
}

/*!
	\return the number of visual lines occupied by the line
	
	This is NOT set to zero when the line is hidden (e.g due to folding).
*/
int QDocumentLine::lineSpan() const
{
	return m_handle && m_handle->document() ? m_handle->m_frontiers.count() + 1 : 0;
}

/*!
	\brief Returns the position of the first non-whitespace character
	\return position of first non-whitespace char or -1 if there is none
*/
int QDocumentLine::firstChar() const
{
	return nextNonSpaceChar(0);
}

/*!
	\brief Returns the position of the last non-whitespace character
	\return position of last non-whitespace char or -1 if there is none
*/
int QDocumentLine::lastChar() const
{
	return previousNonSpaceChar(length() - 1);
}

/*!
	\return true if the line starts with txt, neglecting spaces
*/
bool QDocumentLine::startsWith(const QString &txt)
{
	int pos = firstChar();
	return pos >= 0 && pos == text().indexOf(txt);
}

int QDocumentLine::indent() const
{
	return m_handle ? m_handle->indent() : 0;
}

/*!
	Find the position of the next char that is not a space.
	\param pos Column of the character which is examined first.
	\return True if the specified or a following character is not a space
	Otherwise false.
*/
int QDocumentLine::nextNonSpaceChar(int pos) const
{
	return m_handle ? m_handle->nextNonSpaceChar(pos) : -1;
}

/*!
	\brief Find the position of the previous char that is not a space.
	\param pos Column of the character which is examined first.
	\return The position of the first non-whitespace character preceding pos,
   or -1 if none is found.
*/
int QDocumentLine::previousNonSpaceChar(int pos) const
{
	return m_handle ? m_handle->previousNonSpaceChar(pos) : -1;
}

/*!
	\brief Converts a cursor position (column) to a document position (unconstrained viewport)
	
	\deprecated Use cursorToDocOffset() instead
	
	This function is kept for compatribility only. It dates back to the time before line wrapping
	was implemented. Due to the limitation of its design (i.e signature) it works in a somewhat
	awkard way : the x position returned is that the cursor would have in an unconstrained viewport,
	even when the line is wrapped so it does not map to an actual viewport coordinate, unless of
	course no wrapping is used.
*/
int QDocumentLine::cursorToX(int cpos) const
{
	return m_handle ? m_handle->cursorToX(cpos) : -1;
}

/*!
	\brief Converts a document position (unconstrained viewport) to a cursor position (column)
	
	\deprecated Use cursorToDocOffset() instead
	
	\see cursorToX() for more informations about this function
*/
int QDocumentLine::xToCursor(int xpos) const
{
	return m_handle ? m_handle->xToCursor(xpos) : -1;
}

/*!
	\return The wrapped line (i.e "subline") to which a given cursor position resides
	\param cpos cursor position, as a text column
*/
int QDocumentLine::wrappedLineForCursor(int cpos) const
{
	return m_handle ? m_handle->wrappedLineForCursor(cpos) : -1;
}

/*!
	\brief Converts a document offset (viewport) to a cursor position (character / text column)
	
	The (x, y) coordinates given by this function are relative to the absolute
	position of the line, which can be obtained from the document.
*/
int QDocumentLine::documentOffsetToCursor(int x, int y) const
{
	return m_handle ? m_handle->documentOffsetToCursor(x, y) : -1;
}

/*!
	\brief Converts a cursor position (character / text column) to a document offset (viewport)
	
	The (x, y) coordinates given by this function are relative to the absolute
	position of the line, which can be obtained from the document.
*/
void QDocumentLine::cursorToDocumentOffset(int cpos, int& x, int& y) const
{
	if ( m_handle )
		m_handle->cursorToDocumentOffset(cpos, x, y);
}

/*!
	\overload
*/
QPoint QDocumentLine::cursorToDocumentOffset(int cpos) const
{
	return m_handle ? m_handle->cursorToDocumentOffset(cpos) : QPoint();
}

/*!
	\brief Toggle a mark on the line
*/
void QDocumentLine::addMark(int id)
{
	if ( !document() )
		return;
	
	document()->impl()->addMark(m_handle, id);
	setFlag(QDocumentLine::LayoutDirty,true);
}

/*!
	\brief Toggle a mark on the line
*/
void QDocumentLine::toggleMark(int id)
{
	if ( !document() )
		return;
	
	document()->impl()->toggleMark(m_handle, id);
	setFlag(QDocumentLine::LayoutDirty,true);
}

/*!
	\brief Remove a mark from the line
*/
void QDocumentLine::removeMark(int id)
{
	if ( !document() )
		return;
	
	document()->impl()->removeMark(m_handle, id);
	setFlag(QDocumentLine::LayoutDirty,true);
}

/*!
	\return the list of marks set on this line
*/
QList<int> QDocumentLine::marks() const
{
	return document() ? document()->impl()->marks(m_handle) : QList<int>();
}

/*!
	\return Whether a given mark has been set on the line
*/
bool QDocumentLine::hasMark(int id) const
{
	return marks().contains(id);
}

/*!
	\brief Set the formatting of the line
	
	\note this method is made available for syntax engine use.
	If you want to apply extra formatting on a line use overlays
	instead
	
	\see addOverlay()
*/
void QDocumentLine::setFormats(const QVector<int>& formats)
{
	if ( !m_handle )
		return;
	
	m_handle->setFormats(formats);
}

QVector<int> QDocumentLine::compose(){
    if(!m_handle) return QVector<int> ();
    m_handle->lockForRead();
    QVector<int> result=m_handle->compose();
    m_handle->unlock();
    return result;
}

QVector<int> QDocumentLine::getFormats(){
    if(!m_handle) return QVector<int> ();
    return m_handle->getFormats();
}

int QDocumentLine::getFormatAt(int pos){
	if( !m_handle ) return -1;
	if (pos < 0 ) return -1;
	const QVector<int>& formats = m_handle->getFormats();
	if( pos >= formats.size() ) return -1;
	return formats.at(pos);
}

QVariant QDocumentLine::getCookie(int type){
    if(!m_handle) return QVariant();
    m_handle->lockForRead();
    QVariant result=m_handle->getCookie(type);
    m_handle->unlock();
    return result;
}

void QDocumentLine::setCookie(int type,QVariant data){
	if(!m_handle) return;
    m_handle->lockForWrite();
	m_handle->setCookie(type,data);
    m_handle->unlock();
}

void QDocumentLine::removeCookie(int type){
	if (!m_handle) return;
    m_handle->lockForWrite();
	m_handle->removeCookie(type);
    m_handle->unlock();
}

bool QDocumentLine::isRTLByLayout() const{
    if (!m_handle) return false;
     return m_handle->isRTLByLayout();
}

bool QDocumentLine::isRTLByText() const{
    if (!m_handle) return false;
     return m_handle->isRTLByText();
}

QTextLayout* QDocumentLine::getLayout() const{
	return m_handle ? m_handle->m_layout : 0;
}

/*!
	\return whether the line has at least one overlay of a given format id
*/
bool QDocumentLine::hasOverlay(int fid) const
{
	if ( !m_handle )
		return false;
	
	foreach ( const QFormatRange& r, m_handle->m_overlays )
		if ( r.format == fid )
			return true;
	
	return false;
}

/*!
	\brief Clear all overlays applied to the line
*/
QList<QFormatRange> QDocumentLine::overlays() const
{
	if ( !m_handle )
		return QList<QFormatRange>();
	
	return m_handle->m_overlays;
}

/*!
	\brief Clear all overlays applied to the line
*/
void QDocumentLine::clearOverlays()
{
	if ( !m_handle )
		return;
	
	m_handle->clearOverlays();
}

void QDocumentLine::clearOverlays(int formatToClear){
	if ( !m_handle )
		return;

    m_handle->clearOverlays(formatToClear);
}

void QDocumentLine::clearOverlays(QList<int> formatsToClear){
    if ( !m_handle )
        return;

    m_handle->clearOverlays(formatsToClear);
}

/*!
	\brief Add an overlay to the line
	
	Overlays are format range that get applied on top of regular formatting.
	
	They are typically used to display search matches, matching braces, ...
*/
void QDocumentLine::addOverlay(const QFormatRange& over)
{
	if ( !m_handle )
		return;
	
	m_handle->addOverlay(over);
}

/*!
	\brief Remove an overlay from the line
*/
void QDocumentLine::removeOverlay(const QFormatRange& over)
{
	if ( !m_handle )
		return;
	
	m_handle->removeOverlay(over);
}

bool QDocumentLine::hasOverlay(int id){
    if ( !m_handle ) return false;
    return m_handle->hasOverlay(id);
}
QList<QFormatRange> QDocumentLine::getOverlays(int preferredFormat){
	if ( !m_handle )
		return QList<QFormatRange>();
	
	return m_handle->getOverlays(preferredFormat);
}
QFormatRange QDocumentLine::getOverlayAt(int index, int preferredFormat){
	if ( !m_handle )
		return QFormatRange(0,0,0);
	
	return m_handle->getOverlayAt(index,preferredFormat);
}

QFormatRange QDocumentLine::getFirstOverlayBetween(int start, int end, int preferredFormat){
	if ( !m_handle )
		return QFormatRange(0,0,0);
	
	return m_handle->getFirstOverlayBetween(start,end,preferredFormat);
}
QFormatRange QDocumentLine::getLastOverlayBetween(int start, int end, int preferredFormat){
	if ( !m_handle )
		return QFormatRange(0,0,0);
	
	return m_handle->getLastOverlayBetween(start,end,preferredFormat);
}


/*!
	\return the list of parentheses present on the line
	
	\note This is language dependent.
*/
const QVector<QParenthesis>& QDocumentLine::parentheses() const
{
	Q_CHECK_PTR(m_handle);
	
	return m_handle->m_parens;
}

/*!
	\brief Set the parentheses present on that line
	
	\note this should only be used by syntax engines
*/
void QDocumentLine::setParentheses(const QVector<QParenthesis>& parentheses)
{
	if ( !m_handle )
		return;
	
	m_handle->m_parens = parentheses;
}

/*!
	Reserved to syntax engines. do not mess with this unless you know what you are doing.
*/
QNFAMatchContext* QDocumentLine::matchContext()
{
	return m_handle ? &m_handle->m_context : 0;
}

/*! @} */