Skip to content

Commit 6e95d47

Browse files
committed
feat(database): improve finding nearest SQL definition
This commit adds a more efficient way to find the nearest SQL definition in the `SqlLivingDocumentationProvider` class. Instead of using a `when` statement, it now uses `PsiTreeUtil.getParentOfType` to find the closest `SqlDefinition` or `PsiNameIdentifierOwner`. This improves the performance and readability of the code.
1 parent 6669b4b commit 6e95d47

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/provider/SqlLivingDocumentationProvider.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.intellij.openapi.editor.SelectionModel
88
import com.intellij.psi.PsiElement
99
import com.intellij.psi.PsiNameIdentifierOwner
1010
import com.intellij.psi.codeStyle.CodeStyleManager
11+
import com.intellij.psi.util.PsiTreeUtil
1112
import com.intellij.sql.psi.SqlDefinition
1213

1314
class SqlLivingDocumentationProvider : LivingDocumentation {
@@ -35,14 +36,14 @@ class SqlLivingDocumentationProvider : LivingDocumentation {
3536
}
3637

3738
override fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner? {
38-
// todo: find nearest sql definition
39-
when (psiElement) {
40-
is SqlDefinition -> {
41-
return psiElement
42-
}
39+
if (psiElement is SqlDefinition) return psiElement
40+
41+
val closestId = PsiTreeUtil.getParentOfType(psiElement, PsiNameIdentifierOwner::class.java)
42+
if (closestId !is SqlDefinition) {
43+
return PsiTreeUtil.getParentOfType(psiElement, SqlDefinition::class.java) ?: closestId
4344
}
4445

45-
return null
46+
return closestId
4647
}
4748

4849
override fun findDocTargetsInSelection(

0 commit comments

Comments
 (0)