Skip to content

Commit ad8b217

Browse files
committed
Changed code completion to partial match search
1 parent d2c3157 commit ad8b217

File tree

12 files changed

+92
-101
lines changed

12 files changed

+92
-101
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiParentClass.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class PsiParentClass(
4949

5050
fun searchField(fieldName: String): List<PsiField>? =
5151
fields?.filter { f ->
52-
f.name.startsWith(fieldName) &&
52+
f.name.contains(fieldName) &&
5353
PropertyModifyUtil.filterPrivateField(f, type)
5454
}
5555

@@ -80,7 +80,7 @@ class PsiParentClass(
8080
fields
8181
?.filter { f ->
8282
f.hasModifierProperty(PsiModifier.STATIC) &&
83-
f.name.startsWith(fieldName) &&
83+
f.name.contains(fieldName) &&
8484
PropertyModifyUtil.filterPrivateField(f, type)
8585
}
8686

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class PsiClassTypeUtil {
2424
companion object {
2525
fun getPsiTypeByList(
2626
classType: PsiClassType,
27+
project: Project,
2728
useListParam: Boolean,
2829
): PsiType? {
29-
if (classType.className == "List" && useListParam) {
30+
val isIterableType = isIterableType(classType, project)
31+
if (isIterableType && useListParam) {
3032
return classType.parameters.firstOrNull()
3133
}
3234
return classType

src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/EmbeddedDirectiveHandler.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement
2121
import com.intellij.psi.PsiFile
2222
import com.intellij.psi.PsiMethod
2323
import org.domaframework.doma.intellij.common.dao.findDaoMethod
24+
import org.domaframework.doma.intellij.extension.psi.searchParameter
2425

2526
class EmbeddedDirectiveHandler(
2627
private val originalFile: PsiFile,
@@ -36,11 +37,8 @@ class EmbeddedDirectiveHandler(
3637
result,
3738
) { daoMethod, bind ->
3839
daoMethod
39-
?.parameterList
40-
?.parameters
41-
?.filter {
42-
it.name.startsWith(bind)
43-
}?.map { param -> VariableLookupItem(param) }
40+
?.searchParameter(bind)
41+
?.map { param -> VariableLookupItem(param) }
4442
?.toList()
4543
?: emptyList()
4644
}

src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/LiteralDirectiveHandler.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.intellij.psi.PsiFile
2222
import com.intellij.psi.PsiMethod
2323
import org.domaframework.doma.intellij.common.dao.findDaoMethod
2424
import org.domaframework.doma.intellij.common.psi.PsiTypeChecker
25+
import org.domaframework.doma.intellij.extension.psi.searchParameter
2526

2627
class LiteralDirectiveHandler(
2728
private val originalFile: PsiFile,
@@ -37,11 +38,9 @@ class LiteralDirectiveHandler(
3738
result,
3839
) { daoMethod, bind ->
3940
daoMethod
40-
?.parameterList
41-
?.parameters
41+
?.searchParameter(bind)
4242
?.filter {
43-
it.name.startsWith(bind) &&
44-
PsiTypeChecker.isTargetType(it.type)
43+
PsiTypeChecker.isTargetType(it.type)
4544
}?.map { param -> VariableLookupItem(param) }
4645
?.toList()
4746
?: emptyList()

src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/PercentDirectiveHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PercentDirectiveHandler(
4848
"for",
4949
"!",
5050
).filter {
51-
it.startsWith(bind)
51+
it.contains(bind)
5252
}.map {
5353
LookupElementBuilder
5454
.create(it)

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

Lines changed: 35 additions & 18 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.psi.PsiClassType
1819
import com.intellij.psi.PsiElement
1920
import com.intellij.psi.PsiType
2021
import com.intellij.psi.util.elementType
@@ -24,15 +25,13 @@ import org.domaframework.doma.intellij.common.sql.cleanString
2425
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationCompleteResult
2526
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationPropertyResult
2627
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationResult
27-
import org.domaframework.doma.intellij.extension.psi.getMethodReturnType
28-
import org.domaframework.doma.intellij.extension.psi.getParameterType
2928
import org.domaframework.doma.intellij.psi.SqlElIdExpr
3029
import org.domaframework.doma.intellij.psi.SqlElParameters
3130
import org.domaframework.doma.intellij.psi.SqlTypes
3231

3332
abstract class SqlElChildElementValidator(
3433
open val blocks: List<PsiElement>,
35-
open val shorName: String,
34+
open val shorName: String = "",
3635
) {
3736
abstract fun validateChildren(dropIndex: Int = 0): ValidationResult?
3837

@@ -61,7 +60,12 @@ abstract class SqlElChildElementValidator(
6160
findFieldMethod: ((PsiType) -> PsiParentClass)? = { type -> PsiParentClass(type) },
6261
complete: ((PsiParentClass) -> Unit) = { parent: PsiParentClass? -> },
6362
): ValidationResult? {
63+
val project = blocks.firstOrNull()?.project ?: return null
64+
6465
var parent = topParent
66+
val parentType = parent.type
67+
val classType = parentType as? PsiClassType ?: return null
68+
6569
var competeResult: ValidationCompleteResult? = null
6670

6771
if (dropLastIndex > 0 && blocks.drop(1).dropLast(dropLastIndex).isEmpty()) {
@@ -71,9 +75,9 @@ abstract class SqlElChildElementValidator(
7175
)
7276
}
7377

74-
var getMethodReturnType: PsiType? =
75-
if (PsiClassTypeUtil.isCollect(topParent.type)) {
76-
topParent.type
78+
var parentListBaseType: PsiType? =
79+
if (PsiClassTypeUtil.isIterableType(classType, project)) {
80+
parentType
7781
} else {
7882
null
7983
}
@@ -90,28 +94,41 @@ abstract class SqlElChildElementValidator(
9094

9195
val field =
9296
parent
93-
.findField(element.text)
97+
.findField(searchElm)
9498
?.let { match ->
95-
val type = match.type
96-
val methodReturnType =
97-
getMethodReturnType?.let { match.getParameterType(it, type, listParamIndex) }
98-
?: type
99-
if (PsiClassTypeUtil.isCollect(type)) {
100-
getMethodReturnType = type
99+
val type =
100+
parentListBaseType?.let { PsiClassTypeUtil.getParameterType(project, match.type, it, listParamIndex) }
101+
?: match.type
102+
val classType = type as? PsiClassType
103+
if (classType != null && PsiClassTypeUtil.isIterableType(classType, element.project)) {
104+
parentListBaseType = type
101105
listParamIndex = 0
102106
}
103-
findFieldMethod?.invoke(methodReturnType)
107+
findFieldMethod?.invoke(type)
104108
}
105109
val method =
106110
parent
107-
.findMethod(element.text)
111+
.findMethod(searchElm)
108112
?.let { match ->
109113
val returnType = match.returnType ?: return null
110114
val methodReturnType =
111-
getMethodReturnType?.let { match.getMethodReturnType(it, listParamIndex) }
115+
parentListBaseType?.let {
116+
PsiClassTypeUtil.getParameterType(
117+
project,
118+
returnType,
119+
it,
120+
listParamIndex,
121+
)
122+
}
112123
?: returnType
113-
if (PsiClassTypeUtil.isCollect(returnType)) {
114-
getMethodReturnType = returnType
124+
val classType = methodReturnType as? PsiClassType
125+
if (classType != null &&
126+
PsiClassTypeUtil.isIterableType(
127+
classType,
128+
element.project,
129+
)
130+
) {
131+
parentListBaseType = methodReturnType
115132
listParamIndex = 0
116133
}
117134
findFieldMethod?.invoke(methodReturnType)

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@ package org.domaframework.doma.intellij.common.sql.validator
1818
import com.intellij.psi.PsiElement
1919
import com.intellij.psi.PsiType
2020
import org.domaframework.doma.intellij.common.psi.PsiParentClass
21+
import org.domaframework.doma.intellij.common.psi.PsiStaticElement
2122
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationCompleteResult
2223
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationIgnoreResult
2324
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationPropertyResult
2425
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationResult
2526
import org.domaframework.doma.intellij.extension.expr.fqdn
26-
import org.domaframework.doma.intellij.extension.getJavaClazz
2727
import org.domaframework.doma.intellij.extension.psi.psiClassType
2828
import org.domaframework.doma.intellij.psi.SqlElParameters
2929
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
30-
import org.jetbrains.kotlin.idea.base.util.module
3130

3231
class SqlElStaticFieldAccessorChildElementValidator(
3332
override val blocks: List<PsiElement>,
3433
private val staticAccuser: SqlElStaticFieldAccessExpr,
35-
override val shorName: String,
34+
override val shorName: String = "",
3635
) : SqlElChildElementValidator(blocks, shorName) {
3736
override fun validateChildren(
3837
dropIndex: Int,
3938
findFieldMethod: (PsiType) -> PsiParentClass,
4039
complete: (PsiParentClass) -> Unit,
4140
): ValidationResult? {
42-
val errorElement = getParent()
41+
val errorElement = getFieldTopParent()
4342
when (errorElement) {
4443
is ValidationCompleteResult -> {
4544
val parent = errorElement.parentClass
4645
return validateFieldAccess(
4746
parent,
47+
dropLastIndex = dropIndex,
4848
complete = complete,
4949
)
5050
}
@@ -54,28 +54,34 @@ class SqlElStaticFieldAccessorChildElementValidator(
5454
}
5555

5656
override fun validateChildren(dropIndex: Int): ValidationResult? {
57-
val getParentResult = getParent()
57+
val getParentResult = getFieldTopParent()
5858
when (getParentResult) {
5959
is ValidationCompleteResult -> {
60+
if (blocks.size == 1) {
61+
return getParentResult
62+
}
6063
val parent = getParentResult.parentClass
61-
return validateFieldAccess(parent)
64+
return validateFieldAccess(parent, dropLastIndex = dropIndex)
6265
}
6366
is ValidationIgnoreResult -> return null
6467
else -> return getParentResult
6568
}
6669
}
6770

68-
private fun getParent(): ValidationResult {
71+
private fun getFieldTopParent(): ValidationResult {
6972
val staticTopElement =
7073
blocks.firstOrNull()
7174
?: return ValidationIgnoreResult(blocks.firstOrNull())
72-
val module = staticAccuser.module ?: return ValidationIgnoreResult(staticTopElement)
7375
val fqdn = staticAccuser.fqdn
74-
val clazz =
75-
module.getJavaClazz(false, fqdn)
76-
?: return ValidationIgnoreResult(staticTopElement)
76+
val file = staticAccuser.containingFile
77+
val psiStaticElement = PsiStaticElement(fqdn, file)
78+
val clazz = psiStaticElement.getRefClazz() ?: return ValidationIgnoreResult(staticTopElement)
7779

7880
var parent = PsiParentClass(clazz.psiClassType)
81+
if (blocks.size == 1) {
82+
return ValidationCompleteResult(blocks.first(), parent)
83+
}
84+
7985
val nextSibling = staticTopElement.nextSibling
8086
val topField =
8187
if (nextSibling !is SqlElParameters) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ val PsiClass.psiClassType: PsiClassType
2929
fun PsiClass.searchStaticField(searchName: String): Array<PsiField> =
3030
this.allFields
3131
.filter {
32-
it.name.startsWith(searchName) &&
32+
it.name.contains(searchName) &&
3333
it.hasModifierProperty(PsiModifier.STATIC) &&
3434
PropertyModifyUtil.filterPrivateField(it, this.psiClassType)
3535
}.toTypedArray()
@@ -44,7 +44,7 @@ fun PsiClass.findStaticField(searchName: String): PsiField? =
4444
fun PsiClass.searchStaticMethod(searchName: String): Array<PsiMethod> =
4545
this.allMethods
4646
.filter {
47-
it.name.startsWith(searchName) &&
47+
it.name.contains(searchName) &&
4848
it.hasModifierProperty(PsiModifier.STATIC) &&
4949
it.hasModifierProperty(PsiModifier.PUBLIC)
5050
}.toTypedArray()

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
package org.domaframework.doma.intellij.extension.psi
1717

1818
import com.intellij.codeInsight.AnnotationUtil
19-
import com.intellij.psi.PsiClassType
2019
import com.intellij.psi.PsiMethod
2120
import com.intellij.psi.PsiParameter
22-
import com.intellij.psi.PsiType
23-
import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil
2421

2522
fun PsiMethod.findParameter(searchName: String): PsiParameter? = this.methodParameters.firstOrNull { it.name == searchName }
2623

2724
val PsiMethod.methodParameters: List<PsiParameter>
2825
get() = this.parameterList.parameters.toList()
2926

30-
fun PsiMethod.searchParameter(searchName: String): List<PsiParameter> = this.methodParameters.filter { it.name.startsWith(searchName) }
27+
fun PsiMethod.searchParameter(searchName: String): List<PsiParameter> = this.methodParameters.filter { it.name.contains(searchName) }
3128

3229
@OptIn(ExperimentalStdlibApi::class)
3330
fun PsiMethod.getDomaAnnotationType(): DomaAnnotationType {
@@ -38,18 +35,3 @@ fun PsiMethod.getDomaAnnotationType(): DomaAnnotationType {
3835
}
3936
return DomaAnnotationType.Unknown
4037
}
41-
42-
/**
43-
* If the type of the variable referenced from the Dao argument is List type,
44-
* search processing to obtain the nested type
45-
*/
46-
fun PsiMethod.getMethodReturnType(
47-
preReturnListType: PsiType,
48-
index: Int,
49-
): PsiClassType? =
50-
PsiClassTypeUtil.getParameterType(
51-
this.project,
52-
this.returnType,
53-
preReturnListType,
54-
index,
55-
)

0 commit comments

Comments
 (0)