Skip to content

Commit 763fb99

Browse files
committed
Handle "_has_next" and "_index" as specified types
1 parent 56341a0 commit 763fb99

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.intellij.psi.PsiClass
2020
import com.intellij.psi.PsiClassType
2121
import com.intellij.psi.PsiElement
2222
import com.intellij.psi.PsiType
23+
import com.intellij.psi.search.GlobalSearchScope
2324
import com.intellij.psi.util.CachedValue
2425
import com.intellij.psi.util.CachedValueProvider
2526
import com.intellij.psi.util.CachedValuesManager
@@ -225,7 +226,8 @@ class ForDirectiveUtil {
225226
var isBatchAnnotation = false
226227
val forItemDeclarationBlocks =
227228
if (forDirectiveDeclaration.element is SqlElStaticFieldAccessExpr) {
228-
val staticFieldAccessExpr = forDirectiveDeclaration.element as SqlElStaticFieldAccessExpr
229+
val staticFieldAccessExpr =
230+
forDirectiveDeclaration.element as SqlElStaticFieldAccessExpr
229231
staticFieldAccessExpr.accessElements
230232
} else {
231233
forDirectiveDeclaration.getDeclarationChildren()
@@ -234,18 +236,25 @@ class ForDirectiveUtil {
234236
// Defined by StaticFieldAccess
235237
if (forDirectiveDeclaration.element is SqlElStaticFieldAccessExpr) {
236238
val file = topForDirectiveItem.containingFile
237-
val staticFieldAccessExpr = forDirectiveDeclaration.element as SqlElStaticFieldAccessExpr
239+
val staticFieldAccessExpr =
240+
forDirectiveDeclaration.element as SqlElStaticFieldAccessExpr
238241
val clazz = staticFieldAccessExpr.elClass
239242
val staticElement = PsiStaticElement(clazz.elIdExprList, file)
240243
val referenceClazz = staticElement.getRefClazz() ?: return null
241244

242245
// In the case of staticFieldAccess, the property that is called first is retrieved.
243-
fieldAccessTopParentClass = getStaticFieldAccessTopElementClassType(staticFieldAccessExpr, referenceClazz)
246+
fieldAccessTopParentClass =
247+
getStaticFieldAccessTopElementClassType(
248+
staticFieldAccessExpr,
249+
referenceClazz,
250+
)
244251
} else {
245252
// Defined by Dao parameter
246253
val file = topForDirectiveItem.containingFile ?: return null
247254
val daoMethod = findDaoMethod(file) ?: return null
248-
val topElementText = forDirectiveDeclaration.getDeclarationChildren().firstOrNull()?.text ?: return null
255+
val topElementText =
256+
forDirectiveDeclaration.getDeclarationChildren().firstOrNull()?.text
257+
?: return null
249258
isBatchAnnotation = PsiDaoMethod(project, daoMethod).daoType.isBatchAnnotation()
250259

251260
val matchParam = daoMethod.findParameter(cleanString(topElementText))
@@ -450,5 +459,28 @@ class ForDirectiveUtil {
450459
} else {
451460
""
452461
}
462+
463+
fun resolveForDirectiveClassTypeIfSuffixExists(
464+
project: Project,
465+
searchName: String,
466+
): PsiType? {
467+
if (searchName.endsWith("_has_next")) {
468+
return PsiType.getTypeByName(
469+
"java.lang.Boolean",
470+
project,
471+
GlobalSearchScope.allScope(project),
472+
)
473+
}
474+
475+
if (searchName.endsWith("_index")) {
476+
return PsiType.getTypeByName(
477+
"java.lang.Integer",
478+
project,
479+
GlobalSearchScope.allScope(project),
480+
)
481+
}
482+
483+
return null
484+
}
453485
}
454486
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,20 +419,28 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
419419
positionText: String,
420420
result: CompletionResultSet,
421421
): Boolean {
422+
val searchWord = cleanString(positionText)
422423
val project = top.project
423424
val forDirectiveBlocks = ForDirectiveUtil.getForDirectiveBlocks(top)
424425
ForDirectiveUtil.findForItem(top, forDirectives = forDirectiveBlocks) ?: return false
425426

426427
val forItemClassType = ForDirectiveUtil.getForDirectiveItemClassType(project, forDirectiveBlocks) ?: return false
428+
val specifiedClassType = ForDirectiveUtil.resolveForDirectiveClassTypeIfSuffixExists(project, top.text)
429+
val topClassType =
430+
if (specifiedClassType != null) {
431+
PsiParentClass(specifiedClassType)
432+
} else {
433+
forItemClassType
434+
}
435+
427436
val result =
428437
ForDirectiveUtil.getFieldAccessLastPropertyClassType(
429438
elements,
430439
project,
431-
forItemClassType,
440+
topClassType,
432441
shortName = "",
433442
dropLastIndex = 1,
434443
complete = { lastType ->
435-
val searchWord = cleanString(positionText)
436444
setFieldsAndMethodsCompletionResultSet(
437445
lastType.searchField(searchWord)?.toTypedArray() ?: emptyArray(),
438446
lastType.searchMethod(searchWord)?.toTypedArray() ?: emptyArray(),

src/main/kotlin/org/domaframework/doma/intellij/document/generator/DocumentDaoParameterGenerator.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ class DocumentDaoParameterGenerator(
4646

4747
var isBatchAnnotation = false
4848
if (ForDirectiveUtil.findForItem(searchElement, forDirectives = forDirectives) != null) {
49-
topParentType = ForDirectiveUtil.getForDirectiveItemClassType(project, forDirectives)
49+
val forItemClassType = ForDirectiveUtil.getForDirectiveItemClassType(project, forDirectives)
50+
val specifiedClassType = ForDirectiveUtil.resolveForDirectiveClassTypeIfSuffixExists(project, searchElement.text)
51+
topParentType =
52+
if (specifiedClassType != null) {
53+
PsiParentClass(specifiedClassType)
54+
} else {
55+
forItemClassType
56+
}
5057
} else {
5158
val daoMethod = findDaoMethod(originalElement.containingFile) ?: return
5259
val param = daoMethod.findParameter(originalElement.text) ?: return

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ class SqlInspectionVisitor(
135135
errorHighlight(topElement, daoMethod, holder)
136136
return
137137
}
138-
result
138+
val specifiedClassType =
139+
ForDirectiveUtil.resolveForDirectiveClassTypeIfSuffixExists(project, topElement.text)
140+
if (specifiedClassType != null) {
141+
PsiParentClass(specifiedClassType)
142+
} else {
143+
result
144+
}
139145
} else {
140146
val paramType = daoMethod.findParameter(cleanString(topElement.text))?.type
141147
if (paramType == null) {

0 commit comments

Comments
 (0)