diff options
author | George Fraser <george@fivetran.com> | 2018-09-16 20:06:44 -0700 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-09-16 20:07:11 -0700 |
commit | ca614cd7f02918b89f0857e23d760d9b25543420 (patch) | |
tree | ec25981360383f0bb14ef6b8a34f8be3ed5aafb1 /src | |
parent | 177395e3bd3f59064adc0e8744af6173dd8aae03 (diff) | |
download | java-language-server-ca614cd7f02918b89f0857e23d760d9b25543420.zip |
Eliminate depndency on remark
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/module-info.java | 2 | ||||
-rw-r--r-- | src/main/java/org/javacs/Docs.java | 13 | ||||
-rw-r--r-- | src/main/java/org/javacs/InferConfig.java | 1 | ||||
-rw-r--r-- | src/main/java/org/javacs/TipFormatter.java | 66 | ||||
-rw-r--r-- | src/test/java/org/javacs/TipFormatterTest.java | 16 |
5 files changed, 85 insertions, 13 deletions
diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 32d049b..a99ddc8 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -2,6 +2,7 @@ module javacs { requires com.google.common; requires jdk.compiler; requires java.logging; + requires java.xml; requires org.eclipse.lsp4j; requires gson; requires org.eclipse.lsp4j.jsonrpc; @@ -9,5 +10,4 @@ module javacs { requires com.fasterxml.jackson.databind; requires com.fasterxml.jackson.datatype.jdk8; requires com.fasterxml.jackson.datatype.jsr310; - requires remark; } diff --git a/src/main/java/org/javacs/Docs.java b/src/main/java/org/javacs/Docs.java index 2e5e5e0..d2d4c69 100644 --- a/src/main/java/org/javacs/Docs.java +++ b/src/main/java/org/javacs/Docs.java @@ -1,7 +1,5 @@ package org.javacs; -import com.overzealous.remark.Options; -import com.overzealous.remark.Remark; import com.sun.source.doctree.DocCommentTree; import com.sun.source.tree.*; import com.sun.source.util.DocTrees; @@ -201,19 +199,10 @@ class Docs { return false; } - // TODO is this still necessary? /** If `commentText` looks like HTML, convert it to markdown */ static String htmlToMarkdown(String commentText) { if (isHtml(commentText)) { - var options = new Options(); - - options.tables = Options.Tables.CONVERT_TO_CODE_BLOCK; - options.hardwraps = true; - options.inlineLinks = true; - options.autoLinks = true; - options.reverseHtmlSmartPunctuation = true; - - return new Remark(options).convertFragment(commentText); + return TipFormatter.asMarkdown(commentText); } else return commentText; } diff --git a/src/main/java/org/javacs/InferConfig.java b/src/main/java/org/javacs/InferConfig.java index 5803260..f6c5dea 100644 --- a/src/main/java/org/javacs/InferConfig.java +++ b/src/main/java/org/javacs/InferConfig.java @@ -288,6 +288,7 @@ class InferConfig { // Tell maven to output deps to a temporary file var outputFile = Files.createTempFile("deps", ".txt"); + // TODO consider using mvn dependency:copy-dependencies instead var cmd = String.format( "%s dependency:list -DincludeScope=test -DoutputFile=%s", getMvnCommand(), outputFile); diff --git a/src/main/java/org/javacs/TipFormatter.java b/src/main/java/org/javacs/TipFormatter.java new file mode 100644 index 0000000..723987b --- /dev/null +++ b/src/main/java/org/javacs/TipFormatter.java @@ -0,0 +1,66 @@ +package org.javacs; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.function.Function; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +class TipFormatter { + + private static Document parse(String html) { + try { + var xml = "<wrapper>" + html + "</wrapper>"; + var factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(false); + var builder = factory.newDocumentBuilder(); + return builder.parse(new InputSource(new StringReader(xml))); + } catch (ParserConfigurationException | SAXException | IOException e) { + throw new RuntimeException(e); + } + } + + private static void replaceNodes(Document doc, String tagName, Function<String, String> replace) { + var nodes = doc.getElementsByTagName(tagName); + for (var i = 0; i < nodes.getLength(); i++) { + var node = nodes.item(i); + var parent = node.getParentNode(); + var text = replace.apply(node.getTextContent()); + var replacement = doc.createTextNode(text); + parent.replaceChild(replacement, node); + } + } + + private static String print(Document doc) { + try { + var tf = TransformerFactory.newInstance(); + var transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + var writer = new StringWriter(); + transformer.transform(new DOMSource(doc), new StreamResult(writer)); + var wrapped = writer.getBuffer().toString(); + return wrapped.substring("<wrapper>".length(), wrapped.length() - "</wrapper>".length()); + } catch (TransformerException e) { + throw new RuntimeException(e); + } + } + + static String asMarkdown(String html) { + var doc = parse(html); + + replaceNodes(doc, "i", contents -> String.format("*%s*", contents)); + replaceNodes(doc, "b", contents -> String.format("**%s**", contents)); + replaceNodes(doc, "pre", contents -> String.format("`%s`", contents)); + + return print(doc); + } +} diff --git a/src/test/java/org/javacs/TipFormatterTest.java b/src/test/java/org/javacs/TipFormatterTest.java new file mode 100644 index 0000000..2358013 --- /dev/null +++ b/src/test/java/org/javacs/TipFormatterTest.java @@ -0,0 +1,16 @@ +package org.javacs; + +import static org.hamcrest.Matchers.*; +import static org.javacs.TipFormatter.asMarkdown; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TipFormatterTest { + @Test + public void formatSimpleTags() { + assertThat(asMarkdown("<i>foo</i>"), equalTo("*foo*")); + assertThat(asMarkdown("<b>foo</b>"), equalTo("**foo**")); + assertThat(asMarkdown("<pre>foo</pre>"), equalTo("`foo`")); + } +} |