Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sourcegraph.lsif_java
import scala.jdk.CollectionConverters._

import com.sourcegraph.lsif_java.commands.CommentSyntax
import com.sourcegraph.lsif_java.commands.SnapshotLsifCommand
import com.sourcegraph.lsif_semanticdb.LsifTextDocument
import com.sourcegraph.lsif_semanticdb.SignatureFormatter
import com.sourcegraph.lsif_semanticdb.Symtab
Expand Down Expand Up @@ -88,11 +89,23 @@ object SemanticdbPrinters {
.append(
symtab.symbols.asScala.get(occ.getSymbol) match {
case Some(info) if occ.getRole == Role.DEFINITION =>
val sig = new SignatureFormatter(info, symtab).formatSymbol()
if (sig.isEmpty)
val signature: String =
if (info.hasSignature) {
new SignatureFormatter(info, symtab)
.formatSymbol()
.trim
.replace('\n', ' ')
} else if (info.hasDocumentation) {
SnapshotLsifCommand
.signatureLines(info.getDocumentation.getMessage)
.mkString(" ")
} else {
""
}
if (signature.isEmpty)
" " + info.getDisplayName
else
" " + sig.trim.replace('\n', ' ')
" " + signature
case _ =>
""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ object SnapshotLsifCommand {
resultSetId <- lsif.next.get(o.getId).toList
hoverId <- lsif.hoverEdges.get(resultSetId).toList
hover <- lsif.hoverVertexes.get(hoverId).toList
line <- hover
.getContents
.getValue
.linesIterator
.dropWhile(!_.startsWith("```"))
.drop(1)
.takeWhile(_ != "```")
line <- signatureLines(hover.getContents.getValue)
} yield line
).mkString("\n")
val symInfo = SymbolInformation
Expand All @@ -164,6 +158,14 @@ object SnapshotLsifCommand {
lsif.documents.values.map(_.build()).toList
}

def signatureLines(documentation: String): Iterator[String] = {
documentation
.linesIterator
.dropWhile(!_.startsWith("```"))
.drop(1)
.takeWhile(_ != "```")
}

class IndexedLsif(
val path: Path,
val objects: mutable.Buffer[LsifObject],
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/src/test/scala/tests/SemanticdbPrintersSuite.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tests

import com.sourcegraph.lsif_java.SemanticdbPrinters
import com.sourcegraph.semanticdb_javac.Semanticdb.Documentation
import com.sourcegraph.semanticdb_javac.Semanticdb.Range
import com.sourcegraph.semanticdb_javac.Semanticdb.SymbolInformation
import com.sourcegraph.semanticdb_javac.Semanticdb.SymbolOccurrence
import com.sourcegraph.semanticdb_javac.Semanticdb.SymbolOccurrence.Role
import com.sourcegraph.semanticdb_javac.Semanticdb.TextDocument
Expand Down Expand Up @@ -84,4 +86,42 @@ class SemanticdbPrintersSuite extends FunSuite {
)
}

test("documentation") {
val doc = TextDocument
.newBuilder()
.setText("fun main() {}\n")
.addOccurrences(
SymbolOccurrence
.newBuilder()
.setSymbol("main().")
.setRange(
Range
.newBuilder()
.setStartLine(0)
.setStartCharacter(4)
.setEndLine(0)
.setEndCharacter(8)
)
.setRole(Role.DEFINITION)
)
.addSymbols(
SymbolInformation
.newBuilder()
.setSymbol("main().")
.setDocumentation(
Documentation
.newBuilder()
.setMessage("```kt\nfun main(): kotlin.Unit\n```")
)
)
.build()

assertNoDiff(
SemanticdbPrinters.printTextDocument(doc),
"""|fun main() {}
|// ^^^^ definition main(). fun main(): kotlin.Unit
|""".stripMargin
)
}

}