Skip to content

Commit 48143ac

Browse files
nicolas-guichardantonsviridov-src
authored andcommitted
Fix extracting documentation
Down to 13 failing tests.
1 parent 59aaf04 commit 48143ac

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

semanticdb-kotlinc/src/main/kotlin/com/sourcegraph/semanticdb_kotlinc/SemanticdbTextDocumentBuilder.kt

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ import java.nio.file.Path
55
import java.nio.file.Paths
66
import java.security.MessageDigest
77
import kotlin.contracts.ExperimentalContracts
8-
import org.jetbrains.kotlin.KtLightSourceElement
98
import org.jetbrains.kotlin.KtSourceElement
109
import org.jetbrains.kotlin.KtSourceFile
11-
import org.jetbrains.kotlin.com.intellij.lang.LighterASTNode
1210
import org.jetbrains.kotlin.com.intellij.lang.java.JavaLanguage
13-
import org.jetbrains.kotlin.com.intellij.openapi.util.Ref
14-
import org.jetbrains.kotlin.com.intellij.util.diff.FlyweightCapableTreeStructure
1511
import org.jetbrains.kotlin.fir.FirElement
16-
import org.jetbrains.kotlin.fir.render
12+
import org.jetbrains.kotlin.fir.analysis.getChild
13+
import org.jetbrains.kotlin.fir.renderer.*
1714
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
1815
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
1916
import org.jetbrains.kotlin.fir.symbols.impl.*
2017
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
2118
import org.jetbrains.kotlin.idea.KotlinLanguage
2219
import org.jetbrains.kotlin.lexer.KtTokens
2320
import org.jetbrains.kotlin.psi
21+
import org.jetbrains.kotlin.text
2422

2523
@ExperimentalContracts
2624
class SemanticdbTextDocumentBuilder(
@@ -84,7 +82,7 @@ class SemanticdbTextDocumentBuilder(
8482
return SymbolInformation {
8583
this.symbol = symbol.toString()
8684
this.displayName = displayName(firBasedSymbol)
87-
this.documentation = semanticdbDocumentation(firBasedSymbol.fir, element)
85+
this.documentation = semanticdbDocumentation(firBasedSymbol.fir)
8886
this.addAllOverriddenSymbols(supers)
8987
this.language =
9088
when (element.psi?.language ?: KotlinLanguage.INSTANCE) {
@@ -127,35 +125,23 @@ class SemanticdbTextDocumentBuilder(
127125
.digest(file.getContentsAsStream().readBytes())
128126
.joinToString("") { "%02X".format(it) }
129127

130-
private fun semanticdbDocumentation(
131-
element: FirElement,
132-
source: KtSourceElement
133-
): Semanticdb.Documentation = Documentation {
128+
private fun semanticdbDocumentation(element: FirElement): Semanticdb.Documentation = Documentation {
134129
format = Semanticdb.Documentation.Format.MARKDOWN
135-
val renderOutput = element.render()
136-
val kdoc = getKDocFromKtLightSourceElement(source as? KtLightSourceElement) ?: ""
137-
message = "```\n$renderOutput\n```\n${stripKDocAsterisks(kdoc)}"
138-
}
139-
140-
private fun getKDocFromKtLightSourceElement(
141-
lightSourceElement: KtLightSourceElement?
142-
): String? {
143-
if (lightSourceElement == null) return null
144-
val tree = lightSourceElement.treeStructure // FlyweightCapableTreeStructure<LighterASTNode>
145-
val node =
146-
lightSourceElement.lighterASTNode // LighterASTNode, the root of the element's structure
147-
return findKDoc(tree, node)
148-
}
149-
150-
// Helper function to find the KDoc node in the AST
151-
private fun findKDoc(
152-
tree: FlyweightCapableTreeStructure<LighterASTNode>,
153-
node: LighterASTNode
154-
): String? {
155-
// Recursively traverse the light tree to find a DOC_COMMENT node
156-
val kidsRef = Ref<Array<LighterASTNode?>>()
157-
tree.getChildren(node, kidsRef)
158-
return kidsRef.get().singleOrNull { it?.tokenType == KtTokens.DOC_COMMENT }?.toString()
130+
// Like FirRenderer().forReadability, but using FirAllModifierRenderer instead of FirPartialModifierRenderer
131+
val renderer = FirRenderer(
132+
typeRenderer = ConeTypeRenderer(),
133+
idRenderer = ConeIdShortRenderer(),
134+
classMemberRenderer = FirNoClassMemberRenderer(),
135+
bodyRenderer = null,
136+
propertyAccessorRenderer = null,
137+
callArgumentsRenderer = FirCallNoArgumentsRenderer(),
138+
modifierRenderer = FirAllModifierRenderer(),
139+
valueParameterRenderer = FirValueParameterRendererForReadability(),
140+
declarationRenderer = FirDeclarationRenderer("local "),
141+
)
142+
val renderOutput = renderer.renderElementAsString(element)
143+
val kdoc = element.source?.getChild(KtTokens.DOC_COMMENT)?.text?.toString() ?: ""
144+
message = "```kotlin\n$renderOutput\n```${stripKDocAsterisks(kdoc)}"
159145
}
160146

161147
// Returns the kdoc string with all leading and trailing "/*" tokens removed. Naive

semanticdb-kotlinc/src/test/kotlin/com/sourcegraph/semanticdb_kotlinc/test/AnalyzerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class AnalyzerTest {
106106
documentation =
107107
Documentation {
108108
format = Semanticdb.Documentation.Format.MARKDOWN
109-
message = "```kotlin\npublic final class Banana\n```"
109+
message = "```kotlin\npublic final class Banana : Any\n```"
110110
}
111111
},
112112
SymbolInformation {
@@ -116,7 +116,7 @@ class AnalyzerTest {
116116
documentation =
117117
Documentation {
118118
format = Semanticdb.Documentation.Format.MARKDOWN
119-
message = "```kotlin\npublic final fun foo()\n```"
119+
message = "```kotlin\npublic final fun foo(): Unit\n```"
120120
}
121121
})
122122
assertSoftly(document.symbolsList) { withClue(this) { symbols.forEach(::shouldContain) } }

semanticdb-kotlinc/src/test/kotlin/com/sourcegraph/semanticdb_kotlinc/test/SemanticdbSymbolsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ class SemanticdbSymbolsTest {
618618
language = Language.KOTLIN
619619
documentation {
620620
message =
621-
"```kotlin\npublic val x: kotlin.String\n```\n\n----\n\n\nhello world\n test content\n"
621+
"```kotlin\npublic final val x: String\n```\n\n----\n\n\nhello world\n test content\n"
622622
format = Format.MARKDOWN
623623
}
624624
},
@@ -628,7 +628,7 @@ class SemanticdbSymbolsTest {
628628
language = Language.KOTLIN
629629
documentation {
630630
message =
631-
"```kotlin\npublic val x: kotlin.String\n```\n\n----\n\n\nhello world\n test content\n"
631+
"```kotlin\npublic get(): String\n```\n\n----\n\n\nhello world\n test content\n"
632632
format = Format.MARKDOWN
633633
}
634634
}))))

0 commit comments

Comments
 (0)