first cut for strict project matching mode to ensure live hovers are detected and shown only for relevant projects

This commit is contained in:
Martin Lippert
2018-07-11 16:40:18 +02:00
parent 20ff5f406a
commit d7fc10794c
17 changed files with 188 additions and 38 deletions

View File

@@ -29,6 +29,11 @@
"type": "boolean",
"default": false,
"description": "Enable/Disable detecting changes of running Spring Boot applications"
},
"strict-project-matching.on": {
"type": "boolean",
"default": false,
"description": "Use restrictive mechanism to match live running boot apps automatically with projects in your workspace"
}
},
"dependencies": {

View File

@@ -39,6 +39,9 @@ public class BootLanguageServerPreferencesPage extends FieldEditorPreferencePage
BooleanFieldEditor liveChangeDetectionPrefEditor = new BooleanFieldEditor(Constants.PREF_CHANGE_DETECTION, "Live Boot Change Detection", getFieldEditorParent());
addField(liveChangeDetectionPrefEditor);
BooleanFieldEditor strictProjectMatchingPrefEditor = new BooleanFieldEditor(Constants.PREF_STRICT_PROJECT_MATCH, "Use restrictive mechanism to match live running boot apps automatically with projects in your workspace", getFieldEditorParent());
addField(strictProjectMatchingPrefEditor);
}
}

View File

@@ -20,5 +20,6 @@ public class Constants {
public static final String PREF_BOOT_HINTS = "boot-java.boot-hints.on";
public static final String PREF_CHANGE_DETECTION = "boot-java.change-detection.on";
public static final String PREF_STRICT_PROJECT_MATCH = "boot-java.strict-project-matching.on";
}

View File

@@ -125,13 +125,18 @@ public class DelegatingStreamConnectionProvider implements StreamConnectionProvi
Map<String, Object> bootJavaObj = new HashMap<>();
Map<String, Object> bootHint = new HashMap<>();
Map<String, Object> bootChangeDetection = new HashMap<>();
Map<String, Object> bootStrictProjectMatching = new HashMap<>();
bootHint.put("on", BootLanguageServerPlugin.getDefault().getPreferenceStore().getBoolean(Constants.PREF_BOOT_HINTS));
bootChangeDetection.put("on", BootLanguageServerPlugin.getDefault().getPreferenceStore().getBoolean(Constants.PREF_CHANGE_DETECTION));
bootStrictProjectMatching.put("on", BootLanguageServerPlugin.getDefault().getPreferenceStore().getBoolean(Constants.PREF_STRICT_PROJECT_MATCH));
bootJavaObj.put("boot-hints", bootHint);
bootJavaObj.put("change-detection", bootChangeDetection);
bootJavaObj.put("strict-project-matching", bootStrictProjectMatching);
settings.put("boot-java", bootJavaObj);
this.languageServer.getWorkspaceService().didChangeConfiguration(new DidChangeConfigurationParams(settings));
}

View File

@@ -27,6 +27,7 @@ public class PrefsInitializer extends AbstractPreferenceInitializer {
public void initializeDefaultPreferences() {
BootLanguageServerPlugin.getDefault().getPreferenceStore().setDefault(Constants.PREF_BOOT_HINTS, true);
BootLanguageServerPlugin.getDefault().getPreferenceStore().setDefault(Constants.PREF_CHANGE_DETECTION, false);
BootLanguageServerPlugin.getDefault().getPreferenceStore().setDefault(Constants.PREF_STRICT_PROJECT_MATCH, false);
}
}

View File

@@ -53,6 +53,7 @@ import com.sun.tools.attach.VirtualMachineDescriptor;
/**
* @author Martin Lippert
*/
@SuppressWarnings("restriction")
public class SpringBootApp {
private static final String SPRINGFRAMEWORK_BOOT_DOMAIN = "org.springframework.boot";
@@ -163,6 +164,11 @@ public class SpringBootApp {
return (String) props.get("sun.java.command");
}
public String getSystemProperty(String propName) throws IOException {
Properties props = this.vm.getSystemProperties();
return (String) props.get(propName);
}
public boolean containsSystemProperty(Object key) throws IOException {
Properties props = this.vm.getSystemProperties();
return props.containsKey(key);

View File

@@ -16,6 +16,7 @@ import java.util.Arrays;
import java.util.Optional;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.springframework.ide.vscode.boot.java.handlers.DefaultRunningAppProvider;
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
import org.springframework.ide.vscode.boot.java.utils.SpringLiveHoverWatchdog;
import org.springframework.ide.vscode.boot.jdt.ls.JavaProjectsService;
@@ -48,7 +49,7 @@ import org.springframework.ide.vscode.commons.util.text.IDocument;
/**
* Parameters for creating Boot Properties language server
*
*
* @author Alex Boyko
* @author Kris De Volder
*/
@@ -58,10 +59,10 @@ public class BootLanguageServerParams {
public final JavaProjectFinder projectFinder;
public final ProjectObserver projectObserver;
public final SpringPropertyIndexProvider indexProvider;
//Boot Properies
public final TypeUtilProvider typeUtilProvider;
//Boot Java
public final RunningAppProvider runningAppProvider;
public final Duration watchDogInterval;
@@ -100,7 +101,7 @@ public class BootLanguageServerParams {
jdtProjectCache,
indexProvider,
(IDocument doc) -> new TypeUtil(jdtProjectCache.find(new TextDocumentIdentifier(doc.getUri()))),
RunningAppProvider.DEFAULT,
new DefaultRunningAppProvider(),
SpringLiveHoverWatchdog.DEFAULT_INTERVAL
);
};
@@ -108,9 +109,9 @@ public class BootLanguageServerParams {
private static JavaProjectsService createFallbackProjectCache(SimpleLanguageServer server) {
CompositeJavaProjectFinder javaProjectFinder = new CompositeJavaProjectFinder();
JavadocService javadocService = (uri, cpe) -> JavaDocProviders.createFor(cpe);
JavadocService javadocService = (uri, cpe) -> JavaDocProviders.createFor(cpe);
MavenProjectCache mavenProjectCache = new MavenProjectCache(server, MavenCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER), javadocService);
javaProjectFinder.addJavaProjectFinder(new MavenProjectFinder(mavenProjectCache));
@@ -118,19 +119,19 @@ public class BootLanguageServerParams {
javaProjectFinder.addJavaProjectFinder(new GradleProjectFinder(gradleProjectCache));
CompositeProjectOvserver projectObserver = new CompositeProjectOvserver(Arrays.asList(mavenProjectCache, gradleProjectCache));
return new JavaProjectsService() {
@Override
public void removeListener(Listener listener) {
projectObserver.removeListener(listener);
}
@Override
public void addListener(Listener listener) {
projectObserver.addListener(listener);
}
@Override
public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
return javaProjectFinder.find(doc);

View File

@@ -33,6 +33,11 @@ public class BootJavaConfig {
return enabled != null && enabled.booleanValue();
}
public boolean isStrictProjectMatchingEnabled() {
Boolean enabled = settings.getBoolean("boot-java", "strict-project-matching", "on");
return enabled != null && enabled.booleanValue();
}
public void handleConfigurationChange(Settings newConfig) {
Log.info("Settings received: "+newConfig);
this.settings = newConfig;

View File

@@ -36,6 +36,7 @@ import org.springframework.ide.vscode.boot.java.handlers.BootJavaReferencesHandl
import org.springframework.ide.vscode.boot.java.handlers.BootJavaWorkspaceSymbolHandler;
import org.springframework.ide.vscode.boot.java.handlers.CodeLensProvider;
import org.springframework.ide.vscode.boot.java.handlers.CompletionProvider;
import org.springframework.ide.vscode.boot.java.handlers.DefaultRunningAppProvider;
import org.springframework.ide.vscode.boot.java.handlers.HighlightProvider;
import org.springframework.ide.vscode.boot.java.handlers.HoverProvider;
import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider;
@@ -196,6 +197,10 @@ public class BootJavaLanguageServerComponents implements LanguageServerComponent
else {
liveChangeDetectionWatchdog.disableHighlights();
}
if (serverParams.runningAppProvider instanceof DefaultRunningAppProvider) {
((DefaultRunningAppProvider) serverParams.runningAppProvider).setStrictProjectMatching(config.isStrictProjectMatchingEnabled());
}
});
server.onInitialize(this::initialize);

View File

@@ -287,7 +287,7 @@ public class BootJavaHoverProvider implements HoverHandler {
private SpringBootApp[] getRunningSpringApps(IJavaProject project) {
try {
return runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
return runningAppProvider.getAllRunningSpringApps(project).toArray(new SpringBootApp[0]);
} catch (Exception e) {
Log.log(e);
return new SpringBootApp[0];

View File

@@ -0,0 +1,100 @@
/*******************************************************************************
* Copyright (c) 2018 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.handlers;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath.CPE;
import org.springframework.ide.vscode.commons.util.CollectorUtil;
/**
* @author Martin Lippert
*/
public class DefaultRunningAppProvider implements RunningAppProvider {
private boolean strictProjectMatchingEnabled = false;
public void setStrictProjectMatching(boolean strictProjectMatchingEnabled) {
System.out.println("USE STRICT MODE: " + strictProjectMatchingEnabled);
this.strictProjectMatchingEnabled = strictProjectMatchingEnabled;
}
@Override
public Collection<SpringBootApp> getAllRunningSpringApps(IJavaProject project) throws Exception {
Collection<SpringBootApp> apps = SpringBootApp.getAllRunningSpringApps();
if (project != null && strictProjectMatchingEnabled) {
return apps.stream().filter((app) -> {
return doesProjectMatch(app, project);
}).collect(CollectorUtil.toImmutableList());
}
else {
return apps;
}
}
private boolean doesProjectMatch(SpringBootApp app, IJavaProject project) {
if (doesProjectNameMatch(app, project)) return true;
if (doesProjectThinJarWrapperMatch(app, project)) return true;
if (doesClasspathMatch(app, project)) return true;
return false;
}
public static boolean doesClasspathMatch(SpringBootApp app, IJavaProject project) {
try {
Set<String> runningAppClasspath = new HashSet<>();
Collections.addAll(runningAppClasspath, app.getClasspath());
return doesClasspathMatch(runningAppClasspath, project);
}
catch (Exception e) {
return false;
}
}
public static boolean doesClasspathMatch(Set<String> runningAppClasspath, IJavaProject project) throws Exception {
IClasspath classpath = project.getClasspath();
Collection<CPE> entries = classpath.getClasspathEntries();
for (CPE cpe : entries) {
if (Classpath.ENTRY_KIND_SOURCE.equals(cpe.getKind())) {
String path = cpe.getOutputFolder();
if (runningAppClasspath.contains(path)) {
return true;
}
}
}
return false;
}
public static boolean doesProjectThinJarWrapperMatch(SpringBootApp app, IJavaProject project) {
return false;
}
public static boolean doesProjectNameMatch(SpringBootApp app, IJavaProject project) {
try {
String projectName = app.getSystemProperty("spring.boot.project.name");
return projectName != null && projectName.equals(project.getElementName());
}
catch (Exception e) {
return false;
}
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2018 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -13,13 +13,20 @@ package org.springframework.ide.vscode.boot.java.handlers;
import java.util.Collection;
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import com.google.common.collect.ImmutableList;
public interface RunningAppProvider {
public static final RunningAppProvider DEFAULT = SpringBootApp::getAllRunningSpringApps;
public static final RunningAppProvider NULL = () -> ImmutableList.of();
public static final RunningAppProvider NULL = (project) -> ImmutableList.of();
Collection<SpringBootApp> getAllRunningSpringApps() throws Exception;
/**
* returns all running spring boot applications on the local machine and if a project
* gets passed to the method invocation, strict project matching is taken into account if enabled
*
* @param project If set (and strict project macthing is enabled), only those running boot apps that can be matched to the project are returned)
* @throws Exception
*/
Collection<SpringBootApp> getAllRunningSpringApps(IJavaProject project) throws Exception;
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2018 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -41,7 +41,7 @@ public class LiveAppURLSymbolProvider {
List<SymbolInformation> result = new ArrayList<>();
try {
SpringBootApp[] runningApps = runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
SpringBootApp[] runningApps = runningAppProvider.getAllRunningSpringApps(null).toArray(new SpringBootApp[0]);
for (SpringBootApp app : runningApps) {
try {
String host = app.getHost();

View File

@@ -13,7 +13,6 @@ package org.springframework.ide.vscode.boot.java.utils;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -33,18 +32,16 @@ import org.eclipse.lsp4j.Range;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.java.BootJavaLanguageServerComponents;
import org.springframework.ide.vscode.boot.java.handlers.DefaultRunningAppProvider;
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
import org.springframework.ide.vscode.boot.java.links.SourceLinks;
import org.springframework.ide.vscode.boot.java.links.VSCodeSourceLinks;
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath;
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.Classpath.CPE;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
/**
@@ -66,6 +63,7 @@ public class SpringLiveChangeDetectionWatchdog {
private final Set<IJavaProject> observedProjects;
private boolean changeDetectionEnabled = false;
private Timer timer;
public SpringLiveChangeDetectionWatchdog(
@@ -134,7 +132,7 @@ public class SpringLiveChangeDetectionWatchdog {
public void update() {
if (changeDetectionEnabled) {
try {
SpringBootApp[] runningBootApps = runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
SpringBootApp[] runningBootApps = runningAppProvider.getAllRunningSpringApps(null).toArray(new SpringBootApp[0]);
Change[] changes = changeHistory.checkForChanges(runningBootApps);
if (changes != null && changes.length > 0) {
for (Change change : changes) {
@@ -215,16 +213,9 @@ public class SpringLiveChangeDetectionWatchdog {
Collections.addAll(runningClasspath, app.getClasspath());
for (IJavaProject project : this.observedProjects) {
IClasspath classpath = project.getClasspath();
Collection<CPE> entries = classpath.getClasspathEntries();
for (CPE cpe : entries) {
if (Classpath.ENTRY_KIND_SOURCE.equals(cpe.getKind())) {
String path = cpe.getOutputFolder();
if (runningClasspath.contains(path)) {
result.add(project);
break;
}
}
if (DefaultRunningAppProvider.doesClasspathMatch(runningClasspath, project)) {
result.add(project);
break;
}
}
}

View File

@@ -38,7 +38,7 @@ import org.springframework.ide.vscode.commons.util.text.TextDocument;
public class SpringLiveHoverWatchdog {
public static final Duration DEFAULT_INTERVAL = Duration.ofMillis(5000);
Logger logger = LoggerFactory.getLogger(SpringLiveHoverWatchdog.class);
private final long POLLING_INTERVAL_MILLISECONDS;
@@ -53,6 +53,7 @@ public class SpringLiveHoverWatchdog {
private JavaProjectFinder projectFinder;
private void refreshEnablement() {
boolean shouldEnable = highlightsEnabled && hasInterestingProject(watchedDocs.stream());
if (shouldEnable) {
@@ -144,7 +145,8 @@ public class SpringLiveHoverWatchdog {
try {
if (runningBootApps == null) {
runningBootApps = runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
IJavaProject project = identifyProject(docURI);
runningBootApps = runningAppProvider.getAllRunningSpringApps(project).toArray(new SpringBootApp[0]);
}
boolean hasCurrentRunningBootApps = runningBootApps != null && runningBootApps.length > 0;
@@ -167,8 +169,10 @@ public class SpringLiveHoverWatchdog {
protected void update() {
if (this.watchedDocs.size() > 0) {
try {
SpringBootApp[] runningBootApps = runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
for (String docURI : watchedDocs) {
IJavaProject project = identifyProject(docURI);
SpringBootApp[] runningBootApps = runningAppProvider.getAllRunningSpringApps(project).toArray(new SpringBootApp[0]);
update(docURI, runningBootApps);
}
} catch (Exception e) {
@@ -177,6 +181,16 @@ public class SpringLiveHoverWatchdog {
}
}
private IJavaProject identifyProject(String docURI) {
TextDocument doc = this.server.getTextDocumentService().get(docURI);
if (doc != null) {
return projectFinder.find(doc.getId()).orElse(null);
}
else {
return null;
}
}
private void publishLiveHints(String docURI, Range[] ranges) {
server.getClient().highlight(new HighlightParams(new TextDocumentIdentifier(docURI), Arrays.asList(ranges)));
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2018 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,6 +10,7 @@
*******************************************************************************/
package org.springframework.ide.vscode.project.harness;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -32,7 +33,7 @@ public class MockRunningAppProvider {
public MockRunningAppProvider() {
try {
when(provider.getAllRunningSpringApps()).thenReturn(mockedApps);
when(provider.getAllRunningSpringApps(anyObject())).thenReturn(mockedApps);
} catch (Exception e) {
throw ExceptionUtil.unchecked(e);
}

View File

@@ -73,6 +73,11 @@
"default": false,
"description": "Enable/Disable detecting changes of running Spring Boot applications"
},
"boot-java.strict-project-matching.on": {
"type": "boolean",
"default": false,
"description": "Use restrictive mechanism to match live running boot apps automatically with projects in your workspace"
},
"spring-boot.ls.java.home": {
"type": [
"string",