Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve resolving reference #293

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<h2>version 0.3.1</h2>
<p>Features</p>
<ul>
<li>Improve resolving reference [#293]</li>
<li>Improve coding style [#292]</li>
<li>Support GenericModel [#289]</li>
<li>Support frozen on config [#288]</li>
Expand Down
12 changes: 2 additions & 10 deletions src/com/koxudaxi/pydantic/Pydantic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.ResolveResult
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.QualifiedName
import com.jetbrains.extensions.ModuleBasedContextAnchor
Expand Down Expand Up @@ -245,16 +244,9 @@ private fun getAliasedFieldName(
}


fun getResolveElements(referenceExpression: PyReferenceExpression, context: TypeEvalContext): Array<ResolveResult> {
return PyResolveContext.defaultContext().withTypeEvalContext(context).let {
referenceExpression.getReference(it).multiResolve(false)
}

}


fun getResolvedPsiElements(referenceExpression: PyReferenceExpression, context: TypeEvalContext): List<PsiElement> {
return getResolveElements(referenceExpression, context).let { PyUtil.filterTopPriorityResults(it) }
return PyUtil.multiResolveTopPriority(referenceExpression,
PyResolveContext.defaultContext().withTypeEvalContext(context))
}

val PyType.pyClassTypes: List<PyClassType>
Expand Down
8 changes: 4 additions & 4 deletions src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {

return getResolvedPsiElements(referenceExpression, context)
.asSequence()
.map {
.mapNotNull {
when {
it is PyClass -> getPydanticTypeForClass(it, context, true, pyCallExpression)
it is PyParameter && it.isSelf ->
Expand Down Expand Up @@ -672,9 +672,9 @@ class PydanticTypeProvider : PyTypeProviderBase() {
val callee = (assignedValue as? PyCallExpressionImpl)?.callee ?: return assignedValue
val referenceExpression = callee.reference?.element as? PyReferenceExpression ?: return ellipsis

val resolveResults = getResolveElements(referenceExpression, context)
val resolveResults = getResolvedPsiElements(referenceExpression, context)
if (isDataclass) {
PyUtil.filterTopPriorityResults(resolveResults)
resolveResults
.any {
it.isDataclassField
}
Expand All @@ -687,7 +687,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
} else {

val versionZero = pydanticVersion?.major == 0
PyUtil.filterTopPriorityResults(resolveResults)
resolveResults
.any {
when {
versionZero -> isPydanticSchemaByPsiElement(it, context)
Expand Down