Commit 44e33d84 authored by Nikita Sirgienko's avatar Nikita Sirgienko

- Rename "addUserStuff" to "addUserDefinition"

- Move code for functions handling to base highlighter class
parent c02608c7
......@@ -81,7 +81,7 @@ void RHighlighter::highlightBlock(const QString& text)
formatRule(QRegExp(QLatin1String("\"[^\"]+\"")),stringFormat(),text); // WARNING a bit redundant
}
void RHighlighter::addUserStuff(const QStringList& names, QVector<QRegExp>& vector)
void RHighlighter::addUserDefinition(const QStringList& names, QVector<QRegExp>& vector)
{
for (const QString s : names)
if (!s.contains(QRegExp(QLatin1String("[^A-Za-z0-9_.]"))))
......@@ -90,7 +90,7 @@ void RHighlighter::addUserStuff(const QStringList& names, QVector<QRegExp>& vect
emit rulesChanged();
}
void RHighlighter::removeUserStuff(const QStringList& names, QVector<QRegExp>& vector)
void RHighlighter::removeUserDefinition(const QStringList& names, QVector<QRegExp>& vector)
{
for (const QString var : names)
for (int i = 0; i < vector.size(); i++)
......@@ -105,20 +105,20 @@ void RHighlighter::removeUserStuff(const QStringList& names, QVector<QRegExp>& v
void RHighlighter::addUserVariable(const QStringList& vars)
{
addUserStuff(vars, variables);
addUserDefinition(vars, variables);
}
void RHighlighter::removeUserVariable(const QStringList& vars)
{
removeUserStuff(vars, variables);
removeUserDefinition(vars, variables);
}
void RHighlighter::removeUserFunction(const QStringList& funcs)
{
removeUserStuff(funcs, functions);
removeUserDefinition(funcs, functions);
}
void RHighlighter::addUserFunction(const QStringList& funcs)
{
addUserStuff(funcs, functions);
addUserDefinition(funcs, functions);
}
......@@ -44,8 +44,8 @@ class RHighlighter : public Cantor::DefaultHighlighter
inline void formatRule(const QRegExp &p, const QTextCharFormat &fmt, const QString& text,bool shift=false);
inline void massFormat(const QVector<QRegExp>& rules, const QTextCharFormat &fmt, const QString& text,bool shift=false);
void addUserStuff(const QStringList& names, QVector<QRegExp>& vector);
void removeUserStuff(const QStringList& names, QVector<QRegExp>& vector);
void addUserDefinition(const QStringList& names, QVector<QRegExp>& vector);
void removeUserDefinition(const QStringList& names, QVector<QRegExp>& vector);
static const QStringList operators_list;
static const QStringList specials_list;
......
......@@ -133,8 +133,8 @@ QSyntaxHighlighter* RSession::syntaxHighlighter(QObject* parent)
RHighlighter *h=new RHighlighter(parent);
connect(m_variableModel, &Cantor::DefaultVariableModel::variablesAdded, h, &RHighlighter::addUserVariable);
connect(m_variableModel, &Cantor::DefaultVariableModel::variablesRemoved, h, &RHighlighter::removeUserVariable);
connect(m_variableModel, &RVariableModel::functionsAdded, h, &RHighlighter::addUserFunction);
connect(m_variableModel, &RVariableModel::functionsRemoved, h, &RHighlighter::removeUserFunction);
connect(m_variableModel, &Cantor::DefaultVariableModel::functionsAdded, h, &RHighlighter::addUserFunction);
connect(m_variableModel, &Cantor::DefaultVariableModel::functionsRemoved, h, &RHighlighter::removeUserFunction);
return h;
}
......
......@@ -45,36 +45,5 @@ void RVariableModel::parseResult(const QStringList& names, const QStringList& va
setVariables(vars);
// Handle functions
QStringList addedFuncs;
QStringList removedFuncs;
int i = 0;
while (i < m_functions.size())
if (!funcs.contains(m_functions[i]))
{
removedFuncs << m_functions[i];
m_functions.removeAt(i);
}
else
i++;
for (const QString newFunc : funcs)
if(!m_functions.contains(newFunc))
{
addedFuncs << newFunc;
m_functions << newFunc;
}
if (!addedFuncs.isEmpty())
emit functionsAdded(addedFuncs);
if (!removedFuncs.isEmpty())
emit functionsRemoved(removedFuncs);
}
void RVariableModel::clearFunctions()
{
emit functionsRemoved(m_functions);
m_functions.clear();
setFunctions(funcs);
}
......@@ -33,11 +33,6 @@ class RVariableModel : public Cantor::DefaultVariableModel
~RVariableModel() override = default;
void update() override;
void clearFunctions();
Q_SIGNALS:
void functionsAdded(const QStringList& names);
void functionsRemoved(const QStringList& names);
public Q_SLOTS:
void parseResult(const QStringList& names, const QStringList& values, const QStringList& funcs);
......
......@@ -170,8 +170,8 @@ QSyntaxHighlighter *JuliaSession::syntaxHighlighter(QObject *parent)
connect( m_variableModel, &DefaultVariableModel::variablesAdded, highlighter, &JuliaHighlighter::addUserVariable);
connect( m_variableModel, &DefaultVariableModel::variablesRemoved, highlighter, &JuliaHighlighter::removeUserVariable);
connect( m_variableModel, &JuliaVariableModel::functionsAdded, highlighter, &JuliaHighlighter::addUserFunctions);
connect( m_variableModel, &JuliaVariableModel::functionsRemoved, highlighter, &JuliaHighlighter::removeUserFunctions);
connect( m_variableModel, &DefaultVariableModel::functionsAdded, highlighter, &JuliaHighlighter::addUserFunctions);
connect( m_variableModel, &DefaultVariableModel::functionsRemoved, highlighter, &JuliaHighlighter::removeUserFunctions);
return highlighter;
}
......
......@@ -89,40 +89,5 @@ void JuliaVariableModel::update()
const QStringList& newFuncs =
static_cast<QDBusReply<QStringList>>(m_interface->call(QLatin1String("functionsList"))).value();
QStringList addedFuncs;
QStringList removedFuncs;
//remove the old variables
int i = 0;
while (i < m_functions.size())
{
//check if this var is present in the new variables
bool found=false;
for (const QString& func : newFuncs)
if(m_functions[i] == func)
{
found=true;
break;
}
if(!found)
{
removedFuncs<<m_functions[i];
m_functions.removeAt(i);
}
else
i++;
}
for (const QString& func : newFuncs)
{
if (!m_functions.contains(func))
{
addedFuncs<<func;
m_functions.append(func);
}
}
emit functionsAdded(addedFuncs);
emit functionsRemoved(removedFuncs);
setFunctions(newFuncs);
}
......@@ -39,10 +39,6 @@ class JuliaVariableModel : public Cantor::DefaultVariableModel
void setJuliaServer(QDBusInterface* interface);
Q_SIGNALS:
void functionsAdded(const QStringList funcs);
void functionsRemoved(const QStringList funcs);
private:
static const QRegularExpression typeVariableInfo;
......
......@@ -40,12 +40,6 @@ m_functionExpression(nullptr)
{
}
void MaximaVariableModel::clearFunctions()
{
emit functionsRemoved(functionNames());
m_functions.clear();
}
void MaximaVariableModel::update()
{
if (!m_variableExpression)
......@@ -171,46 +165,14 @@ void MaximaVariableModel::parseNewFunctions(Cantor::Expression::Status status)
// List of variables?
QList<Variable> newFuncs=parse(static_cast<MaximaExpression*>(m_functionExpression));
QStringList addedFuncs;
QStringList removedFuncs;
//remove the old variables
int i = 0;
while (i < m_functions.size())
{
//check if this var is present in the new variables
bool found=false;
for (const Variable& func : newFuncs)
if(m_functions[i] == func.name)
{
found=true;
break;
}
if(!found)
{
removedFuncs<<m_functions[i];
m_functions.removeAt(i);
}
else
i++;
}
for (Variable func : newFuncs)
{
if (!m_functions.contains(func.name))
{
addedFuncs<<func.name;
m_functions.append(func.name);
}
}
QStringList functions;
for (Variable var : newFuncs)
functions << var.name;
setFunctions(functions);
//the expression is not needed anymore
m_functionExpression->deleteLater();
m_functionExpression = nullptr;
emit functionsAdded(addedFuncs);
emit functionsRemoved(removedFuncs);
}
MaximaSession* MaximaVariableModel::maximaSession()
......
......@@ -36,8 +36,6 @@ class MaximaVariableModel : public Cantor::DefaultVariableModel
explicit MaximaVariableModel( MaximaSession* session);
~MaximaVariableModel() override = default;
void clearFunctions();
QStringList functionNames(bool stripParameters=false);
void update() override;
......@@ -46,10 +44,6 @@ class MaximaVariableModel : public Cantor::DefaultVariableModel
void parseNewVariables(Cantor::Expression::Status status);
void parseNewFunctions(Cantor::Expression::Status status);
Q_SIGNALS:
void functionsAdded(const QStringList funcs);
void functionsRemoved(const QStringList funcs);
private:
MaximaSession* maximaSession();
......
......@@ -31,6 +31,7 @@ class DefaultVariableModelPrivate
{
public:
QList<DefaultVariableModel::Variable> variables;
QStringList functions;
Session* session;
VariableManagementExtension* extension;
......@@ -190,6 +191,14 @@ void DefaultVariableModel::clearVariables()
emit variablesRemoved(names);
}
void DefaultVariableModel::clearFunctions()
{
Q_D(DefaultVariableModel);
QStringList names = d->functions;
d->functions.clear();
emit functionsRemoved(names);
}
void DefaultVariableModel::setVariables(const QList<DefaultVariableModel::Variable>& newVars)
{
Q_D(DefaultVariableModel);
......@@ -253,6 +262,47 @@ void DefaultVariableModel::setVariables(const QList<DefaultVariableModel::Variab
emit variablesRemoved(removedVars);
}
void DefaultVariableModel::setFunctions(const QStringList& newFuncs)
{
Q_D(DefaultVariableModel);
QStringList addedFuncs;
QStringList removedFuncs;
//remove the old variables
int i = 0;
while (i < d->functions.size())
{
//check if this var is present in the new variables
bool found=false;
for (const QString& func : newFuncs)
if(d->functions[i] == func)
{
found=true;
break;
}
if(!found)
{
removedFuncs<<d->functions[i];
d->functions.removeAt(i);
}
else
i++;
}
for (const QString& func : newFuncs)
{
if (!d->functions.contains(func))
{
addedFuncs<<func;
d->functions.append(func);
}
}
emit functionsAdded(addedFuncs);
emit functionsRemoved(removedFuncs);
}
Session* DefaultVariableModel::session() const
{
Q_D(const DefaultVariableModel);
......
......@@ -129,6 +129,12 @@ public Q_SLOTS:
*/
void clearVariables();
/**
* Clears all functions
*/
void clearFunctions();
Q_SIGNALS:
/**
* Emitted after adding new variables
......@@ -142,6 +148,16 @@ Q_SIGNALS:
*/
void variablesRemoved(const QStringList& variables);
/**
* Similar to @c variablesAdded
*/
void functionsAdded(const QStringList& names);
/**
* Similar to @c variablesRemoved
*/
void functionsRemoved(const QStringList funcs);
protected:
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
......@@ -152,6 +168,7 @@ protected:
Qt::ItemFlags flags(const QModelIndex& index) const override;
void setVariables(const QList<DefaultVariableModel::Variable>& newVars);
void setFunctions(const QStringList& newFuns);
enum Column
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment