-
Notifications
You must be signed in to change notification settings - Fork 408
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
Provide 'clean build' quickfix #1440
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8be5bbe
Provide 'clean build' quickfix
jdneo 4dcb7a2
Check client capabilities when resolving 'clean build' quickfix
jdneo 47724e0
Update the test: NavigateToTypeDefinitionHandlerTest.testLocalVariable
jdneo a560e67
Add test to check client capability
jdneo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 57 additions & 0 deletions
57
...re/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/CleanBuildSubProcessor.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,57 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Microsoft Corporation 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 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.jdt.ls.core.internal.corrections.proposals; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.eclipse.jdt.core.IClassFile; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.IType; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.Name; | ||
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore; | ||
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; | ||
import org.eclipse.jdt.ls.core.internal.corrections.IInvocationContext; | ||
import org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal; | ||
import org.eclipse.lsp4j.CodeActionKind; | ||
|
||
public class CleanBuildSubProcessor { | ||
public static void cleanBuildForUnresolvedImportProposals(IInvocationContext context, IProblemLocationCore problem, | ||
Collection<ChangeCorrectionProposal> proposals) { | ||
if (!JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isClientBuildCommandSupported()) { | ||
return; | ||
} | ||
|
||
final ICompilationUnit cu= context.getCompilationUnit(); | ||
ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot()); | ||
if (coveringNode instanceof Name) { | ||
String fullyQualifiedName = ((Name) coveringNode).getFullyQualifiedName(); | ||
try { | ||
IType type = cu.getJavaProject().findType(fullyQualifiedName); | ||
if (type != null) { | ||
ICompilationUnit compilationUnit = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT); | ||
IClassFile cf = (IClassFile) type.getAncestor(IJavaElement.CLASS_FILE); | ||
if (compilationUnit != null && cf == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to check if the client supports that command |
||
proposals.add(new CUCorrectionCommandProposal("Execute 'clean build'", CodeActionKind.QuickFix, cu, IProposalRelevance.NO_SUGGESSTIONS_AVAILABLE, "java.workspace.compile", Arrays.asList(Boolean.TRUE))); | ||
} | ||
} | ||
} catch (JavaModelException e) { | ||
// do nothing | ||
} | ||
} | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/org/sample/TestImport.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,5 @@ | ||
package org.sample; | ||
|
||
public class TestImport { | ||
|
||
} |
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
74 changes: 74 additions & 0 deletions
74
....jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/CleanBuildQuickFixTest.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,74 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Microsoft Corporation 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 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.jdt.ls.core.internal.correction; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IResource; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.ls.core.internal.JDTUtils; | ||
import org.eclipse.jdt.ls.core.internal.ResourceUtils; | ||
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Command; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.junit.Test; | ||
|
||
public class CleanBuildQuickFixTest extends AbstractQuickFixTest { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need test when(clientPreferences.isClientBuildCommandSupported()).thenReturn(false); |
||
@Test | ||
public void testCleanBuildForUnresolvedImport() throws Exception { | ||
importProjects("maven/salut"); | ||
IProject project = WorkspaceHelper.getProject("salut"); | ||
|
||
Path compilationUnitPath = Paths.get(project.getRawLocation().toOSString(), "src", "main", "java", "java", "Foo3.java"); | ||
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(compilationUnitPath.toUri().toString()); | ||
|
||
Path classesPath = Paths.get(project.getRawLocation().toOSString(), "target", "classes"); | ||
File classDir = classesPath.toFile(); | ||
FileUtils.deleteDirectory(classDir); | ||
|
||
IFile sourceFile = project.getFile("src/main/java/java/Foo3.java"); | ||
String content = ResourceUtils.getContent(sourceFile); | ||
content = content.replace("// TestImport testImport = new TestImport();", "TestImport testImport = new TestImport();"); | ||
ResourceUtils.setContent(sourceFile, content); | ||
cu.getUnderlyingResource().refreshLocal(IResource.DEPTH_ZERO, monitor); | ||
waitForBackgroundJobs(); | ||
|
||
List<Either<Command, CodeAction>> codeActions = evaluateCodeActionsForIMarker(cu); | ||
Expected e1 = new Expected("Execute 'clean build'", ""); | ||
for (Either<Command, CodeAction> c : codeActions) { | ||
if (Objects.equals("Execute 'clean build'", getTitle(c))) { | ||
Command commandInCodeAction = c.getRight().getCommand(); | ||
assertEquals("java.workspace.compile", commandInCodeAction.getCommand()); | ||
|
||
List<Object> argumentList = commandInCodeAction.getArguments(); | ||
assertEquals(1, argumentList.size()); | ||
assertEquals(true, argumentList.get(0)); | ||
return; | ||
} | ||
} | ||
fail(e1.name + " not found"); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setup