From a20d22b6f772812e4c3c514610a9a26ec7d7b482 Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Sun, 9 Oct 2022 21:52:05 +0800 Subject: [PATCH] Avoid unnecessary project updates when default vm changes --- .../jdt/ls/core/internal/JVMConfigurator.java | 5 ++--- .../internal/managers/GradleBuildSupport.java | 8 +++++++- .../internal/managers/GradleProjectImporter.java | 11 +++++++++++ .../ls/core/internal/managers/IBuildSupport.java | 10 +++++++++- .../core/internal/managers/ProjectsManager.java | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java index b9c8eba1e7..ebe3ea5004 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019-2020 Red Hat Inc. and others. + * Copyright (c) 2019-2022 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 @@ -302,8 +302,7 @@ public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) { configureJVMSettings(javaProject, current); } ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager(); - if (projectsManager != null) { - //TODO Only trigger update if the project uses the default JVM + if (projectsManager != null && projectsManager.useDefaultVM(project, current)) { JavaLanguageServerPlugin.logInfo("defaultVMInstallChanged -> force update of " + project.getName()); projectsManager.updateProject(project, true); } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java index d39a2548b6..f88a974c3d 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016-2020 Red Hat Inc. and others. + * Copyright (c) 2016-2022 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 @@ -42,6 +42,7 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.ResourceUtils; @@ -163,6 +164,11 @@ public boolean fileChanged(IResource resource, CHANGE_TYPE changeType, IProgress return IBuildSupport.super.fileChanged(resource, changeType, monitor) || isBuildFile(resource); } + @Override + public boolean useDefaultVM(IProject project, IVMInstall defaultVM) { + return GradleProjectImporter.useDefaultVM(); + } + /** * save gradle project preferences * diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java index ae95eae8db..f96a0441c8 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java @@ -467,6 +467,17 @@ public static BuildConfiguration getBuildConfiguration(Path rootFolder) { return build; } + static boolean useDefaultVM() { + File javaHome = getGradleJavaHomeFile(); + if (javaHome == null) { + IVMInstall javaDefaultRuntime = JavaRuntime.getDefaultVMInstall(); + return javaDefaultRuntime != null + && javaDefaultRuntime.getVMRunner(ILaunchManager.RUN_MODE) != null; + } + + return false; + } + private static File getJavaHome(Preferences preferences) { File javaHome = getGradleJavaHomeFile(); if (javaHome == null) { diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IBuildSupport.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IBuildSupport.java index 4f151ffdbb..c20a4511d2 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IBuildSupport.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/IBuildSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016-2020 Red Hat Inc. and others. + * Copyright (c) 2016-2022 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 @@ -25,6 +25,7 @@ import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE; import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager; @@ -81,6 +82,13 @@ default boolean fileChanged(IResource resource, CHANGE_TYPE changeType, IProgres return false; } + /** + * Check if the build support for the specified project depends on the default VM. + */ + default boolean useDefaultVM(IProject project, IVMInstall defaultVM) { + return false; + } + default void refresh(IResource resource, CHANGE_TYPE changeType, IProgressMonitor monitor) throws CoreException { if (resource == null) { return; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java index a4df9c8741..acca9f6f28 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java @@ -443,6 +443,22 @@ public Optional getBuildSupport(IProject project) { return buildSupports().filter(bs -> bs.applies(project)).findFirst(); } + /** + * Check if the build support for the specified project depends on the default VM. + */ + public boolean useDefaultVM(IProject project, IVMInstall defaultVM) { + if (project == null) { + return false; + } + + IBuildSupport buildSupport = getBuildSupport(project).orElse(null); + if (buildSupport != null) { + return buildSupport.useDefaultVM(project, defaultVM); + } + + return false; + } + private Stream buildSupports() { return Stream.of(new EclipseBuildSupport()); }