simple syntax highlighting

master
Marc Wäckerlin 8 years ago
parent cb040f47a1
commit 1a91f9bf64
  1. 2
      scripts/wt-mode.el
  2. 33
      src/editor.hxx

@ -25,7 +25,7 @@
;; echo $(webrunner -h | sed -n 's, COMMAND: ,,p') | sed 's, ,\\\\|,g'
(defconst wt-font-lock-keywords
(list
'("^ *\\(ca-certificate\\|call\\|case\\|check\\|clear-cookies\\|click\\|clicktype\\|client-certificate\\|do\\|download\\|echo\\|execute\\|exists\\|exit\\|expect\\|fail\\|for\\|function\\|if\\|ignoreto\\|include\\|label\\|load\\|not\\|offline-storage-path\\|open\\|screenshot\\|set\\|setvalue\\|sleep\\|testcase\\|testsuite\\|timeout\\|unset\\|upload\\) " . font-lock-builtin-face)
'("^ *\\(auth\\|ca-certificate\\|call\\|case\\|check\\|clear-cookies\\|click\\|clicktype\\|client-certificate\\|do\\|download\\|echo\\|execute\\|exists\\|exit\\|expect\\|fail\\|for\\|function\\|if\\|ignoreto\\|include\\|label\\|load\\|not\\|offline-storage-path\\|open\\|screenshot\\|set\\|setvalue\\|sleep\\|testcase\\|testsuite\\|timeout\\|unset\\|upload\\) " . font-lock-builtin-face)
'("^ *#.*$" . font-lock-comment-face))
"Highlighting expressions for Webtester")

@ -6,6 +6,38 @@
#include <QPainter>
#include <QTextBlock>
#include <QResizeEvent>
#include <QSyntaxHighlighter>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
class Highlighter: public QSyntaxHighlighter {
Q_OBJECT;
public:
Highlighter(QTextDocument *parent): QSyntaxHighlighter(parent) {
QString commands="auth|ca-certificate|call|case|check|clear-cookies|click|clicktype|client-certificate|do|download|echo|execute|exists|exit|expect|fail|for|function|if|ignoreto|include|label|load|not|offline-storage-path|open|screenshot|set|setvalue|sleep|testcase|testsuite|timeout|unset|upload";
_expressions<<Expression("^ *("+commands+")\\b").weight(QFont::Bold).fg(Qt::darkBlue)
<<Expression("^ *#.*$").weight(QFont::Bold).fg(Qt::black);
}
protected:
void highlightBlock(const QString &text) {
for (auto e: _expressions) {
auto m(e.re.match(text));
for (int i(0); i<=m.lastCapturedIndex(); ++i) {
setFormat(m.capturedStart(i), m.capturedLength(i), e.fmt);
}
}
}
private:
struct Expression {
Expression(QString s): re(s) {}
Expression(QString s, QTextCharFormat f): re(s), fmt(f) {}
Expression& weight(int w) {fmt.setFontWeight(w); return *this;}
Expression& fg(const QBrush& b) {fmt.setForeground(b); return *this;}
QRegularExpression re;
QTextCharFormat fmt;
};
QList<Expression> _expressions;
};
class CodeEditor;
@ -23,6 +55,7 @@ class CodeEditor: public QPlainTextEdit {
Q_OBJECT;
public:
CodeEditor(QWidget *parent = 0): QPlainTextEdit(parent) {
new Highlighter(document());
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));

Loading…
Cancel
Save