[python] Allow modification of python SDK in headless mode for IDEA

GitOrigin-RevId: c2ed190ada73e46752f4135f864cf279c3c1f2ca
This commit is contained in:
Konstantin Nisht
2023-09-30 16:08:05 +02:00
committed by intellij-monorepo-bot
parent 7d9f718f82
commit 5011269a9e
3 changed files with 35 additions and 7 deletions

View File

@@ -24,5 +24,6 @@
<typeProvider implementation="com.jetbrains.python.psi.impl.PyJavaTypeProvider"/>
<pySuperMethodsSearch implementation="com.jetbrains.python.psi.impl.PyJavaSuperMethodsSearchExecutor"/>
<importCandidateProvider implementation="com.jetbrains.python.psi.impl.PyJavaImportCandidateProvider"/>
<pythonHeadlessSdkModifier implementation="com.jetbrains.python.inspections.PythonIdeaHeadlessSdkModifier"/>
</extensions>
</idea-plugin>

View File

@@ -0,0 +1,14 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.python.inspections
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.jetbrains.python.sdk.headless.PythonHeadlessSdkModifier
class PythonIdeaHeadlessSdkModifier : PythonHeadlessSdkModifier {
override fun setSdk(project: Project, sdk: Sdk): Boolean {
PythonPluginCommandLineInspectionProjectConfigurator.configurePythonSdkForAllPythonModules(project, sdk, null)
return true
}
}

View File

@@ -1,4 +1,4 @@
// 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.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.python.inspections;
import com.intellij.facet.FacetManager;
@@ -117,6 +117,10 @@ public class PythonPluginCommandLineInspectionProjectConfigurator implements Com
return;
}
configurePythonSdkForAllPythonModules(project, null, logger);
}
static void configurePythonSdkForAllPythonModules(@NotNull Project project, @Nullable Sdk sdk, @Nullable CommandLineInspectionProgressReporter logger) {
final PythonFacetType facetType = PythonFacetType.getInstance();
int skippedModules = 0;
for (Module m : ModuleManager.getInstance(project).getModules()) {
@@ -129,24 +133,33 @@ public class PythonPluginCommandLineInspectionProjectConfigurator implements Com
final var facet = facetManager.getFacetByType(facetType.getId());
if (facet == null) {
logger.reportMessage(3, "Setting Python facet for: " + m.getName());
if (logger != null) {
logger.reportMessage(3, "Setting Python facet for: " + m.getName());
}
invokeLaterOnWriteThreadUnderLock(
() -> {
final PythonFacet addedFacet = facetManager.addFacet(facetType, facetType.getPresentableName(), null);
if (sdk != null) {
addedFacet.getConfiguration().setSdk(sdk);
}
PySdkExtKt.excludeInnerVirtualEnv(m, addedFacet.getConfiguration().getSdk());
}
);
}
else {
logger.reportMessage(3, "Python facet already here: " + m.getName());
if (logger != null) {
logger.reportMessage(3, "Python facet already here: " + m.getName());
}
}
}
logger.reportMessage(
3,
"Skipped Python interpreter configuration for " + skippedModules + " module(s) because they don't contain any Python files"
);
if (logger != null) {
logger.reportMessage(
3,
"Skipped Python interpreter configuration for " + skippedModules + " module(s) because they don't contain any Python files"
);
}
}
private static void invokeLaterOnWriteThreadUnderLock(@NotNull Runnable runnable) {