Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(intellij): add kotlin language support for collecting declaration. #3415

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading