Skip to content

Commit 68c9bb6

Browse files
authored
Merge pull request #160 from domaframework/feature/add-suggestion-for-directive-item
Completion for directive items
2 parents 7e840cf + 48ab871 commit 68c9bb6

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
275275
val searchText = cleanString(getSearchElementText(position))
276276
var topElementType: PsiType? = null
277277
if (elements.isEmpty() && daoMethod != null) {
278-
getElementTypeByFieldAccess(originalFile, elements, daoMethod, result)
278+
getElementTypeByFieldAccess(originalFile, position, elements, daoMethod, result)
279279
return
280280
}
281281
val top = elements.first()
@@ -301,7 +301,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
301301
isBatchAnnotation = psiDaoMethod.daoType.isBatchAnnotation()
302302
if (isFieldAccessByForItem(top, elements, searchText, isBatchAnnotation, result)) return
303303
topElementType =
304-
getElementTypeByFieldAccess(originalFile, elements, daoMethod, result) ?: return
304+
getElementTypeByFieldAccess(originalFile, position, elements, daoMethod, result) ?: return
305305
}
306306

307307
setCompletionFieldAccess(
@@ -351,18 +351,22 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
351351
*/
352352
private fun getElementTypeByFieldAccess(
353353
originalFile: PsiFile,
354+
position: PsiElement,
354355
elements: List<PsiElement>,
355356
daoMethod: PsiMethod,
356357
result: CompletionResultSet,
357358
): PsiType? {
358-
val topText = cleanString(getSearchElementText(elements.firstOrNull()))
359+
val topElement = elements.firstOrNull()
360+
val topText = cleanString(getSearchElementText(topElement))
359361
val matchParams = daoMethod.searchParameter(topText)
360362
val findParam = matchParams.find { it.name == topText }
361363
if (elements.size <= 1 && findParam == null) {
362-
// TODO Add For Directive Items
363364
matchParams.map { match ->
364365
result.addElement(LookupElementBuilder.create(match.name))
365366
}
367+
// Add ForDirective Items
368+
val forDirectives = ForDirectiveUtil.getForDirectiveBlocks(position)
369+
addForDirectiveSuggestions(forDirectives, result)
366370
return null
367371
}
368372
if (findParam == null) {
@@ -372,6 +376,17 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
372376
return PsiClassTypeUtil.convertOptionalType(immediate, originalFile.project)
373377
}
374378

379+
private fun addForDirectiveSuggestions(
380+
forDirectives: List<ForDirectiveUtil.BlockToken>,
381+
result: CompletionResultSet,
382+
) {
383+
forDirectives.forEach {
384+
result.addElement(LookupElementBuilder.create(it.item.text))
385+
result.addElement(LookupElementBuilder.create("${it.item.text}_has_next"))
386+
result.addElement(LookupElementBuilder.create("${it.item.text}_index"))
387+
}
388+
}
389+
375390
private fun getRefClazz(
376391
top: PsiElement,
377392
fqdnGetter: () -> String,

src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class SqlCompleteTest : DomaSqlTest() {
5858
"$testDapName/completeOptionalStaticProperty.sql",
5959
"$testDapName/completeOptionalByForItem.sql",
6060
"$testDapName/completeOptionalBatchAnnotation.sql",
61+
"$testDapName/completeForDirectiveItem.sql",
6162
)
6263
myFixture.enableInspections(SqlBindVariableValidInspector())
6364
}
@@ -391,6 +392,14 @@ class SqlCompleteTest : DomaSqlTest() {
391392
)
392393
}
393394

395+
fun testCompleteForDirectiveItem() {
396+
innerDirectiveCompleteTest(
397+
"$testDapName/completeForDirectiveItem.sql",
398+
listOf("projects", "project", "project_has_next", "project_index"),
399+
listOf("get()", "size()", "toString()", "projectId"),
400+
)
401+
}
402+
394403
private fun innerDirectiveCompleteTest(
395404
sqlFileName: String,
396405
expectedSuggestions: List<String>,

src/test/testData/src/main/java/doma/example/dao/SqlCompleteTestDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,7 @@ interface SqlCompleteTestDao {
9797
@BatchDelete(sqlFile = true)
9898
int completeOptionalBatchAnnotation(Optional<List<Optional<Project>>> projects);
9999

100+
@Select
101+
Employee completeForDirectiveItem(List<Project> projects);
102+
100103
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SELECT id
2+
, name
3+
, age
4+
FROM employees
5+
WHERE
6+
/*%for project : projects */
7+
id = /* <caret> */0
8+
/*%if project_has_next */
9+
/*# "OR" */
10+
/*%end */
11+
/*%end */

0 commit comments

Comments
 (0)