File: qscilexer.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (740 lines) | stat: -rw-r--r-- 16,884 bytes parent folder | download | duplicates (2)
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// This module implements the QsciLexer class.
//
// Copyright (c) 2015 Riverbank Computing Limited <info@riverbankcomputing.com>
// 
// This file is part of QScintilla.
// 
// This file may be used under the terms of the GNU General Public License
// version 3.0 as published by the Free Software Foundation and appearing in
// the file LICENSE included in the packaging of this file.  Please review the
// following information to ensure the GNU General Public License version 3.0
// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
// 
// If you do not wish to use this file under the terms of the GPL version 3.0
// then you may purchase a commercial license.  For more information contact
// info@riverbankcomputing.com.
// 
// 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 "Qsci/qscilexer.h"

#include <qapplication.h>
#include <qcolor.h>
#include <qfont.h>
#include <qsettings.h>

#include "Qsci/qsciapis.h"
#include "Qsci/qsciscintilla.h"
#include "Qsci/qsciscintillabase.h"


// The ctor.
QsciLexer::QsciLexer(QObject *parent)
    : QObject(parent),
      autoIndStyle(-1), apiSet(0), attached_editor(0)
{
#if defined(Q_OS_WIN)
    defFont = QFont("Verdana",10);
#elif defined(Q_OS_MAC)
    defFont = QFont("Verdana", 12);
#else
    defFont = QFont("Bitstream Vera Sans",9);
#endif

    // Set the default fore and background colours.
    QPalette pal = QApplication::palette();
    defColor = pal.text().color();
    defPaper = pal.base().color();

    // Putting this on the heap means we can keep the style getters const.
    style_map = new StyleDataMap;
    style_map->style_data_set = false;
}


// The dtor.
QsciLexer::~QsciLexer()
{
    delete style_map;
}


// Set the attached editor.
void QsciLexer::setEditor(QsciScintilla *editor)
{
    attached_editor = editor;

    if (attached_editor)
    {
        attached_editor->SendScintilla(QsciScintillaBase::SCI_SETSTYLEBITS,
                styleBitsNeeded());
    }
}


// Return the lexer name.
const char *QsciLexer::lexer() const
{
    return 0;
}


// Return the lexer identifier.
int QsciLexer::lexerId() const
{
    return QsciScintillaBase::SCLEX_CONTAINER;
}


// Return the number of style bits needed by the lexer.
int QsciLexer::styleBitsNeeded() const
{
    if (!attached_editor)
        return 5;

    return attached_editor->SendScintilla(QsciScintillaBase::SCI_GETSTYLEBITSNEEDED);
}


// Make sure the style defaults have been set.
void QsciLexer::setStyleDefaults() const
{
    if (!style_map->style_data_set)
    {
        for (int i = 0; i < 128; ++i)
            if (!description(i).isEmpty())
                styleData(i);

        style_map->style_data_set = true;
    }
}


// Return a reference to a style's data, setting up the defaults if needed.
QsciLexer::StyleData &QsciLexer::styleData(int style) const
{
    StyleData &sd = style_map->style_data[style];

    // See if this is a new style by checking if the colour is valid.
    if (!sd.color.isValid())
    {
        sd.color = defaultColor(style);
        sd.paper = defaultPaper(style);
        sd.font = defaultFont(style);
        sd.eol_fill = defaultEolFill(style);
    }

    return sd;
}


// Set the APIs associated with the lexer.
void QsciLexer::setAPIs(QsciAbstractAPIs *apis)
{
    apiSet = apis;
}


// Return a pointer to the current APIs if there are any.
QsciAbstractAPIs *QsciLexer::apis() const
{
    return apiSet;
}


// Default implementation to return the set of fill up characters that can end
// auto-completion.
const char *QsciLexer::autoCompletionFillups() const
{
    return "(";
}


// Default implementation to return the view used for indentation guides.
int QsciLexer::indentationGuideView() const
{
    return QsciScintillaBase::SC_IV_LOOKBOTH;
}


// Default implementation to return the list of character sequences that can
// separate auto-completion words.
QStringList QsciLexer::autoCompletionWordSeparators() const
{
    return QStringList();
}


// Default implementation to return the list of keywords that can start a
// block.
const char *QsciLexer::blockStartKeyword(int *) const
{
    return 0;
}


// Default implementation to return the list of characters that can start a
// block.
const char *QsciLexer::blockStart(int *) const
{
    return 0;
}


// Default implementation to return the list of characters that can end a
// block.
const char *QsciLexer::blockEnd(int *) const
{
    return 0;
}


// Default implementation to return the style used for braces.
int QsciLexer::braceStyle() const
{
    return -1;
}


// Default implementation to return the number of lines to look back when
// auto-indenting.
int QsciLexer::blockLookback() const
{
    return 20;
}


// Default implementation to return the case sensitivity of the language.
bool QsciLexer::caseSensitive() const
{
    return true;
}


// Default implementation to return the characters that make up a word.
const char *QsciLexer::wordCharacters() const
{
    return 0;
}


// Default implementation to return the style used for whitespace.
int QsciLexer::defaultStyle() const
{
    return 0;
}


// Returns the foreground colour of the text for a style.
QColor QsciLexer::color(int style) const
{
    return styleData(style).color;
}


// Returns the background colour of the text for a style.
QColor QsciLexer::paper(int style) const
{
    return styleData(style).paper;
}


// Returns the font for a style.
QFont QsciLexer::font(int style) const
{
    return styleData(style).font;
}


// Returns the end-of-line fill for a style.
bool QsciLexer::eolFill(int style) const
{
    return styleData(style).eol_fill;
}


// Returns the set of keywords.
const char *QsciLexer::keywords(int) const
{
    return 0;
}


// Returns the default EOL fill for a style.
bool QsciLexer::defaultEolFill(int) const
{
    return false;
}


// Returns the default font for a style.
QFont QsciLexer::defaultFont(int) const
{
    return defaultFont();
}


// Returns the default font.
QFont QsciLexer::defaultFont() const
{
    return defFont;
}


// Sets the default font.
void QsciLexer::setDefaultFont(const QFont &f)
{
    defFont = f;
}


// Returns the default text colour for a style.
QColor QsciLexer::defaultColor(int) const
{
    return defaultColor();
}


// Returns the default text colour.
QColor QsciLexer::defaultColor() const
{
    return defColor;
}


// Sets the default text colour.
void QsciLexer::setDefaultColor(const QColor &c)
{
    defColor = c;
}


// Returns the default paper colour for a styles.
QColor QsciLexer::defaultPaper(int) const
{
    return defaultPaper();
}


// Returns the default paper colour.
QColor QsciLexer::defaultPaper() const
{
    return defPaper;
}


// Sets the default paper colour.
void QsciLexer::setDefaultPaper(const QColor &c)
{
    defPaper = c;

    // Normally the default values are only intended to provide defaults when a
    // lexer is first setup because once a style has been referenced then a
    // copy of the default is made.  However the default paper is a special
    // case because there is no other way to set the background colour used
    // where there is no text.  Therefore we also actively set it.
    setPaper(c, QsciScintillaBase::STYLE_DEFAULT);
}


// Read properties from the settings.
bool QsciLexer::readProperties(QSettings &,const QString &)
{
    return true;
}


// Refresh all properties.
void QsciLexer::refreshProperties()
{
}


// Write properties to the settings.
bool QsciLexer::writeProperties(QSettings &,const QString &) const
{
    return true;
}


// Restore the user settings.
bool QsciLexer::readSettings(QSettings &qs,const char *prefix)
{
    bool ok, flag, rc = true;
    int num;
    QString key, full_key;
    QStringList fdesc;

    setStyleDefaults();

    // Read the styles.
    for (int i = 0; i < 128; ++i)
    {
        // Ignore invalid styles.
        if (description(i).isEmpty())
            continue;

        key.sprintf("%s/%s/style%d/",prefix,language(),i);

        // Read the foreground colour.
        full_key = key + "color";

        ok = qs.contains(full_key);
        num = qs.value(full_key).toInt();

        if (ok)
            setColor(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
        else
            rc = false;

        // Read the end-of-line fill.
        full_key = key + "eolfill";

        ok = qs.contains(full_key);
        flag = qs.value(full_key, false).toBool();

        if (ok)
            setEolFill(flag, i);
        else
            rc = false;

        // Read the font.  First try the deprecated format that uses an integer
        // point size.
        full_key = key + "font";

        ok = qs.contains(full_key);
        fdesc = qs.value(full_key).toStringList();

        if (ok && fdesc.count() == 5)
        {
            QFont f;

            f.setFamily(fdesc[0]);
            f.setPointSize(fdesc[1].toInt());
            f.setBold(fdesc[2].toInt());
            f.setItalic(fdesc[3].toInt());
            f.setUnderline(fdesc[4].toInt());

            setFont(f, i);
        }
        else
            rc = false;

        // Now try the newer font format that uses a floating point point size.
        // It is not an error if it doesn't exist.
        full_key = key + "font2";

        ok = qs.contains(full_key);
        fdesc = qs.value(full_key).toStringList();

        if (ok)
        {
            // Allow for future versions with more fields.
            if (fdesc.count() >= 5)
            {
                QFont f;

                f.setFamily(fdesc[0]);
                f.setPointSizeF(fdesc[1].toDouble());
                f.setBold(fdesc[2].toInt());
                f.setItalic(fdesc[3].toInt());
                f.setUnderline(fdesc[4].toInt());

                setFont(f, i);
            }
            else
            {
                rc = false;
            }
        }

        // Read the background colour.
        full_key = key + "paper";

        ok = qs.contains(full_key);
        num = qs.value(full_key).toInt();

        if (ok)
            setPaper(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
        else
            rc = false;
    }

    // Read the properties.
    key.sprintf("%s/%s/properties/",prefix,language());

    if (!readProperties(qs,key))
        rc = false;

    refreshProperties();

    // Read the rest.
    key.sprintf("%s/%s/",prefix,language());

    // Read the default foreground colour.
    full_key = key + "defaultcolor";

    ok = qs.contains(full_key);
    num = qs.value(full_key).toInt();

    if (ok)
        setDefaultColor(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff));
    else
        rc = false;

    // Read the default background colour.
    full_key = key + "defaultpaper";

    ok = qs.contains(full_key);
    num = qs.value(full_key).toInt();

    if (ok)
        setDefaultPaper(QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff));
    else
        rc = false;

    // Read the default font.  First try the deprecated format that uses an
    // integer point size.
    full_key = key + "defaultfont";

    ok = qs.contains(full_key);
    fdesc = qs.value(full_key).toStringList();

    if (ok && fdesc.count() == 5)
    {
        QFont f;

        f.setFamily(fdesc[0]);
        f.setPointSize(fdesc[1].toInt());
        f.setBold(fdesc[2].toInt());
        f.setItalic(fdesc[3].toInt());
        f.setUnderline(fdesc[4].toInt());

        setDefaultFont(f);
    }
    else
        rc = false;

    // Now try the newer font format that uses a floating point point size.  It
    // is not an error if it doesn't exist.
    full_key = key + "defaultfont2";

    ok = qs.contains(full_key);
    fdesc = qs.value(full_key).toStringList();

    if (ok)
    {
        // Allow for future versions with more fields.
        if (fdesc.count() >= 5)
        {
            QFont f;

            f.setFamily(fdesc[0]);
            f.setPointSizeF(fdesc[1].toDouble());
            f.setBold(fdesc[2].toInt());
            f.setItalic(fdesc[3].toInt());
            f.setUnderline(fdesc[4].toInt());

            setDefaultFont(f);
        }
        else
        {
            rc = false;
        }
    }

    full_key = key + "autoindentstyle";

    ok = qs.contains(full_key);
    num = qs.value(full_key).toInt();

    if (ok)
        setAutoIndentStyle(num);
    else
        rc = false;

    return rc;
}


// Save the user settings.
bool QsciLexer::writeSettings(QSettings &qs,const char *prefix) const
{
    bool rc = true;
    QString key, fmt("%1");
    int num;
    QStringList fdesc;

    setStyleDefaults();

    // Write the styles.
    for (int i = 0; i < 128; ++i)
    {
        // Ignore invalid styles.
        if (description(i).isEmpty())
            continue;

        QColor c;

        key.sprintf("%s/%s/style%d/",prefix,language(),i);

        // Write the foreground colour.
        c = color(i);
        num = (c.red() << 16) | (c.green() << 8) | c.blue();

        qs.setValue(key + "color", num);

        // Write the end-of-line fill.
        qs.setValue(key + "eolfill", eolFill(i));

        // Write the font using the deprecated format.
        QFont f = font(i);

        fdesc.clear();
        fdesc += f.family();
        fdesc += fmt.arg(f.pointSize());

        // The casts are for Borland.
        fdesc += fmt.arg((int)f.bold());
        fdesc += fmt.arg((int)f.italic());
        fdesc += fmt.arg((int)f.underline());

        qs.setValue(key + "font", fdesc);

        // Write the font using the newer format.
        fdesc[1] = fmt.arg(f.pointSizeF());

        qs.setValue(key + "font2", fdesc);

        // Write the background colour.
        c = paper(i);
        num = (c.red() << 16) | (c.green() << 8) | c.blue();

        qs.setValue(key + "paper", num);
    }

    // Write the properties.
    key.sprintf("%s/%s/properties/",prefix,language());

    if (!writeProperties(qs,key))
        rc = false;

    // Write the rest.
    key.sprintf("%s/%s/",prefix,language());

    // Write the default foreground colour.
    num = (defColor.red() << 16) | (defColor.green() << 8) | defColor.blue();

    qs.setValue(key + "defaultcolor", num);

    // Write the default background colour.
    num = (defPaper.red() << 16) | (defPaper.green() << 8) | defPaper.blue();

    qs.setValue(key + "defaultpaper", num);

    // Write the default font using the deprecated format.
    fdesc.clear();
    fdesc += defFont.family();
    fdesc += fmt.arg(defFont.pointSize());

    // The casts are for Borland.
    fdesc += fmt.arg((int)defFont.bold());
    fdesc += fmt.arg((int)defFont.italic());
    fdesc += fmt.arg((int)defFont.underline());

    qs.setValue(key + "defaultfont", fdesc);

    // Write the font using the newer format.
    fdesc[1] = fmt.arg(defFont.pointSizeF());

    qs.setValue(key + "defaultfont2", fdesc);

    qs.setValue(key + "autoindentstyle", autoIndStyle);

    return rc;
}


// Return the auto-indentation style.
int QsciLexer::autoIndentStyle()
{
    // We can't do this in the ctor because we want the virtuals to work.
    if (autoIndStyle < 0)
        autoIndStyle = (blockStartKeyword() || blockStart() || blockEnd()) ?
                    0 : QsciScintilla::AiMaintain;

    return autoIndStyle;
}


// Set the auto-indentation style.
void QsciLexer::setAutoIndentStyle(int autoindentstyle)
{
    autoIndStyle = autoindentstyle;
}


// Set the foreground colour for a style.
void QsciLexer::setColor(const QColor &c, int style)
{
    if (style >= 0)
    {
        styleData(style).color = c;
        emit colorChanged(c, style);
    }
    else
        for (int i = 0; i < 128; ++i)
            if (!description(i).isEmpty())
                setColor(c, i);
}


// Set the end-of-line fill for a style.
void QsciLexer::setEolFill(bool eolfill, int style)
{
    if (style >= 0)
    {
        styleData(style).eol_fill = eolfill;
        emit eolFillChanged(eolfill, style);
    }
    else
        for (int i = 0; i < 128; ++i)
            if (!description(i).isEmpty())
                setEolFill(eolfill, i);
}


// Set the font for a style.
void QsciLexer::setFont(const QFont &f, int style)
{
    if (style >= 0)
    {
        styleData(style).font = f;
        emit fontChanged(f, style);
    }
    else
        for (int i = 0; i < 128; ++i)
            if (!description(i).isEmpty())
                setFont(f, i);
}


// Set the background colour for a style.
void QsciLexer::setPaper(const QColor &c, int style)
{
    if (style >= 0)
    {
        styleData(style).paper = c;
        emit paperChanged(c, style);
    }
    else
    {
        for (int i = 0; i < 128; ++i)
            if (!description(i).isEmpty())
                setPaper(c, i);

        emit paperChanged(c, QsciScintillaBase::STYLE_DEFAULT);
    }
}