File indexing completed on 2024-04-28 11:20:37

0001 /*
0002     SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "qalculatecompletionobject.h"
0008 
0009 #include <QStringList>
0010 
0011 #include <libqalculate/Calculator.h>
0012 #include <libqalculate/Unit.h>
0013 #include <libqalculate/Variable.h>
0014 #include <libqalculate/Function.h>
0015 
0016 #include "qalculatesession.h"
0017 
0018 QalculateCompletionObject::QalculateCompletionObject(const QString& command, int index, QalculateSession* session)
0019     : Cantor::CompletionObject(session)
0020 {
0021     setLine(command, index);
0022 }
0023 
0024 void QalculateCompletionObject::fetchIdentifierType()
0025 {
0026     Variable* var = CALCULATOR->getVariable(identifier().toLatin1().data());
0027     if (var) {
0028         emit fetchingTypeDone(VariableType);
0029         return;
0030     }
0031     MathFunction* func = CALCULATOR->getFunction(identifier().toLatin1().data());
0032     if (!func) // can this happen?
0033         emit fetchingTypeDone(UnknownType);
0034     else if (func->args() == 0)
0035         emit fetchingTypeDone(FunctionWithoutArguments);
0036     else
0037         emit fetchingTypeDone(FunctionWithArguments);
0038 }
0039 
0040 int QalculateCompletionObject::locateIdentifier(const QString& cmd, int index) const
0041 {
0042     if (index < 0)
0043     return -1;
0044 
0045     int i;
0046     int start_index = -1;
0047     for (i=index; i>=0 && mayIdentifierContain(cmd[i]); --i) {
0048     if (mayIdentifierBeginWith(cmd[i]))
0049         start_index = i;
0050     }
0051 
0052     return start_index;
0053 }
0054 
0055 
0056 void QalculateCompletionObject::fetchCompletions()
0057 {
0058     QStringList comp;
0059     // Matching Qt::CaseInsensitive here does not help, because a) Qalculate
0060     // does distinguish cases, and b) KCompletion::makeCompletion matches
0061     // case sensitive.
0062     foreach ( Unit* item, CALCULATOR->units ) {
0063         //TODO: this is fugily...
0064         QString str(QLatin1String(item->name(true).c_str()));
0065         if ( str.startsWith(command(), Qt::CaseSensitive) ) {
0066             comp << str;
0067         }
0068     QString str2(QLatin1String(item->singular().c_str()));
0069     if (str2.startsWith(command(), Qt::CaseSensitive) ) {
0070         comp << str2;
0071     }
0072     // Also include the plural form for completion?
0073     //QString str3(item->plural().c_str());
0074     //if (str3.startsWith(command(), Qt::CaseSensitive) ) {
0075     //    comp << str3;
0076     //}
0077     }
0078     foreach ( ExpressionItem* item, CALCULATOR->variables ) {
0079         //TODO: this is fugly...
0080         QString str(QLatin1String(item->name(true).c_str()));
0081         if ( str.startsWith(command(), Qt::CaseSensitive) ) {
0082             comp << str;
0083         }
0084     }
0085     foreach ( ExpressionItem* item, CALCULATOR->functions ) {
0086         //TODO: this is fugly...
0087         QString str(QLatin1String(item->name(true).c_str()));
0088         if ( str.startsWith(command(), Qt::CaseSensitive) ) {
0089             comp << str;
0090         }
0091     }
0092 
0093     setCompletions(comp);
0094     emit fetchingDone();
0095 }