-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intellij): add kotlin language support for collecting declaratio…
…n. (#3415)
- Loading branch information
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...rc/main/kotlin/com/tabbyml/intellijtabby/languageSupport/KotlinLanguageSupportProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.tabbyml.intellijtabby.languageSupport | ||
|
||
import com.intellij.openapi.application.runReadAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.tabbyml.intellijtabby.languageSupport.LanguageSupportProvider.* | ||
import io.ktor.util.* | ||
import org.eclipse.lsp4j.SemanticTokenTypes | ||
import org.jetbrains.kotlin.psi.* | ||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType | ||
|
||
class KotlinLanguageSupportProvider : LanguageSupportProvider { | ||
override fun provideSemanticTokensRange(project: Project, fileRange: FileRange): List<SemanticToken>? { | ||
val psiFile = fileRange.file | ||
if (psiFile.language.id.toUpperCasePreservingASCIIRules() != "KOTLIN") { | ||
return null | ||
} | ||
return runReadAction { | ||
val semanticTokens = mutableListOf<SemanticToken>() | ||
psiFile.forEachDescendantOfType<KtReferenceExpression> { element -> | ||
if (element.children.isEmpty() && fileRange.range.contains(element.textRange)) { | ||
val referenceTarget = | ||
element.references.map { it.resolve() }.firstNotNullOfOrNull { it } ?: return@forEachDescendantOfType | ||
val type = parseReferenceType(referenceTarget) | ||
semanticTokens.add( | ||
SemanticToken( | ||
text = element.text, | ||
range = element.textRange, | ||
type = type, | ||
) | ||
) | ||
} | ||
} | ||
semanticTokens.toList() | ||
} | ||
} | ||
|
||
override fun provideDeclaration(project: Project, filePosition: FilePosition): List<FileRange>? { | ||
val psiFile = filePosition.file | ||
if (psiFile.language.id.toUpperCasePreservingASCIIRules() != "KOTLIN") { | ||
return null | ||
} | ||
return runReadAction { | ||
val element = psiFile.findElementAt(filePosition.offset) | ||
val referenceExpression = | ||
element as? KtReferenceExpression ?: element?.parent as? KtReferenceExpression ?: return@runReadAction listOf() | ||
val referenceTarget = | ||
referenceExpression.references.map { it.resolve() }.firstNotNullOfOrNull { it } ?: return@runReadAction listOf() | ||
val file = referenceTarget.containingFile ?: return@runReadAction listOf() | ||
val range = referenceTarget.textRange | ||
listOf(FileRange(file, range)) | ||
} | ||
} | ||
|
||
private fun parseReferenceType(referenceTarget: PsiElement): String { | ||
return when (referenceTarget) { | ||
is KtClassOrObject -> SemanticTokenTypes.Class | ||
is KtFunction -> SemanticTokenTypes.Function | ||
is KtProperty -> SemanticTokenTypes.Property | ||
is KtParameter -> SemanticTokenTypes.Parameter | ||
is KtVariableDeclaration -> SemanticTokenTypes.Variable | ||
|
||
// 1. Fallback to `Type` for other kotlin element declarations | ||
// 2. The reference target may be declared in Java, fallback to `Type` for now | ||
else -> SemanticTokenTypes.Type | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
clients/intellij/src/main/resources/META-INF/plugin-kotlin.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<idea-plugin> | ||
<extensions defaultExtensionNs="com.tabbyml.intellij-tabby"> | ||
<languageSupportProvider | ||
implementation="com.tabbyml.intellijtabby.languageSupport.KotlinLanguageSupportProvider"/> | ||
</extensions> | ||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters