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 580b5b9
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 40 deletions.
39 changes: 30 additions & 9 deletions microprofile.jdt/com.redhat.microprofile.jdt.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,47 @@
</delegateCommandHandler>
</extension>

<extension point="com.redhat.microprofile.jdt.core.projectLabelProviders">
<provider class="com.redhat.microprofile.jdt.internal.core.providers.MavenProjectLabelProvider" />
<provider class="com.redhat.microprofile.jdt.internal.core.providers.GradleProjectLabelProvider" />
</extension>

<!-- Microprofile Config support -->

<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" />
</extension>

<extension point="com.redhat.microprofile.jdt.core.javaFeatureParticipants">
<!-- Java hover for the MicroProfile @ConfigProperty annotation -->
<hover class="com.redhat.microprofile.jdt.internal.config.java.MicroProfileConfigHoverParticipant" />
</extension>

<!-- Microprofile Fault Tolerance support -->

<extension point="com.redhat.microprofile.jdt.core.propertiesProviders">
<!-- 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 -->
<provider class="com.redhat.microprofile.jdt.internal.restclient.properties.MicroProfileRegisterRestClientProvider" />
</extension>

<!-- Microprofile Health support -->

<extension point="com.redhat.microprofile.jdt.core.javaFeatureParticipants">
<!-- Java diagnostics for the MicroProfile Health -->
<diagnostics class="com.redhat.microprofile.jdt.internal.health.java.MicroProfileHealthDiagnosticsParticipant" />
<!-- Java diagnostics for the MicroProfile RestClient -->
<diagnostics class="com.redhat.microprofile.jdt.internal.restclient.java.MicroProfileRestClientDiagnosticsParticipant" />
</extension>

<extension point="com.redhat.microprofile.jdt.core.projectLabelProviders">
<provider class="com.redhat.microprofile.jdt.internal.core.providers.MavenProjectLabelProvider" />
<provider class="com.redhat.microprofile.jdt.internal.core.providers.GradleProjectLabelProvider" />
<!-- Microprofile RestClient support -->

<extension point="com.redhat.microprofile.jdt.core.propertiesProviders">
<!-- Properties provider from the MicroProfile @RegisterRestClient annotation -->
<provider class="com.redhat.microprofile.jdt.internal.restclient.properties.MicroProfileRegisterRestClientProvider" />
</extension>

<extension point="com.redhat.microprofile.jdt.core.javaFeatureParticipants">
<!-- Java diagnostics for the MicroProfile RestClient -->
<diagnostics class="com.redhat.microprofile.jdt.internal.restclient.java.MicroProfileRestClientDiagnosticsParticipant" />
</extension>


</plugin>
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
@@ -0,0 +1,50 @@
/*******************************************************************************
* 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.internal.config.java;

import static com.redhat.microprofile.jdt.core.MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;

import com.redhat.microprofile.jdt.core.java.IJavaHoverParticipant;
import com.redhat.microprofile.jdt.core.java.JavaHoverContext;
import com.redhat.microprofile.jdt.core.utils.JDTTypeUtils;

/**
*
* MicroProfile Config Hover
*
* @author Angelo ZERR
*
* @See https://github.com/eclipse/microprofile-config
*
*/
public class MicroProfileConfigHoverParticipant implements IJavaHoverParticipant {

@Override
public boolean isAdaptedForHover(JavaHoverContext context, IProgressMonitor monitor) throws JavaModelException {
// Hover is done only if microprofile-config is on the classpath
IJavaProject javaProject = context.getJavaProject();
return JDTTypeUtils.findType(javaProject, CONFIG_PROPERTY_ANNOTATION) != null;
}

@Override
public void collectHover(JavaHoverContext context, IProgressMonitor monitor) throws CoreException {
ITypeRoot typeRoot = context.getTypeRoot();
IJavaElement[] elements = typeRoot.getChildren();
}
}
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
Loading

0 comments on commit 580b5b9

Please sign in to comment.