From c1a1581af336480d77171e4851bc6f33aa7b9d08 Mon Sep 17 00:00:00 2001 From: Zhiming Ma Date: Fri, 8 Nov 2024 19:27:37 +0800 Subject: [PATCH] fix(intellij): add try-catch for findTargetElement. --- .../DefaultLanguageSupportProvider.kt | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/languageSupport/DefaultLanguageSupportProvider.kt b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/languageSupport/DefaultLanguageSupportProvider.kt index d32205b31910..bd7face285d0 100644 --- a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/languageSupport/DefaultLanguageSupportProvider.kt +++ b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/languageSupport/DefaultLanguageSupportProvider.kt @@ -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 @@ -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() private val targetElementUtil = TargetElementUtil.getInstance() override fun provideSemanticTokensRange(project: Project, fileRange: FileRange): List? { @@ -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 { @@ -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))