private static WorkspaceEdit convertChangeToWorkspaceEdit(ICompilationUnit unit, Change change) throws CoreException { WorkspaceEdit $ = new WorkspaceEdit(); if (change instanceof TextChange) { TextEditConverter converter = new TextEditConverter(unit, ((TextChange) change).getEdit()); String uri = JDTUtils.toURI(unit); $.getChanges().put(uri, converter.convert()); } else if (change instanceof ResourceChange) { ChangeUtil.convertResourceChange((ResourceChange) change, $); } else if (change instanceof CompositeChange) { ChangeUtil.convertCompositeChange(change, $); } return $; }
private static void doConvertCompositeChange(Change change, WorkspaceEdit edit) throws CoreException { Object modifiedElement = change.getModifiedElement(); if (!(modifiedElement instanceof IJavaElement)) { return; } if (change instanceof TextChange) { convertTextChange(edit, (IJavaElement) modifiedElement, (TextChange) change); } else if (change instanceof ResourceChange) { ResourceChange resourceChange = (ResourceChange) change; convertResourceChange(resourceChange, edit); } }
private static void convertTextChange(WorkspaceEdit root, IJavaElement element, TextChange textChange) { TextEdit textEdits = textChange.getEdit(); if (textEdits == null) { return; } ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); convertTextEdit(root, compilationUnit, textEdits); }
/** * Converts changes to resource operations if resource operations are supported * by the client otherwise converts to TextEdit changes. * * @param resourceChange * resource changes after Refactoring operation * @param edit * instance of workspace edit changes * @throws CoreException */ public static void convertResourceChange(ResourceChange resourceChange, WorkspaceEdit edit) throws CoreException { if (!JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) { return; } List<Either<TextDocumentEdit, ResourceOperation>> changes = edit.getDocumentChanges(); if (changes == null) { changes = new LinkedList<>(); edit.setDocumentChanges(changes); } // Resource change is needed and supported by client if (resourceChange instanceof RenameCompilationUnitChange) { convertCUResourceChange(edit, (RenameCompilationUnitChange) resourceChange); } else if (resourceChange instanceof RenamePackageChange) { convertRenamePackcageChange(edit, (RenamePackageChange) resourceChange); } }
CompilationUnit unit = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL).parse(cu, true); ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); updatePackageStatement(unit, packageChange.getNewName(), rewrite, cu); TextEdit textEdit = rewrite.rewriteAST(); convertTextEdit(edit, cu, textEdit);
/** * @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; }
/** * Converts changes to resource operations if resource operations are supported * by the client otherwise converts to TextEdit changes. * * @param change * changes after Refactoring operation * @param edit * instance of workspace edit changes * @throws CoreException */ public static void convertCompositeChange(Change change, WorkspaceEdit edit) throws CoreException { if (!(change instanceof CompositeChange)) { return; } Change[] changes = ((CompositeChange) change).getChildren(); for (Change ch : changes) { if (ch instanceof DynamicValidationRefactoringChange) { CompositeChange compositeChange = (CompositeChange) ch; for (Change child : compositeChange.getChildren()) { doConvertCompositeChange(child, edit); } } else { doConvertCompositeChange(ch, edit); } } }
private static ICompilationUnit getNewCompilationUnit(IType type, String newName) { ICompilationUnit cu = type.getCompilationUnit(); if (isPrimaryType(type)) { IPackageFragment parent = type.getPackageFragment(); String renamedCUName = JavaModelUtil.getRenamedCUName(cu, newName); return parent.getCompilationUnit(renamedCUName); } else { return cu; } }
ChangeUtil.convertCompositeChange(change, edit); } catch (CoreException ex) { JavaLanguageServerPlugin.logException("Problem with rename for " + params.getTextDocument().getUri(), ex);
/** * 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)); } }