Changeset View
Changeset View
Standalone View
Standalone View
src/backends/julia/juliahighlighter.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * This program is free software; you can redistribute it and/or | ||||
3 | * modify it under the terms of the GNU General Public License | ||||
4 | * as published by the Free Software Foundation; either version 2 | ||||
5 | * of the License, or (at your option) any later version. | ||||
6 | * | ||||
7 | * This program is distributed in the hope that it will be useful, | ||||
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
10 | * GNU General Public License for more details. | ||||
11 | * | ||||
12 | * You should have received a copy of the GNU General Public License | ||||
13 | * along with this program; if not, write to the Free Software | ||||
14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
15 | * Boston, MA 02110-1301, USA. | ||||
16 | * | ||||
17 | * --- | ||||
18 | * Copyright (C) 2016 Ivan Lakhtanov <ivan.lakhtanov@gmail.com> | ||||
19 | */ | ||||
20 | | ||||
21 | #include "juliahighlighter.h" | ||||
22 | #include "juliakeywords.h" | ||||
23 | | ||||
24 | #include <climits> | ||||
25 | #include <QTextEdit> | ||||
26 | | ||||
27 | JuliaHighlighter::JuliaHighlighter(QObject *parent) | ||||
28 | : Cantor::DefaultHighlighter(parent) | ||||
29 | { | ||||
30 | addRule(QRegExp(QLatin1String("\\b\\w+(?=\\()")), functionFormat()); | ||||
31 | | ||||
32 | // Code highlighting the different keywords | ||||
33 | addKeywords(JuliaKeywords::instance()->keywords()); | ||||
34 | addVariables(JuliaKeywords::instance()->variables()); | ||||
35 | } | ||||
36 | | ||||
37 | void JuliaHighlighter::highlightBlock(const QString &text) | ||||
38 | { | ||||
39 | if (skipHighlighting(text)) { | ||||
40 | return; | ||||
41 | } | ||||
42 | | ||||
43 | // Do some backend independent highlighting (brackets etc.) | ||||
44 | DefaultHighlighter::highlightBlock(text); | ||||
45 | | ||||
46 | const int IN_MULTILINE_COMMENT = 1; | ||||
47 | const int IN_CHARACTER = 2; | ||||
48 | const int IN_SINGLE_QUOTE_STRING = 4; | ||||
49 | const int IN_TRIPLE_QUOTE_STRING = 8; | ||||
50 | | ||||
51 | QRegExp multiLineCommentStart(QLatin1String("#=")); | ||||
52 | QRegExp multiLineCommentEnd(QLatin1String("=#")); | ||||
53 | QRegExp characterStartEnd(QLatin1String("'")); | ||||
54 | QRegExp singleQuoteStringStartEnd(QLatin1String("\"")); | ||||
55 | QRegExp tripleQuoteStringStartEnd(QLatin1String("\"\"\"")); | ||||
56 | QRegExp singleLineCommentStart(QLatin1String("#(?!=)")); | ||||
57 | | ||||
58 | int state = previousBlockState(); | ||||
59 | if (state == -1) { | ||||
60 | state = 0; | ||||
61 | } | ||||
62 | | ||||
63 | QList<int> flags = { | ||||
64 | IN_TRIPLE_QUOTE_STRING, | ||||
65 | IN_SINGLE_QUOTE_STRING, | ||||
66 | IN_CHARACTER, | ||||
67 | IN_MULTILINE_COMMENT | ||||
68 | }; | ||||
69 | QList<QRegExp> regexps_starts = { | ||||
70 | tripleQuoteStringStartEnd, | ||||
71 | singleQuoteStringStartEnd, | ||||
72 | characterStartEnd, | ||||
73 | multiLineCommentStart | ||||
74 | }; | ||||
75 | QList<QRegExp> regexps_ends = { | ||||
76 | tripleQuoteStringStartEnd, | ||||
77 | singleQuoteStringStartEnd, | ||||
78 | characterStartEnd, | ||||
79 | multiLineCommentEnd | ||||
80 | }; | ||||
81 | QList<QTextCharFormat> formats = { | ||||
82 | stringFormat(), | ||||
83 | stringFormat(), | ||||
84 | stringFormat(), | ||||
85 | commentFormat() | ||||
86 | }; | ||||
87 | | ||||
88 | int pos = 0; | ||||
89 | while (pos < text.length()) { | ||||
90 | // Trying to close current environments | ||||
91 | bool triggered = false; | ||||
92 | for (int i = 0; i < flags.size() and not triggered; i++) { | ||||
93 | int flag = flags[i]; | ||||
94 | QRegExp ®exp = regexps_ends[i]; | ||||
95 | QTextCharFormat &format = formats[i]; | ||||
96 | if (state & flag) { | ||||
97 | int new_pos = regexp.indexIn(text, pos); | ||||
98 | int length; | ||||
99 | if (new_pos == -1) { | ||||
100 | length = text.length() - pos; | ||||
101 | } else { | ||||
102 | length = new_pos - pos + regexp.matchedLength(); | ||||
103 | state -= flag; | ||||
104 | } | ||||
105 | setFormat(pos, length, format); | ||||
106 | pos = pos + length; | ||||
107 | triggered = true; | ||||
108 | } | ||||
109 | } | ||||
110 | if (triggered) { | ||||
111 | continue; | ||||
112 | } | ||||
113 | | ||||
114 | QRegExp *minRegexp = nullptr; | ||||
115 | int minPos = INT_MAX; | ||||
116 | int minIdx = -1; | ||||
117 | for (int i = 0; i < regexps_starts.size(); i++) { | ||||
118 | QRegExp ®exp = regexps_starts[i]; | ||||
119 | int newPos = regexp.indexIn(text, pos); | ||||
120 | if (newPos != -1) { | ||||
121 | minPos = qMin(minPos, newPos); | ||||
122 | minRegexp = ®exp; | ||||
123 | minIdx = i; | ||||
124 | } | ||||
125 | } | ||||
126 | | ||||
127 | int singleLineCommentStartPos = | ||||
128 | singleLineCommentStart.indexIn(text, pos); | ||||
129 | | ||||
130 | if (singleLineCommentStartPos != -1 | ||||
131 | and singleLineCommentStartPos < minPos) { | ||||
132 | setFormat(pos, text.length() - pos, commentFormat()); | ||||
133 | break; | ||||
134 | } else if (minRegexp) { | ||||
135 | state += flags[minIdx]; | ||||
136 | pos = minPos + minRegexp->matchedLength(); | ||||
137 | setFormat(minPos, minRegexp->matchedLength(), formats[minIdx]); | ||||
138 | } else { | ||||
139 | break; | ||||
140 | } | ||||
141 | } | ||||
142 | | ||||
143 | setCurrentBlockState(state); | ||||
144 | } | ||||
145 | | ||||
146 | void JuliaHighlighter::updateHighlight() | ||||
147 | { | ||||
148 | addVariables(JuliaKeywords::instance()->variables()); | ||||
149 | rehighlight(); | ||||
150 | } | ||||
151 | | ||||
152 | QString JuliaHighlighter::nonSeparatingCharacters() const | ||||
153 | { | ||||
154 | return QLatin1String("[\\w\\u00A1-\\uFFFF!]"); | ||||
155 | } |