From 69eaf4fe24f6a1eb06e29a135581eaa239e0404c Mon Sep 17 00:00:00 2001 From: xterao Date: Fri, 9 May 2025 15:06:43 +0900 Subject: [PATCH 1/2] Fixed "has_next" and "_index" element types to be treated as primitive types --- .../intellij/common/util/ForDirectiveUtil.kt | 29 +++++-------------- .../intellij/complate/sql/SqlCompleteTest.kt | 14 +++++++-- .../document/SqlSymbolDocumentTestCase.kt | 4 +-- .../bindVariableForItemHasNextAndIndex.sql | 8 ++--- .../completeForItemIndex.sql | 2 +- .../documentForItemElementByFieldAccess.sql | 2 +- 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt index 877a8295..c217a100 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt @@ -20,7 +20,7 @@ import com.intellij.psi.PsiClass import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElement import com.intellij.psi.PsiType -import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.PsiTypes import com.intellij.psi.util.CachedValue import com.intellij.psi.util.CachedValueProvider import com.intellij.psi.util.CachedValuesManager @@ -337,6 +337,7 @@ class ForDirectiveUtil { topParent } val parentType = parent.type + // TODO: Display an error message that the property cannot be called. val classType = parentType as? PsiClassType ?: return null var competeResult: ValidationCompleteResult? = null @@ -460,27 +461,13 @@ class ForDirectiveUtil { "" } - fun resolveForDirectiveClassTypeIfSuffixExists( - project: Project, - searchName: String, - ): PsiType? { + fun resolveForDirectiveItemClassTypeBySuffixElement(searchName: String): PsiType? = if (searchName.endsWith("_has_next")) { - return PsiType.getTypeByName( - "java.lang.Boolean", - project, - GlobalSearchScope.allScope(project), - ) - } - - if (searchName.endsWith("_index")) { - return PsiType.getTypeByName( - "java.lang.Integer", - project, - GlobalSearchScope.allScope(project), - ) + PsiTypes.booleanType() + } else if (searchName.endsWith("_index")) { + PsiTypes.intType() + } else { + null } - - return null - } } } diff --git a/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt b/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt index 5cc758ea..96b18692 100644 --- a/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt +++ b/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt @@ -112,21 +112,32 @@ class SqlCompleteTest : DomaSqlTest() { fun testCompleteForItemHasNext() { innerDirectiveCompleteTest( "$testDapName/completeForItemHasNext.sql", + emptyList(), listOf( + "get()", + "startsWith()", + "permissions", + "MAX_VALUE", + "MIN_VALUE", "FALSE", "TRUE", "TYPE", "toString()", "booleanValue()", ), - listOf("get()", "startsWith()", "permissions", "MAX_VALUE", "MIN_VALUE"), ) } fun testCompleteForItemIndex() { innerDirectiveCompleteTest( "$testDapName/completeForItemIndex.sql", + emptyList(), listOf( + "get()", + "startsWith()", + "permissions", + "FALSE", + "TRUE", "BYTES", "MAX_VALUE", "MIN_VALUE", @@ -134,7 +145,6 @@ class SqlCompleteTest : DomaSqlTest() { "TYPE", "Integer()", ), - listOf("get()", "startsWith()", "permissions", "FALSE", "TRUE"), ) } diff --git a/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt b/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt index bfff1e99..4cd8df16 100644 --- a/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt +++ b/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt @@ -101,7 +101,7 @@ class SqlSymbolDocumentTestCase : DomaSqlTest() { fun testDocumentForItemHasNext() { val sqlName = "documentForItemHasNext" val result = - "Boolean item_has_next" + "boolean item_has_next" documentationFindTextTest(sqlName, "item_has_next", result) } @@ -109,7 +109,7 @@ class SqlSymbolDocumentTestCase : DomaSqlTest() { fun testDocumentForItemIndex() { val sqlName = "documentForItemIndex" val result = - "Integer item_index" + "int item_index" documentationFindTextTest(sqlName, "item_index", result) } diff --git a/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql b/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql index f608f1a3..0b2c6733 100644 --- a/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql +++ b/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql @@ -17,10 +17,10 @@ select p.project_id /*# "or" */ /*%end */ p.employee_id = /* member.employeeId */0 - and p.not_next = /* member_has_next.NotTRUE */false - and p.next = /* member_has_next.TRUE */false - and p.not_index = /* member_index.nextValue() */999 - and p.index = /* member_index.MIN_VALUE */0 + and p.not_next = /* member_has_next */false + and p.next = /* member_has_next */false + and p.not_index = /* member_index */999 + and p.index = /* member_index */0 /*%end */ p.employee_id = /* employees.get(0).employeeId */0 /*%end */ \ No newline at end of file diff --git a/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql b/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql index 6cd5603a..2291af63 100644 --- a/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql +++ b/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql @@ -3,6 +3,6 @@ where /*%for item : principal.permissions */ index = /* item_index. */0 /*%if item_has_next */ - OR flag = /* item_has_next.FALSE */false + OR flag = /* item_has_next */false /*%end */ /*%end */ \ No newline at end of file diff --git a/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql b/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql index 90886586..a62f054e 100644 --- a/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql +++ b/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql @@ -3,7 +3,7 @@ SELECT id WHERE category = 'category' -- employeesList -> List> /*%for employees : employeesList */ - -- employees_has_next -> -> List + -- employees_has_next -> boolean /*%if employees_has_next */ /*# "OR" */ /*%end */ From 20720f93df8a05b0574eb15ecdd07c9434e8f881 Mon Sep 17 00:00:00 2001 From: xterao Date: Fri, 9 May 2025 15:06:43 +0900 Subject: [PATCH 2/2] Fixed "has_next" and "_index" element types to be treated as primitive types --- .../intellij/common/util/ForDirectiveUtil.kt | 29 +++++-------------- .../SqlParameterCompletionProvider.kt | 2 +- .../DocumentDaoParameterGenerator.kt | 5 +++- .../sql/visitor/SqlInspectionVisitor.kt | 2 +- .../intellij/complate/sql/SqlCompleteTest.kt | 14 +++++++-- .../document/SqlSymbolDocumentTestCase.kt | 4 +-- .../bindVariableForItemHasNextAndIndex.sql | 8 ++--- .../completeForItemIndex.sql | 2 +- .../documentForItemElementByFieldAccess.sql | 2 +- 9 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt index 877a8295..c217a100 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/util/ForDirectiveUtil.kt @@ -20,7 +20,7 @@ import com.intellij.psi.PsiClass import com.intellij.psi.PsiClassType import com.intellij.psi.PsiElement import com.intellij.psi.PsiType -import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.PsiTypes import com.intellij.psi.util.CachedValue import com.intellij.psi.util.CachedValueProvider import com.intellij.psi.util.CachedValuesManager @@ -337,6 +337,7 @@ class ForDirectiveUtil { topParent } val parentType = parent.type + // TODO: Display an error message that the property cannot be called. val classType = parentType as? PsiClassType ?: return null var competeResult: ValidationCompleteResult? = null @@ -460,27 +461,13 @@ class ForDirectiveUtil { "" } - fun resolveForDirectiveClassTypeIfSuffixExists( - project: Project, - searchName: String, - ): PsiType? { + fun resolveForDirectiveItemClassTypeBySuffixElement(searchName: String): PsiType? = if (searchName.endsWith("_has_next")) { - return PsiType.getTypeByName( - "java.lang.Boolean", - project, - GlobalSearchScope.allScope(project), - ) - } - - if (searchName.endsWith("_index")) { - return PsiType.getTypeByName( - "java.lang.Integer", - project, - GlobalSearchScope.allScope(project), - ) + PsiTypes.booleanType() + } else if (searchName.endsWith("_index")) { + PsiTypes.intType() + } else { + null } - - return null - } } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt b/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt index 2538e08c..22005f13 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt @@ -425,7 +425,7 @@ class SqlParameterCompletionProvider : CompletionProvider( ForDirectiveUtil.findForItem(top, forDirectives = forDirectiveBlocks) ?: return false val forItemClassType = ForDirectiveUtil.getForDirectiveItemClassType(project, forDirectiveBlocks) ?: return false - val specifiedClassType = ForDirectiveUtil.resolveForDirectiveClassTypeIfSuffixExists(project, top.text) + val specifiedClassType = ForDirectiveUtil.resolveForDirectiveItemClassTypeBySuffixElement(top.text) val topClassType = if (specifiedClassType != null) { PsiParentClass(specifiedClassType) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/document/generator/DocumentDaoParameterGenerator.kt b/src/main/kotlin/org/domaframework/doma/intellij/document/generator/DocumentDaoParameterGenerator.kt index 20b5602d..7121b363 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/document/generator/DocumentDaoParameterGenerator.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/document/generator/DocumentDaoParameterGenerator.kt @@ -47,7 +47,10 @@ class DocumentDaoParameterGenerator( var isBatchAnnotation = false if (ForDirectiveUtil.findForItem(searchElement, forDirectives = forDirectives) != null) { val forItemClassType = ForDirectiveUtil.getForDirectiveItemClassType(project, forDirectives) - val specifiedClassType = ForDirectiveUtil.resolveForDirectiveClassTypeIfSuffixExists(project, searchElement.text) + val specifiedClassType = + ForDirectiveUtil.resolveForDirectiveItemClassTypeBySuffixElement( + searchElement.text, + ) topParentType = if (specifiedClassType != null) { PsiParentClass(specifiedClassType) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/visitor/SqlInspectionVisitor.kt b/src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/visitor/SqlInspectionVisitor.kt index 7ac21ba2..1d23cd2b 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/visitor/SqlInspectionVisitor.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/visitor/SqlInspectionVisitor.kt @@ -136,7 +136,7 @@ class SqlInspectionVisitor( return } val specifiedClassType = - ForDirectiveUtil.resolveForDirectiveClassTypeIfSuffixExists(project, topElement.text) + ForDirectiveUtil.resolveForDirectiveItemClassTypeBySuffixElement(topElement.text) if (specifiedClassType != null) { PsiParentClass(specifiedClassType) } else { diff --git a/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt b/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt index 5cc758ea..96b18692 100644 --- a/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt +++ b/src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt @@ -112,21 +112,32 @@ class SqlCompleteTest : DomaSqlTest() { fun testCompleteForItemHasNext() { innerDirectiveCompleteTest( "$testDapName/completeForItemHasNext.sql", + emptyList(), listOf( + "get()", + "startsWith()", + "permissions", + "MAX_VALUE", + "MIN_VALUE", "FALSE", "TRUE", "TYPE", "toString()", "booleanValue()", ), - listOf("get()", "startsWith()", "permissions", "MAX_VALUE", "MIN_VALUE"), ) } fun testCompleteForItemIndex() { innerDirectiveCompleteTest( "$testDapName/completeForItemIndex.sql", + emptyList(), listOf( + "get()", + "startsWith()", + "permissions", + "FALSE", + "TRUE", "BYTES", "MAX_VALUE", "MIN_VALUE", @@ -134,7 +145,6 @@ class SqlCompleteTest : DomaSqlTest() { "TYPE", "Integer()", ), - listOf("get()", "startsWith()", "permissions", "FALSE", "TRUE"), ) } diff --git a/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt b/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt index bfff1e99..4cd8df16 100644 --- a/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt +++ b/src/test/kotlin/org/domaframework/doma/intellij/document/SqlSymbolDocumentTestCase.kt @@ -101,7 +101,7 @@ class SqlSymbolDocumentTestCase : DomaSqlTest() { fun testDocumentForItemHasNext() { val sqlName = "documentForItemHasNext" val result = - "Boolean item_has_next" + "boolean item_has_next" documentationFindTextTest(sqlName, "item_has_next", result) } @@ -109,7 +109,7 @@ class SqlSymbolDocumentTestCase : DomaSqlTest() { fun testDocumentForItemIndex() { val sqlName = "documentForItemIndex" val result = - "Integer item_index" + "int item_index" documentationFindTextTest(sqlName, "item_index", result) } diff --git a/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql b/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql index f608f1a3..0b2c6733 100644 --- a/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql +++ b/src/test/testData/src/main/resources/META-INF/doma/example/dao/EmployeeSummaryDao/bindVariableForItemHasNextAndIndex.sql @@ -17,10 +17,10 @@ select p.project_id /*# "or" */ /*%end */ p.employee_id = /* member.employeeId */0 - and p.not_next = /* member_has_next.NotTRUE */false - and p.next = /* member_has_next.TRUE */false - and p.not_index = /* member_index.nextValue() */999 - and p.index = /* member_index.MIN_VALUE */0 + and p.not_next = /* member_has_next */false + and p.next = /* member_has_next */false + and p.not_index = /* member_index */999 + and p.index = /* member_index */0 /*%end */ p.employee_id = /* employees.get(0).employeeId */0 /*%end */ \ No newline at end of file diff --git a/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql b/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql index 6cd5603a..2291af63 100644 --- a/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql +++ b/src/test/testData/src/main/resources/META-INF/doma/example/dao/SqlCompleteTestDao/completeForItemIndex.sql @@ -3,6 +3,6 @@ where /*%for item : principal.permissions */ index = /* item_index. */0 /*%if item_has_next */ - OR flag = /* item_has_next.FALSE */false + OR flag = /* item_has_next */false /*%end */ /*%end */ \ No newline at end of file diff --git a/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql b/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql index 90886586..a62f054e 100644 --- a/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql +++ b/src/test/testData/src/main/resources/META-INF/doma/example/dao/document/DocumentTestDao/documentForItemElementByFieldAccess.sql @@ -3,7 +3,7 @@ SELECT id WHERE category = 'category' -- employeesList -> List> /*%for employees : employeesList */ - -- employees_has_next -> -> List + -- employees_has_next -> boolean /*%if employees_has_next */ /*# "OR" */ /*%end */