summaryrefslogtreecommitdiff
path: root/src/plugins/python/pythonutils.cpp
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2020-03-10 16:26:20 +0100
committerEike Ziller <eike.ziller@qt.io>2020-03-19 13:04:48 +0000
commit3b5fc296f26b9fa093d0d332654e6937fff7a132 (patch)
treece4c360d830e3098fe98f09ac3be293e2b511095 /src/plugins/python/pythonutils.cpp
parentbe9d5fa0d9524b6d0968f5a8f271d12a355b7294 (diff)
downloadqt-creator-3b5fc296f26b9fa093d0d332654e6937fff7a132.tar.gz
Python editor: Add buttons & actions for opening REPL
Opens interactive Python, optionally with the current file imported, for testing and experimentation. Change-Id: Ieb120e3698bdba77a1445c40fe7fda533773a0cf Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/python/pythonutils.cpp')
-rw-r--r--src/plugins/python/pythonutils.cpp57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/plugins/python/pythonutils.cpp b/src/plugins/python/pythonutils.cpp
index e584a90e4d..de52e9413a 100644
--- a/src/plugins/python/pythonutils.cpp
+++ b/src/plugins/python/pythonutils.cpp
@@ -43,6 +43,8 @@
#include <texteditor/textdocument.h>
+#include <utils/consoleprocess.h>
+#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
@@ -170,8 +172,10 @@ static FilePath detectPython(const FilePath &documentPath)
{
FilePath python;
- PythonProject *project = qobject_cast<PythonProject *>(
- ProjectExplorer::SessionManager::projectForFile(documentPath));
+ PythonProject *project = documentPath.isEmpty()
+ ? nullptr
+ : qobject_cast<PythonProject *>(
+ ProjectExplorer::SessionManager::projectForFile(documentPath));
if (!project)
project = qobject_cast<PythonProject *>(ProjectExplorer::SessionManager::startupProject());
@@ -480,6 +484,55 @@ PyLSConfigureAssistant::PyLSConfigureAssistant(QObject *parent)
});
}
+static QStringList replImportArgs(const FilePath &pythonFile, ReplType type)
+{
+ using MimeTypes = QList<MimeType>;
+ const MimeTypes mimeTypes = pythonFile.isEmpty() || type == ReplType::Unmodified
+ ? MimeTypes()
+ : mimeTypesForFileName(pythonFile.toString());
+ const bool isPython = Utils::anyOf(mimeTypes, [](const MimeType &mt) {
+ return mt.inherits("text/x-python") || mt.inherits("text/x-python3");
+ });
+ if (type == ReplType::Unmodified || !isPython)
+ return {};
+ const auto import = type == ReplType::Import
+ ? QString("import %1").arg(pythonFile.toFileInfo().completeBaseName())
+ : QString("from %1 import *")
+ .arg(pythonFile.toFileInfo().completeBaseName());
+ return {"-c", QString("%1; print('Running \"%1\"')").arg(import)};
+}
+
+void openPythonRepl(const FilePath &file, ReplType type)
+{
+ static const auto workingDir = [](const FilePath &file) {
+ if (file.isEmpty()) {
+ if (ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject())
+ return project->projectDirectory().toFileInfo().filePath();
+ return QDir::currentPath();
+ }
+ return file.toFileInfo().path();
+ };
+
+ const auto args = QStringList{"-i"} + replImportArgs(file, type);
+ auto process = new ConsoleProcess;
+ const FilePath pythonCommand = detectPython(file);
+ process->setCommand({pythonCommand, args});
+ process->setWorkingDirectory(workingDir(file));
+ const QString commandLine = process->command().toUserOutput();
+ QObject::connect(process,
+ &ConsoleProcess::processError,
+ process,
+ [process, commandLine](const QString &errorString) {
+ Core::MessageManager::write(
+ QCoreApplication::translate("Python",
+ "Failed to run Python (%1): \"%2\".")
+ .arg(commandLine, errorString));
+ process->deleteLater();
+ });
+ QObject::connect(process, &ConsoleProcess::stubStopped, process, &QObject::deleteLater);
+ process->start();
+}
+
} // namespace Internal
} // namespace Python