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 @@ -128,11 +128,13 @@ object SnapshotLsifCommand {
resultSetId <- lsif.next.get(o.getId).toList
hoverId <- lsif.hoverEdges.get(resultSetId).toList
hover <- lsif.hoverVertexes.get(hoverId).toList
contents <- hover.getContentsList.asScala
if contents.getLanguage !=
Language.UNKNOWN_LANGUAGE.toString.toLowerCase
} yield contents.getValue
).mkString("\n")
contents <- hover.getContents.getValue
} yield contents
).mkString
.split("\n---\n")
.last
.stripPrefix("```java\n")
.stripSuffix("\n```")
val symInfo = SymbolInformation
.newBuilder()
// we cheese it a bit here, as this is less work than trying to reconstruct
Expand Down Expand Up @@ -174,14 +176,19 @@ object SnapshotLsifCommand {
.map(m => m.getIdentifier -> m)
.toMap
val hovers: Map[String, String] = monikers.map { case (sym, obj) =>
val hoverMessage =
val hover =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for comprehensions become hard to read when you add a complicated chain of operations on top of it. I think this diff would read cleaner by keeping the original for comprehension and adding a separate statement instead

for {
resultSet <- monikerInverse.get(obj.getId).toList
hoverEdge <- hoverEdges.get(resultSet).toList
hoverResult <- hoverVertexes.get(hoverEdge).toList
contents <- hoverResult.getContentsList.asScala
} yield contents.getValue.trim
sym -> hoverMessage.mkString("\n\n")
contents = hoverResult.getContents.getValue.trim
codeFence <- contents
.linesIterator
.dropWhile(_ != "```java")
.drop(1)
.takeWhile(_ != "```")
} yield codeFence
sym -> hover.mkString("\n\n")
}

def visualizeGraph(): String = {
Expand All @@ -193,7 +200,7 @@ object SnapshotLsifCommand {
import org.scalameta.ascii.graph.Graph
val monikers =
byLabel("moniker")
.filter(o => o.getType() == "vertex" && o.getIdentifier() == symbol)
.filter(o => o.getType == "vertex" && o.getIdentifier == symbol)
.toList
val moniker =
monikers match {
Expand All @@ -209,56 +216,54 @@ object SnapshotLsifCommand {
val edges = ListBuffer.empty[(String, String)]
val inputs = mutable.Map.empty[String, Input]
def input(range: LsifObject): Input = {
val uri = G
.predecessors(range)
.asScala
.iterator
.filter(_.getLabel() == "document")
.next()
.getUri()
val uri =
G.predecessors(range)
.asScala
.iterator
.filter(_.getLabel == "document")
.next()
.getUri
inputs.getOrElseUpdate(uri, Input.path(Paths.get(URI.create(uri))))
}
def renderPos(pos: LsifPosition): String = {
s"${pos.getLine()}:${pos.getCharacter()}"
s"${pos.getLine}:${pos.getCharacter}"
}
val addedVertexes = mutable.Set.empty[String]
def render(node: LsifObject): String = {
(node.getType(), node.getLabel()) match {
(node.getType, node.getLabel) match {
Comment thread
Strum355 marked this conversation as resolved.
case ("vertex", "document") =>
val filename = sourceroot
.relativize(Paths.get(URI.create(node.getUri())))
.relativize(Paths.get(URI.create(node.getUri)))
.iterator()
.asScala
.mkString("/")
s"document ${filename}"
case ("vertex", "hoverResult") =>
val contents = node
.getResult()
.getContentsList()
.asScala
.map(_.getValue())
.mkString("\n")
.getResult
.getContents
.getValue
.replace("\n", "\\n")
.trim()
s"hoverResult(${node.getId()}) ${contents}"
s"hoverResult(${node.getId}) ${contents}"
case ("vertex", "moniker") =>
s"moniker ${node.getIdentifier()}"
s"moniker ${node.getIdentifier}"
case ("vertex", "range") =>
val i = input(node)
val p = Position.range(
i,
node.getStart().getLine(),
node.getStart().getCharacter(),
node.getEnd().getLine(),
node.getEnd().getCharacter()
node.getStart.getLine,
node.getStart.getCharacter,
node.getEnd.getLine,
node.getEnd.getCharacter
)
s"range(${node.getId()}) ${renderPos(node.getStart())} '${p.text}'"
s"range(${node.getId}) ${renderPos(node.getStart)} '${p.text}'"
case ("vertex", "packageInformation") =>
s"${node.getName()}(${node.getId()})"
s"${node.getName}(${node.getId})"
case ("vertex", label) =>
s"${label}(${node.getId()})"
s"${label}(${node.getId})"
case _ =>
s"${node.getType}/${node.getLabel} (${node.getId()})"
s"${node.getType}/${node.getLabel} (${node.getId})"
}
}
def isAdded(vertex: LsifObject) = addedVertexes.contains(render(vertex))
Expand Down Expand Up @@ -309,14 +314,14 @@ object SnapshotLsifCommand {
val S = GraphBuilder.directed().immutable[LsifObject]()
val visited = mutable.Set.empty[Int]
def loop(l: LsifObject): Unit = {
if (visited.add(l.getId())) {
if (visited.add(l.getId)) {
S.addNode(l)
G.predecessors(l)
.forEach { n =>
S.putEdge(EndpointPair.ordered(l, n))
loop(n)
}
if (isSuccessorRelevantLabel(l.getLabel())) {
if (isSuccessorRelevantLabel(l.getLabel)) {
G.successors(l)
.forEach { n =>
S.putEdge(EndpointPair.ordered(n, l))
Expand Down Expand Up @@ -357,7 +362,7 @@ object SnapshotLsifCommand {
} {
B.addEdge(
EndpointPair.ordered(outV, inV),
o.toBuilder().clearInVs().setInV(inId).build()
o.toBuilder.clearInVs().setInV(inId).build()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sourcegraph.lsif_semanticdb;

import com.sourcegraph.lsif_protocol.MarkupKind;
import com.sourcegraph.semanticdb_javac.Semanticdb;
import com.sourcegraph.semanticdb_javac.Semanticdb.SymbolInformation;
import com.sourcegraph.semanticdb_javac.Semanticdb.SymbolOccurrence;
Expand All @@ -8,12 +9,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -127,21 +123,35 @@ private Integer processDocument(
}

// Hover
ArrayList<MarkedString> markedStrings = new ArrayList<>();
String documentation = symbolInformation.getDocumentation().getMessage();

StringBuilder markupContent = new StringBuilder(documentation.length());

if (!documentation.isEmpty()) {
markedStrings.add(new MarkedString(Semanticdb.Language.UNKNOWN_LANGUAGE, documentation));
markupContent.append(documentation.replaceAll("\n", "\n\n"));
}

if (symbolInformation.hasSignature()) {
markedStrings.add(
new MarkedString(
doc.semanticdb.getLanguage(),
new SignatureFormatter(symbolInformation, symtab).formatSymbol()));
if (markupContent.length() != 0) markupContent.append("\n---\n");

String language =
doc.semanticdb.getLanguage().toString().toLowerCase(Locale.ROOT).intern();
String signature = new SignatureFormatter(symbolInformation, symtab).formatSymbol();

markupContent.ensureCapacity(
markupContent.length() + signature.length() + language.length() + 8);
markupContent
.append("```")
.append(language)
.append('\n')
.append(signature)
.append("\n```");
}

if (!markedStrings.isEmpty()) {
int hoverId = writer.emitHoverResult(markedStrings.toArray(new MarkedString[0]));
if (markupContent.length() != 0) {
int hoverId =
writer.emitHoverResult(
new MarkupContent(MarkupKind.MARKDOWN, markupContent.toString()));
writer.emitHoverEdge(ids.resultSet, hoverId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,15 @@ public int emitDefinitionResult(int resultSet) {
return definitionResult;
}

public int emitHoverResult(MarkedString[] markedStrings) {
public int emitHoverResult(MarkupContent markupContents) {
return emitObject(
lsifVertex("hoverResult")
.setResult(
LsifHover.newBuilder()
.addAllContents(
Arrays.stream(markedStrings)
.map(
(ms) ->
Content.newBuilder()
.setLanguage(
ms.language.toString().toLowerCase(Locale.ROOT))
.setValue(ms.value)
.build())
.collect(Collectors.toList()))));
.setContents(
Content.newBuilder()
.setKind(markupContents.kind.toString().toLowerCase())
.setValue(markupContents.value))));
}

public void emitHoverEdge(int outV, int inV) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sourcegraph.lsif_semanticdb;

import com.sourcegraph.lsif_protocol.MarkupKind;

public class MarkupContent {
public final String value;
public final MarkupKind kind;

public MarkupContent(MarkupKind kind, String value) {
this.value = value;
this.kind = kind;
}
}
9 changes: 7 additions & 2 deletions lsif-semanticdb/src/main/protobuf/lsif.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ message LsifPosition {
}

message LsifHover {
repeated Content contents = 1;
Content contents = 1;
message Content {
string language = 1;
string kind = 1; // not MarkupKind because must be lowercase
string value = 2;
}
}

enum MarkupKind {
PLAINTEXT = 0;
MARKDOWN = 1;
}
72 changes: 38 additions & 34 deletions tests/snapshots/src/main/generated/index-semanticdb/locals
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,41 @@ public class Example {
──────────────────────────────────
│ LSIF Graph for symbol 'local0' │
──────────────────────────────────
╭──────────╮
│project(2)│
╰─────┬────╯
v
╭─────────────────────────────╮ ╭───────────────────╮
│document example/Example.java│ │referenceResult(48)│
╰┬─────────────────┬──────────╯ ╰────┬───┬──────────╯
│ │ │^ │
│ │ ╭─────────────╯│ │
│ │ │ │ │
│ v v │ │
│ ╭──────────────────╮ │ │
│ │range(56) 3:46 'n'│ │ │
│ ╰──────────┬───────╯ │ │
│ │ ╭───────────╯ │
│ v │ │
│ ╭─────────┴───╮ │
│ │resultSet(43)│ │
│ ╰──┬──┬──┬────╯ │
│ │ │ │ ^╭───────────╯
│ │ │ ╰──┼┼────────────────╮
│ │ ╰─────┼┼╮ │
│ v │││ │
│ ╭────────────────────╮ │││ │
│ │definitionResult(46)│ │││ │
│ ╰────────────────┬───╯ │││ │
╰─────────────────╮ │ ╭──╯││ │
╭────────────┼───┼───┼───┼╯ │
│ │ │ │ │ │
v v v │ v v
╭──────────────╮ ╭───────────┴──────╮ ╭─────────────────────╮
│moniker local0│ │range(50) 3:34 'n'│ │hoverResult(54) int n│
╰──────────────╯ ╰──────────────────╯ ╰─────────────────────╯
╭──────────╮
│project(2)│
╰─────┬────╯
╭───────────────╯
v
╭─────────────────────────────╮ ╭───────────────────╮
│document example/Example.java│ │referenceResult(48)│
╰──────┬─────────────┬────────╯ ╰────┬───────┬──────╯
│ │ │^ │
╭───────╯ │ ╭───────────────╯│ ╰───╮
│ │ │ │ │
│ v v │ │
│ ╭──────────────────╮ │ │
│ │range(56) 3:46 'n'│ │ │
│ ╰────────────┬─────╯ │ │
│ │ ╭───────────╯ │
│ v │ │
│ ╭─────────┴───╮ │
│ │resultSet(43)│ │
│ ╰──┬─────┬───┬╯ │
│ │ │ ^│ │
│ v │ ││ │
│ ╭────────────────────╮ │ ││ │
│ │definitionResult(46)│ │ ││ │
│ ╰────────────┬───────╯ │ ││ │
│ │ │ ││ │
│ │ ╰──┼┼──────────────╮ │
│ │ ╭──────────╯│ │ │
╰───────────────╮ │ │ │ │ │
╭────────────┼───┼───┼───────────╯ │ │
│ │ │ │ ╭──────────────────────┼────╯
│ │ │ │ │ │
v v v │ v v
╭──────────────╮ ╭───────────┴──────╮ ╭───────────────────────────────────╮
│moniker local0│ │range(50) 3:34 'n'│ │hoverResult(54) ```java\nint n\n```│
╰──────────────╯ ╰──────────────────╯ ╰───────────────────────────────────╯
Loading