Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBuildSupport extension point #1455

Merged
merged 4 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions org.eclipse.jdt.ls.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<extension-point id="org.eclipse.jdt.ls.core.delegateCommandHandler" name="delegateCommandHandler" schema="schema/org.eclipse.jdt.ls.core.delegateCommandHandler.exsd"/>
<extension-point id="org.eclipse.jdt.ls.core.importers" name="JDT LS Project Importer" schema="schema/org.eclipse.jdt.ls.core.importers.exsd"/>
<extension-point id="org.eclipse.jdt.ls.core.contentProvider" name="contentProvider" schema="schema/org.eclipse.jdt.ls.core.contentProvider.exsd"/>
<extension-point id="org.eclipse.jdt.ls.core.buildSupport" name="Build Support" schema="schema/org.eclipse.jdt.ls.core.buildSupport.exsd"/>
<extension
id="id1"
point="org.eclipse.core.runtime.applications">
Expand Down Expand Up @@ -115,4 +116,28 @@
order="1500"
class="org.eclipse.jdt.ls.core.internal.managers.InvisibleProjectImporter" />
</extension>
<extension
id="buildSupport"
point="org.eclipse.jdt.ls.core.buildSupport">
<buildSupport
id="gradleBuildSupport"
order="300"
class="org.eclipse.jdt.ls.core.internal.managers.GradleBuildSupport" />
<buildSupport
id="mavenBuildSupport"
order="400"
class="org.eclipse.jdt.ls.core.internal.managers.MavenBuildSupport" />
<buildSupport
id="invisibleProjectBuildSupport"
order="1000"
class="org.eclipse.jdt.ls.core.internal.managers.InvisibleProjectBuildSupport" />
<buildSupport
id="defaultProjectBuildSupport"
order="1500"
class="org.eclipse.jdt.ls.core.internal.managers.DefaultProjectBuildSupport" />
<buildSupport
id="eclipseBuildSupport"
order="2000"
class="org.eclipse.jdt.ls.core.internal.managers.EclipseBuildSupport" />
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="org.eclipse.jdt.ls.core" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appinfo>
<meta.schema plugin="org.eclipse.jdt.ls.core" id="org.eclipse.jdt.ls.core.buildSupport" name="Build Support"/>
</appinfo>
<documentation>
This extension point represents different kinds of project build supporters for the JDT LS.
Each extension must implement &lt;code&gt;org.eclipse.jdt.ls.core.internal.managers.IBuildSupport&lt;/code&gt;.
</documentation>
</annotation>

<element name="extension">
<annotation>
<appinfo>
<meta.element />
</appinfo>
</annotation>
<complexType>
<sequence>
<element ref="buildSupport" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>
a fully qualified identifier of the target extension point
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>
an optional identifier of the extension instance
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
an optional name of the extension instance
</documentation>
</annotation>
</attribute>
</complexType>
</element>

<element name="buildSupport">
<complexType>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
A unique identifier that can be used to reference this IBuildSupport.
</documentation>
</annotation>
</attribute>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>
The class that implements this build support. The class must implement IBuildSupport.
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn=":org.eclipse.jdt.ls.core.internal.managers.IBuildSupport"/>
</appinfo>
</annotation>
</attribute>
<attribute name="order" type="string" use="required">
<annotation>
<documentation>
beginningineer marked this conversation as resolved.
Show resolved Hide resolved
The query order of the build support. Lowest value has the highest priority.
</documentation>
</annotation>
</attribute>
</complexType>
</element>


<annotation>
<appinfo>
<meta.section type="examples"/>
</appinfo>
<documentation>
The following is an example of a language server content provider extension:

&lt;pre&gt;
&lt;extension
id=&quot;buildSupport&quot;
point=&quot;org.eclipse.jdt.ls.core.buildSupport&quot;&gt;
&lt;buildSupport
id=&quot;someBuildSupport&quot;
order=&quot;200&quot;
class=&quot;com.example.SomeBuildSupport&quot; /&gt;
&lt;/extension&gt;
&lt;/pre&gt;
</documentation>
</annotation>




</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
package org.eclipse.jdt.ls.core.internal.managers;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;

public class DefaultProjectBuildSupport extends EclipseBuildSupport implements IBuildSupport {

private ProjectsManager projectManager;

public DefaultProjectBuildSupport(ProjectsManager projectManager) {
this.projectManager = projectManager;
}

@Override
public boolean applies(IProject project) {
return projectManager.getDefaultProject().equals(project);
return JavaLanguageServerPlugin.getProjectsManager().getDefaultProject().equals(project);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.eclipse.buildship.core.BuildConfiguration;
Expand Down Expand Up @@ -45,6 +47,7 @@ public class GradleBuildSupport implements IBuildSupport {

public static final String GRADLE_SUFFIX = ".gradle";
public static final String GRADLE_PROPERTIES = "gradle.properties";
public static final List<String> WATCH_FILE_PATTERNS = Arrays.asList("**/*.gradle", "**/gradle.properties");

@Override
public boolean applies(IProject project) {
Expand Down Expand Up @@ -131,4 +134,10 @@ public static void saveModels() {
public ILaunchConfiguration getLaunchConfiguration(IJavaProject javaProject, String scope) throws CoreException {
return new JavaApplicationLaunchConfiguration(javaProject.getProject(), scope, GradleClasspathProvider.ID);
}

@Override
public List<String> getWatchPatterns() {
return WATCH_FILE_PATTERNS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -109,4 +112,8 @@ default ILaunchConfiguration getLaunchConfiguration(IJavaProject javaProject, St
return new JavaApplicationLaunchConfiguration(javaProject.getProject(), scope, null);
}

default List<String> getWatchPatterns() {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -57,6 +58,7 @@
public class MavenBuildSupport implements IBuildSupport {

private static final int MAX_TIME_MILLIS = 3000;
private static final List<String> WATCH_FILE_PATTERNS = Collections.singletonList("**/pom.xml");
private static Cache<String, Boolean> downloadRequestsCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(1, TimeUnit.HOURS).build();

private IProjectConfigurationManager configurationManager;
Expand Down Expand Up @@ -185,4 +187,10 @@ public void discoverSource(IClassFile classFile, IProgressMonitor monitor) throw
public ILaunchConfiguration getLaunchConfiguration(IJavaProject javaProject, String scope) throws CoreException {
return new JavaApplicationLaunchConfiguration(javaProject.getProject(), scope, MavenRuntimeClasspathProvider.MAVEN_CLASSPATH_PROVIDER);
}

@Override
public List<String> getWatchPatterns() {
return WATCH_FILE_PATTERNS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -42,11 +44,14 @@
import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
Expand All @@ -55,6 +60,7 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.ActionableNotification;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.JobHelpers;
Expand All @@ -72,14 +78,12 @@
import org.eclipse.lsp4j.WatchKind;

public class StandardProjectsManager extends ProjectsManager {
protected static final String BUILD_SUPPORT_EXTENSION_POINT_ID = "buildSupport";
private static final Set<String> watchers = new LinkedHashSet<>();
private PreferenceManager preferenceManager;
//@formatter:off
private static final List<String> basicWatchers = Arrays.asList(
"**/*.java",
"**/pom.xml",
"**/*.gradle",
"**/gradle.properties",
"**/.project",
"**/.classpath",
"**/.settings/*.prefs",
Expand Down Expand Up @@ -272,8 +276,19 @@ public Optional<IBuildSupport> getBuildSupport(IProject project) {
return buildSupports().filter(bs -> bs.applies(project)).findFirst();
}

private Stream<IBuildSupport> buildSupports() {
return Stream.of(new GradleBuildSupport(), new MavenBuildSupport(), new InvisibleProjectBuildSupport(), new DefaultProjectBuildSupport(this), new EclipseBuildSupport());
protected Stream<IBuildSupport> buildSupports() {
Map<Integer, IBuildSupport> supporters = new TreeMap<>();
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(IConstants.PLUGIN_ID, BUILD_SUPPORT_EXTENSION_POINT_ID);
IConfigurationElement[] configs = extensionPoint.getConfigurationElements();
for (IConfigurationElement config : configs) {
try {
Integer order = Integer.valueOf(config.getAttribute("order"));
supporters.put(order, (IBuildSupport) config.createExecutableExtension("class")); //$NON-NLS-1$
} catch (CoreException e) {
JavaLanguageServerPlugin.logError(config.getAttribute("class") + " implementation was skipped \n" + e.getStatus());
}
}
return supporters.values().stream();
beginningineer marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down Expand Up @@ -302,6 +317,7 @@ public List<FileSystemWatcher> registerWatchers() {
logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
if (preferenceManager.getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
Set<String> patterns = new LinkedHashSet<>(basicWatchers);
buildSupports().forEach(e -> e.getWatchPatterns().forEach(patterns::add));
Set<IPath> sources = new HashSet<>();
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (c) 2020 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 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.StandardPreferenceManager;
import org.junit.Test;

/**
* @author siarhei_leanavets1
*
*/
public class StandardProjectManagerTest {

private class StandardProjectsManagerDummy extends StandardProjectsManager {
/**
* @param preferenceManager
*/
public StandardProjectsManagerDummy(PreferenceManager preferenceManager) {
super(preferenceManager);
}

@Override
public Stream<IBuildSupport> buildSupports() {
return super.buildSupports();
}
}

@Test
public void testCheckBuildSupportOrder() {
PreferenceManager preferenceManager = mock(StandardPreferenceManager.class);
StandardProjectsManagerDummy projectsManagerDummy = new StandardProjectsManagerDummy(preferenceManager);
List<Class<? extends IBuildSupport>> expectedList = Arrays.asList(GradleBuildSupport.class, MavenBuildSupport.class, InvisibleProjectBuildSupport.class, DefaultProjectBuildSupport.class, EclipseBuildSupport.class);
List<IBuildSupport> actualList = projectsManagerDummy.buildSupports().collect(Collectors.toList());
for (int i = 0; i < expectedList.size(); i++) {
assertEquals(expectedList.get(i), actualList.get(i).getClass());
}
}

}