From 4fd9972f483437f8ed178a67665ac0c3b122331e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Wed, 4 Aug 2021 12:53:03 +0200 Subject: [PATCH] Swap order of signature and docstrings in hover messages Previously, the docstring appeared above the signature in hover messages. This was not ideal because it meant you had to scroll down to see the signature when the docstring is very long (which happens frequently). This commit moves the signature to the top of the hover message so you can always see it without scrolling. Fixes https://github.com/sourcegraph/lsif-java/issues/284 --- .../commands/SnapshotLsifCommand.scala | 16 +++-- .../lsif_semanticdb/LsifSemanticdb.java | 35 +++++---- project/plugins.sbt | 2 +- .../main/generated/index-semanticdb/multifile | 72 +++++++++---------- .../main/generated/index-semanticdb/reference | 55 +++++++------- 5 files changed, 90 insertions(+), 90 deletions(-) diff --git a/lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/SnapshotLsifCommand.scala b/lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/SnapshotLsifCommand.scala index 5d325afd..9b45200f 100644 --- a/lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/SnapshotLsifCommand.scala +++ b/lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/SnapshotLsifCommand.scala @@ -148,13 +148,15 @@ object SnapshotLsifCommand { resultSetId <- lsif.next.get(o.getId).toList hoverId <- lsif.hoverEdges.get(resultSetId).toList hover <- lsif.hoverVertexes.get(hoverId).toList - contents <- hover.getContents.getValue - } yield contents - ).mkString - .split("\n---\n") - .last - .stripPrefix("```java\n") - .stripSuffix("\n```") + line <- hover + .getContents + .getValue + .linesIterator + .dropWhile(!_.startsWith("```")) + .drop(1) + .takeWhile(_ != "```") + } yield line + ).mkString("\n") val symInfo = SymbolInformation .newBuilder() // we cheese it a bit here, as this is less work than trying to reconstruct diff --git a/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/LsifSemanticdb.java b/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/LsifSemanticdb.java index 03182e4a..729b51ef 100644 --- a/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/LsifSemanticdb.java +++ b/lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/LsifSemanticdb.java @@ -133,40 +133,37 @@ private Integer processDocumentUnsafe( String.format("no definition ID for symbol '%s'", occ.getSymbol()))); } - // Hover + // Hover 1: signature String documentation = symbolInformation.getDocumentation().getMessage(); - StringBuilder markupContent = new StringBuilder(documentation.length()); - - if (!documentation.isEmpty()) { - markupContent.append(documentation.replaceAll("\n", "\n\n")); - } - if (symbolInformation.hasSignature()) { - 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```"); - } else { - markupContent.append(symbolInformation.getDisplayName()); } - if (markupContent.length() != 0) { - int hoverId = - writer.emitHoverResult( - new MarkupContent(MarkupKind.MARKDOWN, markupContent.toString())); - writer.emitHoverEdge(ids.resultSet, hoverId); + // Hover 2: docstring + if (!documentation.isEmpty()) { + if (markupContent.length() != 0) markupContent.append("\n---\n"); + markupContent.append(documentation.replaceAll("\n", "\n\n")); } + + if (markupContent.length() == 0) { + // Always emit a non-empty hover message to prevent Sourcegraph from falling back to + // Search-Based hover messages. + markupContent.append(symbolInformation.getDisplayName()); + } + + int hoverId = + writer.emitHoverResult( + new MarkupContent(MarkupKind.MARKDOWN, markupContent.toString())); + writer.emitHoverEdge(ids.resultSet, hoverId); } // Overrides diff --git a/project/plugins.sbt b/project/plugins.sbt index 1a8192d9..a612e5f6 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -6,7 +6,7 @@ addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.2.21") addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.29") addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.0") addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.8-94-1cfdf0bd") -addSbtPlugin("com.sourcegraph" % "sbt-sourcegraph" % "0.2.0") +addSbtPlugin("com.sourcegraph" % "sbt-sourcegraph" % "0.2.1") addSbtPlugin("com.lightbend.sbt" % "sbt-java-formatter" % "0.6.0") addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.3") addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0") diff --git a/tests/snapshots/src/main/generated/index-semanticdb/multifile b/tests/snapshots/src/main/generated/index-semanticdb/multifile index 7d02bc2e..d9f95605 100644 --- a/tests/snapshots/src/main/generated/index-semanticdb/multifile +++ b/tests/snapshots/src/main/generated/index-semanticdb/multifile @@ -28,39 +28,39 @@ public class Example2 { │project(2)│ ╰───┬┬─────╯ ││ - ╭────────────────────────────╯╰───────────────────────╮ - │ │ - v │ - ╭──────────────────────────────╮ │ ╭──────────────────╮ - │document example/Example2.java│ │ │referenceResult(9)│ - ╰────────┬─────────────────────╯ │ ╰────┬───┬─────────╯ - │ │ │ │^ - │ ╭────────────────────────────────────────────────────┼───────╯ ││ - │ │ │ ││ - v v │ ││ - ╭────────────────────────╮ │ ││ - │range(58) 3:9 'Example1'│ │ ││ - ╰─────────────┬──────────╯ │ ││ - │ ╭───────────────────────────────────────────────────┼───────────┼╯ - │ │ │ ╰──────────────────────────╮ - v │ │ │ - ╭────────┴───╮ │ │ - │resultSet(4)│ │ │ - ╰───┬───┬──┬─╯ │ │ - │ │ ^│ │ │ - ╭────────────╯ │ ││ │ │ - │ │ ││ │ │ - v │ ││ v │ - ╭───────────────────╮ │ ││ ╭──────────────────────────────╮ │ - │definitionResult(7)│ │ ││ │document example/Example1.java│ │ - ╰─────────┬─────────╯ │ ││ ╰───────────────┬──────────────╯ │ - │ │ ││ │ │ - │ │ ││ ╰─────────────────────────────────╮ │ - │ │ ╰┼──────────────────────────────────────────────────────────────────────────────╮ │ │ - ╰────────────────┼──┼─────────────────────────────────────────────────────────────────────────╮ │ │ │ - ╰──┼─────────────────────────────────────────────────────╮ │ │ │ │ - │ │ │ │ │ │ - v v v │ v v - ╭─────────────────────────────────────────────────────────────────────────────╮ ╭────────────────────────╮ ╭──────────┴──────────────╮ - │hoverResult(15) Example1 docstring \n---\n```java\npublic class Example1\n```│ │export example/Example1#│ │range(11) 3:13 'Example1'│ - ╰─────────────────────────────────────────────────────────────────────────────╯ ╰────────────────────────╯ ╰─────────────────────────╯ + ╭─────────────────────────────╯│ + │ │ + v │ + ╭──────────────────────────────╮ │ ╭──────────────────╮ + │document example/Example2.java│ │ │referenceResult(9)│ + ╰─────────┬────────────────────╯ │ ╰────┬──────┬──────╯ + │ ╰───────────────────────╮ │ ^ │ + │ │ │ │ ╰─────────────────────────╮ + │ ╭────────────────────────────────────────────────────┼────╯ │ │ + │ │ │ │ │ + v v │ │ │ + ╭────────────────────────╮ │ │ │ + │range(58) 3:9 'Example1'│ │ │ │ + ╰─────────────┬──────────╯ │ │ │ + │ ╭───────────────────────────────────────────────────┼─────────╯ │ + v │ │ │ + ╭────────┴───╮ │ │ + │resultSet(4)│ │ │ + ╰───┬───┬──┬─╯ │ │ + │ │ ^│ │ │ + ╭────────────╯ │ ││ │ │ + │ │ ││ │ │ + v │ ││ v │ + ╭───────────────────╮ │ ││ ╭──────────────────────────────╮ │ + │definitionResult(7)│ │ ││ │document example/Example1.java│ │ + ╰─────────┬─────────╯ │ ││ ╰───────────────┬──────────────╯ │ + │ │ ││ │ │ + │ │ ││ ╰────────────────────────────────╮ │ + │ │ ╰┼─────────────────────────────────────────────────────────────────────────────╮ │ │ + ╰────────────────┼──┼────────────────────────────────────────────────────────────────────────╮ │ │ │ + ╰──┼────────────────────────────────────────────────────╮ │ │ │ │ + │ │ │ │ │ │ + v v v │ v v + ╭────────────────────────────────────────────────────────────────────────────╮ ╭────────────────────────╮ ╭──────────┴──────────────╮ + │hoverResult(15) ```java\npublic class Example1\n```\n---\nExample1 docstring│ │export example/Example1#│ │range(11) 3:13 'Example1'│ + ╰────────────────────────────────────────────────────────────────────────────╯ ╰────────────────────────╯ ╰─────────────────────────╯ diff --git a/tests/snapshots/src/main/generated/index-semanticdb/reference b/tests/snapshots/src/main/generated/index-semanticdb/reference index d03f61c0..0fb6ac01 100644 --- a/tests/snapshots/src/main/generated/index-semanticdb/reference +++ b/tests/snapshots/src/main/generated/index-semanticdb/reference @@ -27,30 +27,31 @@ public class Example { │document example/Example.java│ │referenceResult(9)│ ╰─┬───────────────────┬───────╯ ╰────┬───────────┬─╯ │ │ ^ │ │ - │ │ ╭─────────────────╯ │ ╰╮ - │ │ ╭──────────┼─────────────────────╯ │ - │ │ │ │ │ - │ v v │ │ - │ ╭───────────────────────╮ │ │ - │ │range(30) 3:2 'Example'│ │ │ - │ ╰──────────────────────┬╯ │ │ - │ │ │ │ - │ v │ │ - │ ╭────────┴───╮ │ - │ │resultSet(4)│ │ - │ ╰───┬┬──┬────╯ │ - │ ││ │ ^ │ - │ ╭───────────────────────╯│ │ ╰────────────────────────────╮ │ - ╰─────────┼────────────────────────┼──┼────────────────────╮ │ │ - v │ │ │ │ │ - ╭───────────────────╮ │ │ │ │ │ - │definitionResult(7)│ │ │ │ │ │ - ╰─────────┬─────────╯ │ │ │ │ │ - │ ╭──────────────────────╯ │ │ │ │ - │ │ ╰────────────────────┼─────────┼────┼──────────────────╮ - ╰─┼──────────────────────────────────────────────┼────╮ │ │ │ - │ │ │ │ │ │ - v v v │ v v - ╭───────────────────────────────────────────────────────────────────────────────╮ ╭──────────────┴─────────╮ ╭───────────────────────╮ - │hoverResult(15) Docstring for Example \n---\n```java\npublic class Example\n```│ │range(11) 2:13 'Example'│ │export example/Example#│ - ╰───────────────────────────────────────────────────────────────────────────────╯ ╰────────────────────────╯ ╰───────────────────────╯ + │ │ ╭─────────────────╯ │ ╰──────╮ + │ │ ╭──────────┼─────────────────────╯ │ + │ │ │ │ │ + │ v v │ │ + │ ╭───────────────────────╮ │ │ + │ │range(30) 3:2 'Example'│ │ │ + │ ╰──────────────────────┬╯ │ │ + │ │ │ │ + │ v │ │ + │ ╭────────┴───╮ │ + │ │resultSet(4)│ │ + │ ╰───┬┬──┬────╯ │ + │ ││ │ ^ │ + │ ╭───────────────────────╯│ │ ╰───────────────────────────╮ │ + ╰─────────┼────────────────────────┼──┼───────────────────╮ │ │ + v │ │ │ │ │ + ╭───────────────────╮ │ │ │ │ │ + │definitionResult(7)│ │ │ │ │ │ + ╰─────────┬─────────╯ │ │ │ │ │ + │ ╭──────────────────────╯ │ │ │ │ + │ │ ╰───────────────────┼─────────┼───────────┼───────────╮ + ╰─┼─────────────────────────────────────────────┼────╮ │ │ │ + │ │ │ │ ╭──────╯ │ + │ │ │ │ │ │ + v v v │ v v + ╭──────────────────────────────────────────────────────────────────────────────╮ ╭──────────────┴─────────╮ ╭───────────────────────╮ + │hoverResult(15) ```java\npublic class Example\n```\n---\nDocstring for Example│ │range(11) 2:13 'Example'│ │export example/Example#│ + ╰──────────────────────────────────────────────────────────────────────────────╯ ╰────────────────────────╯ ╰───────────────────────╯