Search in sources :

Example 6 with CodeActionContext

use of org.eclipse.lsp4j.CodeActionContext in project sonarlint-core by SonarSource.

the class ServerMainTest method testCodeActionRuleDescription.

@Test
public void testCodeActionRuleDescription() throws Exception {
    String uri = getUri("foo.js");
    VersionedTextDocumentIdentifier docId = new VersionedTextDocumentIdentifier(1);
    docId.setUri(uri);
    lsProxy.getTextDocumentService().didChange(new DidChangeTextDocumentParams(docId, Collections.singletonList(new TextDocumentContentChangeEvent("function foo() {\n  alert('toto');\n}"))));
    List<? extends Command> codeActions = lsProxy.getTextDocumentService().codeAction(new CodeActionParams(new TextDocumentIdentifier(uri), new Range(new Position(1, 4), new Position(1, 4)), new CodeActionContext(waitForDiagnostics(uri)))).get();
    assertThat(codeActions).hasSize(1);
    String ruleKey = (String) codeActions.get(0).getArguments().get(0);
    assertThat(ruleKey).isEqualTo("javascript:S1442");
    lsProxy.getWorkspaceService().executeCommand(new ExecuteCommandParams(codeActions.get(0).getCommand(), codeActions.get(0).getArguments())).get();
    assertThat(client.ruleDescs).hasSize(1);
    assertThat(client.ruleDescs.get(0).getKey()).isEqualTo("javascript:S1442");
    assertThat(client.ruleDescs.get(0).getName()).contains("\"alert(...)\" should not be used");
    assertThat(client.ruleDescs.get(0).getHtmlDescription()).contains("can be useful for debugging during development");
    assertThat(client.ruleDescs.get(0).getType()).isEqualTo("VULNERABILITY");
    assertThat(client.ruleDescs.get(0).getSeverity()).isEqualTo("MINOR");
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) Range(org.eclipse.lsp4j.Range) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) Test(org.junit.Test)

Example 7 with CodeActionContext

use of org.eclipse.lsp4j.CodeActionContext in project eclipse.jdt.ls by eclipse.

the class CodeActionHandlerTest method testCodeAction_removeUnusedImport.

@Test
public void testCodeAction_removeUnusedImport() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n" + "	void foo() {\n" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = getRange(unit, "java.sql");
    params.setRange(range);
    params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnusedImport), range))));
    List<? extends Command> commands = server.codeAction(params).join();
    Assert.assertNotNull(commands);
    Assert.assertEquals(2, commands.size());
    Command c = commands.get(0);
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 8 with CodeActionContext

use of org.eclipse.lsp4j.CodeActionContext in project eclipse.jdt.ls by eclipse.

the class CodeActionHandlerTest method testCodeAction_superfluousSemicolon.

@Test
@Ignore
public void testCodeAction_superfluousSemicolon() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "	void foo() {\n" + ";" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = getRange(unit, ";");
    params.setRange(range);
    params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.SuperfluousSemicolon), range))));
    List<? extends Command> commands = server.codeAction(params).join();
    Assert.assertNotNull(commands);
    Assert.assertEquals(1, commands.size());
    Command c = commands.get(0);
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
    Assert.assertNotNull(c.getArguments());
    Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
    WorkspaceEdit we = (WorkspaceEdit) c.getArguments().get(0);
    List<org.eclipse.lsp4j.TextEdit> edits = we.getChanges().get(JDTUtils.toURI(unit));
    Assert.assertEquals(1, edits.size());
    Assert.assertEquals("", edits.get(0).getNewText());
    Assert.assertEquals(range, edits.get(0).getRange());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Range(org.eclipse.lsp4j.Range) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with CodeActionContext

use of org.eclipse.lsp4j.CodeActionContext in project eclipse.jdt.ls by eclipse.

the class CodeActionHandlerTest method testCodeAction_exception.

@Test
public void testCodeAction_exception() throws JavaModelException {
    URI uri = project.getFile("nopackage/Test.java").getRawLocationURI();
    ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
    try {
        cu.becomeWorkingCopy(new NullProgressMonitor());
        CodeActionParams params = new CodeActionParams();
        params.setTextDocument(new TextDocumentIdentifier(uri.toString()));
        final Range range = new Range();
        range.setStart(new Position(0, 17));
        range.setEnd(new Position(0, 17));
        params.setRange(range);
        CodeActionContext context = new CodeActionContext();
        context.setDiagnostics(Collections.emptyList());
        params.setContext(context);
        List<? extends Command> commands = server.codeAction(params).join();
        Assert.assertNotNull(commands);
        Assert.assertEquals(0, commands.size());
    } finally {
        cu.discardWorkingCopy();
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) URI(java.net.URI) Test(org.junit.Test)

Aggregations

CodeActionContext (org.eclipse.lsp4j.CodeActionContext)9 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)9 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)9 Range (org.eclipse.lsp4j.Range)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 Command (org.eclipse.lsp4j.Command)6 Test (org.junit.Test)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 CompletionList (org.eclipse.lsp4j.CompletionList)2 Position (org.eclipse.lsp4j.Position)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableList (com.google.common.collect.ImmutableList)1 Multimap (com.google.common.collect.Multimap)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1