Search in sources :

Example 1 with CodeActionHandler

use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.

the class ShowAllQuickFixTest method testShowAt.

@Test
public void testShowAt() throws Exception {
    String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
    try {
        IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
        StringBuilder buf = new StringBuilder();
        buf.append("package test1;\n");
        buf.append("import java.math.BigDecimal;\n");
        buf.append("public class F {\n");
        buf.append("    private int i; private BigDecimal b;private void test() {}\n");
        buf.append("    public static void main(String[] args) {\n");
        buf.append("        System.out.println(greeting());\n");
        buf.append("    }\n");
        buf.append("}\n");
        ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
        cu.becomeWorkingCopy(null);
        CodeActionParams codeActionParams = new CodeActionParams();
        TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
        textDocument.setUri(JDTUtils.toURI(cu));
        codeActionParams.setTextDocument(textDocument);
        codeActionParams.setRange(new Range(new Position(3, 9), new Position(3, 9)));
        CodeActionContext context = new CodeActionContext();
        context.setDiagnostics(Collections.emptyList());
        context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
        codeActionParams.setContext(context);
        List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(0, codeActions.size());
        codeActionParams.setRange(new Range(new Position(3, 4), new Position(3, 40)));
        codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(0, codeActions.size());
        codeActionParams.setRange(new Range(new Position(5, 1), new Position(5, 1)));
        codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(0, codeActions.size());
        preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
        codeActionParams.setRange(new Range(new Position(3, 9), new Position(3, 9)));
        CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
        cu.makeConsistent(null);
        List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 4);
        context.setDiagnostics(diagnostics);
        codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(3, codeActions.size());
        codeActionParams.setRange(new Range(new Position(3, 4), new Position(3, 40)));
        codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(3, codeActions.size());
        codeActionParams.setRange(new Range(new Position(5, 1), new Position(5, 1)));
        diagnostics = getDiagnostics(cu, astRoot, 6);
        context.setDiagnostics(diagnostics);
        codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(1, codeActions.size());
        CodeAction greeting = codeActions.get(0).getRight();
        assertNotNull(greeting);
        assertEquals("Create method 'greeting()'", greeting.getTitle());
    } finally {
        preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 2 with CodeActionHandler

use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.

the class ShowAllQuickFixTest method testDuplicateQuickFix.

// https://github.com/redhat-developer/vscode-java/issues/2236
@Test
public void testDuplicateQuickFix() throws Exception {
    String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
    try {
        IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
        StringBuilder buf = new StringBuilder();
        buf.append("package test1;\n");
        buf.append("public class F {\n");
        buf.append("    List list = List.of();\n");
        buf.append("}\n");
        ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
        cu.becomeWorkingCopy(null);
        preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
        CodeActionParams codeActionParams = new CodeActionParams();
        TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
        textDocument.setUri(JDTUtils.toURI(cu));
        codeActionParams.setTextDocument(textDocument);
        codeActionParams.setRange(new Range(new Position(2, 6), new Position(2, 6)));
        CodeActionContext context = new CodeActionContext();
        CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
        List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 3);
        context.setDiagnostics(diagnostics);
        context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
        codeActionParams.setContext(context);
        List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(5, codeActions.size());
        CodeAction action = codeActions.get(0).getRight();
        assertNotNull(action);
        assertEquals("Import 'List' (java.util)", action.getTitle());
        action = codeActions.get(1).getRight();
        assertNotNull(action);
        assertNotEquals("Import 'List' (java.util)", action.getTitle());
    } finally {
        preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 3 with CodeActionHandler

use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.

the class AbstractQuickFixTest method evaluateCodeActions.

protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit cu, Range range) throws JavaModelException {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    CodeActionParams parms = new CodeActionParams();
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    parms.setTextDocument(textDocument);
    parms.setRange(range);
    CodeActionContext context = new CodeActionContext();
    context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true));
    context.setOnly(onlyKinds);
    parms.setContext(context);
    List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
    if (onlyKinds != null && !onlyKinds.isEmpty()) {
        for (Either<Command, CodeAction> codeAction : codeActions) {
            Stream<String> acceptedActionKinds = onlyKinds.stream();
            String kind = codeAction.getRight().getKind();
            assertTrue(codeAction.getRight().getTitle() + " has kind " + kind + " but only " + onlyKinds + " are accepted", acceptedActionKinds.filter(k -> kind != null && kind.startsWith(k)).findFirst().isPresent());
        }
    }
    if (this.ignoredKinds != null) {
        List<Either<Command, CodeAction>> filteredList = codeActions.stream().filter(Either::isRight).filter(codeAction -> {
            for (String str : this.ignoredKinds) {
                if (codeAction.getRight().getKind().matches(str)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
        codeActions.removeAll(filteredList);
    }
    if (this.ignoredCommands != null) {
        List<Either<Command, CodeAction>> filteredList = new ArrayList<>();
        for (Either<Command, CodeAction> codeAction : codeActions) {
            for (String str : this.ignoredCommands) {
                if (getTitle(codeAction).matches(str)) {
                    filteredList.add(codeAction);
                    break;
                }
            }
        }
        codeActions.removeAll(filteredList);
    }
    return codeActions;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) Range(org.eclipse.lsp4j.Range) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Function(java.util.function.Function) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Document(org.eclipse.jface.text.Document) TextEdit(org.eclipse.lsp4j.TextEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BadLocationException(org.eclipse.jface.text.BadLocationException) Position(org.eclipse.lsp4j.Position) Assert.fail(org.junit.Assert.fail) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) TextEditUtil(org.eclipse.jdt.ls.core.internal.TextEditUtil) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Assert.assertFalse(org.junit.Assert.assertFalse) Entry(java.util.Map.Entry) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) ArrayList(java.util.ArrayList) IProblem(org.eclipse.jdt.core.compiler.IProblem) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either)

Example 4 with CodeActionHandler

use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.

the class NonProjectFixTest method getCodeActions.

private List<Either<Command, CodeAction>> getCodeActions(ICompilationUnit cu, IProblem problem) throws JavaModelException {
    CodeActionParams parms = new CodeActionParams();
    Range range = JDTUtils.toRange(cu, problem.getSourceStart(), 0);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    parms.setTextDocument(textDocument);
    parms.setRange(range);
    CodeActionContext context = new CodeActionContext();
    context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problem), true));
    context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
    parms.setContext(context);
    return new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Range(org.eclipse.lsp4j.Range)

Example 5 with CodeActionHandler

use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.

the class ShowAllQuickFixTest method testHandledProblems.

// preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
@Test
public void testHandledProblems() throws Exception {
    String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
    try {
        preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
        IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
        StringBuilder buf = new StringBuilder();
        buf.append("package test1;\n");
        buf.append("import java.util.List;\n");
        buf.append("public class F implements List {\n");
        buf.append("}\n");
        ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
        CodeActionParams codeActionParams = new CodeActionParams();
        TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
        textDocument.setUri(JDTUtils.toURI(cu));
        codeActionParams.setTextDocument(textDocument);
        codeActionParams.setRange(new Range(new Position(2, 13), new Position(2, 16)));
        CodeActionContext context = new CodeActionContext();
        CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
        List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 3);
        context.setDiagnostics(diagnostics);
        context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
        codeActionParams.setContext(context);
        List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
        assertEquals(1, codeActions.size());
    } finally {
        preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 CodeActionHandler (org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler)5 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)5 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)5 Range (org.eclipse.lsp4j.Range)5 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Position (org.eclipse.lsp4j.Position)4 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 CodeAction (org.eclipse.lsp4j.CodeAction)3 Diagnostic (org.eclipse.lsp4j.Diagnostic)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1