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

Provide 'clean build' quickfix #1440

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -35,6 +35,7 @@
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.CleanBuildSubProcessor;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.GetterSetterCorrectionSubProcessor;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.IProposalRelevance;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.JavadocTagsSubProcessor;
Expand Down Expand Up @@ -639,5 +640,12 @@ private void process(IInvocationContext context, IProblemLocationCore problem, C
// }
// ConfigureProblemSeveritySubProcessor.addConfigureProblemSeverityProposal(context,
// problem, proposals);

// Add proposals related with project steup, like: clean build, clean workspace, etc...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup

switch(id) {
case IProblem.ImportNotFound:
CleanBuildSubProcessor.cleanBuildForUnresolvedImportProposals(context, problem, proposals);
break;
}
}
}
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ public boolean isClientDocumentSymbolProviderRegistered() {
return Boolean.parseBoolean(extendedClientCapabilities.getOrDefault("clientDocumentSymbolProvider", "false").toString());
}

public boolean isClientBuildCommandSupported() {
return Boolean.parseBoolean(extendedClientCapabilities.getOrDefault("buildCommandSupport", "false").toString());
}

public boolean isSupportsCompletionDocumentationMarkdown() {
//@formatter:off
return v3supported && capabilities.getTextDocument().getCompletion() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.sample.TestImport;

public class Foo3 {

Expand All @@ -18,4 +19,9 @@ public void localVariable() {
System.out.println(this.properties);
System.out.println(util);
}

public void importTest() {
// TestImport testImport = new TestImport();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sample;

public class TestImport {

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaModelMarker;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.CompilationUnit;
Expand All @@ -41,6 +45,8 @@
import org.eclipse.jdt.ls.core.internal.TextEditUtil;
import org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler;
import org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler;
import org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers;
import org.eclipse.jdt.ls.core.internal.handlers.WorkspaceDiagnosticsHandler;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
Expand Down Expand Up @@ -240,6 +246,22 @@ protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit
return evaluateCodeActions(cu, range);
}

protected List<Either<Command, CodeAction>> evaluateCodeActionsForIMarker(ICompilationUnit cu) throws CoreException {
IMarker[] markers = cu.getUnderlyingResource().findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_ONE);
CodeActionParams parms = new CodeActionParams();

TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
parms.setTextDocument(textDocument);
parms.setRange(JDTUtils.toRange(cu, 0, 0));
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(WorkspaceDiagnosticsHandler.toDiagnosticsArray(JsonRpcHelpers.toDocument(cu.getBuffer()), markers, true));
context.setOnly(onlyKinds);
parms.setContext(context);

return resolveCodeActions(parms);
}

protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit cu, Range range) throws JavaModelException {

CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
Expand All @@ -256,6 +278,10 @@ protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit
context.setOnly(onlyKinds);
parms.setContext(context);

return resolveCodeActions(parms);
}

private List<Either<Command, CodeAction>> resolveCodeActions(CodeActionParams parms) {
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
if (onlyKinds != null && !onlyKinds.isEmpty()) {
for (Either<Command, CodeAction> codeAction : codeActions) {
Expand Down
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 {

Copy link
Contributor

Choose a reason for hiding this comment

The 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testAttachedSource() throws Exception {

@Test
public void testLocalVariable() throws Exception {
testClass("java.Foo3", 18, 24);
testClass("java.Foo3", 19, 24);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ protected ClientPreferences initPreferenceManager(boolean supportClassFileConten
when(clientPreferences.isAdvancedGenerateAccessorsSupported()).thenReturn(true);
when(clientPreferences.isGenerateConstructorsPromptSupported()).thenReturn(true);
when(clientPreferences.isGenerateDelegateMethodsPromptSupported()).thenReturn(true);
when(clientPreferences.isClientBuildCommandSupported()).thenReturn(true);
return clientPreferences;
}

Expand Down