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");
}
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());
}
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());
}
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();
}
}
Aggregations