|
| 1 | +/* Inspired from a code in the Qt Examples - BSD License */ |
| 2 | + |
| 3 | +#include <QtWidgets> |
| 4 | + |
| 5 | +#include "luaeditor.h" |
| 6 | + |
| 7 | +LuaEditor::LuaEditor(QWidget *parent) : QPlainTextEdit(parent) |
| 8 | +{ |
| 9 | + highlighter = new LuaHighlighter(document()); |
| 10 | + |
| 11 | + lineNumberArea = new LineNumberArea(this); |
| 12 | + |
| 13 | + connect(this, &QPlainTextEdit::blockCountChanged, this, &LuaEditor::updateLineNumberAreaWidth); |
| 14 | + connect(this, &QPlainTextEdit::updateRequest, this, &LuaEditor::updateLineNumberArea); |
| 15 | + connect(this, &QPlainTextEdit::cursorPositionChanged, this, &LuaEditor::highlightCurrentLine); |
| 16 | + |
| 17 | + updateLineNumberAreaWidth(0); |
| 18 | + highlightCurrentLine(); |
| 19 | +} |
| 20 | + |
| 21 | +int LuaEditor::lineNumberAreaWidth() |
| 22 | +{ |
| 23 | + int digits = 1; |
| 24 | + int max = qMax(1, blockCount()); |
| 25 | + while (max >= 10) { |
| 26 | + max /= 10; |
| 27 | + ++digits; |
| 28 | + } |
| 29 | + |
| 30 | + int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; |
| 31 | + |
| 32 | + return space; |
| 33 | +} |
| 34 | + |
| 35 | +void LuaEditor::updateLineNumberAreaWidth(int /* newBlockCount */) |
| 36 | +{ |
| 37 | + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); |
| 38 | +} |
| 39 | + |
| 40 | +void LuaEditor::updateLineNumberArea(const QRect &rect, int dy) |
| 41 | +{ |
| 42 | + if (dy) |
| 43 | + lineNumberArea->scroll(0, dy); |
| 44 | + else |
| 45 | + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); |
| 46 | + |
| 47 | + if (rect.contains(viewport()->rect())) |
| 48 | + updateLineNumberAreaWidth(0); |
| 49 | +} |
| 50 | + |
| 51 | +void LuaEditor::resizeEvent(QResizeEvent *e) |
| 52 | +{ |
| 53 | + QPlainTextEdit::resizeEvent(e); |
| 54 | + |
| 55 | + QRect cr = contentsRect(); |
| 56 | + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); |
| 57 | +} |
| 58 | + |
| 59 | +void LuaEditor::highlightCurrentLine() |
| 60 | +{ |
| 61 | + QList<QTextEdit::ExtraSelection> extraSelections; |
| 62 | + |
| 63 | + if (!isReadOnly()) { |
| 64 | + QTextEdit::ExtraSelection selection; |
| 65 | + |
| 66 | + QColor lineColor = QColor(Qt::yellow).lighter(160); |
| 67 | + |
| 68 | + selection.format.setBackground(lineColor); |
| 69 | + selection.format.setProperty(QTextFormat::FullWidthSelection, true); |
| 70 | + selection.cursor = textCursor(); |
| 71 | + selection.cursor.clearSelection(); |
| 72 | + extraSelections.append(selection); |
| 73 | + } |
| 74 | + |
| 75 | + setExtraSelections(extraSelections); |
| 76 | +} |
| 77 | + |
| 78 | +void LuaEditor::lineNumberAreaPaintEvent(QPaintEvent *event) |
| 79 | +{ |
| 80 | + QPainter painter(lineNumberArea); |
| 81 | + painter.fillRect(event->rect(), Qt::lightGray); |
| 82 | + painter.setPen(Qt::black); |
| 83 | + |
| 84 | + QTextBlock block = firstVisibleBlock(); |
| 85 | + int blockNumber = block.blockNumber(); |
| 86 | + int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); |
| 87 | + int bottom = top + (int) blockBoundingRect(block).height(); |
| 88 | + |
| 89 | + while (block.isValid() && top <= event->rect().bottom()) |
| 90 | + { |
| 91 | + if (block.isVisible() && bottom >= event->rect().top()) |
| 92 | + { |
| 93 | + QString number = QString::number(blockNumber + 1); |
| 94 | + painter.drawText(-2, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number); |
| 95 | + } |
| 96 | + block = block.next(); |
| 97 | + top = bottom; |
| 98 | + bottom = top + (int) blockBoundingRect(block).height(); |
| 99 | + ++blockNumber; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/* Highlighter */ |
| 104 | + |
| 105 | +LuaHighlighter::LuaHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) |
| 106 | +{ |
| 107 | + HighlightingRule rule; |
| 108 | + |
| 109 | + keywordFormat.setForeground(Qt::darkBlue); |
| 110 | + keywordFormat.setFontWeight(QFont::Bold); |
| 111 | + QStringList keywordPatterns; |
| 112 | + keywordPatterns << "\\band\\b" << "\\bbreak\\b" << "\\bdo\\b" << "\\belse\\b" << "\\belseif\\b" |
| 113 | + << "\\bend\\b" << "\\bfor\\b" << "\\bif\\b" << "\\bin\\b" << "\\blocal\\b" |
| 114 | + << "\\bnot\\b" << "\\bor\\b" << "\\brepeat\\b" << "\\breturn\\b" << "\\bthen\\b" |
| 115 | + << "\\buntil\\b" << "\\bwhile\\n"; |
| 116 | + foreach (const QString &pattern, keywordPatterns) { |
| 117 | + rule.pattern = QRegExp(pattern); |
| 118 | + rule.format = keywordFormat; |
| 119 | + highlightingRules.append(rule); |
| 120 | + } |
| 121 | + |
| 122 | + builtinFormat.setForeground(Qt::darkCyan); |
| 123 | + builtinFormat.setFontWeight(QFont::Bold); |
| 124 | + QStringList builtinPatterns; |
| 125 | + builtinPatterns << "\\b_G\\b" << "\\b_VERSION\\b" << "\\bassert\\b" << "\\bcollectgarbage\\b" |
| 126 | + << "\\bdofile\\b" << "\\berror\\b" << "\\bgetfenv\\b" << "\\bgetmetatable\\b" |
| 127 | + << "\\bipairs\\b" << "\\bload\\b" << "\\bloadfile\\b" << "\\bloadstring\\b" |
| 128 | + << "\\bmodule\\b" << "\\bnext\\b" << "\\bpairs\\b" << "\\bpcall\\b" << "\\bprint\\b" |
| 129 | + << "\\brawequal\\b" << "\\brawget\\b" << "\\brawset\\b" << "\\brequire\\b" |
| 130 | + << "\\bselect\\b" << "\\bsetfenv\\b" << "\\bsetmetatable\\b" << "\\btonumber\\b" |
| 131 | + << "\\btostring\\b" << "\\btype\\b" << "\\bunpack\\b" << "\\bxpcall\\b" |
| 132 | + << "\\bcoroutine\\b" << "\\bdebug\\b" << "\\bio\\b" << "\\bmath\\b" << "\\bos\\b" |
| 133 | + << "\\bpackage\\b" << "\\bstring\\b" << "\\btable\\b"; |
| 134 | + foreach (const QString &pattern, builtinPatterns) { |
| 135 | + rule.pattern = QRegExp(pattern); |
| 136 | + rule.format = builtinFormat; |
| 137 | + highlightingRules.append(rule); |
| 138 | + } |
| 139 | + |
| 140 | + literalFormat.setFontWeight(QFont::Bold); |
| 141 | + literalFormat.setForeground(Qt::darkMagenta); |
| 142 | + rule.pattern = QRegExp("\\b(nil|true|false)\\b"); |
| 143 | + rule.format = literalFormat; |
| 144 | + highlightingRules.append(rule); |
| 145 | + |
| 146 | + cemuGlobalsFormat.setFontUnderline(true); |
| 147 | + cemuGlobalsFormat.setForeground(Qt::red); |
| 148 | + rule.pattern = QRegExp("\\b(cpu|devices|mem|gui|R|F)\\b"); |
| 149 | + rule.format = cemuGlobalsFormat; |
| 150 | + highlightingRules.append(rule); |
| 151 | + |
| 152 | + numberFormat.setForeground(Qt::darkMagenta); |
| 153 | + rule.pattern = QRegExp("(\\b0[xX][0-9a-fA-F]+\\b)|(((\\b[0-9]+)?\\.)?\\b[0-9]+([eE][-+]?[0-9]+)?\\b)"); |
| 154 | + rule.format = numberFormat; |
| 155 | + highlightingRules.append(rule); |
| 156 | + |
| 157 | + quotationFormat.setForeground(Qt::darkGreen); |
| 158 | + rule.pattern = QRegExp("(\"[^\"]*\")|('[^']*')|(\\[=*\\[.*\\]=*\\])"); |
| 159 | + rule.format = quotationFormat; |
| 160 | + highlightingRules.append(rule); |
| 161 | + |
| 162 | + functionFormat.setForeground(Qt::blue); |
| 163 | + rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); |
| 164 | + rule.format = functionFormat; |
| 165 | + highlightingRules.append(rule); |
| 166 | + |
| 167 | + singleLineCommentFormat.setForeground(Qt::gray); |
| 168 | + rule.pattern = QRegExp("--[^\n]*"); |
| 169 | + rule.format = singleLineCommentFormat; |
| 170 | + highlightingRules.append(rule); |
| 171 | + |
| 172 | + multiLineCommentFormat.setForeground(Qt::gray); |
| 173 | + |
| 174 | + commentStartExpression = QRegExp("--\\[=*\\["); |
| 175 | + commentEndExpression = QRegExp("\\]=*\\]"); |
| 176 | +} |
| 177 | + |
| 178 | +void LuaHighlighter::highlightBlock(const QString &text) |
| 179 | +{ |
| 180 | + foreach (const HighlightingRule &rule, highlightingRules) { |
| 181 | + QRegExp expression(rule.pattern); |
| 182 | + int index = expression.indexIn(text); |
| 183 | + while (index >= 0) { |
| 184 | + int length = expression.matchedLength(); |
| 185 | + setFormat(index, length, rule.format); |
| 186 | + index = expression.indexIn(text, index + length); |
| 187 | + } |
| 188 | + } |
| 189 | + setCurrentBlockState(0); |
| 190 | + |
| 191 | + int startIndex = 0; |
| 192 | + if (previousBlockState() != 1) |
| 193 | + startIndex = commentStartExpression.indexIn(text); |
| 194 | + |
| 195 | + while (startIndex >= 0) { |
| 196 | + int endIndex = commentEndExpression.indexIn(text, startIndex); |
| 197 | + int commentLength; |
| 198 | + if (endIndex == -1) { |
| 199 | + setCurrentBlockState(1); |
| 200 | + commentLength = text.length() - startIndex; |
| 201 | + } else { |
| 202 | + commentLength = endIndex - startIndex + commentEndExpression.matchedLength(); |
| 203 | + } |
| 204 | + setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 205 | + startIndex = commentStartExpression.indexIn(text, startIndex + commentLength); |
| 206 | + } |
| 207 | +} |
0 commit comments