Changeset View
Changeset View
Standalone View
Standalone View
src/backends/julia/juliasession.cpp
Show All 25 Lines | |||||
26 | #include <QStandardPaths> | 26 | #include <QStandardPaths> | ||
27 | 27 | | |||
28 | #include "defaultvariablemodel.h" | 28 | #include "defaultvariablemodel.h" | ||
29 | 29 | | |||
30 | #include "juliaexpression.h" | 30 | #include "juliaexpression.h" | ||
31 | #include "settings.h" | 31 | #include "settings.h" | ||
32 | #include "juliahighlighter.h" | 32 | #include "juliahighlighter.h" | ||
33 | #include "juliakeywords.h" | 33 | #include "juliakeywords.h" | ||
34 | #include "juliavariablemodel.h" | ||||
34 | #include "juliaextensions.h" | 35 | #include "juliaextensions.h" | ||
35 | #include "juliabackend.h" | 36 | #include "juliabackend.h" | ||
36 | #include "juliacompletionobject.h" | 37 | #include "juliacompletionobject.h" | ||
37 | #include <julia/julia_version.h> | 38 | #include <julia/julia_version.h> | ||
38 | 39 | | |||
39 | const QRegularExpression JuliaSession::typeVariableInfo = QRegularExpression(QLatin1String("\\w+\\[")); | 40 | using namespace Cantor; | ||
40 | 41 | | |||
41 | JuliaSession::JuliaSession(Cantor::Backend *backend) | 42 | JuliaSession::JuliaSession(Cantor::Backend *backend) | ||
42 | : Session(backend) | 43 | : Session(backend) | ||
43 | , m_process(nullptr) | 44 | , m_process(nullptr) | ||
44 | , m_interface(nullptr) | 45 | , m_interface(nullptr) | ||
45 | , m_currentExpression(nullptr) | 46 | , m_currentExpression(nullptr) | ||
46 | , m_variableModel(new Cantor::DefaultVariableModel(this)) | 47 | , m_variableModel(new JuliaVariableModel(this)) | ||
48 | , m_needUpdate(false) | ||||
47 | { | 49 | { | ||
48 | } | 50 | } | ||
49 | 51 | | |||
50 | void JuliaSession::login() | 52 | void JuliaSession::login() | ||
51 | { | 53 | { | ||
52 | emit loginStarted(); | 54 | emit loginStarted(); | ||
53 | 55 | | |||
54 | if (m_process) { | 56 | if (m_process) { | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | 96 | if (!m_interface->isValid()) { | |||
96 | return; | 98 | return; | ||
97 | } | 99 | } | ||
98 | 100 | | |||
99 | m_interface->call( | 101 | m_interface->call( | ||
100 | QString::fromLatin1("login"), | 102 | QString::fromLatin1("login"), | ||
101 | JuliaSettings::self()->replPath().path() | 103 | JuliaSettings::self()->replPath().path() | ||
102 | ); | 104 | ); | ||
103 | 105 | | |||
104 | listVariables(); | 106 | m_variableModel->setJuliaServer(m_interface); | ||
107 | m_variableModel->update(); | ||||
105 | 108 | | |||
106 | // Plots integration | 109 | // Plots integration | ||
107 | if (integratePlots()) { | 110 | if (integratePlots()) { | ||
108 | runJuliaCommand( | 111 | runJuliaCommand( | ||
109 | QLatin1String("import GR; ENV[\"GKS_WSTYPE\"] = \"nul\"") | 112 | QLatin1String("import GR; ENV[\"GKS_WSTYPE\"] = \"nul\"") | ||
110 | ); | 113 | ); | ||
111 | } | 114 | } | ||
112 | 115 | | |||
113 | changeStatus(Session::Done); | 116 | changeStatus(Session::Done); | ||
114 | emit loginDone(); | 117 | emit loginDone(); | ||
115 | qDebug() << "login to julia " << JULIA_VERSION_STRING << "done"; | 118 | qDebug() << "login to julia " << JULIA_VERSION_STRING << "done"; | ||
116 | } | 119 | } | ||
117 | 120 | | |||
118 | void JuliaSession::logout() | 121 | void JuliaSession::logout() | ||
119 | { | 122 | { | ||
120 | m_process->terminate(); | 123 | m_process->terminate(); | ||
121 | 124 | | |||
122 | JuliaKeywords::instance()->clearVariables(); | | |||
123 | JuliaKeywords::instance()->clearFunctions(); | | |||
124 | m_variableModel->clearVariables(); | 125 | m_variableModel->clearVariables(); | ||
125 | emit updateHighlighter(); | | |||
126 | 126 | | |||
127 | changeStatus(Status::Disable); | 127 | changeStatus(Status::Disable); | ||
128 | } | 128 | } | ||
129 | 129 | | |||
130 | void JuliaSession::interrupt() | 130 | void JuliaSession::interrupt() | ||
131 | { | 131 | { | ||
132 | if (m_process->pid()) { | 132 | if (m_process->pid()) { | ||
133 | m_process->kill(); | 133 | m_process->kill(); | ||
Show All 28 Lines | 160 | Cantor::CompletionObject *JuliaSession::completionFor( | |||
162 | int index) | 162 | int index) | ||
163 | { | 163 | { | ||
164 | return new JuliaCompletionObject(command, index, this); | 164 | return new JuliaCompletionObject(command, index, this); | ||
165 | } | 165 | } | ||
166 | 166 | | |||
167 | QSyntaxHighlighter *JuliaSession::syntaxHighlighter(QObject *parent) | 167 | QSyntaxHighlighter *JuliaSession::syntaxHighlighter(QObject *parent) | ||
168 | { | 168 | { | ||
169 | JuliaHighlighter *highlighter = new JuliaHighlighter(parent); | 169 | JuliaHighlighter *highlighter = new JuliaHighlighter(parent); | ||
170 | QObject::connect( | 170 | | ||
171 | this, SIGNAL(updateHighlighter()), highlighter, SLOT(updateHighlight()) | 171 | connect( m_variableModel, &DefaultVariableModel::variablesAdded, highlighter, &JuliaHighlighter::addUserVariable); | ||
172 | ); | 172 | connect( m_variableModel, &DefaultVariableModel::variablesRemoved, highlighter, &JuliaHighlighter::removeUserVariable); | ||
173 | connect( m_variableModel, &DefaultVariableModel::functionsAdded, highlighter, &JuliaHighlighter::addUserFunctions); | ||||
174 | connect( m_variableModel, &DefaultVariableModel::functionsRemoved, highlighter, &JuliaHighlighter::removeUserFunctions); | ||||
175 | | ||||
173 | return highlighter; | 176 | return highlighter; | ||
174 | } | 177 | } | ||
175 | 178 | | |||
176 | void JuliaSession::runJuliaCommand(const QString &command) const | 179 | void JuliaSession::runJuliaCommand(const QString &command) const | ||
177 | { | 180 | { | ||
178 | m_interface->call(QLatin1String("runJuliaCommand"), command); | 181 | m_interface->call(QLatin1String("runJuliaCommand"), command); | ||
179 | } | 182 | } | ||
180 | 183 | | |||
181 | void JuliaSession::runJuliaCommandAsync(const QString &command) | 184 | void JuliaSession::runJuliaCommandAsync(const QString &command) | ||
182 | { | 185 | { | ||
183 | m_interface->callWithCallback( | 186 | m_interface->callWithCallback( | ||
184 | QLatin1String("runJuliaCommand"), | 187 | QLatin1String("runJuliaCommand"), | ||
185 | {command}, | 188 | {command}, | ||
186 | this, | 189 | this, | ||
187 | SLOT(onResultReady()) | 190 | SLOT(onResultReady()) | ||
188 | ); | 191 | ); | ||
189 | } | 192 | } | ||
190 | 193 | | |||
191 | void JuliaSession::onResultReady() | 194 | void JuliaSession::onResultReady() | ||
192 | { | 195 | { | ||
193 | m_currentExpression->finalize(); | 196 | m_currentExpression->finalize(); | ||
197 | m_needUpdate |= !m_currentExpression->isInternal(); | ||||
194 | m_runningExpressions.removeAll(m_currentExpression); | 198 | m_runningExpressions.removeAll(m_currentExpression); | ||
195 | 199 | | |||
196 | listVariables(); | 200 | if(m_needUpdate) | ||
201 | { | ||||
202 | m_variableModel->update(); | ||||
203 | m_needUpdate = false; | ||||
204 | } | ||||
197 | 205 | | |||
198 | changeStatus(Cantor::Session::Done); | 206 | changeStatus(Cantor::Session::Done); | ||
199 | } | 207 | } | ||
200 | 208 | | |||
201 | void JuliaSession::runExpression(JuliaExpression *expr) | 209 | void JuliaSession::runExpression(JuliaExpression *expr) | ||
202 | { | 210 | { | ||
203 | m_runningExpressions.append(expr); | 211 | m_runningExpressions.append(expr); | ||
204 | m_currentExpression = expr; | 212 | m_currentExpression = expr; | ||
Show All 18 Lines | |||||
223 | 231 | | |||
224 | bool JuliaSession::getWasException() | 232 | bool JuliaSession::getWasException() | ||
225 | { | 233 | { | ||
226 | const QDBusReply<bool> &reply = | 234 | const QDBusReply<bool> &reply = | ||
227 | m_interface->call(QLatin1String("getWasException")); | 235 | m_interface->call(QLatin1String("getWasException")); | ||
228 | return reply.isValid() && reply.value(); | 236 | return reply.isValid() && reply.value(); | ||
229 | } | 237 | } | ||
230 | 238 | | |||
231 | void JuliaSession::listVariables() | | |||
232 | { | | |||
233 | JuliaKeywords::instance()->clearVariables(); | | |||
234 | JuliaKeywords::instance()->clearFunctions(); | | |||
235 | | ||||
236 | m_interface->call(QLatin1String("parseModules")); | | |||
237 | | ||||
238 | const QStringList& variables = | | |||
239 | static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("variablesList"))).value(); | | |||
240 | const QStringList& values = | | |||
241 | static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("variableValuesList"))).value(); | | |||
242 | for (int i = 0; i < variables.size(); i++) | | |||
243 | { | | |||
244 | if (i >= values.size()) | | |||
245 | { | | |||
246 | qWarning() << "Don't have value for variable from julia server response, something wrong!"; | | |||
247 | continue; | | |||
248 | } | | |||
249 | | ||||
250 | const QString& name = variables[i]; | | |||
251 | QString value = values[i]; | | |||
252 | if (value != JuliaVariableManagementExtension::REMOVED_VARIABLE_MARKER) | | |||
253 | { | | |||
254 | // Register variable | | |||
255 | // We use replace here, because julia return data type for some variables, and we need | | |||
256 | // remove it to make variable view more consistent with the other backends | | |||
257 | // More info: https://bugs.kde.org/show_bug.cgi?id=377771 | | |||
258 | m_variableModel->addVariable(name, value.replace(typeVariableInfo,QLatin1String("["))); | | |||
259 | JuliaKeywords::instance()->addVariable(name); | | |||
260 | } | | |||
261 | else | | |||
262 | m_variableModel->removeVariable(name); | | |||
263 | } | | |||
264 | | ||||
265 | const QStringList& functions = | | |||
266 | static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("functionsList"))).value(); | | |||
267 | foreach (const QString& name, functions) | | |||
268 | { | | |||
269 | JuliaKeywords::instance()->addFunction(name); | | |||
270 | } | | |||
271 | | ||||
272 | emit updateHighlighter(); | | |||
273 | } | | |||
274 | | ||||
275 | QAbstractItemModel *JuliaSession::variableModel() | 239 | QAbstractItemModel *JuliaSession::variableModel() | ||
276 | { | 240 | { | ||
277 | return m_variableModel; | 241 | return m_variableModel; | ||
278 | } | 242 | } | ||
279 | 243 | | |||
280 | 244 | | |||
281 | bool JuliaSession::integratePlots() | 245 | bool JuliaSession::integratePlots() | ||
282 | { | 246 | { | ||
283 | return JuliaSettings::integratePlots(); | 247 | return JuliaSettings::integratePlots(); | ||
284 | } | 248 | } | ||
285 | 249 | | |||
286 | 250 | |