Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
JSHighlighter.h
Go to the documentation of this file.
1 #ifndef JSHIGHLIGHTER_H
2 #define JSHIGHLIGHTER_H
3 
4 #include <QSyntaxHighlighter>
5 #include <QTextDocument>
6 #include <QTextFormat>
7 #include <QStringList>
8 #include <QRegExp>
9 #include <QVector>
10 #include <QTextCharFormat>
11 
12 // incomplete example from
13 // http://www.qtcentre.org/threads/25501-QtScript-script-text-editor-for-Qt-4-4-3-with-Syntax-Highlighter
14 
15 class JSHighlighter : public QSyntaxHighlighter
16 {
17  Q_OBJECT
18 public:
20  {
21  QRegExp pattern;
22  QTextCharFormat format;
23  };
24  QVector<HighlightingRule> highlightingRules;
25 
26  void SetRule(QString kind, QString pattern, QTextCharFormat format)
27  {
28  HighlightingRule rule;
29  rule.pattern = QRegExp(pattern);
30  rule.format = format;
31  highlightingRules.append(rule);
32  }
33 
34  JSHighlighter(QTextDocument *parent = 0): QSyntaxHighlighter(parent)
35  {
36  QTextCharFormat keywordFormat;
37  keywordFormat.setForeground(Qt::black);
38  keywordFormat.setFontWeight(QFont::Bold);
39  QStringList keywordPatterns;
40  keywordPatterns << "\\bvar\\b" << "\\bArray\\b" << "\\bfunction\\b"
41  << "\\breturn\\b" << "\\barguments\\b" << "\\bif\\b"
42  << "\\belse\\b" << "\\bfor\\b" << "\\bswitch\\b"
43  << "\\bcase\\b" << "\\bbreak\\b" << "\\bwhile\\b";
44  int i = 0;
45  foreach (const QString &pattern, keywordPatterns) {
46  SetRule(QString("00_KeyWord_%1").arg(i),pattern,keywordFormat);
47  ++i;
48  }
49  // Values
50  QTextCharFormat valueFormat, classFormat;
51  valueFormat.setForeground(Qt::blue);
52  SetRule("03_Values","\\btrue\\b|\\bfalse\\b|\\b[0-9]+\\b",valueFormat);
53  QTextCharFormat functionFormat;
54  //functionFormat.setFontItalic(false);
55  functionFormat.setForeground(Qt::darkBlue);
56  SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
57  // Qt Classes
58  classFormat.setFontWeight(QFont::Bold);
59  classFormat.setForeground(Qt::darkMagenta);
60  SetRule("06_QtClasses","\\bQ[A-Z]+[A-Za-z]+\\b",classFormat);
61  // Quotation
62  QTextCharFormat quotationFormat;
63  quotationFormat.setForeground(Qt::blue);
64  SetRule("z1_Quotations","\"[^\"]*\"",quotationFormat);
65  // Single Line Comments
66  QTextCharFormat singleLineCommentFormat;
67  singleLineCommentFormat.setForeground(Qt::darkGreen);
68  SetRule("z2_SingleLineComments","//[^\n]*",singleLineCommentFormat);
69  }
70 };
71 
72 #endif // JSHIGHLIGHTER_H
JSHighlighter::HighlightingRule
Definition: JSHighlighter.h:19
JSHighlighter
Definition: jsedit.h:121
JSHighlighter::SetRule
void SetRule(QString kind, QString pattern, QTextCharFormat format)
Definition: JSHighlighter.h:26
JSHighlighter::JSHighlighter
JSHighlighter(QTextDocument *parent=0)
Definition: JSHighlighter.h:34
JSHighlighter::highlightingRules
QVector< HighlightingRule > highlightingRules
Definition: JSHighlighter.h:24
JSHighlighter::HighlightingRule::pattern
QRegExp pattern
Definition: JSHighlighter.h:21
JSHighlighter::HighlightingRule::format
QTextCharFormat format
Definition: JSHighlighter.h:22