File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed
utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -318,7 +318,11 @@ fun getDirectoriesForSysPath(
318
318
if (element != null ) {
319
319
val directory = element.parent
320
320
if (directory is PsiDirectory ) {
321
- importedPaths.add(directory.virtualFile)
321
+ // If we have `import a.b.c` we need to add syspath to module `a` only
322
+ val additionalLevel = importTarget.importedQName?.componentCount?.dec() ? : 0
323
+ directory.topParent(additionalLevel)?.let { dir ->
324
+ importedPaths.add(dir.virtualFile)
325
+ }
322
326
}
323
327
}
324
328
}
@@ -329,7 +333,11 @@ fun getDirectoriesForSysPath(
329
333
importTarget.resolveImportSourceCandidates().forEach {
330
334
val directory = it.parent
331
335
if (directory is PsiDirectory ) {
332
- importedPaths.add(directory.virtualFile)
336
+ // If we have `from a.b.c import d` we need to add syspath to module `a` only
337
+ val additionalLevel = importTarget.importSourceQName?.componentCount?.dec() ? : 0
338
+ directory.topParent(additionalLevel)?.let { dir ->
339
+ importedPaths.add(dir.virtualFile)
340
+ }
333
341
}
334
342
}
335
343
}
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project
4
4
import com.intellij.openapi.roots.ProjectFileIndex
5
5
import com.intellij.openapi.vfs.VfsUtil
6
6
import com.intellij.openapi.vfs.VirtualFile
7
+ import com.intellij.psi.PsiDirectory
7
8
import com.intellij.psi.PsiElement
8
9
import com.jetbrains.python.psi.PyClass
9
10
import com.jetbrains.python.psi.PyDecorator
@@ -55,4 +56,12 @@ fun fineFunction(function: PyFunction): Boolean =
55
56
56
57
fun fineClass (pyClass : PyClass ): Boolean =
57
58
getAncestors(pyClass).dropLast(1 ).all { it !is PyClass && it !is PyFunction } &&
58
- pyClass.methods.any { fineFunction(it) }
59
+ pyClass.methods.any { fineFunction(it) }
60
+
61
+ fun PsiDirectory.topParent (level : Int ): PsiDirectory ? {
62
+ var directory: PsiDirectory ? = this
63
+ repeat(level) {
64
+ directory = directory?.parent
65
+ }
66
+ return directory
67
+ }
You can’t perform that action at this time.
0 commit comments