PY-83395: Add registry keys to tweak python root detection

GitOrigin-RevId: 9615ca048c30da26d0bed1a4c26ac7e7ea4e2b70
This commit is contained in:
Ilia Zakoulov
2025-11-26 18:31:24 +00:00
committed by intellij-monorepo-bot
parent cc6625402e
commit 6061cfc7d6
3 changed files with 20 additions and 2 deletions
@@ -620,6 +620,10 @@
key="python.sdk.installation.notification.group"/>
<statistics.notificationIdsHolder implementation="com.jetbrains.python.statistics.PythonInterpreterInstallationIdsHolder"/>
<!-- Source root detection -->
<registryKey key="python.source.root.detection.enabled" defaultValue="true" description="Enables source root detection"/>
<registryKey key="python.source.root.suggest.quickfix" defaultValue="true" description="Suggests to mark a detected source root via quick-fix for the red import"/>
<registryKey key="python.source.root.suggest.notification" defaultValue="true" description="Suggests to mark a detected source root via in-editor notification"/>
<notificationGroup id="Python source root detection" displayType="STICKY_BALLOON" isLogByDefault="true" bundle="messages.PyBundle"
key="python.source.root.detection.confirm.notification.group"/>
<editorNotificationProvider implementation="com.jetbrains.python.module.PySourceRootDetectedNotificationPanel"/>
@@ -16,6 +16,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.profile.codeInspection.InspectionProjectProfileManager
import com.intellij.psi.PsiElement
@@ -118,6 +119,9 @@ class PyUnresolvedReferencesInspection : PyUnresolvedReferencesInspectionBase()
}
override fun getAddSourceRootQuickFix(node: PyElement): LocalQuickFix? {
if (!Registry.`is`("python.source.root.detection.enabled")) {
return null
}
val importStatementBase = PsiTreeUtil.getParentOfType(node, PyAstImportStatementBase::class.java, true)
?: return null
@@ -159,6 +163,9 @@ class PyUnresolvedReferencesInspection : PyUnresolvedReferencesInspectionBase()
val resolveResult: List<PsiElement> = resolveInRoot(qname, containingDirectory, context)
if (!resolveResult.isEmpty()) {
project.getService(PySourceRootDetectionService::class.java).onSourceRootDetected(containingDirectory)
if (!Registry.`is`("python.source.root.suggest.quickfix") || !Registry.`is`("python.source.root.detection.enabled")) {
return null
}
return PyMarkDirectoryAsSourceRootQuickFix(project, containingDirectory)
}
}
@@ -4,6 +4,7 @@ package com.jetbrains.python.module
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.impl.text.TextEditorImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.EditorNotificationProvider
@@ -14,10 +15,16 @@ import java.util.function.Function
import javax.swing.JComponent
internal class PySourceRootDetectedNotificationPanel : EditorNotificationProvider {
override fun collectNotificationData(project: Project, file: VirtualFile): Function<in FileEditor, out JComponent?> {
override fun collectNotificationData(project: Project, file: VirtualFile): Function<in FileEditor, out JComponent?>? {
val isEnabled = Registry.`is`("python.source.root.suggest.notification") && Registry.`is`("python.source.root.detection.enabled")
if (!isEnabled) {
return null
}
if (file.fileType != PythonFileType.INSTANCE) {
return null
}
return Function { editor ->
when {
file.fileType != PythonFileType.INSTANCE -> return@Function null
editor !is TextEditorImpl -> return@Function null
}
val pySourceRootDetectionService = project.getService(PySourceRootDetectionService::class.java)