forked from eclipse-jdtls/eclipse.jdt.ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return completionItem documentation as plain text
Fixes redhat-developer/vscode-java#215 Signed-off-by: Fred Bricon <fbricon@gmail.com>
- Loading branch information
Showing
15 changed files
with
595 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...se.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/javadoc/AbstractJavaDocConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
114 changes: 114 additions & 0 deletions
114
org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/javadoc/HtmlToPlainText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
....jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/javadoc/JavaDoc2PlainTextConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.