Changeset View
Changeset View
Standalone View
Standalone View
src/backends/maxima/maximahighlighter.cpp
Show All 17 Lines | 1 | /* | |||
---|---|---|---|---|---|
18 | Copyright (C) 2009-2012 Alexander Rieder <alexanderrieder@gmail.com> | 18 | Copyright (C) 2009-2012 Alexander Rieder <alexanderrieder@gmail.com> | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "maximahighlighter.h" | 21 | #include "maximahighlighter.h" | ||
22 | #include "maximakeywords.h" | 22 | #include "maximakeywords.h" | ||
23 | #include "maximasession.h" | 23 | #include "maximasession.h" | ||
24 | #include "maximavariablemodel.h" | 24 | #include "maximavariablemodel.h" | ||
25 | 25 | | |||
26 | MaximaHighlighter::MaximaHighlighter(QObject* parent, MaximaSession* session) : Cantor::DefaultHighlighter(parent) | 26 | MaximaHighlighter::MaximaHighlighter(QObject* parent, MaximaSession* session) | ||
27 | : Cantor::DefaultHighlighter(parent, session) | ||||
27 | { | 28 | { | ||
28 | //addRule(QRegExp("\\b[A-Za-z0-9_]+(?=\\()"), functionFormat()); | 29 | //addRule(QRegExp("\\b[A-Za-z0-9_]+(?=\\()"), functionFormat()); | ||
29 | 30 | | |||
30 | //Code highlighting the different keywords | 31 | //Code highlighting the different keywords | ||
31 | addKeywords(MaximaKeywords::instance()->keywords()); | 32 | addKeywords(MaximaKeywords::instance()->keywords()); | ||
32 | 33 | | |||
33 | addRule(QLatin1String("FIXME"), commentFormat()); | 34 | addRule(QLatin1String("FIXME"), commentFormat()); | ||
34 | addRule(QLatin1String("TODO"), commentFormat()); | 35 | addRule(QLatin1String("TODO"), commentFormat()); | ||
35 | 36 | | |||
36 | addFunctions(MaximaKeywords::instance()->functions()); | 37 | addFunctions(MaximaKeywords::instance()->functions()); | ||
37 | addVariables(MaximaKeywords::instance()->variables()); | 38 | addVariables(MaximaKeywords::instance()->variables()); | ||
38 | 39 | | |||
39 | //addRule(QRegExp("\".*\""), stringFormat()); | 40 | //addRule(QRegExp("\".*\""), stringFormat()); | ||
40 | //addRule(QRegExp("'.*'"), stringFormat()); | 41 | //addRule(QRegExp("'.*'"), stringFormat()); | ||
41 | 42 | | |||
42 | commentStartExpression = QRegExp(QLatin1String("/\\*")); | 43 | commentStartExpression = QRegExp(QLatin1String("/\\*")); | ||
43 | commentEndExpression = QRegExp(QLatin1String("\\*/")); | 44 | commentEndExpression = QRegExp(QLatin1String("\\*/")); | ||
44 | | ||||
45 | connect(session->variableModel(), SIGNAL(variablesAdded(QStringList)), this, SLOT(addUserVariables(QStringList))); | | |||
46 | connect(session->variableModel(), SIGNAL(variablesRemoved(QStringList)), this, SLOT(removeUserVariables(QStringList))); | | |||
47 | connect(session->variableModel(), SIGNAL(functionsAdded(QStringList)), this, SLOT(addUserFunctions(QStringList))); | | |||
48 | connect(session->variableModel(), SIGNAL(functionsRemoved(QStringList)), this, SLOT(removeUserFunctions(QStringList))); | | |||
49 | | ||||
50 | MaximaVariableModel* model=static_cast<MaximaVariableModel*>(session->variableModel()); | | |||
51 | addUserVariables(model->variableNames()); | | |||
52 | addUserFunctions(model->functionNames()); | | |||
53 | } | 45 | } | ||
54 | 46 | | |||
55 | void MaximaHighlighter::highlightBlock(const QString& text) | 47 | void MaximaHighlighter::highlightBlock(const QString& text) | ||
56 | { | 48 | { | ||
57 | if (skipHighlighting(text)) | 49 | if (skipHighlighting(text)) | ||
58 | return; | 50 | return; | ||
59 | 51 | | |||
60 | //Do some backend independent highlighting (brackets etc.) | 52 | //Do some backend independent highlighting (brackets etc.) | ||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Line(s) | 96 | if (inString) { | |||
105 | setCurrentBlockState(-2); | 97 | setCurrentBlockState(-2); | ||
106 | setFormat(startIndex, text.size() - startIndex, stringFormat()); | 98 | setFormat(startIndex, text.size() - startIndex, stringFormat()); | ||
107 | } else if (commentLevel > 0) { | 99 | } else if (commentLevel > 0) { | ||
108 | setCurrentBlockState(commentLevel); | 100 | setCurrentBlockState(commentLevel); | ||
109 | setFormat(startIndex, text.size() - startIndex, commentFormat()); | 101 | setFormat(startIndex, text.size() - startIndex, commentFormat()); | ||
110 | } | 102 | } | ||
111 | } | 103 | } | ||
112 | 104 | | |||
113 | void MaximaHighlighter::addUserVariables(const QStringList variables) | | |||
114 | { | | |||
115 | addVariables(variables); | | |||
116 | } | | |||
117 | | ||||
118 | void MaximaHighlighter::removeUserVariables(const QStringList variables) | | |||
119 | { | | |||
120 | for (const QString& var : variables) | | |||
121 | removeRule(var); | | |||
122 | } | | |||
123 | | ||||
124 | void MaximaHighlighter::addUserFunctions(const QStringList functions) | | |||
125 | { | | |||
126 | //remove the trailing (x) | | |||
127 | for (const QString& func : functions) | | |||
128 | { | | |||
129 | int idx=func.lastIndexOf(QLatin1Char('(')); | | |||
130 | addRule(func.left(idx), functionFormat()); | | |||
131 | } | | |||
132 | } | | |||
133 | | ||||
134 | void MaximaHighlighter::removeUserFunctions(const QStringList functions) | | |||
135 | { | | |||
136 | //remove the trailing (x) | | |||
137 | for (const QString& func : functions) | | |||
138 | { | | |||
139 | int idx=func.lastIndexOf(QLatin1Char('(')); | | |||
140 | removeRule(func.left(idx)); | | |||
141 | } | | |||
142 | } | | |||
143 | | ||||
144 | QString MaximaHighlighter::nonSeparatingCharacters() const | 105 | QString MaximaHighlighter::nonSeparatingCharacters() const | ||
145 | { | 106 | { | ||
146 | return QLatin1String("%"); | 107 | return QLatin1String("%"); | ||
147 | } | 108 | } |