forked from qt-creator/qt-creator
PythonEditor: Modernize/Streamline
Remove virtual, add override, add = delete, remove QLatin1*, flatten filesystem hierarchy, remove unneeded 'inline', use initializer list, etc. Change-Id: I0a95d806d73ca1a33cfd1ba85c2664d9ebc32690 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
#include "pythoneditorplugin.h"
|
||||
#include "pythoneditor.h"
|
||||
#include "pythoneditorconstants.h"
|
||||
#include "tools/pythonhighlighter.h"
|
||||
#include "pythonhighlighter.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <coreplugin/coreconstants.h>
|
||||
@@ -65,139 +65,6 @@ using namespace ProjectExplorer;
|
||||
using namespace PythonEditor::Constants;
|
||||
using namespace Utils;
|
||||
|
||||
/*******************************************************************************
|
||||
* List of Python keywords (includes "print" that isn't keyword in python 3
|
||||
******************************************************************************/
|
||||
static const char *const LIST_OF_PYTHON_KEYWORDS[] = {
|
||||
"and",
|
||||
"as",
|
||||
"assert",
|
||||
"break",
|
||||
"class",
|
||||
"continue",
|
||||
"def",
|
||||
"del",
|
||||
"elif",
|
||||
"else",
|
||||
"except",
|
||||
"exec",
|
||||
"finally",
|
||||
"for",
|
||||
"from",
|
||||
"global",
|
||||
"if",
|
||||
"import",
|
||||
"in",
|
||||
"is",
|
||||
"lambda",
|
||||
"not",
|
||||
"or",
|
||||
"pass",
|
||||
"print",
|
||||
"raise",
|
||||
"return",
|
||||
"try",
|
||||
"while",
|
||||
"with",
|
||||
"yield"
|
||||
};
|
||||
|
||||
/*******************************************************************************
|
||||
* List of Python magic methods and attributes
|
||||
******************************************************************************/
|
||||
static const char *const LIST_OF_PYTHON_MAGICS[] = {
|
||||
// ctor & dtor
|
||||
"__init__",
|
||||
"__del__",
|
||||
// string conversion functions
|
||||
"__str__",
|
||||
"__repr__",
|
||||
"__unicode__",
|
||||
// attribute access functions
|
||||
"__setattr__",
|
||||
"__getattr__",
|
||||
"__delattr__",
|
||||
// binary operators
|
||||
"__add__",
|
||||
"__sub__",
|
||||
"__mul__",
|
||||
"__truediv__",
|
||||
"__floordiv__",
|
||||
"__mod__",
|
||||
"__pow__",
|
||||
"__and__",
|
||||
"__or__",
|
||||
"__xor__",
|
||||
"__eq__",
|
||||
"__ne__",
|
||||
"__gt__",
|
||||
"__lt__",
|
||||
"__ge__",
|
||||
"__le__",
|
||||
"__lshift__",
|
||||
"__rshift__",
|
||||
"__contains__",
|
||||
// unary operators
|
||||
"__pos__",
|
||||
"__neg__",
|
||||
"__inv__",
|
||||
"__abs__",
|
||||
"__len__",
|
||||
// item operators like []
|
||||
"__getitem__",
|
||||
"__setitem__",
|
||||
"__delitem__",
|
||||
"__getslice__",
|
||||
"__setslice__",
|
||||
"__delslice__",
|
||||
// other functions
|
||||
"__cmp__",
|
||||
"__hash__",
|
||||
"__nonzero__",
|
||||
"__call__",
|
||||
"__iter__",
|
||||
"__reversed__",
|
||||
"__divmod__",
|
||||
"__int__",
|
||||
"__long__",
|
||||
"__float__",
|
||||
"__complex__",
|
||||
"__hex__",
|
||||
"__oct__",
|
||||
"__index__",
|
||||
"__copy__",
|
||||
"__deepcopy__",
|
||||
"__sizeof__",
|
||||
"__trunc__",
|
||||
"__format__",
|
||||
// magic attributes
|
||||
"__name__",
|
||||
"__module__",
|
||||
"__dict__",
|
||||
"__bases__",
|
||||
"__doc__"
|
||||
};
|
||||
|
||||
/*******************************************************************************
|
||||
* List of python built-in functions and objects
|
||||
******************************************************************************/
|
||||
static const char *const LIST_OF_PYTHON_BUILTINS[] = {
|
||||
"range",
|
||||
"xrange",
|
||||
"int",
|
||||
"float",
|
||||
"long",
|
||||
"hex",
|
||||
"oct"
|
||||
"chr",
|
||||
"ord",
|
||||
"len",
|
||||
"abs",
|
||||
"None",
|
||||
"True",
|
||||
"False"
|
||||
};
|
||||
|
||||
namespace PythonEditor {
|
||||
namespace Internal {
|
||||
|
||||
@@ -275,7 +142,7 @@ public:
|
||||
PythonProjectFile(PythonProject *parent, QString fileName) : m_project(parent)
|
||||
{
|
||||
setId("Generic.ProjectFile");
|
||||
setMimeType(QLatin1String(PythonMimeType));
|
||||
setMimeType(PythonMimeType);
|
||||
setFilePath(FileName::fromString(fileName));
|
||||
}
|
||||
|
||||
@@ -405,12 +272,12 @@ PythonRunConfiguration::PythonRunConfiguration(Target *parent, Core::Id id) :
|
||||
m_enabled(true)
|
||||
{
|
||||
Environment sysEnv = Environment::systemEnvironment();
|
||||
const QString exec = sysEnv.searchInPath(QLatin1String("python")).toString();
|
||||
m_interpreter = exec.isEmpty() ? QLatin1String("python") : exec;
|
||||
const QString exec = sysEnv.searchInPath("python").toString();
|
||||
m_interpreter = exec.isEmpty() ? "python" : exec;
|
||||
|
||||
addExtraAspect(new LocalEnvironmentAspect(this, LocalEnvironmentAspect::BaseEnvironmentModifier()));
|
||||
addExtraAspect(new ArgumentsAspect(this, QStringLiteral("PythonEditor.RunConfiguration.Arguments")));
|
||||
addExtraAspect(new TerminalAspect(this, QStringLiteral("PythonEditor.RunConfiguration.UseTerminal")));
|
||||
addExtraAspect(new ArgumentsAspect(this, "PythonEditor.RunConfiguration.Arguments"));
|
||||
addExtraAspect(new TerminalAspect(this, "PythonEditor.RunConfiguration.UseTerminal"));
|
||||
setDefaultDisplayName(defaultDisplayName());
|
||||
}
|
||||
|
||||
@@ -426,25 +293,23 @@ PythonRunConfiguration::PythonRunConfiguration(Target *parent, PythonRunConfigur
|
||||
QVariantMap PythonRunConfiguration::toMap() const
|
||||
{
|
||||
QVariantMap map(RunConfiguration::toMap());
|
||||
map.insert(QLatin1String(MainScriptKey), m_mainScript);
|
||||
map.insert(QLatin1String(InterpreterKey), m_interpreter);
|
||||
map.insert(MainScriptKey, m_mainScript);
|
||||
map.insert(InterpreterKey, m_interpreter);
|
||||
return map;
|
||||
}
|
||||
|
||||
bool PythonRunConfiguration::fromMap(const QVariantMap &map)
|
||||
{
|
||||
m_mainScript = map.value(QLatin1String(MainScriptKey)).toString();
|
||||
m_interpreter = map.value(QLatin1String(InterpreterKey)).toString();
|
||||
m_mainScript = map.value(MainScriptKey).toString();
|
||||
m_interpreter = map.value(InterpreterKey).toString();
|
||||
return RunConfiguration::fromMap(map);
|
||||
}
|
||||
|
||||
QString PythonRunConfiguration::defaultDisplayName() const
|
||||
{
|
||||
QString result = tr("Run %1").arg(m_mainScript);
|
||||
if (!m_enabled) {
|
||||
result += QLatin1Char(' ');
|
||||
result += tr("(disabled)");
|
||||
}
|
||||
if (!m_enabled)
|
||||
result += ' ' + tr("(disabled)");
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -527,7 +392,7 @@ class PythonRunConfigurationFactory : public IRunConfigurationFactory
|
||||
public:
|
||||
PythonRunConfigurationFactory()
|
||||
{
|
||||
setObjectName(QLatin1String("PythonRunConfigurationFactory"));
|
||||
setObjectName("PythonRunConfigurationFactory");
|
||||
}
|
||||
|
||||
QList<Core::Id> availableCreationIds(Target *parent, CreationMode mode) const override
|
||||
@@ -655,7 +520,7 @@ bool PythonProject::saveRawList(const QStringList &rawList, const QString &fileN
|
||||
if (!saver.hasError()) {
|
||||
QTextStream stream(saver.file());
|
||||
foreach (const QString &filePath, rawList)
|
||||
stream << filePath << QLatin1Char('\n');
|
||||
stream << filePath << '\n';
|
||||
saver.setResult(&stream);
|
||||
}
|
||||
bool result = saver.finalize(ICore::mainWindow());
|
||||
@@ -834,7 +699,7 @@ Project::RestoreResult PythonProject::fromMap(const QVariantMap &map, QString *e
|
||||
const QList<RunConfiguration *> runConfigs = t->runConfigurations();
|
||||
foreach (const QString &file, m_files) {
|
||||
// skip the 'project' file
|
||||
if (file.endsWith(QLatin1String(".pyqtc")))
|
||||
if (file.endsWith(".pyqtc"))
|
||||
continue;
|
||||
const Id id = idFromScript(file);
|
||||
bool alreadyPresent = false;
|
||||
@@ -875,7 +740,7 @@ QHash<QString, QStringList> sortFilesIntoPaths(const QString &base, const QSet<Q
|
||||
} else {
|
||||
// 'file' is not part of the project.
|
||||
relativeFilePath = baseDir.relativeFilePath(absoluteFilePath.toString());
|
||||
if (relativeFilePath.endsWith(QLatin1Char('/')))
|
||||
if (relativeFilePath.endsWith('/'))
|
||||
relativeFilePath.chop(1);
|
||||
}
|
||||
|
||||
@@ -908,7 +773,7 @@ void PythonProjectNode::refresh(QSet<QString> oldFileList)
|
||||
const QString &filePath = it.key();
|
||||
QStringList components;
|
||||
if (!filePath.isEmpty())
|
||||
components = filePath.split(QLatin1Char('/'));
|
||||
components = filePath.split('/');
|
||||
FolderNode *folder = findFolderByName(components, components.size());
|
||||
if (!folder)
|
||||
folder = createFolderByName(components, components.size());
|
||||
@@ -916,7 +781,7 @@ void PythonProjectNode::refresh(QSet<QString> oldFileList)
|
||||
QList<FileNode *> fileNodes;
|
||||
foreach (const QString &file, it.value()) {
|
||||
FileType fileType = SourceType; // ### FIXME
|
||||
if (file.endsWith(QLatin1String(".qrc")))
|
||||
if (file.endsWith(".qrc"))
|
||||
fileType = ResourceType;
|
||||
FileNode *fileNode = new FileNode(FileName::fromString(file),
|
||||
fileType, /*generated = */ false);
|
||||
@@ -932,7 +797,7 @@ void PythonProjectNode::refresh(QSet<QString> oldFileList)
|
||||
const QString &filePath = it.key();
|
||||
QStringList components;
|
||||
if (!filePath.isEmpty())
|
||||
components = filePath.split(QLatin1Char('/'));
|
||||
components = filePath.split('/');
|
||||
FolderNode *folder = findFolderByName(components, components.size());
|
||||
|
||||
QList<FileNode *> fileNodes;
|
||||
@@ -968,7 +833,7 @@ FolderNode *PythonProjectNode::createFolderByName(const QStringList &components,
|
||||
QString folderName;
|
||||
for (int i = 0; i < end; ++i) {
|
||||
folderName.append(components.at(i));
|
||||
folderName += QLatin1Char('/');
|
||||
folderName += '/';
|
||||
}
|
||||
|
||||
const QString component = components.at(end - 1);
|
||||
@@ -993,7 +858,7 @@ FolderNode *PythonProjectNode::findFolderByName(const QStringList &components, i
|
||||
QString folderName;
|
||||
for (int i = 0; i < end; ++i) {
|
||||
folderName.append(components.at(i));
|
||||
folderName += QLatin1Char('/');
|
||||
folderName += '/';
|
||||
}
|
||||
|
||||
FolderNode *parent = findFolderByName(components, end - 1);
|
||||
@@ -1003,7 +868,7 @@ FolderNode *PythonProjectNode::findFolderByName(const QStringList &components, i
|
||||
|
||||
const QString baseDir = filePath().toFileInfo().path();
|
||||
foreach (FolderNode *fn, parent->subFolderNodes()) {
|
||||
if (fn->filePath().toString() == baseDir + QLatin1Char('/') + folderName)
|
||||
if (fn->filePath().toString() == baseDir + '/' + folderName)
|
||||
return fn;
|
||||
}
|
||||
return 0;
|
||||
@@ -1080,15 +945,15 @@ void PythonRunControl::start()
|
||||
{
|
||||
emit started();
|
||||
if (m_interpreter.isEmpty()) {
|
||||
appendMessage(tr("No Python interpreter specified.") + QLatin1Char('\n'), Utils::ErrorMessageFormat);
|
||||
appendMessage(tr("No Python interpreter specified.") + '\n', Utils::ErrorMessageFormat);
|
||||
emit finished();
|
||||
} else if (!QFileInfo::exists(m_interpreter)) {
|
||||
appendMessage(tr("Python interpreter %1 does not exist.").arg(QDir::toNativeSeparators(m_interpreter)) + QLatin1Char('\n'),
|
||||
appendMessage(tr("Python interpreter %1 does not exist.").arg(QDir::toNativeSeparators(m_interpreter)) + '\n',
|
||||
Utils::ErrorMessageFormat);
|
||||
emit finished();
|
||||
} else {
|
||||
m_running = true;
|
||||
QString msg = tr("Starting %1...").arg(QDir::toNativeSeparators(m_interpreter)) + QLatin1Char('\n');
|
||||
QString msg = tr("Starting %1...").arg(QDir::toNativeSeparators(m_interpreter)) + '\n';
|
||||
appendMessage(msg, Utils::NormalMessageFormat);
|
||||
|
||||
StandardRunnable r;
|
||||
@@ -1132,7 +997,7 @@ void PythonRunControl::processExited(int exitCode, QProcess::ExitStatus status)
|
||||
msg = tr("%1 exited with code %2")
|
||||
.arg(QDir::toNativeSeparators(m_interpreter)).arg(exitCode);
|
||||
}
|
||||
appendMessage(msg + QLatin1Char('\n'), Utils::NormalMessageFormat);
|
||||
appendMessage(msg + '\n', Utils::NormalMessageFormat);
|
||||
emit finished();
|
||||
}
|
||||
|
||||
@@ -1149,20 +1014,9 @@ void PythonRunConfigurationWidget::setInterpreter(const QString &interpreter)
|
||||
|
||||
static PythonEditorPlugin *m_instance = 0;
|
||||
|
||||
/// Copies identifiers from array to QSet
|
||||
static void copyIdentifiers(const char * const words[], size_t bytesCount, QSet<QString> &result)
|
||||
{
|
||||
const size_t count = bytesCount / sizeof(const char * const);
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
result.insert(QLatin1String(words[i]));
|
||||
}
|
||||
|
||||
PythonEditorPlugin::PythonEditorPlugin()
|
||||
{
|
||||
m_instance = this;
|
||||
copyIdentifiers(LIST_OF_PYTHON_KEYWORDS, sizeof(LIST_OF_PYTHON_KEYWORDS), m_keywords);
|
||||
copyIdentifiers(LIST_OF_PYTHON_MAGICS, sizeof(LIST_OF_PYTHON_MAGICS), m_magics);
|
||||
copyIdentifiers(LIST_OF_PYTHON_BUILTINS, sizeof(LIST_OF_PYTHON_BUILTINS), m_builtins);
|
||||
}
|
||||
|
||||
PythonEditorPlugin::~PythonEditorPlugin()
|
||||
@@ -1175,7 +1029,7 @@ bool PythonEditorPlugin::initialize(const QStringList &arguments, QString *error
|
||||
Q_UNUSED(arguments)
|
||||
Q_UNUSED(errorMessage)
|
||||
|
||||
MimeDatabase::addMimeTypes(QLatin1String(":/pythoneditor/PythonEditor.mimetypes.xml"));
|
||||
MimeDatabase::addMimeTypes(":/pythoneditor/PythonEditor.mimetypes.xml");
|
||||
|
||||
addAutoReleasedObject(new PythonProjectManager);
|
||||
addAutoReleasedObject(new PythonEditorFactory);
|
||||
@@ -1184,28 +1038,13 @@ bool PythonEditorPlugin::initialize(const QStringList &arguments, QString *error
|
||||
|
||||
// Initialize editor actions handler
|
||||
// Add MIME overlay icons (these icons displayed at Project dock panel)
|
||||
const QIcon icon = QIcon::fromTheme(QLatin1String(C_PY_MIME_ICON));
|
||||
const QIcon icon = QIcon::fromTheme(C_PY_MIME_ICON);
|
||||
if (!icon.isNull())
|
||||
Core::FileIconProvider::registerIconOverlayForMimeType(icon, C_PY_MIMETYPE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QSet<QString> PythonEditorPlugin::keywords()
|
||||
{
|
||||
return m_instance->m_keywords;
|
||||
}
|
||||
|
||||
QSet<QString> PythonEditorPlugin::magics()
|
||||
{
|
||||
return m_instance->m_magics;
|
||||
}
|
||||
|
||||
QSet<QString> PythonEditorPlugin::builtins()
|
||||
{
|
||||
return m_instance->m_builtins;
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace PythonEditor
|
||||
|
||||
|
||||
Reference in New Issue
Block a user