Skip to content

Commit

Permalink
Make lazy collectiing of Workspace Artifacts in completion
Browse files Browse the repository at this point in the history
Make Maven Projects to be loaded/cached lazily when collecting the Workspace Artifacts
when gathering completions n order to make content assist faster and more responsive

Issue: eclipse#444
  • Loading branch information
vrubezhny committed Aug 4, 2023
1 parent f366462 commit 86d02a5
Show file tree
Hide file tree
Showing 13 changed files with 888 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public class LoadedMavenProject {
private final Collection<ModelProblem> problems;
private final DependencyResolutionResult dependencyResolutionResult;

public LoadedMavenProject(MavenProject mavenProject, int version, Collection<ModelProblem> problems,
public LoadedMavenProject(MavenProject mavenProject, Collection<ModelProblem> problems,
DependencyResolutionResult dependencyResolutionResult) {
this.mavenProject = mavenProject;
this.lastCheckedVersion = version;
this.problems = problems;
this.dependencyResolutionResult = dependencyResolutionResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.lemminx.extensions.maven;

import java.io.File;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.maven.model.building.FileModelSource;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.maven.utils.DOMModelSource;
import org.eclipse.lemminx.extensions.maven.MavenProjectCache.ProjectBuildManager;

import org.eclipse.lemminx.services.IXMLDocumentProvider;
import org.eclipse.lemminx.utils.FilesUtils;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;

public class LoadedMavenProjectProvider {
private static final Logger LOGGER = Logger.getLogger(LoadedMavenProjectProvider.class.getName());

private final String uri;

private final IXMLDocumentProvider documentProvider;

private final ProjectBuildManager buildManager;

private int lastCheckedVersion;

private CompletableFuture<LoadedMavenProject> future;
public LoadedMavenProjectProvider(String uri, IXMLDocumentProvider documentProvider, ProjectBuildManager buildManager) {
this.uri = uri;
this.documentProvider = documentProvider;
this.buildManager = buildManager;
this.lastCheckedVersion = -1;
}
public CompletableFuture<LoadedMavenProject> getLoadedMavenProject() {
DOMDocument document = documentProvider.getDocument(uri);
// Check if future must be created
// 1. is the future exist?
boolean shouldLoad = future == null || future.isCompletedExceptionally();
if (!shouldLoad) {
// 2. is the current future is not out of dated?
if (document != null) {
if (lastCheckedVersion != document.getTextDocument().getVersion()) {
shouldLoad = true;
}
}
}

if (shouldLoad) {
if (future != null) {
future.cancel(true);
}
if (document != null) {
lastCheckedVersion = document.getTextDocument().getVersion();
}
future = load(uri, document);
}
return future;
}

private CompletableFuture<LoadedMavenProject> load(String uri, DOMDocument document) {
// return CompletableFutures.computeAsync(cancelChecker -> {
// cancelChecker.checkCanceled();
try {
FileModelSource source = null;
if (document != null) {
source = new DOMModelSource(document);
} else {
source = new FileModelSource(FilesUtils.toFile(uri));
}
return buildManager.build(uri, source);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage() + ": " + uri, e);
throw e;
}
// });
}

public String getUri() {
return uri;
}

/**
* Returns the last checked version of the document of the pom.xml.
* <p>
* 0 means that the loaded maven project has been loaded by a pom.xml file (it
* is not editing).
* </p>
*
* @return the last checked version of the document of the pom.xml.
*/
public int getLastCheckedVersion() {
return lastCheckedVersion;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
import org.eclipse.lemminx.extensions.maven.searcher.RemoteCentralRepositorySearcher;
import org.eclipse.lemminx.extensions.maven.utils.DOMUtils;
import org.eclipse.lemminx.extensions.maven.utils.MavenParseUtils;
import org.eclipse.lemminx.services.IXMLDocumentProvider;
import org.eclipse.lemminx.services.IXMLValidationService;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionParticipant;
Expand Down Expand Up @@ -147,6 +149,8 @@ public class MavenLemminxExtension implements IXMLExtension {

// Thread which loads Maven component (plexus container, maven session, etc) which can take some time.
private CompletableFuture<Void> mavenInitializer;
private IXMLDocumentProvider documentProvider;
private IXMLValidationService validationService;

private ProgressSupport progressSupport;

Expand Down Expand Up @@ -181,6 +185,8 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
this.currentRegistry = registry;
this.resolverExtensionManager = registry.getResolverExtensionManager();
this.progressSupport = registry.getProgressSupport();
this.documentProvider = registry.getDocumentProvider();
this.validationService = registry.getValidationService();
try {
// Do not invoke getters the MavenLemminxExtension in participant constructors,
// or that will trigger loading of plexus, Maven and so on even for non pom files
Expand Down Expand Up @@ -228,14 +234,17 @@ private synchronized CompletableFuture<Void> getOrCreateMavenInitializer() {
// while Maven component is initializing.
if (mavenInitializer == null) {
if (isUnitTestMode()) {
mavenInitializer = new CompletableFuture<>();
doInitialize(() -> {});
mavenInitializer = CompletableFuture.completedFuture(null);
mavenInitializer.complete(null);
} else
mavenInitializer = CompletableFutures.computeAsync(cancelChecker -> {
doInitialize(cancelChecker);
return null;
});
}
// Start Maven Project Cache
mavenInitializer.thenAccept(t -> cache.initialized());
return mavenInitializer;
}

Expand Down Expand Up @@ -294,7 +303,7 @@ private void doInitialize(CancelChecker cancelChecker) {
MavenExecutionResult mavenResult = new DefaultMavenExecutionResult();
// TODO: MavenSession is deprecated. Investigate for alternative
mavenSession = new MavenSession(container, repositorySystemSession, mavenRequest, mavenResult);
cache = new MavenProjectCache(mavenSession);
cache = new MavenProjectCache(mavenSession, documentProvider);

// Step5 : create local repository searcher
cancelChecker.checkCanceled();
Expand Down Expand Up @@ -543,6 +552,10 @@ public static boolean match(Path file) {
|| fileName.endsWith(Maven.POMv4) || fileName.endsWith(".pom"));
}

public IXMLValidationService getValidationService() {
return validationService;
}

public MavenProjectCache getProjectCache() {
initialize();
return this.cache;
Expand Down Expand Up @@ -765,12 +778,28 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
/**
* Returns the list of Maven Projects currently added to the Workspace
*
* @param buildIfNecessary A boolean 'true' indicates that all projects are to
* be returned, not only the cached ones at the moment,
* method should wait for the final build result,
* otherwise the only project that are already built and
* cached are to be returned, the rest of the projects
* are to be built in background
* @return List of Maven Projects
*/
public List<MavenProject> getCurrentWorkspaceProjects() {
return workspaceReader.getCurrentWorkspaceArtifactFiles().stream()
.map(f -> getProjectCache().getLastSuccessfulMavenProject(f))
.filter(Objects::nonNull).toList();
public List<MavenProject> getCurrentWorkspaceProjects(boolean wait) {
if (wait) {
return workspaceReader.getCurrentWorkspaceArtifactFiles().stream()
.map(file -> getProjectCache().getLastSuccessfulMavenProject(file))
.filter(Objects::nonNull)
.toList();
} else {
return workspaceReader.getCurrentWorkspaceArtifactFiles().stream()
.map(file -> getProjectCache().getLoadedMavenProject(toUriASCIIString(file))
.getNow(null))
.filter(Objects::nonNull)
.map(LoadedMavenProject::getMavenProject)
.toList();
}
}

/**
Expand Down Expand Up @@ -830,4 +859,14 @@ public static boolean isUnitTestMode() {
public static void setUnitTestMode(boolean unitTestMode) {
MavenLemminxExtension.unitTestMode = unitTestMode;
}

/**
* Gets a normalized URI ASCII string from the given File
*
* @param file A File
* @return Normalized URI ASCII string
*/
public static String toUriASCIIString(File file) {
return file.toURI().normalize().toASCIIString();
}
}
Loading

0 comments on commit 86d02a5

Please sign in to comment.