Skip to content

Commit

Permalink
Provide hover participant
Browse files Browse the repository at this point in the history
See redhat-developer#229

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Feb 28, 2020
1 parent a7ac0e1 commit 18f01fc
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<extension point="com.redhat.microprofile.jdt.core.propertiesProviders">
<!-- Properties provider from the MicroProfile @ConfigProperty annotation -->
<provider class="com.redhat.microprofile.jdt.internal.core.providers.MicroProfileConfigPropertyProvider" />
<provider class="com.redhat.microprofile.jdt.internal.config.properties.MicroProfileConfigPropertyProvider" />
<!-- Properties provider from the MicroProfile Fault Tolerance annotations -->
<provider class="com.redhat.microprofile.jdt.internal.faulttolerance.properties.MicroProfileFaultToleranceProvider" />
<!-- Properties provider from the MicroProfile @RegisterRestClient annotation -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<choice>
<element ref="diagnostics"/>
<element ref="hover" minOccurs="0" maxOccurs="unbounded"/>
<element ref="diagnostics" minOccurs="0" maxOccurs="unbounded"/>
</choice>
</sequence>
<attribute name="point" type="string" use="required">
Expand Down Expand Up @@ -69,6 +70,26 @@
</complexType>
</element>

<element name="hover">
<annotation>
<documentation>
Java hover participant.
</documentation>
</annotation>
<complexType>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>
Name of a class that implements IJavaHoverParticipant.
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn=":com.redhat.microprofile.jdt.core.java.IJavaHoverParticipant"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appinfo>
<meta.section type="since"/>
Expand All @@ -86,7 +107,10 @@
The following is an example of a java feature participant extension:

&lt;pre&gt;
&lt;extension point=&quot;com.redhat.quarkus.jdt.core.javaFeatureParticipants&quot;&gt;
&lt;extension point=&quot;com.redhat.microprofile.jdt.core.javaFeatureParticipants&quot;&gt;
&lt;hover
class=&quot;com.example.MyJavaHoverParticipant&quot;&gt;
&lt;/hover&gt;
&lt;diagnostics
class=&quot;com.example.MyJavaDiagnosticsParticipant&quot;&gt;
&lt;/diagnostics&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
* 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.microprofile.jdt.core;

/**
* MicroProfile constants
* MicroProfile Config constants
*
* @author Angelo ZERR
*
*/
public class MicroProfileConstants {
public class MicroProfileConfigConstants {

private MicroProfileConstants() {
private MicroProfileConfigConstants() {
}

public static final String INJECT_ANNOTATION = "javax.inject.Inject";

// MicroProfile Core annotations

public static final String CONFIG_PROPERTY_ANNOTATION = "org.eclipse.microprofile.config.inject.ConfigProperty";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ public MicroProfileJavaHoverInfo hover(MicroProfileJavaHoverParams params, IJDTU

IField hoverField = (IField) hoverElement;

IAnnotation annotation = getAnnotation(hoverField, MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION);
IAnnotation annotation = getAnnotation(hoverField, MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION);

if (annotation == null) {
return null;
}

String annotationSource = ((ISourceReference) annotation).getSource();
String propertyKey = getAnnotationMemberValue(annotation,
MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION_NAME);
MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION_NAME);

if (propertyKey == null) {
return null;
Expand All @@ -310,7 +310,7 @@ public MicroProfileJavaHoverInfo hover(MicroProfileJavaHoverParams params, IJDTU
.getProperty(propertyKey, null);
if (propertyValue == null) {
propertyValue = getAnnotationMemberValue(annotation,
MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
if (propertyValue != null && propertyValue.length() == 0) {
propertyValue = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2020 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.microprofile.jdt.core.java;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.JavaModelException;

/**
* Java hover participants API.
*
* @author Angelo ZERR
*
*/
public interface IJavaHoverParticipant {

/**
* Returns true if hover must be collected for the given context and false
* otherwise.
*
* <p>
* Collection is done by default. Participants can override this to check if
* some classes are on the classpath before deciding to process the collection.
* </p>
*
* @param the java hover context
* @param monitor the progress monitor
* @return true if hover must be collected for the given context and false
* otherwise.
* @throws JavaModelException
*/
default boolean isAdaptedForHover(JavaHoverContext context, IProgressMonitor monitor) throws JavaModelException {
return true;
}

/**
* Collect hover according to the context.
*
* @param context the java hover context
* @param monitor the progress monitor
* @throws JavaModelException
* @throws CoreException
*/
void collectHover(JavaHoverContext context, IProgressMonitor monitor) throws JavaModelException, CoreException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2020 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.microprofile.jdt.core.java;

import java.util.List;

import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.Range;

import com.redhat.microprofile.commons.DocumentFormat;
import com.redhat.microprofile.jdt.core.utils.IJDTUtils;

/**
* Java hover context for a given compilation unit.
*
* @author Angelo ZERR
*
*/
public class JavaHoverContext extends AbtractJavaContext {

private final List<Diagnostic> hover;

private final DocumentFormat documentFormat;

public JavaHoverContext(String uri, ITypeRoot typeRoot, IJDTUtils utils, DocumentFormat documentFormat,
List<Diagnostic> hover) {
super(uri, typeRoot, utils);
this.hover = hover;
this.documentFormat = documentFormat;
}

public List<Diagnostic> getHover() {
return hover;
}

public DocumentFormat getDocumentFormat() {
return documentFormat;
}

public Diagnostic addDiagnostic(String uri, String message, Range range, String source, IJavaErrorCode code) {
Diagnostic diagnostic = createDiagnostic(uri, message, range, source, code);
getHover().add(diagnostic);
return diagnostic;
}

private Diagnostic createDiagnostic(String uri, String message, Range range, String source, IJavaErrorCode code) {
Diagnostic diagnostic = new Diagnostic();
diagnostic.setSource(source);
diagnostic.setMessage(message);
diagnostic.setSeverity(DiagnosticSeverity.Warning);
diagnostic.setRange(range);
if (code != null) {
diagnostic.setCode(code.getCode());
}
return diagnostic;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* 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.microprofile.jdt.internal.core.providers;
package com.redhat.microprofile.jdt.internal.config.properties;

import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotationMemberValue;
import static com.redhat.microprofile.jdt.core.utils.JDTTypeUtils.findType;
Expand All @@ -26,7 +28,7 @@

import com.redhat.microprofile.jdt.core.AbstractAnnotationTypeReferencePropertiesProvider;
import com.redhat.microprofile.jdt.core.IPropertiesCollector;
import com.redhat.microprofile.jdt.core.MicroProfileConstants;
import com.redhat.microprofile.jdt.core.MicroProfileConfigConstants;
import com.redhat.microprofile.jdt.core.SearchContext;

/**
Expand All @@ -39,7 +41,7 @@
*/
public class MicroProfileConfigPropertyProvider extends AbstractAnnotationTypeReferencePropertiesProvider {

private static final String[] ANNOTATION_NAMES = { MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION };
private static final String[] ANNOTATION_NAMES = { MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION };

@Override
protected String[] getAnnotationNames() {
Expand All @@ -52,7 +54,7 @@ protected void processAnnotation(IJavaElement javaElement, IAnnotation configPro
if (javaElement.getElementType() == IJavaElement.FIELD) {
IPropertiesCollector collector = context.getCollector();
String name = getAnnotationMemberValue(configPropertyAnnotation,
MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION_NAME);
MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION_NAME);
if (name != null && !name.isEmpty()) {
IField field = (IField) javaElement;
String fieldTypeName = getResolvedTypeName(field);
Expand All @@ -63,7 +65,7 @@ protected void processAnnotation(IJavaElement javaElement, IAnnotation configPro
String sourceType = getSourceType(field);
String sourceField = getSourceField(field);
String defaultValue = getAnnotationMemberValue(configPropertyAnnotation,
MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
String extensionName = null;

// Enumerations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* 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
*******************************************************************************/
Expand All @@ -12,34 +14,63 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.JavaModelException;

import com.redhat.microprofile.jdt.core.java.IJavaDiagnosticsParticipant;
import com.redhat.microprofile.jdt.core.java.IJavaHoverParticipant;
import com.redhat.microprofile.jdt.core.java.JavaDiagnosticsContext;
import com.redhat.microprofile.jdt.core.java.JavaHoverContext;

/**
* Wrapper class around {@link IJavaDiagnosticsParticipant}.
* Wrapper class around java participants :
*
* <ul>
* <li>{@link IJavaHoverParticipant}.</li>
* <li>{@link IJavaDiagnosticsParticipant}.</li>
* </ul>
*
*/
public class JavaFeatureDefinition implements IJavaDiagnosticsParticipant {
public class JavaFeatureDefinition implements IJavaHoverParticipant, IJavaDiagnosticsParticipant {
private static final Logger LOGGER = Logger.getLogger(JavaFeatureDefinition.class.getName());
private final IJavaDiagnosticsParticipant collector;

public JavaFeatureDefinition(IJavaDiagnosticsParticipant collector) {
this.collector = collector;
private final IJavaHoverParticipant hoverParticipant;
private final IJavaDiagnosticsParticipant diagnosticsParticipant;

public JavaFeatureDefinition(IJavaHoverParticipant hoverParticipant,
IJavaDiagnosticsParticipant diagnosticsParticipant) {
this.hoverParticipant = hoverParticipant;
this.diagnosticsParticipant = diagnosticsParticipant;
}

@Override
public void collectHover(JavaHoverContext context, IProgressMonitor monitor)
throws JavaModelException, CoreException {
if (hoverParticipant == null) {
return;
}
try {
if (hoverParticipant.isAdaptedForHover(context, monitor)) {
hoverParticipant.collectHover(context, monitor);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while collecting hover", e);
}
}

@Override
public void collectDiagnostics(JavaDiagnosticsContext context, IProgressMonitor monitor) {
if (collector == null) {
if (diagnosticsParticipant == null) {
return;
}
try {
if (collector.isAdaptedForDiagnostics(context, monitor)) {
collector.collectDiagnostics(context, monitor);
if (diagnosticsParticipant.isAdaptedForDiagnostics(context, monitor)) {
diagnosticsParticipant.collectDiagnostics(context, monitor);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while collecting diagnostics", e);
}
}

}
Loading

0 comments on commit 18f01fc

Please sign in to comment.