Skip to content

feat: Add implementation provider support #652

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class KotlinLanguageServer(
serverCapabilities.documentRangeFormattingProvider = Either.forLeft(true)
serverCapabilities.executeCommandProvider = ExecuteCommandOptions(ALL_COMMANDS)
serverCapabilities.documentHighlightProvider = Either.forLeft(true)
serverCapabilities.implementationProvider = Either.forLeft(true)

val storagePath = getStoragePath(params)
databaseService.setup(storagePath)
Expand Down
15 changes: 15 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import java.io.Closeable
import java.nio.file.Path
import java.time.Duration
import java.util.concurrent.CompletableFuture
import org.javacs.kt.implementation.findImplementation

class KotlinTextDocumentService(
private val sf: SourceFiles,
Expand Down Expand Up @@ -267,6 +268,20 @@ class KotlinTextDocumentService(
TODO("not implemented")
}

override fun implementation(params: ImplementationParams): CompletableFuture<Either<List<Location>, List<LocationLink>>> = async.compute {
reportTime {
LOG.info("Find implementation at {}", describePosition(params))

val (file, cursor) = recover(params, Recompile.NEVER) ?: return@compute Either.forLeft(emptyList())
val implementations = findImplementation(sp, sf, file, cursor)
if (implementations.isEmpty()) {
noResult("No implementations found at ${describePosition(params)}", Either.forLeft(emptyList()))
} else {
Either.forLeft(implementations)
}
}
}

private fun describePosition(position: TextDocumentPositionParams): String {
return "${describeURI(position.textDocument.uri)} ${position.position.line + 1}:${position.position.character + 1}"
}
Expand Down
9 changes: 9 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/SourceFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.streams.toList

private class SourceVersion(val content: String, val version: Int, val language: Language?, val isTemporary: Boolean)

Expand Down Expand Up @@ -80,6 +81,14 @@ class SourceFiles(
}
}

fun getAllUris(): List<URI> {
return workspaceRoots.flatMap { root ->
Files.walk(root)
.filter { it.toString().endsWith(".kt") || it.toString().endsWith(".kts") }
.map { it.toUri() }.toList()
}
}

fun close(uri: URI) {
if (uri in open) {
open.remove(uri)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.javacs.kt.implementation

import org.eclipse.lsp4j.Location
import org.javacs.kt.CompiledFile
import org.javacs.kt.SourceFiles
import org.javacs.kt.SourcePath
import org.javacs.kt.LOG
import org.javacs.kt.position.location
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces


fun findImplementation(
sp: SourcePath,
sf: SourceFiles,
file: CompiledFile,
cursor: Int
): List<Location> {
val (_, target) = file.referenceExpressionAtPoint(cursor) ?: return emptyList()

LOG.info("Finding implementations for declaration descriptor {}", target)

return when (target) {
is ClassDescriptor -> findImplementations(sp, sf, file, target)
else -> emptyList()
}
}

private fun findImplementations(sp: SourcePath, sf: SourceFiles, file: CompiledFile, descriptor: ClassDescriptor): List<Location> {
val implementations = mutableListOf<Location>()

// Get all Kotlin file URIs by scanning workspace roots
val allUris = sf.getAllUris()
if (descriptor.kind == ClassKind.INTERFACE) {
// Find all classes that implement this interface
allUris.forEach { uri ->
val ktFile = sp.parsedFile(uri)
ktFile.declarations.filterIsInstance<KtClassOrObject>().forEach { ktClass ->
val classDesc = file.compile.get(BindingContext.CLASS, ktClass)
if (classDesc != null && descriptor in classDesc.getSuperInterfaces()) {
location(ktClass)?.let { implementations.add(it) }
}
}
}
} else if (descriptor.kind == ClassKind.CLASS) {
// Find all subclasses
allUris.forEach { uri ->
val ktFile = sp.parsedFile(uri)
ktFile.declarations.filterIsInstance<KtClassOrObject>().forEach { ktClass ->
val classDesc = file.compile.get(BindingContext.CLASS, ktClass)
if (classDesc?.getSuperClassOrAny() == descriptor) {
location(ktClass)?.let { implementations.add(it) }
}
}
}
}

return implementations
}