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 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
9 changes: 8 additions & 1 deletion server/src/main/kotlin/org/javacs/kt/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public data class ExternalSourcesConfiguration(
var autoConvertToKotlin: Boolean = false
)

data class InlayHintsConfiguration(
var typeHints: Boolean = false,
var parameterHints: Boolean = false,
var chainedHints: Boolean = false
)


fun getStoragePath(params: InitializeParams): Path? {
params.initializationOptions?.let { initializationOptions ->
Expand Down Expand Up @@ -81,5 +87,6 @@ public data class Configuration(
val completion: CompletionConfiguration = CompletionConfiguration(),
val linting: LintingConfiguration = LintingConfiguration(),
var indexing: IndexingConfiguration = IndexingConfiguration(),
val externalSources: ExternalSourcesConfiguration = ExternalSourcesConfiguration()
val externalSources: ExternalSourcesConfiguration = ExternalSourcesConfiguration(),
val hints: InlayHintsConfiguration = InlayHintsConfiguration()
)
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, config.hints)
}

override fun hover(position: HoverParams): CompletableFuture<Hover?> = async.compute {
reportTime {
LOG.info("Hovering at {}", describePosition(position))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class KotlinWorkspaceService(
}
}

// Update options for inlay hints
get("inlayHints")?.asJsonObject?.apply {
val hints = config.hints
get("typeHints")?.asBoolean?.let { hints.typeHints = it }
get("parameterHints")?.asBoolean?.let { hints.parameterHints = it }
get("chainedHints")?.asBoolean?.let { hints.chainedHints = it }
}

// Update linter options
get("linting")?.asJsonObject?.apply {
val linting = config.linting
Expand Down
236 changes: 236 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,236 @@
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.InlayHintsConfiguration
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.name.Name
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.KtFunction
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.KtNamedFunction
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.ResolvedValueArgument
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.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.error.ErrorType


private fun PsiElement.determineType(ctx: BindingContext): KotlinType? =
when (this) {
is KtNamedFunction -> {
val descriptor = ctx[BindingContext.FUNCTION, this]
descriptor?.returnType
}
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 -> {
//skip unused variable denoted by underscore
//https://kotlinlang.org/docs/destructuring-declarations.html#underscore-for-unused-variables
if (this.isSingleUnderscore) {
null
} else {
val resolvedCall = ctx[BindingContext.COMPONENT_RESOLVED_CALL, this]
resolvedCall?.resultingDescriptor?.returnType
}
}
is KtProperty -> {
val type = this.getKotlinTypeForComparison(ctx)
if (type is ErrorType) null else type
}
else -> null
}

@Suppress("ReturnCount")
private fun PsiElement.hintBuilder(kind: InlayKind, file: CompiledFile, label: String? = null): InlayHint? {
val element = when(this) {
is KtFunction -> this.valueParameterList!!.originalElement
is PsiNameIdentifierOwner -> this.nameIdentifier
else -> this
} ?: return null

val range = range(file.parse.text, element.textRange)

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

@Suppress("ReturnCount")
private fun callableArgNameHints(
acc: MutableList<InlayHint>,
callExpression: KtCallExpression,
file: CompiledFile,
config: InlayHintsConfiguration
) {
if (!config.parameterHints) return

//hints are not rendered for argument of type lambda expression i.e. list.map { it }
if (callExpression.getChildOfType<KtLambdaArgument>() != null) {
return
}

val resolvedCall = callExpression.getResolvedCall(file.compile)
val entries = resolvedCall?.valueArguments?.entries ?: return

val hints = entries.mapNotNull { (t, u) ->
val valueArg = u.arguments.firstOrNull()
if (valueArg != null && !valueArg.isNamed()) {
val label = getArgLabel(t.name, u)
valueArg.asElement().hintBuilder(InlayKind.ParameterHint, file, label)
} else null
}
acc.addAll(hints)
}

private fun getArgLabel(name: Name, arg: ResolvedValueArgument) =
(name).let {
when (arg) {
is VarargValueArgument -> "...$it"
else -> it.asString()
}
}

private fun lambdaValueParamHints(
acc: MutableList<InlayHint>,
node: KtLambdaArgument,
file: CompiledFile,
config: InlayHintsConfiguration
) {
if (!config.typeHints) return

val params = node.getLambdaExpression()!!.valueParameters

//hint should not be rendered when parameter is of type DestructuringDeclaration
//example: Map.forEach { (k,v) -> _ }
//lambda parameter (k,v) becomes (k :hint, v :hint) :hint <- outer hint isnt needed
params.singleOrNull()?.let {
if (it.destructuringDeclaration != null) return
}

val hints = params.mapNotNull {
it.hintBuilder(InlayKind.TypeHint, file)
}
acc.addAll(hints)
}

private fun chainedExpressionHints(
acc: MutableList<InlayHint>,
node: KtDotQualifiedExpression,
file: CompiledFile,
config: InlayHintsConfiguration
) {
if (!config.chainedHints) return

///chaining is defined as an expression whose next sibling tokens are newline and dot
val next = (node.nextSibling as? PsiWhiteSpace)
val nextSiblingElement = next?.nextSibling?.node?.elementType

if (nextSiblingElement != null && nextSiblingElement == DOT) {
val hints = node.getChildrenOfType<KtCallExpression>().mapNotNull {
it.hintBuilder(InlayKind.ChainingHint, file)
}
acc.addAll(hints)
}
}

private fun destructuringVarHints(
acc: MutableList<InlayHint>,
node: KtDestructuringDeclaration,
file: CompiledFile,
config: InlayHintsConfiguration
) {
if (!config.typeHints) return

val hints = node.entries.mapNotNull {
it.hintBuilder(InlayKind.TypeHint, file)
}
acc.addAll(hints)
}

@Suppress("ReturnCount")
private fun declarationHint(
acc: MutableList<InlayHint>,
node: KtProperty,
file: CompiledFile,
config: InlayHintsConfiguration
) {
if (!config.typeHints) return

//check decleration does not include type i.e. var t1: String
if (node.typeReference != null) return

val hint = node.hintBuilder(InlayKind.TypeHint, file) ?: return
acc.add(hint)
}

private fun functionHint(
themkat marked this conversation as resolved.
Show resolved Hide resolved
acc: MutableList<InlayHint>,
node: KtNamedFunction,
file: CompiledFile,
config: InlayHintsConfiguration
) {
if (!config.typeHints) return

//only render hints for functions without block body
//functions WITH block body will always specify return types explicitly
if (!node.hasDeclaredReturnType() && !node.hasBlockBody()) {
val hint = node.hintBuilder(InlayKind.TypeHint, file) ?: return
acc.add(hint)
}
}

fun provideHints(file: CompiledFile, config: InlayHintsConfiguration): List<InlayHint> {
val res = mutableListOf<InlayHint>()
for (node in file.parse.preOrderTraversal().asIterable()) {
when (node) {
is KtNamedFunction -> functionHint(res, node, file, config)
is KtLambdaArgument -> lambdaValueParamHints(res, node, file, config)
is KtDotQualifiedExpression -> chainedExpressionHints(res, node, file, config)
is KtCallExpression -> callableArgNameHints(res, node, file, config)
is KtDestructuringDeclaration -> destructuringVarHints(res, node, file, config)
is KtProperty -> declarationHint(res, node, file, config)
}
}
return res
}

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