1
1
package org.javacs.kt.hover
2
2
3
3
import org.eclipse.lsp4j.Hover
4
- import org.eclipse.lsp4j.MarkedString
4
+ import org.eclipse.lsp4j.MarkupContent
5
5
import org.eclipse.lsp4j.Range
6
6
import org.eclipse.lsp4j.jsonrpc.messages.Either
7
7
import com.intellij.openapi.util.TextRange
8
8
import com.intellij.psi.PsiElement
9
9
import com.intellij.psi.PsiDocCommentBase
10
- import org.jetbrains.kotlin.idea.kdoc.findKDoc
11
10
import org.jetbrains.kotlin.psi.KtExpression
12
11
import org.jetbrains.kotlin.psi.KtCallableDeclaration
13
12
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
@@ -32,22 +31,23 @@ import org.javacs.kt.signaturehelp.getDocString
32
31
33
32
fun hoverAt (file : CompiledFile , cursor : Int ): Hover ? {
34
33
val (ref, target) = file.referenceAtPoint(cursor) ? : return typeHoverAt(file, cursor)
35
- val javaDoc = target.findKDoc()?.getContent() ? : " "
34
+ val javaDoc = getDocString(file, cursor)
36
35
val location = ref.textRange
37
36
val hoverText = DECL_RENDERER .render(target)
38
- val hover = Either .forRight< String , MarkedString >( MarkedString ( " kotlin" , hoverText ))
37
+ val hover = MarkupContent ( " markdown " , listOf ( " ``` kotlin\n $hoverText \n ``` " , javaDoc).filter { it.isNotEmpty() }.joinToString( " \n --- \n " ))
39
38
val range = Range (
40
39
position(file.content, location.startOffset),
41
40
position(file.content, location.endOffset))
42
- return Hover (listOf ( hover, Either .forLeft(javaDoc)) , range)
41
+ return Hover (hover, range)
43
42
}
44
43
45
44
private fun typeHoverAt (file : CompiledFile , cursor : Int ): Hover ? {
46
45
val expression = file.parseAtPoint(cursor)?.findParent<KtExpression >() ? : return null
47
46
var javaDoc: String = expression.children.mapNotNull { (it as ? PsiDocCommentBase )?.text }.map(::renderJavaDoc).firstOrNull() ? : " "
48
47
val scope = file.scopeAtPoint(cursor) ? : return null
49
48
val hoverText = renderTypeOf(expression, file.bindingContextOf(expression, scope)) ? : return null
50
- return Hover (listOf (Either .forRight(MarkedString (" kotlin" , hoverText)), Either .forLeft(javaDoc)))
49
+ val hover = MarkupContent (" markdown" , listOf (" ```kotlin\n $hoverText \n ```" , javaDoc).filter { it.isNotEmpty() }.joinToString(" \n ---\n " ))
50
+ return Hover (hover)
51
51
}
52
52
53
53
// Source: https://github.com/JetBrains/kotlin/blob/master/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt
0 commit comments