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

Make lazy collectiing of Workspace Artifacts in completion #483

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
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,124 @@
/*******************************************************************************
* 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.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;

/**
* An object aggregating a build results of a Maven Document, controlling the
* asynchronous access to the Maven Project built from the latest version of
* the provided document.
*/
public class LoadedMavenProjectProvider {
vrubezhny marked this conversation as resolved.
Show resolved Hide resolved
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;

/**
* Creates a LoadedMavenProjectProvider using provided URI String identifying the
* document to be built into a Maven Project. A document found by using Document
* Provider is the latest version of the document being currently edited or a document
* read from a file specified by document URI String.
*
* @param uri A URI String identifying the document
* @param documentProvider An IXMLDocumentProvider instance used to find the latest
* version of the document
* @param buildManager A MavenProject builder
*/
public LoadedMavenProjectProvider(String uri, IXMLDocumentProvider documentProvider, ProjectBuildManager buildManager) {
this.uri = uri;
this.documentProvider = documentProvider;
this.buildManager = buildManager;
this.lastCheckedVersion = -1;
}

/**
* Returns a `CompletableFuture<LoadedMavenProject>` for asynchronous access
* to the Maven Project built from the latest version of the document.
*
* @return CompletableFuture of LoadedMavenProject object
*/
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) {
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;
}
}

vrubezhny marked this conversation as resolved.
Show resolved Hide resolved
/**
* Returns URI String identifying a Maven Project document or file
*
* @return
*/
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;
}
}
Loading