Skip to content

Commit

Permalink
Return completionItem documentation as plain text
Browse files Browse the repository at this point in the history
Fixes redhat-developer/vscode-java#215

Signed-off-by: Fred Bricon <fbricon@gmail.com>
  • Loading branch information
fbricon committed May 29, 2017
1 parent 448d862 commit ff9aeba
Show file tree
Hide file tree
Showing 15 changed files with 595 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private String computeJavadocHover(IJavaElement element) throws CoreException {
} else if (element instanceof IMember) {
member= (IMember) element;
} else if (element instanceof IPackageFragment) {
Reader r = JavadocContentAccess.getHTMLContentReader((IPackageFragment) element, true);
Reader r = JavadocContentAccess.getMarkdownContentReader((IPackageFragment) element, true);
if(r == null ) {
return null;
}
Expand All @@ -109,7 +109,7 @@ private String computeJavadocHover(IJavaElement element) throws CoreException {
if(javadocRange == null ) {
return null;
}
Reader r = JavadocContentAccess.getHTMLContentReader(member,true,true);
Reader r = JavadocContentAccess.getMarkdownContentReader(member);
if(r == null ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public CompletionItem resolve(CompletionItem param) {
try {
final IMember curMember = member;
javadoc = new SimpleTimeLimiter().callWithTimeout(() -> {
Reader reader = JavadocContentAccess.getHTMLContentReader(curMember, true, true);
Reader reader = JavadocContentAccess.getPlainTextContentReader(curMember);
return reader == null? null:CharStreams.toString(reader);
}, 500, TimeUnit.MILLISECONDS, true);
} catch (UncheckedTimeoutException tooSlow) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.javadoc;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/**
* Converts JavaDoc tags into an output format.
*
* @author Fred Bricon
*/
abstract class AbstractJavaDocConverter {

private JavaDoc2HTMLTextReader reader;

private boolean read;

private String result;

public AbstractJavaDocConverter(Reader reader) {
setJavaDoc2HTMLTextReader(reader);
}

public AbstractJavaDocConverter(String javadoc) {
setJavaDoc2HTMLTextReader(javadoc == null ? null : new StringReader(javadoc));
}

private void setJavaDoc2HTMLTextReader(Reader reader) {
if (reader == null || reader instanceof JavaDoc2HTMLTextReader) {
this.reader = (JavaDoc2HTMLTextReader) reader;
} else {
this.reader = new JavaDoc2HTMLTextReader(reader);
}
}

public String getAsString() throws IOException {
if (!read && reader != null) {
String rawHtml = reader.getString();
result = convert(rawHtml);
}
return result;
}

public Reader getAsReader() throws IOException {
String m = getAsString();
return m == null ? null : new StringReader(m);
}

abstract String convert(String rawHtml);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright © 2009-2017, Jonathan Hedley <jonathan@hedley.net>
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.eclipse.jdt.ls.core.internal.javadoc;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;

/**
* HTML to plain-text. Uses jsoup to convert HTML input to lightly-formatted
* plain-text. This is a fork of Jsoup's <a href=
* "https://github.com/jhy/jsoup/blob/842977c381b8d48bf12719e3f5cf6fd669379957/src/main/java/org/jsoup/examples/HtmlToPlainText.java">HtmlToPlainText</a>
*
* @author Jonathan Hedley, jonathan@hedley.net
*/
public class HtmlToPlainText {

/**
* Format an Element to plain-text
* @param element the root element to format
* @return formatted text
*/
public String getPlainText(Element element) {
FormattingVisitor formatter = new FormattingVisitor();
NodeTraversor traversor = new NodeTraversor(formatter);
traversor.traverse(element); // walk the DOM, and call .head() and .tail() for each node

return formatter.toString();
}

// the formatting rules, implemented in a breadth-first DOM traverse
private class FormattingVisitor implements NodeVisitor {
private StringBuilder accum = new StringBuilder(); // holds the accumulated text
private int listNesting;
// hit when the node is first seen
@Override
public void head(Node node, int depth) {
String name = node.nodeName();
if (node instanceof TextNode) {
append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
} else if (name.equals("ul")) {
listNesting++;
} else if (name.equals("li")) {
append("\n ");
for (int i = 1; i < listNesting; i++) {
append(" ");
}
if (listNesting == 1) {
append("* ");
} else {
append("- ");
}
} else if (name.equals("dt")) {
append(" ");
} else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr")) {
append("\n");
}
}

// hit when all of the node's children (if any) have been visited
@Override
public void tail(Node node, int depth) {
String name = node.nodeName();
if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5")) {
append("\n");
} else if (StringUtil.in(name, "th", "td")) {
append(" ");
} else if (name.equals("a")) {
append(String.format(" <%s>", node.absUrl("href")));
} else if (name.equals("ul")) {
listNesting--;
}
}

// appends text to the string builder with a simple word wrap method
private void append(String text) {
if (text.equals(" ") &&
(accum.length() == 0 || StringUtil.in(accum.substring(accum.length() - 1), " ", "\n")))
{
return; // don't accumulate long runs of empty spaces
}
accum.append(text);
}

@Override
public String toString() {
return accum.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.javadoc;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import com.overzealous.remark.Options;
import com.overzealous.remark.Options.Tables;
Expand All @@ -23,12 +21,10 @@
*
* @author Fred Bricon
*/
public class JavaDoc2MarkdownConverter {
public class JavaDoc2MarkdownConverter extends AbstractJavaDocConverter {

private static Remark remark;

private String markDown;

static {
Options options = new Options();
options.tables = Tables.CONVERT_TO_CODE_BLOCK;
Expand All @@ -38,38 +34,17 @@ public class JavaDoc2MarkdownConverter {
options.reverseHtmlSmartPunctuation = true;
remark = new Remark(options);
}

private JavaDoc2HTMLTextReader reader;

private boolean read;

public JavaDoc2MarkdownConverter(Reader reader) {
setJavaDoc2HTMLTextReader(reader);
super(reader);
}


public JavaDoc2MarkdownConverter(String javadoc) {
setJavaDoc2HTMLTextReader(javadoc== null? null:new StringReader(javadoc));
}

private void setJavaDoc2HTMLTextReader(Reader reader) {
if (reader == null || reader instanceof JavaDoc2HTMLTextReader) {
this.reader = (JavaDoc2HTMLTextReader) reader;
} else {
this.reader = new JavaDoc2HTMLTextReader(reader);
}
}

public String getAsString() throws IOException {
if (!read && reader != null) {
String rawHtml = reader.getString();
markDown = remark.convert(rawHtml);
}
return markDown;
super(javadoc);
}

public Reader getAsReader() throws IOException {
String m = getAsString();
return m == null ? null : new StringReader(m);
@Override
String convert(String rawHtml) {
return remark.convert(rawHtml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.javadoc;

import java.io.Reader;

import org.jsoup.Jsoup;

/**
* Converts JavaDoc tags into plain text equivalent.
*
* @author Fred Bricon
*/
public class JavaDoc2PlainTextConverter extends AbstractJavaDocConverter {

public JavaDoc2PlainTextConverter(Reader reader) {
super(reader);
}

public JavaDoc2PlainTextConverter(String javadoc) {
super(javadoc);
}

@Override
String convert(String rawHtml) {
HtmlToPlainText formatter = new HtmlToPlainText();
return formatter.getPlainText(Jsoup.parse(rawHtml));
}
}
Loading

0 comments on commit ff9aeba

Please sign in to comment.