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

Support for inlay hints #498

Merged
merged 21 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -77,6 +77,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
serverCapabilities.workspace.workspaceFolders = WorkspaceFoldersOptions()
serverCapabilities.workspace.workspaceFolders.supported = true
serverCapabilities.workspace.workspaceFolders.changeNotifications = Either.forRight(true)
serverCapabilities.inlayHintProvider = Either.forLeft(true)
serverCapabilities.hoverProvider = Either.forLeft(true)
serverCapabilities.renameProvider = Either.forLeft(true)
serverCapabilities.completionProvider = CompletionOptions(false, listOf("."))
Expand Down
18 changes: 12 additions & 6 deletions server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.lsp4j.services.TextDocumentService
import org.javacs.kt.codeaction.codeActions
import org.javacs.kt.completion.*
import org.javacs.kt.completion.completions
import org.javacs.kt.definition.goToDefinition
import org.javacs.kt.diagnostic.convertDiagnostic
import org.javacs.kt.formatting.formatKotlinCode
Expand All @@ -16,17 +16,18 @@ import org.javacs.kt.position.position
import org.javacs.kt.references.findReferences
import org.javacs.kt.semantictokens.encodedSemanticTokens
import org.javacs.kt.signaturehelp.fetchSignatureHelpAt
import org.javacs.kt.rename.renameSymbol
import org.javacs.kt.highlight.documentHighlightsAt
import org.javacs.kt.inlayhints.provideHints
import org.javacs.kt.symbols.documentSymbols
import org.javacs.kt.util.noResult
import org.javacs.kt.util.AsyncExecutor
import org.javacs.kt.util.Debouncer
import org.javacs.kt.util.filePath
import org.javacs.kt.util.TemporaryDirectory
import org.javacs.kt.util.parseURI
import org.javacs.kt.util.describeURI
import org.javacs.kt.util.describeURIs
import org.javacs.kt.rename.renameSymbol
import org.javacs.kt.highlight.documentHighlightsAt
import org.javacs.kt.util.filePath
import org.javacs.kt.util.noResult
import org.javacs.kt.util.parseURI
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import java.net.URI
import java.io.Closeable
Expand Down Expand Up @@ -95,6 +96,11 @@ class KotlinTextDocumentService(
codeActions(file, sp.index, params.range, params.context)
}

override fun inlayHint(params: InlayHintParams): CompletableFuture<List<InlayHint>> = async.compute {
val (file, _) = recover(params.textDocument.uri, params.range.start, Recompile.ALWAYS)
provideHints(file)
}

override fun hover(position: HoverParams): CompletableFuture<Hover?> = async.compute {
reportTime {
LOG.info("Hovering at {}", describePosition(position))
Expand Down
151 changes: 151 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/inlayhints/InlayHint.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package org.javacs.kt.inlayhints

import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.PsiWhiteSpace
import org.eclipse.lsp4j.InlayHint
import org.eclipse.lsp4j.InlayHintKind
import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.javacs.kt.CompiledFile
import org.javacs.kt.completion.DECL_RENDERER
import org.javacs.kt.position.range
import org.javacs.kt.util.preOrderTraversal
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.lexer.KtTokens.DOT
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
import org.jetbrains.kotlin.resolve.calls.smartcasts.getKotlinTypeForComparison
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.error.ErrorType


enum class InlayKind(val base: InlayHintKind) {
TypeHint(InlayHintKind.Type),
ParameterHint(InlayHintKind.Parameter),
ChainingHint(InlayHintKind.Type),
}

private fun PsiElement.determineType(ctx: BindingContext): KotlinType? =
when (this) {
is KtCallExpression -> {
this.getKotlinTypeForComparison(ctx)
}
is KtParameter -> {
if (this.isLambdaParameter and (this.typeReference == null)) {
val descriptor = ctx[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as CallableDescriptor
descriptor.returnType
} else null
}
is KtDestructuringDeclarationEntry -> {
val resolvedCall = ctx[BindingContext.COMPONENT_RESOLVED_CALL, this]
resolvedCall?.resultingDescriptor?.returnType
}
is KtProperty -> {
//TODO: better handling for unresolved-type error
elamc-2 marked this conversation as resolved.
Show resolved Hide resolved
val type = this.getKotlinTypeForComparison(ctx)
if (type is ErrorType) null else type
}
else -> null
}

private fun PsiElement.hintBuilder(kind: InlayKind, file: CompiledFile, label: String? = null): InlayHint? {
val namedElement = ((this as? PsiNameIdentifierOwner)?.nameIdentifier ?: this)
val range = range(file.parse.text, namedElement.textRange)

val hint = when(kind) {
InlayKind.ParameterHint -> InlayHint(range.start, Either.forLeft("$label:"))
else ->
this.determineType(file.compile) ?.let {
InlayHint(range.end, Either.forLeft(
"${(if (kind == InlayKind.TypeHint) ": " else "")}${DECL_RENDERER.renderType(it)}"
))
} ?: return null
}
hint.kind = kind.base
hint.paddingRight = true
hint.paddingLeft = true
return hint
}

private fun callableArgsToHints(
callExpression: KtCallExpression,
file: CompiledFile,
): List<InlayHint> {
val resolvedCall = callExpression.getResolvedCall(file.compile)

val hints = mutableListOf<InlayHint>()
resolvedCall?.valueArguments?.forEach { (t, u) ->
elamc-2 marked this conversation as resolved.
Show resolved Hide resolved
if (u.arguments.isNotEmpty()) {
val valueArg = u.arguments.first()

if (!valueArg.isNamed()) {
val label = (t.name).let { name ->
when (u) {
is VarargValueArgument -> "...$name"
else -> name.asString()
}
}
valueArg.asElement().hintBuilder(InlayKind.ParameterHint, file, label)?.let { hints.add(it) }
}

}
}
return hints
}

private fun lambdaValueParamsToHints(node: KtLambdaArgument, file: CompiledFile): List<InlayHint> {
return node.getLambdaExpression()!!.valueParameters.mapNotNull {
it.hintBuilder(InlayKind.TypeHint, file)
}
}

private fun chainedMethodsHints(node: KtDotQualifiedExpression, file: CompiledFile): List<InlayHint> {
return node.getChildrenOfType<KtCallExpression>().mapNotNull {
it.hintBuilder(InlayKind.ChainingHint, file)
}
}

fun provideHints(file: CompiledFile): List<InlayHint> {
val hints = mutableListOf<InlayHint>()
for (node in file.parse.preOrderTraversal().asIterable()) {
when (node) {
is KtLambdaArgument -> {
hints.addAll(lambdaValueParamsToHints(node, file))
}
is KtDotQualifiedExpression -> {
///chaining is defined as an expression whose next sibling tokens are newline and dot
(node.nextSibling as? PsiWhiteSpace)?.let {
if (it.nextSibling.node.elementType == DOT) {
hints.addAll(chainedMethodsHints(node, file))
}
}
}
is KtCallExpression -> {
//hints are not rendered for argument of type lambda expression i.e. list.map { it }
if (node.getChildOfType<KtLambdaArgument>() == null) {
hints.addAll(callableArgsToHints(node, file))
}
}
is KtDestructuringDeclaration -> {
hints.addAll(node.entries.mapNotNull { it.hintBuilder(InlayKind.TypeHint, file) })
}
is KtProperty -> {
//check decleration does not include type i.e. var t1: String
if (node.typeReference == null) {
node.hintBuilder(InlayKind.TypeHint, file)?.let { hints.add(it) }
}
}
}
}
return hints
}
154 changes: 154 additions & 0 deletions server/src/test/kotlin/org/javacs/kt/InlayHintTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.javacs.kt

import org.eclipse.lsp4j.InlayHint
import org.eclipse.lsp4j.Position
import org.hamcrest.Matchers.isIn
import org.hamcrest.Matchers.hasSize
import org.hamcrest.Matchers.everyItem
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.equalTo
import org.junit.Assert.assertThat
import org.junit.Assert.assertTrue
import org.junit.Assert.assertEquals
import org.junit.Test


private fun predicate(pos: Position, label: String) = {
hint: InlayHint -> hint.position == pos && hint.label.left.contains(label)
}

private fun nPredicateFilter(
hints: List<InlayHint>,
predicates: List<(InlayHint) -> Boolean>
): List<InlayHint> =
hints.filter {
predicates.any { p -> p(it) }
}


class InlayHintDeclarationTest : SingleFileTestFixture("inlayhints", "Declarations.kt") {

private val hints = languageServer.textDocumentService.inlayHint(inlayHintParams(file, range(0, 0, 0, 0))).get()

@Test
fun `lambda declaration hints`() {
val result = hints.filter {
it.position == Position(2, 10)
}
assertThat(result, hasSize(1))

val label = result.single().label.left.replaceBefore("(", "")
val regex = Regex("\\(([^)]+)\\) -> .*")
assertTrue(label.matches(regex))
}

@Test
fun `destrucuted declaration hints`() {
val predicates = listOf(
predicate(Position(17, 10), "Float"),
predicate(Position(17, 13), "Double"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(2))
assertThat(result, everyItem(isIn(hints)))
}

@Test
fun `should not render hint with explicit type`() {
val result = hints.filter {
it.label.left.contains("Type")
}
assertTrue(result.isEmpty())
}

@Test
fun `generic type hints`() {
val expected = listOf(Position(5, 13), Position(20, 7))

val result = hints.filter {
it.label.left.matches(Regex(": Box<([^)]+)>"))
}.map { it.position }

assertEquals(result.size, expected.size)
assertEquals(result.sortedBy { it.line }, expected.sortedBy { it.line })
}

}

class InlayHintCallableParameterTest : SingleFileTestFixture("inlayhints", "Parameters.kt") {

private val hints = languageServer.textDocumentService.inlayHint(inlayHintParams(file, range(0, 0, 0, 0))).get()

@Test
fun `class parameter hints`() {
val predicates = listOf(
predicate(Position(13, 4), "x"),
predicate(Position(14, 4), "y"),
predicate(Position(15, 4), "z"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(3))
assertThat(result, everyItem(isIn(hints)))
}

@Test
fun `has one vararg parameter hint`() {
val varargHintCount = hints.filter {
it.label.left.contains("ints")
}.size
assertThat(varargHintCount, equalTo(1))
}

@Test
fun `mixed parameter types`(){
val predicates = listOf(
predicate(Position(17, 14), "d"),
predicate(Position(17, 19), "p1"),
predicate(Position(17, 25), "ints"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(3))
assertThat(result, everyItem(isIn(hints)))
}

@Test
fun `inferred lambda parameter type`() {
val hint = hints.filter {
it.label.left.contains("Int")
}
assertThat(hint, hasSize(1))
assertThat(hint.single().label.left, containsString("Int"))
}

}

class InlayHintChainedTest : SingleFileTestFixture("inlayhints", "ChainedMethods.kt") {

private val hints = languageServer.textDocumentService.inlayHint(inlayHintParams(file, range(0, 0, 0, 0))).get()

@Test
fun `chained hints`() {
val predicates = listOf(
predicate(Position(17, 34), "List<String>"),
predicate(Position(18, 26), "List<Int>"),
themkat marked this conversation as resolved.
Show resolved Hide resolved
predicate(Position(19, 19), "Array<Int>"),
)
val result = nPredicateFilter(hints, predicates)

assertThat(result, hasSize(3))
assertThat(result, everyItem(isIn(hints)))
}

@Test
fun `generic chained hints`() {
val predicates = listOf(
predicate(Position(22, 16), "A<Int>"),
predicate(Position(23, 8), "B<Int>"),
)
val result = nPredicateFilter(hints, predicates)

assertThat(result, hasSize(2))
assertThat(result, everyItem(isIn(hints)))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : Langua
fun hoverParams(relativePath: String, line: Int, column: Int): HoverParams =
textDocumentPosition(relativePath, line, column).run { HoverParams(textDocument, position) }

fun inlayHintParams(relativePath: String, range: Range): InlayHintParams =
textDocumentPosition(relativePath, 0, 0).run { InlayHintParams(textDocument, range) }

fun semanticTokensParams(relativePath: String): SemanticTokensParams =
textDocumentPosition(relativePath, 0, 0).run { SemanticTokensParams(textDocument) }

Expand Down
25 changes: 25 additions & 0 deletions server/src/test/resources/inlayhints/ChainedMethods.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package inlayhints


class A<T>(private val self: List<T>) {
fun a(): B<T> {
return B(self)
}
}

class B<T>(private val self: List<T>) {
fun b(): List<T> {
return self
}
}

val foo = listOf(1, 2, 3, 4)

val bar = listOf("hello", "world")
.map { it.length * 2 }
.toTypedArray()
.contains(2)

val baz = A(foo)
.a()
.b()
Loading