src/ldrawsyntaxhighlighter.cpp

Sun, 09 Apr 2023 16:30:33 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 16:30:33 +0300
changeset 367
df61cd3d5ab4
parent 343
4a82990affd5
permissions
-rw-r--r--

Also connect up the "Delete" action

331
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
1 #include "ldrawsyntaxhighlighter.h"
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
2
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
3 #define NUMBER_REGEX R"([+-]?(?:(?:\d+\.?\d*)|(?:\.\d+)))"
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
4 #define SPACE_REGEX R"(\s+)"
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
5 #define VEC3_REGEX "(" NUMBER_REGEX SPACE_REGEX NUMBER_REGEX SPACE_REGEX NUMBER_REGEX ")"
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
6 #define TWO_VECTORS VEC3_REGEX SPACE_REGEX VEC3_REGEX
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
7 #define THREE_VECTORS TWO_VECTORS SPACE_REGEX VEC3_REGEX
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
8 #define FOUR_VECTORS THREE_VECTORS SPACE_REGEX VEC3_REGEX
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
9
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
10 LDrawSyntaxHighlighter::LDrawSyntaxHighlighter(QTextDocument* parent) :
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
11 QSyntaxHighlighter{parent}
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
12 {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
13 this->lineTypeFormat.setFontWeight(QFont::Bold);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
14 this->point1Format.setForeground(Qt::green);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
15 this->point2Format.setForeground(Qt::yellow);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
16 this->point3Format.setForeground(Qt::cyan);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
17 this->point4Format.setForeground(Qt::magenta);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
18 this->point2Format.setFontWeight(QFont::Bold);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
19 this->point4Format.setFontWeight(QFont::Bold);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
20 this->nameFormat.setFontItalic(true);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
21 this->bfcFormat.setForeground(Qt::green);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
22 this->bfcFormat.setFontWeight(QFont::Bold);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
23 this->errorFormat.setUnderlineColor(Qt::yellow);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
24 this->errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
25 this->refPattern = QRegularExpression{R"(^\s*(1)\s+(\d+)\s+)" FOUR_VECTORS SPACE_REGEX R"(([^ ]+)\s*$)"};
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
26 this->trianglePattern = QRegularExpression{R"(^\s*(3)\s+(\d+)\s+)" THREE_VECTORS R"(\s*$)"};
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
27 this->quadrilateralPattern = QRegularExpression{R"(^\s*(4)\s+(\d+)\s+)" FOUR_VECTORS R"(\s*$)"};
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
28 this->cedgePattern = QRegularExpression{R"(^\s*(5)\s+(\d+)\s+)" FOUR_VECTORS R"(\s*$)"};
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
29 this->bfcPattern = QRegularExpression{QStringLiteral(
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
30 R"(^\s*(0) (BFC (?:CERTIFY CCW|CERTIFY CW|NOCERTIFY|INVERTNEXT|CLIP|NOCLIP))\s*$)"
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
31 )};
342
9fd7dcab6d76 Fix line type 0 format regular expression
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 331
diff changeset
32 this->commentPattern = QRegularExpression{R"(^\s*(0)(\s+.*)?$)"};
331
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
33 }
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
34
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
35 void LDrawSyntaxHighlighter::highlightBlock(const QString& text)
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
36 {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
37 bool matched = false;
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
38 const auto matchRegex = [&text, this, &matched](
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
39 const QRegularExpression& re,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
40 const QVector<const QTextCharFormat*> formats
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
41 ){
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
42 if (not matched) {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
43 QRegularExpressionMatch match = re.match(text);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
44 if (match.hasMatch()) {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
45 for (int i = 0; i < formats.size(); ++i) {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
46 this->setFormat(match.capturedStart(i + 1), match.capturedLength(i + 1), *formats[i]);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
47 }
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
48 }
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
49 matched = match.hasMatch();
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
50 }
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
51 };
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
52 matchRegex(this->refPattern, {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
53 &this->lineTypeFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
54 &this->colorFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
55 &this->point1Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
56 &this->point2Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
57 &this->point3Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
58 &this->point4Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
59 &this->nameFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
60 });
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
61 matchRegex(this->trianglePattern, {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
62 &this->lineTypeFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
63 &this->colorFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
64 &this->point1Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
65 &this->point2Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
66 &this->point3Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
67 });
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
68 matchRegex(this->quadrilateralPattern, {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
69 &this->lineTypeFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
70 &this->colorFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
71 &this->point1Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
72 &this->point2Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
73 &this->point3Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
74 &this->point4Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
75 });
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
76 matchRegex(this->cedgePattern, {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
77 &this->lineTypeFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
78 &this->colorFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
79 &this->point1Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
80 &this->point2Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
81 &this->point3Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
82 &this->point4Format,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
83 });
343
4a82990affd5 Fix BFC formatting not working due to being evaluated after comment format
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 342
diff changeset
84 matchRegex(this->bfcPattern, {
4a82990affd5 Fix BFC formatting not working due to being evaluated after comment format
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 342
diff changeset
85 &this->lineTypeFormat,
4a82990affd5 Fix BFC formatting not working due to being evaluated after comment format
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 342
diff changeset
86 &this->bfcFormat,
4a82990affd5 Fix BFC formatting not working due to being evaluated after comment format
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 342
diff changeset
87 });
331
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
88 matchRegex(this->commentPattern, {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
89 &this->lineTypeFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
90 &this->nameFormat,
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
91 });
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
92 if (not matched) {
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
93 this->setFormat(0, text.length(), this->errorFormat);
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
94 }
638a7458ef5e Add basic syntax highlighting
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
95 }

mercurial