Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class StaticDirectiveHandler(
): Boolean {
val clazzRef =
PsiTreeUtil
.getChildOfType(element.prevSibling, SqlElClass::class.java) // getStaticFieldAccessClazzRef(element) ?: return false
.getChildOfType(element.prevSibling, SqlElClass::class.java)
val fqdn =
PsiTreeUtil.getChildrenOfTypeAsList(clazzRef, PsiElement::class.java).joinToString("") { it.text }
val candidates = processor(fqdn, bindText) ?: return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import org.domaframework.doma.intellij.psi.SqlElLeExpr
import org.domaframework.doma.intellij.psi.SqlElLtExpr
import org.domaframework.doma.intellij.psi.SqlElNeExpr
import org.domaframework.doma.intellij.psi.SqlElOrExpr
import org.domaframework.doma.intellij.psi.SqlElParameters
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
import org.domaframework.doma.intellij.psi.SqlTypes
import org.jetbrains.kotlin.idea.base.util.module
Expand Down Expand Up @@ -164,11 +165,12 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
return prevElms
}
}

// If the parent has field access, get its child element
if (targetElement.parent is SqlElFieldAccessExpr) {
if (parent is SqlElFieldAccessExpr) {
blocks =
PsiTreeUtil
.getChildrenOfTypeAsList(targetElement.parent, PsiElement::class.java)
.getChildrenOfTypeAsList(parent, PsiElement::class.java)
.filter {
(
it is SqlElPrimaryExpr ||
Expand All @@ -178,7 +180,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
}.toList()
if (blocks.isEmpty()) {
val parent =
PsiTreeUtil.findFirstParent(targetElement.parent) {
PsiTreeUtil.findFirstParent(parent) {
it !is PsiDirectory &&
it !is PsiFile &&
it is SqlElFieldAccessExpr
Expand All @@ -192,14 +194,38 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
(targetElement.startOffsetInParent) >= it.startOffsetInParent
}.toList()
}
} else {
// Completion for the first argument
var parameterParent: PsiElement? =
PsiTreeUtil.getParentOfType(targetElement, SqlElParameters::class.java)
if (parameterParent != null) {
blocks = emptyList()
} else {
// Completion for subsequent arguments
parameterParent =
targetElement.prevLeafs
.takeWhile {
it.isNotWhiteSpace() &&
it.elementType != SqlTypes.LEFT_PAREN
}.firstOrNull {
PsiTreeUtil.getParentOfType(
it,
SqlElParameters::class.java,
) != null
}
if (parameterParent != null) {
blocks = emptyList()
}
}
}

// If the element has no parent-child relationship,
// create a list that also adds itself at the end.
if (blocks.isEmpty()) {
val prevElms =
targetElement.findSelfBlocks()
if (prevElms.isNotEmpty()) {
return prevElms
blocks = prevElms
}
}
return blocks.sortedBy { it.textOffset }
Expand All @@ -220,7 +246,8 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
elements.isEmpty() -> return
else -> elements.first()
}
val topText = cleanString(top.text)

val topText = cleanString(getSearchElementText(top))
val prevWord = PsiPatternUtil.getBindSearchWord(originalFile, elements.last(), " ")
if (prevWord.startsWith("@") && prevWord.endsWith("@")) {
val clazz = getRefClazz(top) { prevWord.replace("@", "") } ?: return
Expand Down Expand Up @@ -252,7 +279,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
var listParamIndex = 0
for (elm in elements.drop(1)) {
index++
val searchElm = cleanString(elm.text)
val searchElm = cleanString(getSearchElementText(elm))
if (searchElm.isEmpty()) {
setFieldsAndMethodsCompletionResultSet(
(psiParentClass.searchField(searchElm)?.toTypedArray() ?: emptyArray()),
Expand Down Expand Up @@ -287,6 +314,18 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
}
}

private fun getSearchElementText(elm: PsiElement): String =
if (elm is SqlElPrimaryExpr || elm.elementType == SqlTypes.EL_IDENTIFIER) {
elm.text
} else {
""
}

/**
* Retrieves the referenced class from a static field access element
* and searches for a field or method matching the specified identifier name.
* If no match is found, returns null.
*/
private fun getElementTypeByStaticFieldAccess(
top: PsiElement,
staticDirective: PsiElement,
Expand All @@ -295,7 +334,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
val clazz =
getRefClazz(top) {
staticDirective.children
.firstOrNull { it.elementType == SqlTypes.EL_CLASS }
.firstOrNull { it is SqlElClass }
?.text
?: ""
} ?: return null
Expand All @@ -304,6 +343,12 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
?: clazz.findStaticMethod(topText)?.returnType
}

/**
* Retrieves the DAO method parameters that match the text of the top element.
* If the element list contains one or fewer items,
* the DAO method parameters are registered as suggestions and this method returns null.
* If there are additional elements, it returns the class type of the top element.
*/
private fun getElementTypeByFieldAccess(
originalFile: PsiFile,
topText: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ fun PsiElement.isNotWhiteSpace(): Boolean = this !is PsiWhiteSpace
fun PsiElement.findSelfBlocks(): List<PsiElement> {
var elms = emptyList<PsiElement>()
for (it in this.prevLeafs) {
// When performing code completion within a [SqlElParameter] element,
// there is a possibility that a “)” may be inserted for the final argument, so it is excluded.
elms = elms.plus(it)
if (!it.isNotWhiteSpace() || it.elementType == SqlTypes.AT_SIGN) break
if (!it.isNotWhiteSpace() ||
it.elementType == SqlTypes.AT_SIGN ||
it.elementType == SqlTypes.LEFT_PAREN ||
it.elementType == SqlTypes.COMMA
) {
break
}
}

elms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class SqlCompleteTest : DomaSqlTest() {
"SqlCompleteTestDao/completeDirectiveFieldInsideFor.sql",
"SqlCompleteTestDao/completeConcatenationOperator.sql",
"SqlCompleteTestDao/completeComparisonOperator.sql",
"SqlCompleteTestDao/completeParameterFirst.sql",
"SqlCompleteTestDao/completeParameterFirstProperty.sql",
"SqlCompleteTestDao/completeParameterSecond.sql",
"SqlCompleteTestDao/completeParameterSecondProperty.sql",
)
myFixture.enableInspections(SqlBindVariableValidInspector())
}
Expand Down Expand Up @@ -227,6 +231,32 @@ class SqlCompleteTest : DomaSqlTest() {
)
}

fun testCompleteParameter() {
innerDirectiveCompleteTest(
"SqlCompleteTestDao/completeParameterFirst.sql",
listOf("employee"),
listOf("employeeId", "department", "rank", "startWith"),
)

innerDirectiveCompleteTest(
"SqlCompleteTestDao/completeParameterFirstProperty.sql",
listOf("employeeId", "department", "rank"),
listOf("employee"),
)

innerDirectiveCompleteTest(
"SqlCompleteTestDao/completeParameterSecond.sql",
listOf("employee"),
listOf("employeeId", "department", "rank", "startWith"),
)

innerDirectiveCompleteTest(
"SqlCompleteTestDao/completeParameterSecondProperty.sql",
listOf("managerId"),
listOf("employee", "department", "rank"),
)
}

private fun innerDirectiveCompleteTest(
sqlFileName: String,
expectedSuggestions: List<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ interface SqlCompleteTestDao {
@Insert(sqlFile = true)
int completeConcatenationOperator(Employee employee,Integer point);

@Select
Employee completeParameterFirst(Employee employee);

@Select
Employee completeParameterFirstProperty(Employee employee);

@Select
Employee completeParameterSecond(Employee employee);

@Select
Employee completeParameterSecondProperty(Employee employee);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ private String getEmployeeRank() {
return rank;
}

public Integer employeeParam(Integer p1, Integer p2) {
return p1 + p2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select * from employee
where id = /* employee.department.startWith(<caret>) */1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from employee where id = /* employee.department.startWith(employee.<caret>) */1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from employee where id = /* employee.employeeParam(3, <caret>) */1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select * from employee
where id = /* employee.employeeParam(employee.employeeId, employee.<caret>m) */1
Loading