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

Fix hover for managed deps #355

Merged
merged 2 commits into from
Feb 1, 2023
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019-2022 Red Hat Inc. and others.
* Copyright (c) 2019-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/
Expand Down Expand Up @@ -433,6 +433,7 @@ private GAVInsertionStrategy computeGAVInsertionStrategy(ICompletionRequest requ
};
}

@SuppressWarnings("deprecation")
private Optional<MavenProject> computeFilesystemParent(ICompletionRequest request) {
Optional<String> relativePath = null;
if (request.getParentElement().getLocalName().equals(PARENT_ELT)) {
Expand Down Expand Up @@ -480,7 +481,7 @@ private CompletionItem createMinimalPOMCompletionSnippet(ICompletionRequest requ
private Collection<CompletionItem> collectGoals(ICompletionRequest request) {
PluginDescriptor pluginDescriptor;
try {
pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(request, plugin);
pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(request.getNode(), plugin);
return collectSimpleCompletionItems(pluginDescriptor.getMojos(), MojoDescriptor::getGoal,
MojoDescriptor::getDescription, request);
} catch (PluginResolutionException | PluginDescriptorParsingException | InvalidPluginDescriptorException e) {
Expand All @@ -495,7 +496,7 @@ private CompletionItem toGAVCompletionItem(ArtifactWithDescription artifactInfo,
|| DOMUtils.findChildElementText(request.getParentElement(), GROUP_ID_ELT).isPresent();
boolean insertArtifactIsEnd = !request.getParentElement().hasEndTag();
boolean insertGroupId = strategy instanceof GAVInsertionStrategy.NodeWithChildrenInsertionStrategy || !hasGroupIdSet;
boolean isExclusion = DOMUtils.findClosestParentNode(request, DOMConstants.EXCLUSIONS_ELT) != null;
boolean isExclusion = DOMUtils.findClosestParentNode(request.getNode(), DOMConstants.EXCLUSIONS_ELT) != null;
boolean insertVersion = !isExclusion && (strategy instanceof GAVInsertionStrategy.NodeWithChildrenInsertionStrategy || !DOMUtils
.findChildElementText(request.getParentElement().getParentElement(), VERSION_ELT).isPresent());
CompletionItem item = new CompletionItem();
Expand Down Expand Up @@ -725,6 +726,7 @@ private void internalCollectRemoteGAVCompletion(ICompletionRequest request, bool
});
}

@SuppressWarnings("deprecation")
private Collection<CompletionItem> collectSubModuleCompletion(ICompletionRequest request) {
DOMDocument doc = request.getXMLDocument();
File docFolder = new File(URI.create(doc.getTextDocument().getUri())).getParentFile();
Expand Down Expand Up @@ -763,6 +765,7 @@ private Collection<CompletionItem> collectMojoParametersDefaultCompletion(ICompl
.collect(Collectors.toList());
}

@SuppressWarnings("deprecation")
private Collection<CompletionItem> collectRelativePathCompletion(ICompletionRequest request) {
DOMDocument doc = request.getXMLDocument();
File docFile = new File(URI.create(doc.getTextDocument().getUri()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020-2022 Red Hat Inc. and others.
* Copyright (c) 2020-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/
Expand All @@ -23,18 +23,13 @@

import org.apache.maven.Maven;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.InvalidPluginDescriptorException;
import org.apache.maven.plugin.PluginDescriptorParsingException;
import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.extensions.maven.MavenLemminxExtension;
import org.eclipse.lemminx.extensions.maven.utils.DOMUtils;
import org.eclipse.lemminx.extensions.maven.utils.MavenPluginUtils;
import org.eclipse.lemminx.extensions.maven.utils.ParticipantUtils;
import org.eclipse.lemminx.services.extensions.IDefinitionParticipant;
import org.eclipse.lemminx.services.extensions.IDefinitionRequest;
Expand All @@ -52,6 +47,7 @@ public MavenDefinitionParticipant(MavenLemminxExtension plugin) {
this.plugin = plugin;
}

@SuppressWarnings("deprecation")
@Override
public void findDefinition(IDefinitionRequest request, List<LocationLink> locations, CancelChecker cancelChecker) {
if (!MavenLemminxExtension.match(request.getXMLDocument())) {
Expand All @@ -67,7 +63,11 @@ public void findDefinition(IDefinitionRequest request, List<LocationLink> locati
}

DOMElement element = ParticipantUtils.findInterestingElement(request.getNode());
if (element != null && MODULE_ELT.equals(element.getLocalName())) {
if (element == null) {
return;
}

if (MODULE_ELT.equals(element.getLocalName())) {
File subModuleFile = new File(currentFolder,
element.getFirstChild().getTextContent() + File.separator + Maven.POMv4);
if (subModuleFile.isFile()) {
Expand All @@ -77,9 +77,9 @@ public void findDefinition(IDefinitionRequest request, List<LocationLink> locati
}

MavenProject p = plugin.getProjectCache().getLastSuccessfulMavenProject(element.getOwnerDocument());
Dependency dependency = ParticipantUtils.getArtifactToSearch(p, request);
Dependency dependency = ParticipantUtils.getArtifactToSearch(p, request.getNode());

DOMNode parentNode = DOMUtils.findClosestParentNode(request, PARENT_ELT);
DOMNode parentNode = DOMUtils.findClosestParentNode(request.getNode(), PARENT_ELT);
if (parentNode != null && parentNode.isElement()) {
// Find in workspace
if (ParticipantUtils.isWellDefinedDependency(dependency)) {
Expand Down Expand Up @@ -118,37 +118,9 @@ public void findDefinition(IDefinitionRequest request, List<LocationLink> locati
}
}
}
if (dependency != null && element != null) {
if (!ParticipantUtils.isWellDefinedDependency(dependency)) {
if (ParticipantUtils.isPlugin(element)) {
try {
PluginDescriptor pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(request, plugin);
if (pluginDescriptor != null) {
dependency.setGroupId(pluginDescriptor.getGroupId());
dependency.setArtifactId(pluginDescriptor.getArtifactId());
dependency.setVersion(pluginDescriptor.getVersion());
}
} catch (PluginResolutionException | PluginDescriptorParsingException
| InvalidPluginDescriptorException e) {
// Ignore
}
} else if (ParticipantUtils.isDependency(element)) {
if (dependency.getGroupId() == null || dependency.getVersion() == null) {
if (p != null) {
final Dependency originalDependency = dependency;
dependency = p.getDependencies().stream()
.filter(dep -> (originalDependency.getGroupId() == null
|| originalDependency.getGroupId().equals(dep.getGroupId())
&& (originalDependency.getArtifactId() == null
|| originalDependency.getArtifactId().equals(dep.getArtifactId()))
&& (originalDependency.getVersion() == null
|| originalDependency.getVersion().equals(dep.getVersion()))))
.findFirst().orElse(dependency);
}
}
}
}

dependency = ParticipantUtils.resolveDependency(p, dependency, element, plugin);
if (dependency != null) {
// Find in workspace
if (ParticipantUtils.isWellDefinedDependency(dependency)) {
Artifact artifact = ParticipantUtils.findWorkspaceArtifact(plugin, request, dependency);
Expand All @@ -158,6 +130,7 @@ public void findDefinition(IDefinitionRequest request, List<LocationLink> locati
}
}

// Find in local repository
File localArtifactLocation = plugin.getLocalRepositorySearcher().findLocalFile(dependency);
if (localArtifactLocation != null && localArtifactLocation.isFile()) {
locations.add(toLocationNoRange(localArtifactLocation, element));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019-2020 Red Hat Inc. and others.
* Copyright (c) 2019-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/
Expand Down Expand Up @@ -42,13 +42,13 @@ public PluginValidator(MavenLemminxExtension plugin) {

public Optional<List<Diagnostic>> validatePluginResolution(DiagnosticRequest diagnosticRequest) {
try {
MavenPluginUtils.getContainingPluginDescriptor(diagnosticRequest, plugin);
MavenPluginUtils.getContainingPluginDescriptor(diagnosticRequest.getNode(), plugin);
} catch (PluginResolutionException | PluginDescriptorParsingException | InvalidPluginDescriptorException e) {
LOGGER.log(Level.WARNING, "Could not resolve plugin description", e);

// Add artifactId diagnostic
String errorMessage = e.getMessage();
DOMNode pluginNode = DOMUtils.findClosestParentNode(diagnosticRequest, "plugin");
DOMNode pluginNode = DOMUtils.findClosestParentNode(diagnosticRequest.getNode(), "plugin");
Optional<DOMNode> artifactNode = pluginNode.getChildren().stream().filter(node -> !node.isComment())
.filter(node -> node.getLocalName().equals("artifactId")).findAny();
List<Diagnostic> diagnostics = new ArrayList<>();
Expand Down Expand Up @@ -113,7 +113,7 @@ public Optional<List<Diagnostic>> validateGoal(DiagnosticRequest diagnosticReque
if (node.isElement() && node.hasChildNodes()) {
PluginDescriptor pluginDescriptor;
try {
pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(diagnosticRequest, plugin);
pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(diagnosticRequest.getNode(), plugin);
if (pluginDescriptor != null) {
internalValidateGoal(diagnosticRequest, pluginDescriptor).ifPresent(diagnostics::add);;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020-2022 Red Hat Inc. and others.
* Copyright (c) 2020-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/
Expand All @@ -20,7 +20,6 @@

import java.io.File;
import java.text.MessageFormat;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -31,7 +30,6 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
Expand Down Expand Up @@ -111,7 +109,7 @@ public Hover onText(IHoverRequest request) throws Exception {
}

MavenProject p = plugin.getProjectCache().getLastSuccessfulMavenProject(request.getXMLDocument());
Dependency artifactToSearch = ParticipantUtils.getArtifactToSearch(p, request);
Dependency artifactToSearch = ParticipantUtils.getArtifactToSearch(p, tag);

return switch (parent.getLocalName()) {
case GROUP_ID_ELT, ARTIFACT_ID_ELT, VERSION_ELT -> {
Expand Down Expand Up @@ -184,7 +182,7 @@ private String getManagedVersionText(IHoverRequest request) {

boolean isPlugin = PLUGIN_ELT.equals(element.getLocalName());
MavenProject p = plugin.getProjectCache().getLastSuccessfulMavenProject(element.getOwnerDocument());
Dependency dependency = ParticipantUtils.getArtifactToSearch(p, request);
Dependency dependency = ParticipantUtils.getArtifactToSearch(p, request.getNode());

// Search for DEPENDENCY/PLUGIN through the parents pom's
File parentPomFile = getParentPomFile(p);
Expand Down Expand Up @@ -302,50 +300,45 @@ private Hover collectArtifactDescription(IHoverRequest request) {
boolean supportsMarkdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN);

MavenProject p = plugin.getProjectCache().getLastSuccessfulMavenProject(request.getXMLDocument());
Dependency artifactToSearch = ParticipantUtils.getArtifactToSearch(p, request);
boolean wellDefined = ParticipantUtils.isWellDefinedDependency(artifactToSearch);
Dependency dependency = ParticipantUtils.getArtifactToSearch(p, request.getNode());
boolean wellDefined = ParticipantUtils.isWellDefinedDependency(dependency);
DOMElement element = ParticipantUtils.findInterestingElement(request.getNode());
dependency = ParticipantUtils.resolveDependency(p, dependency, element, plugin);

try {
ModelBuilder builder = plugin.getProjectCache().getPlexusContainer().lookup(ModelBuilder.class);
Optional<String> localDescription = plugin.getLocalRepositorySearcher()
.getLocalArtifactsLastVersion().stream()
.filter(gav -> (artifactToSearch.getGroupId() == null
|| artifactToSearch.getGroupId().equals(gav.getGroupId()))
&& (artifactToSearch.getArtifactId() == null
|| artifactToSearch.getArtifactId().equals(gav.getArtifactId()))
&& (artifactToSearch.getVersion() == null
|| artifactToSearch.getVersion().equals(gav.getVersion())))
.sorted(Comparator.comparing((Artifact artifact) -> new DefaultArtifactVersion(artifact.getVersion())).reversed())
.findFirst().map(plugin.getLocalRepositorySearcher()::findLocalFile).map(file ->
builder.buildRawModel(file, ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL, true).get())
.map(model -> {
UnaryOperator<String> toBold = supportsMarkdown ? MarkdownUtils::toBold
: UnaryOperator.identity();
String lineBreak = MarkdownUtils.getLineBreak(supportsMarkdown);
String message = "";

if (model.getName() != null) {
message += toBold.apply(model.getName());
}
// Find in local repository
File localArtifactLocation = plugin.getLocalRepositorySearcher().findLocalFile(dependency);
if (localArtifactLocation != null && localArtifactLocation.isFile()) {
Model model = builder.buildRawModel(localArtifactLocation, ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL, true).get();
if (model != null) {
UnaryOperator<String> toBold = supportsMarkdown ? MarkdownUtils::toBold
: UnaryOperator.identity();
String lineBreak = MarkdownUtils.getLineBreak(supportsMarkdown);
String message = "";

if (model.getName() != null) {
message += toBold.apply(model.getName());
}

if (model.getDescription() != null) {
message += lineBreak + model.getDescription();
}
if (model.getDescription() != null) {
message += lineBreak + model.getDescription();
}

if (!wellDefined) {
String managedVersion = getManagedVersionText(request);
if (managedVersion == null) {
managedVersion = getActualVersionText(supportsMarkdown, model);
}
if (managedVersion != null) {
message += lineBreak + managedVersion;
}
if (!wellDefined) {
String managedVersion = getManagedVersionText(request);
if (managedVersion == null) {
managedVersion = getActualVersionText(supportsMarkdown, model);
}
if (managedVersion != null) {
message += lineBreak + managedVersion;
}
return message;
}).map(message -> (message.length() > 2 ? message : null));
if (localDescription.isPresent()) {
return new Hover(new MarkupContent(supportsMarkdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT,
localDescription.get()));
}

if (message.length() > 2) {
return new Hover(new MarkupContent(supportsMarkdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT, message));
}
}
}
} catch (Exception e1) {
LOGGER.log(Level.SEVERE, e1.toString(), e1);
Expand All @@ -358,7 +351,7 @@ private Hover collectArtifactDescription(IHoverRequest request) {
private Hover collectGoal(IPositionRequest request) {
DOMNode node = request.getNode();
try {
PluginDescriptor pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(request, plugin);
PluginDescriptor pluginDescriptor = MavenPluginUtils.getContainingPluginDescriptor(node, plugin);
if (pluginDescriptor == null) { // probable incorrect pom file at this moment
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* Copyright (c) 2020, 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/
Expand All @@ -24,17 +24,17 @@

public class DOMUtils {

public static DOMNode findClosestParentNode(final IPositionRequest request, final String localName) {
if (localName == null || request == null) {
public static DOMNode findClosestParentNode(final DOMNode node, final String localName) {
if (localName == null || node == null) {
return null;
}

DOMNode pluginNode = request.getNode();
while (pluginNode != null) {
if (localName.equals(pluginNode.getLocalName())) {
return pluginNode;
DOMNode parentNode = node;
while (parentNode != null) {
if (localName.equals(parentNode.getLocalName())) {
return parentNode;
}
pluginNode = pluginNode.getParentNode();
parentNode = parentNode.getParentNode();
}

return null;
Expand Down
Loading