Python: simplify detect python for document path

Change-Id: I49b943e8b89db32c11b12ee66c0ad3add9a695af
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2022-06-09 15:21:35 +02:00
parent 3341fa827f
commit 02bfd03c22

View File

@@ -47,8 +47,6 @@ namespace Internal {
FilePath detectPython(const FilePath &documentPath) FilePath detectPython(const FilePath &documentPath)
{ {
FilePath python;
PythonProject *project = documentPath.isEmpty() PythonProject *project = documentPath.isEmpty()
? nullptr ? nullptr
: qobject_cast<PythonProject *>( : qobject_cast<PythonProject *>(
@@ -60,23 +58,21 @@ FilePath detectPython(const FilePath &documentPath)
if (auto target = project->activeTarget()) { if (auto target = project->activeTarget()) {
if (auto runConfig = target->activeRunConfiguration()) { if (auto runConfig = target->activeRunConfiguration()) {
if (auto interpreter = runConfig->aspect<InterpreterAspect>()) if (auto interpreter = runConfig->aspect<InterpreterAspect>())
python = interpreter->currentInterpreter().command; return interpreter->currentInterpreter().command;
} }
} }
} }
// check whether this file is inside a python virtual environment // check whether this file is inside a python virtual environment
QList<Interpreter> venvInterpreters = PythonSettings::detectPythonVenvs(documentPath); const QList<Interpreter> venvInterpreters = PythonSettings::detectPythonVenvs(documentPath);
if (!python.exists() && !venvInterpreters.isEmpty()) if (!venvInterpreters.isEmpty())
python = venvInterpreters.first().command; return venvInterpreters.first().command;
if (!python.exists()) auto defaultInterpreter = PythonSettings::defaultInterpreter().command;
python = PythonSettings::defaultInterpreter().command; if (defaultInterpreter.exists())
return defaultInterpreter;
if (!python.exists() && !PythonSettings::interpreters().isEmpty()) return PythonSettings::interpreters().value(0).command;
python = PythonSettings::interpreters().constFirst().command;
return python;
} }
static QStringList replImportArgs(const FilePath &pythonFile, ReplType type) static QStringList replImportArgs(const FilePath &pythonFile, ReplType type)