Skip to content

Commit

Permalink
feat(intellij): add kotlin language support for collecting declaratio…
Browse files Browse the repository at this point in the history
…n. (#3415)
  • Loading branch information
icycodes authored Nov 15, 2024
1 parent 4787ef7 commit e9b2ded
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
7 changes: 6 additions & 1 deletion clients/intellij/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ repositories {
dependencies {
intellijPlatform {
intellijIdeaCommunity("2023.1")
bundledPlugins(listOf("Git4Idea"))
bundledPlugins(
listOf(
"Git4Idea",
"org.jetbrains.kotlin",
)
)
pluginVerifier()
zipSigner()
instrumentationTools()
Expand Down
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
}
}
}
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>
2 changes: 1 addition & 1 deletion clients/intellij/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>
<depends optional="true" config-file="plugin-Git4Idea.xml">Git4Idea</depends>

<depends optional="true" config-file="plugin-kotlin.xml">org.jetbrains.kotlin</depends>

<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
Expand Down

0 comments on commit e9b2ded

Please sign in to comment.