/** * @return <code>true</code> if a {@link WorkspaceEdit} contains any actual * changes, <code>false</code> otherwise. */ public static boolean hasChanges(WorkspaceEdit edit) { if (edit == null) { return false; } if (edit.getDocumentChanges() != null && !edit.getDocumentChanges().isEmpty()) { return true; } boolean hasChanges = false; //@formatter:off if ((edit.getChanges() != null && !edit.getChanges().isEmpty())) { hasChanges = edit.getChanges().values().stream() .filter(changes -> changes != null && !changes.isEmpty() && hasChanges(changes)) .findFirst() .isPresent(); } //@formatter:on return hasChanges; }
/** * Adds imports collected by importRewrite to item * @param item */ private void addImports(List<org.eclipse.lsp4j.TextEdit> additionalEdits) { if(this.importRewrite != null ){ try { TextEdit edit = this.importRewrite.rewriteImports(new NullProgressMonitor()); TextEditConverter converter = new TextEditConverter(this.compilationUnit, edit); List<org.eclipse.lsp4j.TextEdit> edits = converter.convert(); if (ChangeUtil.hasChanges(edits)) { additionalEdits.addAll(edits); } } catch (CoreException e) { JavaLanguageServerPlugin.logException("Error adding imports",e); } } }
private void addWorkspaceEdit(ICompilationUnit cu, CUCorrectionProposal proposal, WorkspaceEdit rootEdit) throws CoreException { TextChange textChange = proposal.getTextChange(); TextEdit edit = textChange.getEdit(); TextEditConverter converter = new TextEditConverter(cu, edit); List<org.eclipse.lsp4j.TextEdit> edits = converter.convert(); if (ChangeUtil.hasChanges(edits)) { rootEdit.getChanges().put(JDTUtils.toURI(cu), edits); } } }
private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(CUCorrectionProposal proposal, CodeActionContext context) throws CoreException { String name = proposal.getName(); ICompilationUnit unit = proposal.getCompilationUnit(); WorkspaceEdit edit = convertChangeToWorkspaceEdit(unit, proposal.getChange()); if (!ChangeUtil.hasChanges(edit)) { return Optional.empty(); } Command command = new Command(name, COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit)); if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(proposal.getKind())) { CodeAction codeAction = new CodeAction(name); codeAction.setKind(proposal.getKind()); codeAction.setCommand(command); codeAction.setDiagnostics(context.getDiagnostics()); return Optional.of(Either.forRight(codeAction)); } else { return Optional.of(Either.forLeft(command)); } }