Skip to content

Commit bd43651

Browse files
authored
Merge pull request #181 from lahodaj/jep-467-tables
Fixing support for Markdown tables.
2 parents 37ee8fd + 4892518 commit bd43651

File tree

1 file changed

+174
-5
lines changed

1 file changed

+174
-5
lines changed

patches/7491-preliminary.diff

Lines changed: 174 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,19 +1500,22 @@ index 9ac80a3c09..1f7e220870 100644
15001500
<code-name-base>org.netbeans.libs.javacapi</code-name-base>
15011501
<build-prerequisite/>
15021502
diff --git a/java/java.sourceui/src/org/netbeans/api/java/source/ui/ElementJavadoc.java b/java/java.sourceui/src/org/netbeans/api/java/source/ui/ElementJavadoc.java
1503-
index fdabe50444..b1d1aa9ffd 100644
1503+
index fdabe50444..0ce9410f9e 100644
15041504
--- a/java/java.sourceui/src/org/netbeans/api/java/source/ui/ElementJavadoc.java
15051505
+++ b/java/java.sourceui/src/org/netbeans/api/java/source/ui/ElementJavadoc.java
1506-
@@ -119,6 +119,8 @@ import javax.tools.SimpleJavaFileObject;
1506+
@@ -119,6 +119,11 @@ import javax.tools.SimpleJavaFileObject;
15071507
import com.sun.source.tree.ImportTree;
15081508
import com.sun.source.tree.Tree;
15091509
import com.sun.source.util.JavacTask;
1510+
+import com.vladsch.flexmark.ext.tables.TablesExtension;
15101511
+import com.vladsch.flexmark.html.HtmlRenderer;
15111512
+import com.vladsch.flexmark.parser.Parser;
1513+
+import com.vladsch.flexmark.util.data.DataHolder;
1514+
+import com.vladsch.flexmark.util.data.MutableDataSet;
15121515
import javax.lang.model.element.RecordComponentElement;
15131516
import org.netbeans.api.java.queries.SourceLevelQuery;
15141517
import org.netbeans.api.java.queries.SourceLevelQuery.Profile;
1515-
@@ -1275,7 +1277,7 @@ public class ElementJavadoc {
1518+
@@ -1275,7 +1280,7 @@ public class ElementJavadoc {
15161519
private StringBuilder inlineTags(List<? extends DocTree> tags, TreePath docPath, DocCommentTree doc, DocTrees trees, CharSequence inherited) {
15171520
StringBuilder sb = new StringBuilder();
15181521
Integer snippetCount=0;
@@ -1521,7 +1524,7 @@ index fdabe50444..b1d1aa9ffd 100644
15211524
switch (tag.getKind()) {
15221525
case REFERENCE:
15231526
ReferenceTree refTag = (ReferenceTree)tag;
1524-
@@ -1395,6 +1397,42 @@ public class ElementJavadoc {
1527+
@@ -1395,6 +1400,53 @@ public class ElementJavadoc {
15251528
return sb;
15261529
}
15271530

@@ -1543,7 +1546,18 @@ index fdabe50444..b1d1aa9ffd 100644
15431546
+ }
15441547
+ }
15451548
+
1546-
+ String html = HtmlRenderer.builder().build().render(Parser.builder().build().parse(markdownSource.toString()));
1549+
+ TablesExtension tablesExtension = TablesExtension.create();
1550+
+
1551+
+ Parser.Builder parserBuilder = Parser.builder();
1552+
+ tablesExtension.extend(parserBuilder);
1553+
+
1554+
+ HtmlRenderer.Builder rendererBuilder = HtmlRenderer.builder();
1555+
+ tablesExtension.extend(rendererBuilder, "HTML");
1556+
+
1557+
+ String html = rendererBuilder.build()
1558+
+ .render(parserBuilder.build()
1559+
+ .parse(markdownSource.toString()));
1560+
+
15471561
+ if (html.startsWith("<p>")) {
15481562
+ html = html.substring("<p>".length());
15491563
+ }
@@ -1564,3 +1578,158 @@ index fdabe50444..b1d1aa9ffd 100644
15641578
private void processDocSnippet(StringBuilder sb, SnippetTree javadocSnippet, Integer snippetCount, TreePath docPath,DocCommentTree doc, DocTrees trees) {
15651579
sb.append("<div id=\"snippet").append(snippetCount).append("\" style=\"font-size: 10px; border: 1px solid black; margin-top: 2px; margin-bottom: 2px\">"); //NOI18N
15661580
sb.append("<div align=right>" //NOI18N
1581+
diff --git a/java/java.sourceui/test/unit/src/org/netbeans/api/java/source/ui/ElementJavadocTest.java b/java/java.sourceui/test/unit/src/org/netbeans/api/java/source/ui/ElementJavadocTest.java
1582+
new file mode 100644
1583+
index 0000000000..9aaa49ecb5
1584+
--- /dev/null
1585+
+++ b/java/java.sourceui/test/unit/src/org/netbeans/api/java/source/ui/ElementJavadocTest.java
1586+
@@ -0,0 +1,149 @@
1587+
+/*
1588+
+ * Licensed to the Apache Software Foundation (ASF) under one
1589+
+ * or more contributor license agreements. See the NOTICE file
1590+
+ * distributed with this work for additional information
1591+
+ * regarding copyright ownership. The ASF licenses this file
1592+
+ * to you under the Apache License, Version 2.0 (the
1593+
+ * "License"); you may not use this file except in compliance
1594+
+ * with the License. You may obtain a copy of the License at
1595+
+ *
1596+
+ * http://www.apache.org/licenses/LICENSE-2.0
1597+
+ *
1598+
+ * Unless required by applicable law or agreed to in writing,
1599+
+ * software distributed under the License is distributed on an
1600+
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1601+
+ * KIND, either express or implied. See the License for the
1602+
+ * specific language governing permissions and limitations
1603+
+ * under the License.
1604+
+ */
1605+
+package org.netbeans.api.java.source.ui;
1606+
+
1607+
+import com.sun.source.util.TreePath;
1608+
+import java.io.File;
1609+
+import java.util.ArrayList;
1610+
+import java.util.List;
1611+
+import javax.lang.model.element.Element;
1612+
+import javax.swing.text.Document;
1613+
+import org.netbeans.api.java.lexer.JavaTokenId;
1614+
+import org.netbeans.api.java.source.CompilationInfo;
1615+
+import org.netbeans.api.java.source.JavaSource;
1616+
+import org.netbeans.api.java.source.JavaSource.Phase;
1617+
+import org.netbeans.api.java.source.SourceUtilsTestUtil;
1618+
+import org.netbeans.api.java.source.TestUtilities;
1619+
+import org.netbeans.api.lexer.Language;
1620+
+import org.netbeans.junit.NbTestCase;
1621+
+import org.netbeans.modules.java.JavaDataLoader;
1622+
+import org.openide.cookies.EditorCookie;
1623+
+import org.openide.filesystems.FileObject;
1624+
+import org.openide.filesystems.FileUtil;
1625+
+import org.openide.loaders.DataObject;
1626+
+
1627+
+public class ElementJavadocTest extends NbTestCase {
1628+
+
1629+
+ private static final String CARET_MARK = "<caret>";
1630+
+
1631+
+ public ElementJavadocTest(String testName) {
1632+
+ super(testName);
1633+
+ }
1634+
+
1635+
+ private void prepareTest(String fileName, String code) throws Exception {
1636+
+ int pos = code.indexOf(CARET_MARK);
1637+
+
1638+
+ if (pos == (-1)) {
1639+
+ throw new AssertionError("Does not have caret position!");
1640+
+ }
1641+
+
1642+
+ code = code.substring(0, pos) + code.substring(pos + CARET_MARK.length());
1643+
+
1644+
+ List<Object> extras = new ArrayList<>();
1645+
+ extras.add(JavaDataLoader.class);
1646+
+ SourceUtilsTestUtil.prepareTest(new String[] {
1647+
+ "org/netbeans/modules/java/platform/resources/layer.xml",
1648+
+ "org/netbeans/modules/java/j2seplatform/resources/layer.xml"
1649+
+ },
1650+
+ extras.toArray(new Object[0])
1651+
+ );
1652+
+
1653+
+ clearWorkDir();
1654+
+
1655+
+ FileUtil.refreshAll();
1656+
+
1657+
+ FileObject workFO = FileUtil.toFileObject(getWorkDir());
1658+
+
1659+
+ assertNotNull(workFO);
1660+
+
1661+
+ sourceRoot = workFO.createFolder("src");
1662+
+
1663+
+ FileObject buildRoot = workFO.createFolder("build");
1664+
+ FileObject cache = workFO.createFolder("cache");
1665+
+
1666+
+ FileObject data = FileUtil.createData(sourceRoot, fileName);
1667+
+ File dataFile = FileUtil.toFile(data);
1668+
+
1669+
+ assertNotNull(dataFile);
1670+
+
1671+
+ TestUtilities.copyStringToFile(dataFile, code);
1672+
+
1673+
+ SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache, new FileObject[0]);
1674+
+
1675+
+ DataObject od = DataObject.find(data);
1676+
+ EditorCookie ec = od.getCookie(EditorCookie.class);
1677+
+
1678+
+ assertNotNull(ec);
1679+
+
1680+
+ doc = ec.openDocument();
1681+
+ doc.putProperty(Language.class, JavaTokenId.language());
1682+
+
1683+
+ JavaSource js = JavaSource.forFileObject(data);
1684+
+
1685+
+ assertNotNull(js);
1686+
+
1687+
+ info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
1688+
+
1689+
+ assertNotNull(info);
1690+
+
1691+
+ selectedPath = info.getTreeUtilities().pathFor(pos);
1692+
+ selectedElement = info.getTrees().getElement(selectedPath);
1693+
+
1694+
+ assertNotNull(selectedElement);
1695+
+ }
1696+
+
1697+
+ private FileObject sourceRoot;
1698+
+ private CompilationInfo info;
1699+
+ private Document doc;
1700+
+ private TreePath selectedPath;
1701+
+ private Element selectedElement;
1702+
+
1703+
+ protected void performTest(String fileName, String code, int pos, String format, String golden) throws Exception {
1704+
+ prepareTest(fileName, code);
1705+
+
1706+
+ TreePath path = info.getTreeUtilities().pathFor(pos);
1707+
+
1708+
+ assertEquals(golden, ElementHeaders.getHeader(path, info, format));
1709+
+ }
1710+
+
1711+
+ public void testMarkdownTables() throws Exception {
1712+
+ prepareTest("test/Test.java",
1713+
+ "///| header1 | header2 |\n" +
1714+
+ "///|---------|---------|\n" +
1715+
+ "///| cr11 | cr12 |\n" +
1716+
+ "///| cr21 | cr22 |\n" +
1717+
+ "public class T<caret>est {\n" +
1718+
+ "}\n");
1719+
+
1720+
+ String actualJavadoc = ElementJavadoc.create(info, selectedElement).getText();
1721+
+ String expectedJavadoc = "<pre>public class <b>Test</b><br>extends <a href='*0'>Object</a></pre><p><table>\n" +
1722+
+ "<thead>\n" +
1723+
+ "<tr><th>header1</th><th>header2</th></tr>\n" +
1724+
+ "</thead>\n" +
1725+
+ "<tbody>\n" +
1726+
+ "<tr><td>cr11</td><td>cr12</td></tr>\n" +
1727+
+ "<tr><td>cr21</td><td>cr22</td></tr>\n" +
1728+
+ "</tbody>\n" +
1729+
+ "</table>\n" +
1730+
+ "<p>";
1731+
+
1732+
+ assertEquals(expectedJavadoc, actualJavadoc);
1733+
+ }
1734+
+
1735+
+}

0 commit comments

Comments
 (0)