mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-18 12:31:26 +07:00
Update configuring python interpreter for idea + plugin (QD-570)
Main change: setup detected sdk. Iterate through modules and configure python for all of them instead of doing so file by file. GitOrigin-RevId: 6a19eeaa42aa78e1b959a413e2533ad33fca810b
This commit is contained in:
committed by
intellij-monorepo-bot
parent
ee1a69647d
commit
52b113935a
@@ -1,29 +1,27 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.jetbrains.python.inspections;
|
||||
|
||||
import com.intellij.analysis.AnalysisScope;
|
||||
import com.intellij.facet.FacetManager;
|
||||
import com.intellij.ide.CommandLineInspectionProgressReporter;
|
||||
import com.intellij.ide.CommandLineInspectionProjectConfigurator;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.fileTypes.FileTypeRegistry;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.module.ModuleManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.ProjectJdkTable;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PythonFileType;
|
||||
import com.jetbrains.python.facet.PythonFacetType;
|
||||
import com.jetbrains.python.sdk.PyDetectedSdk;
|
||||
import com.jetbrains.python.sdk.PySdkExtKt;
|
||||
import com.jetbrains.python.sdk.PythonSdkUpdater;
|
||||
import com.jetbrains.python.sdk.PythonSdkUtil;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PythonPluginCommandLineInspectionProjectConfigurator implements CommandLineInspectionProjectConfigurator {
|
||||
@@ -39,18 +37,17 @@ public class PythonPluginCommandLineInspectionProjectConfigurator implements Com
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull ConfiguratorContext context) {
|
||||
List<Sdk> sdks = PythonSdkUtil.getAllSdks();
|
||||
if (!sdks.isEmpty()) return false;
|
||||
|
||||
try {
|
||||
boolean hasAnyPythonFiles = Files.walk(context.getProjectPath()).anyMatch(f -> {
|
||||
return f.toString().endsWith(".py");
|
||||
});
|
||||
if (!hasAnyPythonFiles) {
|
||||
context.getLogger().reportMessage(3, "Skipping Python interpreter autodetection because the project doesn't contain any Python files");
|
||||
}
|
||||
try (var stream = Files.walk(context.getProjectPath())) {
|
||||
boolean hasAnyPythonFiles = stream.anyMatch(f -> f.toString().endsWith(".py"));
|
||||
|
||||
return hasAnyPythonFiles;
|
||||
if (!hasAnyPythonFiles) {
|
||||
context.getLogger()
|
||||
.reportMessage(3, "Skipping Python interpreter configuration because the project doesn't contain any Python files");
|
||||
}
|
||||
|
||||
return hasAnyPythonFiles;
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
return false;
|
||||
@@ -59,52 +56,74 @@ public class PythonPluginCommandLineInspectionProjectConfigurator implements Com
|
||||
|
||||
@Override
|
||||
public void configureEnvironment(@NotNull ConfiguratorContext context) {
|
||||
CommandLineInspectionProgressReporter logger = context.getLogger();
|
||||
final CommandLineInspectionProgressReporter logger = context.getLogger();
|
||||
logger.reportMessage(3, "Python environment configuration...");
|
||||
List<Sdk> sdks = PythonSdkUtil.getAllSdks();
|
||||
logger.reportMessage(3, "Python interpreters detected:");
|
||||
|
||||
final List<Sdk> sdks = PythonSdkUtil.getAllSdks();
|
||||
logSdks(logger, sdks, "Previously used Python interpreters:");
|
||||
|
||||
if (sdks.isEmpty()) {
|
||||
logger.reportMessage(3, "No previously used Python interpreters, detecting...");
|
||||
|
||||
final List<Sdk> detectedSdks = PySdkExtKt.findAllPythonSdks(context.getProjectPath());
|
||||
logSdks(logger, detectedSdks, "Python interpreters detected:");
|
||||
|
||||
if (!detectedSdks.isEmpty()) {
|
||||
final Sdk detectedSdk = detectedSdks.get(0);
|
||||
final Sdk sdk = configureSdk(detectedSdk);
|
||||
if (sdk != null) {
|
||||
logger.reportMessage(3, "Python interpreter has been configured: " + sdk.getHomePath());
|
||||
}
|
||||
else {
|
||||
logger.reportMessage(1, "Can't configure Python interpreter: " + detectedSdk.getHomePath());
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.reportMessage(1, "Can't find Python interpreter");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void logSdks(@NotNull CommandLineInspectionProgressReporter logger, @NotNull List<Sdk> sdks, @NotNull String prefix) {
|
||||
logger.reportMessage(3, prefix);
|
||||
for (Sdk sdk : sdks) {
|
||||
logger.reportMessage(3, sdk.getHomePath());
|
||||
}
|
||||
if (sdks.isEmpty()) {
|
||||
final List<Sdk> detectedSdks = PySdkExtKt.findAllPythonSdks(context.getProjectPath());
|
||||
}
|
||||
|
||||
if (detectedSdks.size() > 0) {
|
||||
for (Sdk sdk : detectedSdks) {
|
||||
logger.reportMessage(3, sdk.getHomePath());
|
||||
}
|
||||
final Sdk sdk = detectedSdks.get(0);
|
||||
WriteAction.runAndWait(() -> {
|
||||
logger.reportMessage(1, "Settings up interpreter " + sdk.getName());
|
||||
ProjectJdkTable.getInstance().addJdk(sdk);
|
||||
});
|
||||
private static @Nullable Sdk configureSdk(@NotNull Sdk detectedSdk) {
|
||||
final Sdk sdk = detectedSdk instanceof PyDetectedSdk
|
||||
? PySdkExtKt.setup((PyDetectedSdk)detectedSdk, Arrays.asList(ProjectJdkTable.getInstance().getAllJdks()))
|
||||
: detectedSdk;
|
||||
|
||||
PythonSdkUpdater.updateVersionAndPathsSynchronouslyAndScheduleRemaining(sdk, null);
|
||||
}
|
||||
else {
|
||||
logger.reportMessage(1, "ERROR: Can't find Python interpreter");
|
||||
}
|
||||
if (sdk != null) {
|
||||
WriteAction.runAndWait(() -> ProjectJdkTable.getInstance().addJdk(sdk));
|
||||
}
|
||||
|
||||
return sdk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureProject(@NotNull Project project, @NotNull ConfiguratorContext context) {
|
||||
List<Sdk> sdks = PythonSdkUtil.getAllSdks();
|
||||
if (sdks.isEmpty()) return;
|
||||
final CommandLineInspectionProgressReporter logger = context.getLogger();
|
||||
|
||||
AnalysisScope scope = context.getAnalyzerScope();
|
||||
if (scope == null) return;
|
||||
if (PythonSdkUtil.getAllSdks().isEmpty()) {
|
||||
logger.reportMessage(1, "Python sdks are empty");
|
||||
return;
|
||||
}
|
||||
|
||||
PythonFacetType facetType = PythonFacetType.getInstance();
|
||||
for (VirtualFile f : scope.getFiles()) {
|
||||
if (FileTypeRegistry.getInstance().isFileOfType(f, PythonFileType.INSTANCE)) {
|
||||
final PythonFacetType facetType = PythonFacetType.getInstance();
|
||||
for (Module m : ModuleManager.getInstance(project).getModules()) {
|
||||
final FacetManager facetManager = FacetManager.getInstance(m);
|
||||
|
||||
Module m = ModuleUtilCore.findModuleForFile(f, project);
|
||||
if (m != null && FacetManager.getInstance(m).getFacetByType(facetType.getId()) == null) {
|
||||
WriteAction.runAndWait(() -> {
|
||||
FacetManager.getInstance(m).addFacet(facetType, facetType.getPresentableName(), null);
|
||||
});
|
||||
}
|
||||
final var facet = facetManager.getFacetByType(facetType.getId());
|
||||
if (facet == null) {
|
||||
logger.reportMessage(3, "Setting Python facet for: " + m.getName());
|
||||
|
||||
WriteAction.runAndWait(() -> facetManager.addFacet(facetType, facetType.getPresentableName(), null));
|
||||
}
|
||||
else {
|
||||
logger.reportMessage(3, "Python facet already here: " + m.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user