File indexing completed on 2024-05-19 11:21:25

0001 /*
0002     SPDX-FileCopyrightText: 2010 Miha Čančula <miha.cancula@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "octavecompletionobject.h"
0008 #include "octavekeywords.h"
0009 
0010 #include "session.h"
0011 #include "result.h"
0012 
0013 #include <QDebug>
0014 
0015 OctaveCompletionObject::OctaveCompletionObject(const QString& command, int index, Cantor::Session* parent):
0016     CompletionObject(parent),
0017     m_expression(nullptr)
0018 {
0019     setLine(command, index);
0020 }
0021 
0022 OctaveCompletionObject::~OctaveCompletionObject()
0023 {
0024     if (m_expression)
0025         m_expression->setFinishingBehavior(Cantor::Expression::FinishingBehavior::DeleteOnFinish);
0026 }
0027 
0028 void OctaveCompletionObject::fetchCompletions()
0029 {
0030     if (session()->status() != Cantor::Session::Done)
0031     {
0032         QStringList allCompletions;
0033 
0034         allCompletions << OctaveKeywords::instance()->functions();
0035         allCompletions << OctaveKeywords::instance()->keywords();
0036 
0037         setCompletions(allCompletions);
0038 
0039         emit fetchingDone();
0040     }
0041     else
0042     {
0043         if (m_expression)
0044             return;
0045         qDebug() << "Fetching completions for" << command();
0046         QString expr = QString::fromLatin1("completion_matches('%1')").arg(command());
0047         m_expression = session()->evaluateExpression(expr,Cantor::Expression::FinishingBehavior::DoNotDelete, true);
0048         connect(m_expression, &Cantor::Expression::statusChanged, this, &OctaveCompletionObject::extractCompletions);
0049     }
0050 }
0051 
0052 void OctaveCompletionObject::extractCompletions(Cantor::Expression::Status status)
0053 {
0054     switch(status)
0055     {
0056         case Cantor::Expression::Done:
0057         {
0058             Cantor::Result* result = m_expression->result();
0059             if (result)
0060             {
0061                 QString res = result->data().toString();
0062                 QStringList completions = res.split(QLatin1String("\n"), QString::SkipEmptyParts);
0063                 qDebug() << "Adding" << completions.size() << "completions";
0064                 setCompletions( completions );
0065             }
0066             break;
0067         }
0068         case Cantor::Expression::Interrupted:
0069         case Cantor::Expression::Error:
0070         {
0071             qDebug() << "fetching expression finished with status" << (status == Cantor::Expression::Error? "Error" : "Interrupted");
0072             break;
0073         }
0074         default:
0075             return;
0076     }
0077 
0078     m_expression->deleteLater();
0079     m_expression=nullptr;
0080     emit fetchingDone();
0081 }
0082 
0083 void OctaveCompletionObject::fetchIdentifierType()
0084 {
0085     if (session()->status() != Cantor::Session::Done)
0086     {
0087         qDebug() << "Fetching type of " << identifier();
0088         if (OctaveKeywords::instance()->keywords().contains(identifier()))
0089             emit fetchingTypeDone(KeywordType);
0090         else if (OctaveKeywords::instance()->functions().contains(identifier()))
0091             emit fetchingTypeDone(FunctionType);
0092         else
0093             emit fetchingTypeDone(UnknownType);
0094     }
0095     else
0096     {
0097         if (m_expression)
0098             return;
0099         qDebug() << "Fetching type of " << identifier();
0100         QString expr = QString::fromLatin1("__cantor_tmp__ = [exist('%1'), iskeyword('%1')], clear __cantor_tmp__").arg(identifier());
0101         m_expression = session()->evaluateExpression(expr, Cantor::Expression::FinishingBehavior::DoNotDelete, true);
0102         connect(m_expression, &Cantor::Expression::statusChanged, this, &OctaveCompletionObject::extractIdentifierType);
0103     }
0104 }
0105 
0106 void OctaveCompletionObject::extractIdentifierType(Cantor::Expression::Status status)
0107 {
0108     switch(status)
0109     {
0110         case Cantor::Expression::Error:
0111             qDebug() << "Error with OctaveCompletionObject" << m_expression->errorMessage();
0112             emit fetchingTypeDone(UnknownType);
0113             break;
0114 
0115         case Cantor::Expression::Interrupted:
0116             qDebug() << "OctaveCompletionObject was interrupted";
0117             emit fetchingTypeDone(UnknownType);
0118             break;
0119 
0120         case Cantor::Expression::Done:
0121             if (m_expression->result())
0122             {
0123                 QString res = m_expression->result()->data().toString();
0124                 // Remove '__cantor_tmp__ = \n' from result string
0125                 // size("__cantor_tmp__ = \n") == 18
0126                 res.remove(0,18);
0127 
0128                 const QStringList& ints = res.split(QLatin1String(" "), QString::SkipEmptyParts);
0129                 if (ints.size() != 2)
0130                     emit fetchingTypeDone(UnknownType);
0131                 else if (ints[1].toInt() == 1)
0132                     emit fetchingTypeDone(KeywordType);
0133                 else if (ints[0].toInt() == 1)
0134                     emit fetchingTypeDone(VariableType);
0135                 else if (ints[0].toInt() == 5 || ints[0].toInt() == 103)
0136                     emit fetchingTypeDone(FunctionType);
0137                 else
0138                     emit fetchingTypeDone(UnknownType);
0139             }
0140             break;
0141 
0142         default:
0143             return;
0144     }
0145 
0146     m_expression->deleteLater();
0147     m_expression = nullptr;
0148 }