@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(); } }
@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()); }
@Test public void testCodeAction_removeUnterminatedString() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ "String s = \"some str\n" + " }\n"+ "}\n"); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); final Range range = getRange(unit, "some str"); params.setRange(range); params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnterminatedString), 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()); }
protected List<Command> evaluateCodeActions(ICompilationUnit cu) throws JavaModelException { CompilationUnit astRoot = SharedASTProvider.getInstance().getAST(cu, null); IProblem[] problems = astRoot.getProblems(); Range range = getRange(cu, problems); 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(Arrays.asList(problems))); parms.setContext(context); return new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor()); }
@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()); }
public CodeActionParams(@NonNull final TextDocumentIdentifier textDocument, @NonNull final Range range, @NonNull final CodeActionContext context) { this.textDocument = textDocument; this.range = range; this.context = context; }
/** * Context carrying additional information. */ @Pure @NonNull public CodeActionContext getContext() { return this.context; }
/** * Context carrying additional information. */ public void setContext(@NonNull final CodeActionContext context) { this.context = context; }
@Override public void computeQuickAssistProposals( QuickAssistInvocationContext invocationContext, CodeAssistCallback callback) { LinearRange range = invocationContext.getTextEditor().getSelectedLinearRange(); Document document = invocationContext.getTextEditor().getDocument(); QueryAnnotationsEvent.QueryCallback annotationCallback = new QueryAnnotationsEvent.QueryCallback() { @SuppressWarnings("ReturnValueIgnored") @Override public void respond( Map<Annotation, org.eclipse.che.ide.api.editor.text.Position> annotations) { // iteration with range never returns anything; need to filter ourselves. // https://github.com/eclipse/che/issues/4338 List<Diagnostic> diagnostics = annotations .entrySet() .stream() .filter( (e) -> e.getValue().overlapsWith(range.getStartOffset(), range.getLength())) .map(Entry::getKey) .map(a -> (DiagnosticAnnotation) a) .map(DiagnosticAnnotation::getDiagnostic) .collect(Collectors.toList()); CodeActionContext context = new CodeActionContext(diagnostics); TextPosition start = document.getPositionFromIndex(range.getStartOffset()); TextPosition end = document.getPositionFromIndex(range.getStartOffset() + range.getLength()); Position rangeStart = new Position(start.getLine(), start.getCharacter()); Position rangeEnd = new Position(end.getLine(), end.getCharacter()); Range rangeParam = new Range(rangeStart, rangeEnd); rangeParam.setEnd(rangeEnd); TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(document.getFile().getLocation().toString()); CodeActionParams params = new CodeActionParams(textDocumentIdentifier, rangeParam, context); Promise<List<Command>> codeAction = textDocumentService.codeAction(new CodeActionParamsDto(params)); List<CompletionProposal> proposals = new ArrayList<>(); codeAction.then( (commands) -> { for (Command command : commands) { Action action = actionManager.getAction(command.getCommand()); if (action != null) { proposals.add(new ActionCompletionProposal(command, action)); } } ; callback.proposalComputed(proposals); }); } }; QueryAnnotationsEvent event = new QueryAnnotationsEvent.Builder() .withFilter(a -> a instanceof DiagnosticAnnotation) .withCallback(annotationCallback) .build(); document.getDocumentHandle().getDocEventBus().fireEvent(event); }