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 #452

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and angelozerr committed Oct 5, 2022
1 parent 2da7df2 commit ed5e29a
Show file tree
Hide file tree
Showing 38 changed files with 996 additions and 49 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 @@ -2,8 +2,14 @@

public class B<B1,B2> {

/**
* some docs
*/
public B1 field;

/**
* some docs
*/
public B1 get(B2 param) {
return null;
}
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,129 @@
/*******************************************************************************
* 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);
}

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

QuteJavadocParams params = new QuteJavadocParams(//
"org.acme.qute.generic.B", //
QuteMavenProjectName.qute_quickstart, //
"get", //
"get(param : B2) : B1", //
DocumentFormat.PlainText);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new NullProgressMonitor());
String expected = " some docs ";
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) 2022 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
Expand Up @@ -159,6 +159,7 @@ public static JavaFieldInfo applyGenericTypeInvocation(JavaFieldInfo field, Map<
newSignature.append(" : ");
JavaTypeInfo.applyGenericTypeInvocation(field.getType(), genericMap, newSignature);
newField.setSignature(newSignature.toString());
newField.setGenericMember(field);
return newField;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public abstract class JavaMemberInfo extends JavaElementInfo {

private transient JavaTypeInfo javaType;

private transient JavaMemberInfo genericMember;

/**
* Returns the owner Java type and null otherwise.
*
Expand Down Expand Up @@ -73,4 +75,19 @@ public String resolveJavaElementType(ResolvedJavaTypeInfo argType) {
return getJavaElementType();
}

/**
* Returns the generic version of this member, or null in the case that there is
* no generic version or this is already the generic version.
*
* @return the generic version of this member, or null in the case that there is
* no generic version or this is already the generic version.
*/
public JavaMemberInfo getGenericMember() {
return genericMember;
}

protected void setGenericMember(JavaMemberInfo genericMember) {
this.genericMember = genericMember;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ public static JavaMethodInfo applyGenericTypeInvocation(JavaMethodInfo method, M
JavaTypeInfo.applyGenericTypeInvocation(returnType, genericMap, newSignature);
}
newMethod.setSignature(newSignature.toString());
newMethod.setGenericMember(method);
return newMethod;
}

Expand Down
Loading

0 comments on commit ed5e29a

Please sign in to comment.