-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Quick-Fix to add a dependency on missing types
Currently if a type is missing a user needs to search the workspace, then probably find the project and then go to the pom and add the desired coordinates. This now adds a Quick-Fix to automate this and offers the user to add a dependency to a workspace project that contains the missing type.
- Loading branch information
Showing
13 changed files
with
334 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...e.m2e.jdt.ui/src/org/eclipse/m2e/jdt/ui/internal/AddDependencyJavaCompletionProposal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Christoph Läubrich 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 | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.m2e.jdt.ui.internal; | ||
|
||
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.DEPENDENCIES; | ||
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild; | ||
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument; | ||
|
||
import java.io.IOException; | ||
|
||
import org.w3c.dom.Element; | ||
|
||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.contentassist.IContextInformation; | ||
import org.eclipse.swt.graphics.Image; | ||
import org.eclipse.swt.graphics.Point; | ||
|
||
import org.eclipse.m2e.core.embedder.ArtifactKey; | ||
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation; | ||
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple; | ||
import org.eclipse.m2e.core.ui.internal.editing.PomHelper; | ||
|
||
|
||
@SuppressWarnings("restriction") | ||
public class AddDependencyJavaCompletionProposal implements IJavaCompletionProposal { | ||
|
||
private ArtifactKey artifactKey; | ||
|
||
private IFile pomfile; | ||
|
||
public AddDependencyJavaCompletionProposal(ArtifactKey artifactKey, IFile pomfile) { | ||
this.artifactKey = artifactKey; | ||
this.pomfile = pomfile; | ||
} | ||
|
||
public void apply(IDocument javaDocument) { | ||
try { | ||
performOnDOMDocument(new OperationTuple(pomfile, (Operation) document -> { | ||
Element depsEl = getChild(document.getDocumentElement(), DEPENDENCIES); | ||
PomHelper.addOrUpdateDependency(depsEl, artifactKey.groupId(), artifactKey.artifactId(), artifactKey.version(), | ||
null, "compile", null); | ||
})); | ||
} catch(IOException ex) { | ||
MavenJdtUiPlugin.getDefault().getLog().error("Can't modify file " + pomfile, ex); | ||
} catch(CoreException ex) { | ||
MavenJdtUiPlugin.getDefault().getLog().log(ex.getStatus()); | ||
} | ||
|
||
} | ||
|
||
public Point getSelection(IDocument document) { | ||
return null; | ||
} | ||
|
||
public String getAdditionalProposalInfo() { | ||
return null; | ||
} | ||
|
||
public String getDisplayString() { | ||
return "Add " + artifactKey.groupId() + ":" + artifactKey.artifactId() + ":" + artifactKey.version() | ||
+ " as dependency"; | ||
} | ||
|
||
public Image getImage() { | ||
return MavenJdtUiPlugin.getDefault().getImageRegistry().get(MavenJdtUiPlugin.M2E_ICON); | ||
} | ||
|
||
public IContextInformation getContextInformation() { | ||
return null; | ||
} | ||
|
||
public int getRelevance() { | ||
return 100; | ||
} | ||
|
||
} |
150 changes: 150 additions & 0 deletions
150
...clipse.m2e.jdt.ui/src/org/eclipse/m2e/jdt/ui/internal/AddDependencyQuickFixProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Christoph Läubrich 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 | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.m2e.jdt.ui.internal; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.eclipse.core.runtime.Adapters; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.IType; | ||
import org.eclipse.jdt.core.compiler.IProblem; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.ITypeBinding; | ||
import org.eclipse.jdt.core.dom.Name; | ||
import org.eclipse.jdt.core.dom.SimpleName; | ||
import org.eclipse.jdt.core.search.IJavaSearchConstants; | ||
import org.eclipse.jdt.core.search.IJavaSearchScope; | ||
import org.eclipse.jdt.core.search.SearchEngine; | ||
import org.eclipse.jdt.core.search.SearchMatch; | ||
import org.eclipse.jdt.core.search.SearchParticipant; | ||
import org.eclipse.jdt.core.search.SearchPattern; | ||
import org.eclipse.jdt.core.search.SearchRequestor; | ||
import org.eclipse.jdt.ui.text.java.IInvocationContext; | ||
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; | ||
import org.eclipse.jdt.ui.text.java.IProblemLocation; | ||
import org.eclipse.jdt.ui.text.java.IQuickFixProcessor; | ||
|
||
import org.eclipse.m2e.core.MavenPlugin; | ||
import org.eclipse.m2e.core.embedder.ArtifactKey; | ||
import org.eclipse.m2e.core.project.IMavenProjectFacade; | ||
|
||
|
||
public class AddDependencyQuickFixProcessor implements IQuickFixProcessor { | ||
|
||
public boolean hasCorrections(ICompilationUnit unit, int problemId) { | ||
switch(problemId) { | ||
case IProblem.UndefinedName: | ||
case IProblem.ImportNotFound: | ||
case IProblem.UndefinedType: | ||
case IProblem.UnresolvedVariable: | ||
case IProblem.MissingTypeInMethod: | ||
case IProblem.MissingTypeInConstructor: | ||
IJavaElement parent = unit.getParent(); | ||
if(parent != null) { | ||
IJavaProject project = parent.getJavaProject(); | ||
if(project != null) { | ||
return MavenPlugin.isMavenProject(project.getProject()); | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) | ||
throws CoreException { | ||
Map<IMavenProjectFacade, Set<ArtifactKey>> possibleKeys = new HashMap<>(); | ||
for(IProblemLocation location : locations) { | ||
switch(location.getProblemId()) { | ||
case IProblem.ImportNotFound: | ||
case IProblem.UndefinedName: | ||
case IProblem.UndefinedType: | ||
case IProblem.UnresolvedVariable: | ||
case IProblem.MissingTypeInMethod: | ||
case IProblem.MissingTypeInConstructor: | ||
handleImportNotFound(context, location, possibleKeys); | ||
} | ||
} | ||
return possibleKeys.entrySet().stream() | ||
.flatMap(entry -> entry.getValue().stream() | ||
.map(key -> new AddDependencyJavaCompletionProposal(key, entry.getKey().getPom()))) | ||
.toArray(IJavaCompletionProposal[]::new); | ||
} | ||
|
||
private void handleImportNotFound(IInvocationContext context, IProblemLocation problemLocation, | ||
Map<IMavenProjectFacade, Set<ArtifactKey>> possibleKeys) throws CoreException { | ||
CompilationUnit cu = context.getASTRoot(); | ||
ASTNode selectedNode = problemLocation.getCoveringNode(cu); | ||
if(selectedNode != null) { | ||
String className = getClassName(selectedNode); | ||
if(className != null) { | ||
IMavenProjectFacade currentFacade = Adapters.adapt(cu.getJavaElement().getJavaProject(), | ||
IMavenProjectFacade.class); | ||
if(currentFacade != null) { | ||
Set<ArtifactKey> artifacts = findMatchingArtifacts(className, currentFacade); | ||
if(!artifacts.isEmpty()) { | ||
possibleKeys.computeIfAbsent(currentFacade, x -> new HashSet<>()).addAll(artifacts); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
static Set<ArtifactKey> findMatchingArtifacts(String className, IMavenProjectFacade currentFacade) | ||
throws CoreException { | ||
Set<ArtifactKey> possibleKey = new HashSet<ArtifactKey>(); | ||
SearchPattern typePattern = SearchPattern.createPattern(className, IJavaSearchConstants.TYPE, | ||
IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); | ||
IJavaSearchScope workspaceScope = SearchEngine.createWorkspaceScope(); | ||
SearchEngine searchEngine = new SearchEngine(); | ||
SearchRequestor requestor = new SearchRequestor() { | ||
|
||
@Override | ||
public void acceptSearchMatch(SearchMatch aMatch) { | ||
Object element = aMatch.getElement(); | ||
if(element instanceof IType) { | ||
IType type = (IType) element; | ||
IMavenProjectFacade facade = Adapters.adapt(type.getJavaProject(), IMavenProjectFacade.class); | ||
if(facade != null) { | ||
ArtifactKey artifactKey = facade.getArtifactKey(); | ||
if(!artifactKey.equals(currentFacade.getArtifactKey())) { | ||
possibleKey.add(artifactKey); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
searchEngine.search(typePattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, | ||
workspaceScope, requestor, null); | ||
return possibleKey; | ||
} | ||
|
||
private String getClassName(ASTNode selectedNode) { | ||
String className = null; | ||
if(selectedNode instanceof Name) { | ||
ITypeBinding typeBinding = ((Name) selectedNode).resolveTypeBinding(); | ||
if(typeBinding != null) { | ||
className = typeBinding.getBinaryName(); | ||
} | ||
if(className == null && selectedNode instanceof SimpleName) { // fallback if the type cannot be resolved | ||
className = ((SimpleName) selectedNode).getIdentifier(); | ||
} | ||
} | ||
return className; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
org.eclipse.m2e.jdt/.settings/org.eclipse.pde.ds.annotations.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
dsVersion=V1_3 | ||
eclipse.preferences.version=1 | ||
enabled=true | ||
generateBundleActivationPolicyLazy=true | ||
path=OSGI-INF | ||
validationErrorLevel=error | ||
validationErrorLevel.missingImplicitUnbindMethod=error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/org.eclipse.m2e.*.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.