Skip to content

Commit

Permalink
Documentation when hovering object members in Qute
Browse files Browse the repository at this point in the history
eg.

The documentation for `java.lang.String.length()` will be shown when
hovering here:

```
{myString.len|gth()}
```

Closes redhat-developer#452

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Sep 22, 2022
1 parent 2fcfb40 commit 233f499
Show file tree
Hide file tree
Showing 31 changed files with 956 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

public class Item {

/**
* The name of the item
*/
public final String name;

public final BigDecimal price;
Expand All @@ -17,6 +20,11 @@ public Item(BigDecimal price, String name) {
this.name = name;
}

/**
* Returns the derived items.
*
* @return the derived items
*/
public Item[] getDerivedItems() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ public class ClassA extends ClassC {

public String name;

/**
* cyclic documentation
*/
public String convert() {
return "hello";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void updateVisibilityOfFieldSimple() throws Exception {
WorkspaceEdit actual = QuteSupportForTemplate.getInstance().generateMissingJavaMember(params, getJDTUtils(),
new NullProgressMonitor());
WorkspaceEdit expected = we(
Either.forLeft(tde(project, "src/main/java/org/acme/qute/Item.java", te(12, 1, 12, 8, "public"))));
Either.forLeft(tde(project, "src/main/java/org/acme/qute/Item.java", te(15, 1, 15, 8, "public"))));
assertWorkspaceEdit(expected, actual);

}
Expand All @@ -105,9 +105,12 @@ public void updateVisibilityOfFieldComplex() throws Exception {
WorkspaceEdit actual = QuteSupportForTemplate.getInstance().generateMissingJavaMember(params, getJDTUtils(),
new NullProgressMonitor());
WorkspaceEdit expected = we(Either.forLeft(tde(project, "src/main/java/org/acme/qute/Item.java",
te(6, 1, 10, 35, //
te(6, 1, 13, 35, //
"public int identifier = 0;\r\n" //
+ "\r\n" //
+ "\t/**\r\n" //
+ "\t * The name of the item\r\n" //
+ "\t */\r\n" //
+ "\tpublic final String name;\r\n" //
+ "\r\n" //
+ "\tpublic final BigDecimal price;\r\n" //
Expand Down Expand Up @@ -142,10 +145,13 @@ public void generateGetterWithMatchingField() throws Exception {
WorkspaceEdit actual = QuteSupportForTemplate.getInstance().generateMissingJavaMember(params, getJDTUtils(),
new NullProgressMonitor());
WorkspaceEdit expected = we(Either.forLeft(tde(project, "src/main/java/org/acme/qute/Item.java",
te(6, 1, 10, 18, //
te(6, 1, 13, 18, //
"public int getIdentifier() {\r\n" //
+ "\t\treturn this.identifier;\r\n" //
+ "\t}\r\n\r\n" //
+ "\t/**\r\n" //
+ "\t * The name of the item\r\n" //
+ "\t */\r\n" //
+ "\tpublic final String name;\r\n\r\n" //
+ "\tpublic final BigDecimal price;\r\n" //
+ "\r\n" //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.jdt.template;

import static com.redhat.qute.jdt.QuteProjectTest.getJDTUtils;
import static com.redhat.qute.jdt.QuteProjectTest.loadMavenProject;
import static org.junit.Assert.assertEquals;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.junit.Test;

import com.redhat.qute.commons.DocumentFormat;
import com.redhat.qute.commons.QuteJavadocParams;
import com.redhat.qute.jdt.QuteProjectTest.QuteMavenProjectName;
import com.redhat.qute.jdt.QuteSupportForTemplate;

/**
* Tests for getting the formatted Javadocs for Java members
*
* @author datho7561
*/
public class TemplateGetJavadocTest {

@Test
public void getFieldJavadoc() throws Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavadocParams params = new QuteJavadocParams(//
"org.acme.qute.Item", //
QuteMavenProjectName.qute_quickstart, //
"name", //
"name : java.lang.String", //
DocumentFormat.Markdown);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new NullProgressMonitor());
String expected = "The name of the item";
assertEquals(expected, actual);
}

@Test
public void getMethodJavadoc() throws Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavadocParams params = new QuteJavadocParams(//
"org.acme.qute.Item", //
QuteMavenProjectName.qute_quickstart, //
"getDerivedItems", //
"getDerivedItems() : org.acme.qute.Item[]", //
DocumentFormat.Markdown);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new NullProgressMonitor());
String expected = "Returns the derived items.\n\n * **Returns:**\n \n * the derived items";
assertEquals(expected, actual);
}

@Test
public void getFieldJavadocPlainText() throws Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavadocParams params = new QuteJavadocParams(//
"org.acme.qute.Item", //
QuteMavenProjectName.qute_quickstart, //
"name", //
"name : java.lang.String", //
DocumentFormat.PlainText);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new NullProgressMonitor());
String expected = " The name of the item ";
assertEquals(expected, actual);
}

@Test
public void getMethodJavadocPlainText() throws Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavadocParams params = new QuteJavadocParams(//
"org.acme.qute.Item", //
QuteMavenProjectName.qute_quickstart, //
"getDerivedItems", //
"getDerivedItems() : org.acme.qute.Item[]", //
DocumentFormat.PlainText);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new NullProgressMonitor());
String expected = " Returns the derived items. \n * Returns:\n - the derived items";
assertEquals(expected, actual);
}

@Test
public void getMethodJavadocCyclic() throws Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavadocParams params = new QuteJavadocParams(//
"org.acme.qute.cyclic.ClassC", //
QuteMavenProjectName.qute_quickstart, //
"convert", //
"convert() : java.lang.String", //
DocumentFormat.PlainText);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new NullProgressMonitor());
String expected = " cyclic documentation ";
assertEquals(expected, actual);
}

}
1 change: 1 addition & 0 deletions qute.jdt/com.redhat.qute.jdt/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<command id="qute/template/javaTypes"/>
<command id="qute/template/resolvedJavaType"/>
<command id="qute/template/javaDefinition"/>
<command id="qute/template/javadoc"/>
<command id="qute/template/generateMissingJavaMember"/>
</delegateCommandHandler>
</extension>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.commons;

/**
* Document format.
*
* @author Angelo ZERR
*/
public enum DocumentFormat {

PlainText(1), Markdown(2);

private final int value;

DocumentFormat(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static DocumentFormat forValue(int value) {
DocumentFormat[] allValues = DocumentFormat.values();
if (value < 1 || value > allValues.length)
throw new IllegalArgumentException("Illegal enum value: " + value);
return allValues[value - 1];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.commons;

/**
* Represents the parameters needed to fetch the javadocs from the Java language
* server component.
*
* @author datho7561
*/
public class QuteJavadocParams {

private String sourceType;
private String projectUri;
private String memberName;
private String signature;
private DocumentFormat documentFormat;

public QuteJavadocParams() {
// needed for gson
}

public QuteJavadocParams(String sourceType, String projectUri, String memberName, String signature,
DocumentFormat documentFormat) {
this.sourceType = sourceType;
this.projectUri = projectUri;
this.memberName = memberName;
this.signature = signature;
this.documentFormat = documentFormat;
}

/**
* Returns the fully qualified name of the class from which the Javadocs should
* be retrieved.
*
* @return the fully qualified name of the class from which the Javadocs should
* be retrieved
*/
public String getSourceType() {
return this.sourceType;
}

/**
* Returns the uri of the project where the Javadocs should be retrieved from.
*
* @return the uri of the project where the Javadocs should be retrieved from
*/
public String getProjectUri() {
return this.projectUri;
}

/**
* Returns the name of the field or method to get the documentation of.
*
* @return the name of the field or method to get the documentation of
*/
public String getMemberName() {
return this.memberName;
}

/**
* Returns the signature of the field or method for which the documentation is
* being retrieved.
*
* @return the signature of the field or method for which the documentation is
* being retrieved
*/
public String getSignature() {
return this.signature;
}

/**
* Returns the document format that should be used to represent the
* documentation.
*
* @return the document format that should be used to represent the
* documentation
*/
public DocumentFormat getDocumentFormat() {
return this.documentFormat;
}

}
Loading

0 comments on commit 233f499

Please sign in to comment.