Skip to content

Commit 3360335

Browse files
committed
Fixed reference resolution logic for SqlElStaticField
1 parent d23d437 commit 3360335

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/sql/validator/SqlElChildElementValidator.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.domaframework.doma.intellij.common.sql.validator
1717

18+
import com.intellij.openapi.project.Project
1819
import com.intellij.psi.PsiClassType
1920
import com.intellij.psi.PsiElement
2021
import com.intellij.psi.PsiType
@@ -42,33 +43,34 @@ abstract class SqlElChildElementValidator(
4243
): ValidationResult? = null
4344

4445
fun validateFieldAccess(
46+
project: Project,
4547
topParent: PsiParentClass,
4648
dropLastIndex: Int = 0,
4749
findFieldMethod: ((PsiType) -> PsiParentClass)? = { type -> PsiParentClass(type) },
4850
complete: ((PsiParentClass) -> Unit) = { parent: PsiParentClass? -> },
4951
): ValidationResult? =
5052
getFieldAccessParentClass(
53+
project,
5154
topParent,
5255
dropLastIndex,
5356
findFieldMethod = findFieldMethod,
5457
complete = complete,
5558
)
5659

5760
protected fun getFieldAccessParentClass(
61+
project: Project,
5862
topParent: PsiParentClass,
5963
dropLastIndex: Int = 0,
6064
findFieldMethod: ((PsiType) -> PsiParentClass)? = { type -> PsiParentClass(type) },
6165
complete: ((PsiParentClass) -> Unit) = { parent: PsiParentClass? -> },
6266
): ValidationResult? {
63-
val project = blocks.firstOrNull()?.project ?: return null
64-
6567
var parent = topParent
6668
val parentType = parent.type
6769
val classType = parentType as? PsiClassType ?: return null
6870

6971
var competeResult: ValidationCompleteResult? = null
7072

71-
if (dropLastIndex > 0 && blocks.drop(1).dropLast(dropLastIndex).isEmpty()) {
73+
if (dropLastIndex > 0 && blocks.size <= dropLastIndex + 1) {
7274
return ValidationCompleteResult(
7375
blocks.last(),
7476
parent,

src/main/kotlin/org/domaframework/doma/intellij/common/sql/validator/SqlElFieldAccessorChildElementValidator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class SqlElFieldAccessorChildElementValidator(
3434
override val shorName: String = "",
3535
private val topDaoParameter: PsiParameter? = null,
3636
) : SqlElChildElementValidator(blocks, shorName) {
37+
val project = file.project
38+
3739
override fun validateChildren(
3840
dropIndex: Int,
3941
findFieldMethod: (PsiType) -> PsiParentClass,
@@ -43,6 +45,7 @@ class SqlElFieldAccessorChildElementValidator(
4345
val parentClass = parentClassResult?.parentClass
4446
if (parentClassResult is ValidationCompleteResult && parentClass != null) {
4547
return validateFieldAccess(
48+
project,
4649
parentClass,
4750
complete = complete,
4851
)
@@ -55,7 +58,7 @@ class SqlElFieldAccessorChildElementValidator(
5558
val parentClassResult = getParentClass()
5659
val parentClass = parentClassResult?.parentClass
5760
if (parentClassResult is ValidationCompleteResult && parentClass != null) {
58-
return validateFieldAccess(parentClass)
61+
return validateFieldAccess(project, parentClass)
5962
}
6063

6164
return parentClassResult

src/main/kotlin/org/domaframework/doma/intellij/common/sql/validator/SqlElForItemFieldAccessorChildElementValidator.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ class SqlElForItemFieldAccessorChildElementValidator(
2525
private val declarationType: PsiParentClass,
2626
override val shorName: String = "",
2727
) : SqlElChildElementValidator(blocks, shorName) {
28+
val project = blocks.firstOrNull()?.project
29+
2830
override fun validateChildren(
2931
dropIndex: Int,
3032
findFieldMethod: (PsiType) -> PsiParentClass,
3133
complete: (PsiParentClass) -> Unit,
3234
): ValidationResult? =
33-
validateFieldAccess(
34-
declarationType,
35-
dropIndex,
36-
complete = complete,
37-
)
35+
project?.let {
36+
validateFieldAccess(
37+
it,
38+
declarationType,
39+
dropIndex,
40+
complete = complete,
41+
)
42+
}
3843

39-
override fun validateChildren(dropIndex: Int): ValidationResult? = validateFieldAccess(declarationType, dropIndex)
44+
override fun validateChildren(dropIndex: Int): ValidationResult? = project?.let { validateFieldAccess(it, declarationType, dropIndex) }
4045
}

src/main/kotlin/org/domaframework/doma/intellij/common/sql/validator/SqlElStaticFieldAccessorChildElementValidator.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class SqlElStaticFieldAccessorChildElementValidator(
3333
private val staticAccuser: SqlElStaticFieldAccessExpr,
3434
override val shorName: String = "",
3535
) : SqlElChildElementValidator(blocks, shorName) {
36+
val project = staticAccuser.containingFile.project
37+
3638
override fun validateChildren(
3739
dropIndex: Int,
3840
findFieldMethod: (PsiType) -> PsiParentClass,
@@ -43,6 +45,7 @@ class SqlElStaticFieldAccessorChildElementValidator(
4345
is ValidationCompleteResult -> {
4446
val parent = getParentResult.parentClass
4547
return validateFieldAccess(
48+
project,
4649
parent,
4750
dropLastIndex = dropIndex,
4851
complete = complete,
@@ -57,31 +60,28 @@ class SqlElStaticFieldAccessorChildElementValidator(
5760
val getParentResult = getFieldTopParent()
5861
when (getParentResult) {
5962
is ValidationCompleteResult -> {
60-
if (blocks.size == 1) {
61-
return getParentResult
62-
}
6363
val parent = getParentResult.parentClass
64-
return validateFieldAccess(parent, dropLastIndex = dropIndex)
64+
return validateFieldAccess(project, parent, dropLastIndex = dropIndex)
6565
}
6666
is ValidationIgnoreResult -> return null
6767
else -> return getParentResult
6868
}
6969
}
7070

71-
private fun getFieldTopParent(): ValidationResult {
72-
val staticTopElement =
73-
blocks.firstOrNull()
74-
?: return ValidationIgnoreResult(blocks.firstOrNull())
71+
fun getClassType(): PsiParentClass? {
7572
val fqdn = staticAccuser.fqdn
7673
val file = staticAccuser.containingFile
7774
val psiStaticElement = PsiStaticElement(fqdn, file)
78-
val clazz = psiStaticElement.getRefClazz() ?: return ValidationIgnoreResult(staticTopElement)
75+
val clazz = psiStaticElement.getRefClazz() ?: return null
7976

80-
var parent = PsiParentClass(clazz.psiClassType)
81-
if (blocks.size == 1) {
82-
return ValidationCompleteResult(blocks.first(), parent)
83-
}
77+
return PsiParentClass(clazz.psiClassType)
78+
}
8479

80+
private fun getFieldTopParent(): ValidationResult? {
81+
var parent = getClassType() ?: return ValidationIgnoreResult(null)
82+
val staticTopElement =
83+
blocks.firstOrNull()
84+
?: return ValidationCompleteResult(staticAccuser.elClass, parent)
8585
val nextSibling = staticTopElement.nextSibling
8686
val topField =
8787
if (nextSibling !is SqlElParameters) {

src/main/kotlin/org/domaframework/doma/intellij/inspection/ForDirectiveInspection.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ class ForDirectiveInspection(
118118

119119
for ((i, targetForDirective) in forDirectives.withIndex()) {
120120
if (nestClassType == null) break
121+
val targetDirectiveParent =
122+
PsiTreeUtil.getParentOfType(
123+
targetForDirective.item,
124+
SqlElForDirective::class.java,
125+
)
126+
val targetElementParent =
127+
PsiTreeUtil.getParentOfType(
128+
topElm,
129+
SqlElForDirective::class.java,
130+
)
131+
if (targetDirectiveParent == targetElementParent) continue
121132

122133
val currentForItem = ForItem(targetForDirective.item)
123134
val currentDeclaration = currentForItem.getParentForDirectiveExpr()?.getForItemDeclaration() ?: continue

src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/SqlInspectionVisitor.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.intellij.psi.util.elementType
2626
import org.domaframework.doma.intellij.common.dao.findDaoMethod
2727
import org.domaframework.doma.intellij.common.isInjectionSqlFile
2828
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
29+
import org.domaframework.doma.intellij.common.sql.foritem.ForItem
2930
import org.domaframework.doma.intellij.common.sql.validator.SqlElFieldAccessorChildElementValidator
3031
import org.domaframework.doma.intellij.common.sql.validator.SqlElForItemFieldAccessorChildElementValidator
3132
import org.domaframework.doma.intellij.common.sql.validator.SqlElStaticFieldAccessorChildElementValidator
@@ -35,6 +36,7 @@ import org.domaframework.doma.intellij.common.sql.validator.result.ValidationPro
3536
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationResult
3637
import org.domaframework.doma.intellij.extension.expr.accessElements
3738
import org.domaframework.doma.intellij.extension.psi.findParameter
39+
import org.domaframework.doma.intellij.extension.psi.getForItem
3840
import org.domaframework.doma.intellij.extension.psi.isFirstElement
3941
import org.domaframework.doma.intellij.inspection.ForDirectiveInspection
4042
import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
@@ -107,6 +109,10 @@ class SqlInspectionVisitor(
107109
ForDirectiveInspection(daoMethod, this.shortName)
108110

109111
if (PsiTreeUtil.getParentOfType(element, SqlElForDirective::class.java) != null) {
112+
val currentForItem = ForItem(element)
113+
val leftSideForItem = currentForItem.getParentForDirectiveExpr()?.getForItem()
114+
if (leftSideForItem == element) return
115+
110116
val forDirectivesSize = forDirectiveInspection.getForDirectiveBlockSize(element)
111117
if (forDirectivesSize == 0) return
112118
}

src/main/kotlin/org/domaframework/doma/intellij/reference/SqlElStaticFieldReference.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,19 @@ class SqlElStaticFieldReference(
4040
staticAccessParent,
4141
)
4242

43-
val errorElement = validator.validateChildren(dropIndex = 1)
44-
if (errorElement is ValidationCompleteResult) {
43+
val initialPsiParentClass = validator.getClassType() ?: return null
44+
val project = file.project
45+
val fieldAccessLastParentResult =
46+
if (targetElements.size == 1) {
47+
validator.validateFieldAccess(project, initialPsiParentClass, dropLastIndex = 1)
48+
} else if (targetElements.size >= 2) {
49+
validator.validateChildren(1)
50+
} else {
51+
null
52+
}
53+
if (fieldAccessLastParentResult is ValidationCompleteResult) {
4554
val searchText = element.text ?: ""
46-
val parent = errorElement.parentClass
55+
val parent = fieldAccessLastParentResult.parentClass
4756
return parent.findField(searchText) ?: parent.findMethod(searchText)
4857
}
4958

0 commit comments

Comments
 (0)