Skip to content

Commit 90e5f50

Browse files
committed
Modify the condition to use the IDENTIFIER class.
1 parent 6f47170 commit 90e5f50

File tree

8 files changed

+110
-75
lines changed

8 files changed

+110
-75
lines changed

src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
5959
import org.domaframework.doma.intellij.psi.SqlElForDirective
6060
import org.domaframework.doma.intellij.psi.SqlElGeExpr
6161
import org.domaframework.doma.intellij.psi.SqlElGtExpr
62+
import org.domaframework.doma.intellij.psi.SqlElIdExpr
6263
import org.domaframework.doma.intellij.psi.SqlElIfDirective
6364
import org.domaframework.doma.intellij.psi.SqlElLeExpr
6465
import org.domaframework.doma.intellij.psi.SqlElLtExpr
6566
import org.domaframework.doma.intellij.psi.SqlElNeExpr
6667
import org.domaframework.doma.intellij.psi.SqlElOrExpr
6768
import org.domaframework.doma.intellij.psi.SqlElParameters
68-
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
6969
import org.domaframework.doma.intellij.psi.SqlTypes
7070
import org.jetbrains.kotlin.idea.base.util.module
7171

@@ -176,14 +176,9 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
176176
if (parent is SqlElFieldAccessExpr) {
177177
blocks =
178178
PsiTreeUtil
179-
.getChildrenOfTypeAsList(parent, PsiElement::class.java)
180-
.filter {
181-
(
182-
it is SqlElPrimaryExpr ||
183-
it.elementType == SqlTypes.EL_IDENTIFIER
184-
) &&
185-
it.parent !is SqlElClass
186-
}.toList()
179+
.getChildrenOfTypeAsList(parent, SqlElIdExpr::class.java)
180+
.filter { it.parent !is SqlElClass }
181+
.toList()
187182
if (blocks.isEmpty()) {
188183
val parent =
189184
PsiTreeUtil.findFirstParent(parent) {
@@ -194,10 +189,9 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
194189

195190
blocks =
196191
PsiTreeUtil
197-
.getChildrenOfTypeAsList(parent, PsiElement::class.java)
192+
.getChildrenOfTypeAsList(parent, SqlElIdExpr::class.java)
198193
.filter {
199-
it.elementType == SqlTypes.EL_IDENTIFIER &&
200-
(targetElement.startOffsetInParent) >= it.startOffsetInParent
194+
targetElement.startOffsetInParent >= it.startOffsetInParent
201195
}.toList()
202196
}
203197
} else {
@@ -321,7 +315,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
321315
}
322316

323317
private fun getSearchElementText(elm: PsiElement): String =
324-
if (elm is SqlElPrimaryExpr || elm.elementType == SqlTypes.EL_IDENTIFIER) {
318+
if (elm.elementType == SqlTypes.EL_IDENTIFIER) {
325319
elm.text
326320
} else {
327321
""

src/main/kotlin/org/domaframework/doma/intellij/extension/expr/SqlElExtensions.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,17 @@ import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr
2222
import org.domaframework.doma.intellij.psi.SqlElClass
2323
import org.domaframework.doma.intellij.psi.SqlElElseifDirective
2424
import org.domaframework.doma.intellij.psi.SqlElForDirective
25+
import org.domaframework.doma.intellij.psi.SqlElIdExpr
2526
import org.domaframework.doma.intellij.psi.SqlElIfDirective
26-
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
2727
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
2828
import org.domaframework.doma.intellij.psi.SqlTypes
2929
import kotlin.invoke
3030

3131
val SqlElStaticFieldAccessExpr.accessElements: List<PsiElement>
3232
get() {
3333
return PsiTreeUtil
34-
.getChildrenOfType(this, PsiElement::class.java)
35-
?.filter {
36-
(
37-
it.elementType == SqlTypes.EL_IDENTIFIER ||
38-
it is SqlElPrimaryExpr
39-
)
40-
}?.sortedBy { it.textOffset }
34+
.getChildrenOfType(this, SqlElIdExpr::class.java)
35+
?.sortedBy { it.textOffset }
4136
?.toList()
4237
?: emptyList()
4338
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.extension.psi
17+
18+
import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
19+
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
20+
21+
fun SqlElPrimaryExpr.isFirstElement(): Boolean =
22+
if (this.parent is SqlElFieldAccessExpr) {
23+
this.parent.children.indexOf(this) == 0
24+
} else {
25+
true
26+
}

src/main/kotlin/org/domaframework/doma/intellij/extension/psi/SqlElForDirectiveExtension.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ package org.domaframework.doma.intellij.extension.psi
1717

1818
import com.intellij.psi.PsiElement
1919
import com.intellij.psi.util.PsiTreeUtil
20-
import com.intellij.psi.util.elementType
2120
import org.domaframework.doma.intellij.psi.SqlElForDirective
22-
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
23-
import org.domaframework.doma.intellij.psi.SqlTypes
21+
import org.domaframework.doma.intellij.psi.SqlElIdExpr
2422

2523
fun SqlElForDirective.getForItem(): PsiElement? =
2624
PsiTreeUtil
27-
.getChildrenOfType(this, PsiElement::class.java)
28-
?.firstOrNull {
29-
it.isNotWhiteSpace() &&
30-
(it is SqlElPrimaryExpr || it.elementType == SqlTypes.EL_IDENTIFIER)
31-
}
25+
.getChildOfType(this, SqlElIdExpr::class.java)

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/DaoMethodVariableInspector.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import com.intellij.psi.PsiMethod
2727
import com.intellij.psi.PsiParameter
2828
import com.intellij.psi.PsiRecursiveElementVisitor
2929
import com.intellij.psi.impl.source.PsiParameterImpl
30+
import com.intellij.psi.util.PsiTreeUtil
31+
import com.intellij.psi.util.elementType
3032
import org.domaframework.doma.intellij.bundle.MessageBundle
3133
import org.domaframework.doma.intellij.common.dao.getDaoClass
3234
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
@@ -36,7 +38,8 @@ import org.domaframework.doma.intellij.extension.psi.isCollector
3638
import org.domaframework.doma.intellij.extension.psi.isFunctionClazz
3739
import org.domaframework.doma.intellij.extension.psi.isSelectOption
3840
import org.domaframework.doma.intellij.extension.psi.methodParameters
39-
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
41+
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
42+
import org.domaframework.doma.intellij.psi.SqlTypes
4043

4144
/**
4245
* Check if Dao method arguments are used in the corresponding SQL file
@@ -94,11 +97,16 @@ class DaoMethodVariableInspector : AbstractBaseJavaLocalInspectionTool() {
9497
object : PsiRecursiveElementVisitor() {
9598
// Recursively explore child elements in a file with PsiRecursiveElementVisitor.
9699
override fun visitElement(element: PsiElement) {
97-
if (element is SqlElPrimaryExpr) {
100+
if (element.elementType == SqlTypes.EL_IDENTIFIER && element.prevSibling?.elementType != SqlTypes.DOT) {
98101
iterator = args.minus(elements.toSet()).iterator()
99102
while (iterator.hasNext()) {
100103
val arg = iterator.next()
101-
if (element.text == arg.name) {
104+
val fieldAccessExpr =
105+
PsiTreeUtil.getParentOfType(
106+
element,
107+
SqlElStaticFieldAccessExpr::class.java,
108+
)
109+
if (fieldAccessExpr == null && element.text == arg.name) {
102110
elements.add(arg)
103111
break
104112
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.domaframework.doma.intellij.extension.psi.getDomaAnnotationType
4747
import org.domaframework.doma.intellij.extension.psi.getForItem
4848
import org.domaframework.doma.intellij.extension.psi.getIterableClazz
4949
import org.domaframework.doma.intellij.extension.psi.getMethodReturnType
50+
import org.domaframework.doma.intellij.extension.psi.isFirstElement
5051
import org.domaframework.doma.intellij.extension.psi.methodParameters
5152
import org.domaframework.doma.intellij.extension.psi.psiClassType
5253
import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
@@ -157,18 +158,18 @@ class SqlBindVariableValidInspector : LocalInspectionTool() {
157158

158159
override fun visitElPrimaryExpr(element: SqlElPrimaryExpr) {
159160
super.visitElPrimaryExpr(element)
161+
if (!element.isFirstElement()) return
160162
val file = element.containingFile ?: return
161163
val project = element.project
162164

163165
// Exclude fixed Literal
164166
if (isLiteralOrStatic(element)) return
165167

168+
if (PsiTreeUtil.getParentOfType(element, SqlElForDirective::class.java) != null) return
169+
166170
// For static property references, match against properties in the class definition
167-
if (element.parent is SqlElStaticFieldAccessExpr) {
168-
checkStaticFieldAndMethodAccess(
169-
element.parent as SqlElStaticFieldAccessExpr,
170-
holder,
171-
)
171+
val parentStaticFieldAccessExpr = PsiTreeUtil.getParentOfType(element, SqlElStaticFieldAccessExpr::class.java)
172+
if (parentStaticFieldAccessExpr != null) {
172173
return
173174
}
174175
if (checkInForDirectiveBlock(element)) return
@@ -212,7 +213,7 @@ class SqlBindVariableValidInspector : LocalInspectionTool() {
212213
* to the target element (`targetElement`)
213214
* and obtain the `for` block information to which the `targetElement` belongs.
214215
*/
215-
fun getForDirectiveBlock(targetElement: PsiElement): List<BlockToken> {
216+
private fun getForDirectiveBlock(targetElement: PsiElement): List<BlockToken> {
216217
val topElm = targetElement.containingFile.firstChild ?: return emptyList()
217218
val directiveBlocks =
218219
topElm.nextLeafs

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

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.intellij.psi.PsiReferenceBase
2222
import com.intellij.psi.PsiType
2323
import com.intellij.psi.util.PsiTreeUtil
2424
import com.intellij.psi.util.PsiTypesUtil
25-
import com.intellij.psi.util.elementType
2625
import org.domaframework.doma.intellij.common.PluginLoggerUtil
2726
import org.domaframework.doma.intellij.common.dao.findDaoMethod
2827
import org.domaframework.doma.intellij.common.isSupportFileType
@@ -33,9 +32,9 @@ import org.domaframework.doma.intellij.extension.psi.getDomaAnnotationType
3332
import org.domaframework.doma.intellij.extension.psi.getIterableClazz
3433
import org.domaframework.doma.intellij.extension.psi.methodParameters
3534
import org.domaframework.doma.intellij.psi.SqlElClass
36-
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
35+
import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
36+
import org.domaframework.doma.intellij.psi.SqlElIdExpr
3737
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
38-
import org.domaframework.doma.intellij.psi.SqlTypes
3938

4039
class SqlReference(
4140
element: PsiElement,
@@ -44,17 +43,11 @@ class SqlReference(
4443

4544
override fun resolve(): PsiElement? {
4645
if (file == null || !isSupportFileType(file)) return null
46+
val startTime = System.nanoTime()
4747
val variableName = element.text
4848

49-
val startTime = System.nanoTime()
50-
val staticDirective = getStaticDirective(element, variableName)
49+
val staticDirective = getStaticDirective(element, variableName, startTime)
5150
if (staticDirective != null) {
52-
PluginLoggerUtil.countLogging(
53-
this::class.java.simpleName,
54-
"ReferenceToStatic",
55-
"Reference",
56-
startTime,
57-
)
5851
return staticDirective
5952
}
6053

@@ -65,14 +58,15 @@ class SqlReference(
6558

6659
return when (element.textOffset) {
6760
targetElement.first().textOffset ->
68-
jumpToDaoMethodParameter(
61+
getReferenceDaoMethodParameter(
6962
daoMethod,
7063
element,
7164
startTime,
7265
)
7366

74-
else -> jumpToEntity(daoMethod, targetElement, startTime)
67+
else -> getReferenceEntity(daoMethod, targetElement, startTime)
7568
}
69+
7670
return null
7771
}
7872

@@ -81,6 +75,7 @@ class SqlReference(
8175
private fun getStaticDirective(
8276
staticDirection: PsiElement?,
8377
elementName: String,
78+
startTime: Long,
8479
): PsiElement? {
8580
if (staticDirection == null) return null
8681
val file: PsiFile = file ?: return null
@@ -89,14 +84,18 @@ class SqlReference(
8984
staticDirection.parent is SqlElClass
9085
) {
9186
val psiStaticElement = PsiStaticElement(staticDirection.text, file)
87+
PluginLoggerUtil.countLogging(
88+
this::class.java.simpleName,
89+
"ReferenceStaticClass",
90+
"Reference",
91+
startTime,
92+
)
9293
return psiStaticElement.getRefClazz()
9394
}
9495

9596
// Jump from field or method to definition (assuming the top element is static)
96-
val staticAccessParent = staticDirection.parent
97-
if (staticDirection is SqlElStaticFieldAccessExpr ||
98-
staticAccessParent is SqlElStaticFieldAccessExpr
99-
) {
97+
val staticAccessParent = PsiTreeUtil.getParentOfType(staticDirection, SqlElStaticFieldAccessExpr::class.java)
98+
if (staticAccessParent != null) {
10099
val firstChildText =
101100
staticAccessParent.children
102101
.firstOrNull()
@@ -109,9 +108,21 @@ class SqlReference(
109108
val javaClazz = psiStaticElement.getRefClazz() ?: return null
110109
val psiParentClass = PsiParentClass(PsiTypesUtil.getClassType(javaClazz))
111110
psiParentClass.findField(elementName)?.let {
111+
PluginLoggerUtil.countLogging(
112+
this::class.java.simpleName,
113+
"ReferenceStaticField",
114+
"Reference",
115+
startTime,
116+
)
112117
return it
113118
}
114119
psiParentClass.findMethod(elementName)?.let {
120+
PluginLoggerUtil.countLogging(
121+
this::class.java.simpleName,
122+
"ReferenceStaticMethod",
123+
"Reference",
124+
startTime,
125+
)
115126
return it
116127
}
117128
}
@@ -120,20 +131,21 @@ class SqlReference(
120131

121132
private fun getBlockCommentElements(element: PsiElement): List<PsiElement> {
122133
val nodeElm =
123-
PsiTreeUtil
124-
.getChildrenOfType(element.parent, PsiElement::class.java)
125-
?.filter {
126-
(
127-
it.elementType == SqlTypes.EL_IDENTIFIER ||
128-
it is SqlElPrimaryExpr
129-
) &&
130-
it.textOffset <= element.textOffset
131-
}?.toList()
132-
?.sortedBy { it.textOffset } ?: emptyList()
134+
if (element.parent is SqlElFieldAccessExpr) {
135+
PsiTreeUtil
136+
.getChildrenOfType(
137+
element.parent,
138+
SqlElIdExpr::class.java,
139+
)?.filter { it.textOffset <= element.textOffset }
140+
} else {
141+
listOf(element)
142+
}
133143
return nodeElm
144+
?.toList()
145+
?.sortedBy { it.textOffset } ?: emptyList()
134146
}
135147

136-
private fun jumpToDaoMethodParameter(
148+
private fun getReferenceDaoMethodParameter(
137149
daoMethod: PsiMethod,
138150
it: PsiElement,
139151
startTime: Long,
@@ -147,15 +159,15 @@ class SqlReference(
147159
?.let { originalElm ->
148160
PluginLoggerUtil.countLogging(
149161
this::class.java.simpleName,
150-
"ReferenceToDaoMethodParameter",
162+
"ReferenceDaoMethodParameter",
151163
"Reference",
152164
startTime,
153165
)
154166
return originalElm
155167
} ?: return null
156168
}
157169

158-
private fun jumpToEntity(
170+
private fun getReferenceEntity(
159171
daoMethod: PsiMethod,
160172
targetElement: List<PsiElement>,
161173
startTime: Long,
@@ -170,7 +182,7 @@ class SqlReference(
170182

171183
PluginLoggerUtil.countLogging(
172184
this::class.java.simpleName,
173-
"ReferenceToBindVariable",
185+
"ReferenceEntityProperty",
174186
"Reference",
175187
startTime,
176188
)
@@ -206,17 +218,17 @@ class SqlReference(
206218
parentClass
207219
.findField(elm.text)
208220
?.let {
209-
val bindVal = getBindVariableIfLastIndex(index, it.type, it.originalElement)
210-
if (bindVal != null) return bindVal
221+
val reference = getBindVariableIfLastIndex(index, it.type, it.originalElement)
222+
if (reference != null) return reference
211223
}
212224
if (isExistProperty) continue
213225
parentClass
214226
.findMethod(elm.text)
215227
?.let {
216228
val returnType = it.returnType ?: return null
217-
val bindVal =
229+
val reference =
218230
getBindVariableIfLastIndex(index, returnType, it.originalElement)
219-
if (bindVal != null) return bindVal
231+
if (reference != null) return reference
220232
}
221233
if (!isExistProperty) return null
222234
}

0 commit comments

Comments
 (0)