Skip to content

Commit

Permalink
fix(intellij): add try-catch for findTargetElement.
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes committed Nov 13, 2024
1 parent c73719d commit c1a1581
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tabbyml.intellijtabby.languageSupport

import com.intellij.codeInsight.TargetElementUtil
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiRecursiveElementWalkingVisitor
Expand All @@ -16,6 +17,7 @@ import org.eclipse.lsp4j.SemanticTokenTypes
* This implementation may not work effectively for all languages.
*/
open class DefaultLanguageSupportProvider : LanguageSupportProvider {
private val logger = logger<DefaultLanguageSupportProvider>()
private val targetElementUtil = TargetElementUtil.getInstance()

override fun provideSemanticTokensRange(project: Project, fileRange: FileRange): List<SemanticToken>? {
Expand All @@ -40,12 +42,16 @@ open class DefaultLanguageSupportProvider : LanguageSupportProvider {
})

leafElements.mapNotNull {
val target =
val target = try {
targetElementUtil.findTargetElement(
editor.editor,
TargetElementUtil.ELEMENT_NAME_ACCEPTED or TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED,
it.textRange.startOffset
)
} catch (e: Exception) {
logger.debug("Failed to find target element when providing semantic tokens", e)
null
}
if (target == it || target == null || target.text == null) {
null
} else {
Expand All @@ -64,11 +70,16 @@ open class DefaultLanguageSupportProvider : LanguageSupportProvider {
val editor = project.findEditor(psiFile.virtualFile) ?: return null

return runReadAction {
val target = targetElementUtil.findTargetElement(
editor.editor,
TargetElementUtil.ELEMENT_NAME_ACCEPTED or TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED,
filePosition.offset
)
val target = try {
targetElementUtil.findTargetElement(
editor.editor,
TargetElementUtil.ELEMENT_NAME_ACCEPTED or TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED,
filePosition.offset
)
} catch (e: Exception) {
logger.debug("Failed to find target element at ${psiFile.virtualFile.url}:${filePosition.offset}", e)
null
}
val file = target?.containingFile ?: return@runReadAction listOf()
val range = target.textRange
listOf(FileRange(file, range))
Expand Down

0 comments on commit c1a1581

Please sign in to comment.