Changeset View
Changeset View
Standalone View
Standalone View
src/backends/octave/octavehighlighter.cpp
Show All 17 Lines | |||||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "octavehighlighter.h" | 20 | #include "octavehighlighter.h" | ||
21 | #include "octavekeywords.h" | 21 | #include "octavekeywords.h" | ||
22 | #include "result.h" | 22 | #include "result.h" | ||
23 | 23 | | |||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | #include <QStringList> | 25 | #include <QStringList> | ||
26 | #include "octavesession.h" | ||||
26 | 27 | | |||
27 | using namespace Cantor; | 28 | using namespace Cantor; | ||
28 | 29 | | |||
29 | OctaveHighlighter::OctaveHighlighter(QObject* parent, Session* session): DefaultHighlighter(parent), m_session(session) | 30 | OctaveHighlighter::OctaveHighlighter(QObject* parent, Session* session): DefaultHighlighter(parent, session) | ||
30 | { | 31 | { | ||
31 | addKeywords(OctaveKeywords::instance()->keywords()); | 32 | addKeywords(OctaveKeywords::instance()->keywords()); | ||
32 | addFunctions(OctaveKeywords::instance()->functions()); | 33 | addFunctions(OctaveKeywords::instance()->functions()); | ||
33 | 34 | | |||
34 | QStringList operators; | 35 | QStringList operators; | ||
35 | operators | 36 | operators | ||
36 | << QLatin1String("+") << QLatin1String("-") << QLatin1String("*") << QLatin1String("/") | 37 | << QLatin1String("+") << QLatin1String("-") << QLatin1String("*") << QLatin1String("/") | ||
37 | << QLatin1String(".+") << QLatin1String(".-") << QLatin1String(".*") << QLatin1String("./") | 38 | << QLatin1String(".+") << QLatin1String(".-") << QLatin1String(".*") << QLatin1String("./") | ||
38 | << QLatin1String("=") << QLatin1String("or") << QLatin1String("and") << QLatin1String("xor") | 39 | << QLatin1String("=") << QLatin1String("or") << QLatin1String("and") << QLatin1String("xor") | ||
39 | << QLatin1String("not") << QLatin1String("||") << QLatin1String("&&") << QLatin1String("=="); | 40 | << QLatin1String("not") << QLatin1String("||") << QLatin1String("&&") << QLatin1String("=="); | ||
40 | addRules(operators, operatorFormat()); | 41 | addRules(operators, operatorFormat()); | ||
41 | 42 | | |||
42 | addRule(QRegExp(QLatin1String("\"[^\"]*\"")), stringFormat()); | 43 | addRule(QRegExp(QLatin1String("\"[^\"]*\"")), stringFormat()); | ||
43 | addRule(QRegExp(QLatin1String("'[^']*'")), stringFormat()); | 44 | addRule(QRegExp(QLatin1String("'[^']*'")), stringFormat()); | ||
44 | 45 | | |||
45 | addRule(QRegExp(QLatin1String("#[^\n]*")), commentFormat()); | 46 | addRule(QRegExp(QLatin1String("#[^\n]*")), commentFormat()); | ||
46 | addRule(QRegExp(QLatin1String("%[^\n]*")), commentFormat()); | 47 | addRule(QRegExp(QLatin1String("%[^\n]*")), commentFormat()); | ||
47 | 48 | | |||
48 | rehighlight(); | 49 | rehighlight(); | ||
49 | } | 50 | } | ||
50 | | ||||
51 | void OctaveHighlighter::updateVariables() | | |||
52 | { | | |||
53 | Expression* expr = m_session->evaluateExpression(QLatin1String("who"), Expression::FinishingBehavior::DoNotDelete, true); | | |||
54 | connect(expr, &Expression::statusChanged, [this, expr](Expression::Status status) | | |||
55 | { | | |||
56 | if (status != Expression::Done && status != Expression::Error) | | |||
57 | return; | | |||
58 | | ||||
59 | if (status == Expression::Done && expr->result()) | | |||
60 | { | | |||
61 | QString res = expr->result()->toHtml(); | | |||
62 | res.replace(QLatin1String("<br/>"),QLatin1String(" ")); | | |||
63 | res.remove(0, res.indexOf(QLatin1Char('\n'))); | | |||
64 | res.remove(QLatin1Char('\n')); | | |||
65 | res = res.trimmed(); | | |||
66 | | ||||
67 | QStringList newVariables; | | |||
68 | foreach ( const QString& var, res.split(QLatin1Char(' '), QString::SkipEmptyParts)) | | |||
69 | { | | |||
70 | newVariables << var.trimmed(); | | |||
71 | } | | |||
72 | qDebug() << "Received" << newVariables.size() << "variables"; | | |||
73 | | ||||
74 | for (const QString& newVariable: newVariables) | | |||
75 | if (!m_variables.contains(newVariable)) | | |||
76 | addRule(newVariable, variableFormat()); | | |||
77 | | ||||
78 | for (const QString& variable: m_variables) | | |||
79 | if (!newVariables.contains(variable)) | | |||
80 | removeRule(variable); | | |||
81 | | ||||
82 | m_variables = std::move(newVariables); | | |||
83 | rehighlight(); | | |||
84 | } | | |||
85 | expr->deleteLater(); | | |||
86 | }); | | |||
87 | } | | |||
88 | | ||||
89 | void OctaveHighlighter::sessionStatusChanged(Cantor::Session::Status status) | | |||
90 | { | | |||
91 | if (status == Cantor::Session::Status::Disable) | | |||
92 | for (const QString& variable: m_variables) | | |||
93 | removeRule(variable); | | |||
94 | rehighlight(); | | |||
95 | } | |