Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 73f2024d authored by Nikita Sirgienko's avatar Nikita Sirgienko

[Python] Fix bug with 'print' highlighting (function in Python3, keyword in Python2)

parent afb5a630
......@@ -24,7 +24,7 @@
#include <QTextEdit>
#include <QDebug>
PythonHighlighter::PythonHighlighter(QObject* parent) : Cantor::DefaultHighlighter(parent)
PythonHighlighter::PythonHighlighter(QObject* parent, const int pythonVersion) : Cantor::DefaultHighlighter(parent)
{
qDebug() << "PythonHighlighter constructor";
addRule(QRegExp(QLatin1String("\\b\\w+(?=\\()")), functionFormat());
......@@ -33,6 +33,12 @@ PythonHighlighter::PythonHighlighter(QObject* parent) : Cantor::DefaultHighlight
addKeywords(PythonKeywords::instance()->keywords());
addFunctions(PythonKeywords::instance()->functions());
addVariables(PythonKeywords::instance()->variables());
if (pythonVersion == 2)
{
removeRule(QLatin1String("print"));
addRule(QLatin1String("print"), keywordFormat());
}
}
void PythonHighlighter::highlightBlock(const QString &text)
......
......@@ -28,7 +28,7 @@ class PythonHighlighter : public Cantor::DefaultHighlighter
Q_OBJECT
public:
explicit PythonHighlighter(QObject* parent);
explicit PythonHighlighter(QObject* parent, const int pythonVersion);
~PythonHighlighter() override = default;
public Q_SLOTS:
......
......@@ -82,16 +82,6 @@ void PythonKeywords::loadFromModule(const QString& module, const QStringList& ke
}
}
void PythonKeywords::python2Mode()
{
// In Python 2 print is keyword, not function
m_functions.removeOne(QLatin1String("print"));
m_keywords << QLatin1String("print");
qSort(m_keywords);
qSort(m_functions);
}
const QStringList& PythonKeywords::variables() const
{
return m_variables;
......
......@@ -37,8 +37,6 @@ class PythonKeywords
void loadFromModule(const QString& module, const QStringList& keywords);
void python2Mode();
private:
void loadKeywords();
......
......@@ -49,9 +49,8 @@ PythonSession::PythonSession(Cantor::Backend* backend, int pythonVersion, const
, m_pProcess(nullptr)
, serverName(serverName)
, DbusChannelName(DbusChannelName)
, m_pythonVersion(pythonVersion)
{
if (pythonVersion == 2)
PythonKeywords::instance()->python2Mode();
}
void PythonSession::login()
......@@ -381,7 +380,7 @@ void PythonSession::listVariables()
QSyntaxHighlighter* PythonSession::syntaxHighlighter(QObject* parent)
{
PythonHighlighter* highlighter = new PythonHighlighter(parent);
PythonHighlighter* highlighter = new PythonHighlighter(parent, m_pythonVersion);
QObject::connect(this, SIGNAL(updateHighlighter()), highlighter, SLOT(updateHighlight()));
QObject::connect(this, SIGNAL(newVariable(QString)), highlighter, SLOT(addVariable(QString)));
connect(this, &PythonSession::clearVariables, highlighter, &PythonHighlighter::clearVariables);
......
......@@ -69,6 +69,7 @@ class CANTOR_PYTHONBACKEND_EXPORT PythonSession : public Cantor::Session
QString DbusChannelName;
QString worksheetPath;
int m_pythonVersion;
protected:
QString m_output;
......
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