public List<ReplaceRegion> rewrite() { removeObsoleteStaticImports(); final List<ReplaceRegion> replaceRegions = newArrayList(); if (isSort) { List<XImportDeclaration> allImportDeclarations = newArrayList(); allImportDeclarations.addAll(originalImportDeclarations); allImportDeclarations.addAll(addedImportDeclarations); allImportDeclarations.removeAll(removedImportDeclarations); String newImportSection = serializeImports(allImportDeclarations); importRegion = regionUtil.addLeadingWhitespace(importRegion, resource); importRegion = regionUtil.addTrailingWhitespace(importRegion, resource); return singletonList(new ReplaceRegion(importRegion, newImportSection)); } else { for (XImportDeclaration removedImportDeclaration : removedImportDeclarations) { ICompositeNode node = NodeModelUtils.findActualNodeFor(removedImportDeclaration); if (node != null) { ITextRegion textRegion = node.getTextRegion(); textRegion = regionUtil.addTrailingSingleWhitespace(textRegion, lineSeparator, resource); replaceRegions.add(new ReplaceRegion(textRegion, "")); } } addSectionToAppend( new IAcceptor<ReplaceRegion>() { @Override public void accept(ReplaceRegion t) { replaceRegions.add(t); } }); } return replaceRegions; }
protected void addSectionToAppend(IAcceptor<ReplaceRegion> acceptor) { StringBuilder importDeclarationsToAppend = getImportDeclarationsToAppend(); if (importDeclarationsToAppend.length() == 0) return; importRegion = regionUtil.addLeadingWhitespace(importRegion, resource); importRegion = regionUtil.addTrailingSingleWhitespace(importRegion, lineSeparator, resource); int insertOffset = importRegion.getOffset() + importRegion.getLength(); if (insertOffset != 0 && originalImportDeclarations.isEmpty()) importDeclarationsToAppend.insert(0, lineSeparator); importDeclarationsToAppend.append(lineSeparator); int insertLength = -importRegion.getLength(); insertLength += regionUtil.addTrailingWhitespace(importRegion, resource).getLength(); ReplaceRegion appendDeclarations = new ReplaceRegion( new TextRegion(insertOffset, insertLength), importDeclarationsToAppend.toString()); acceptor.accept(appendDeclarations); }
protected boolean needsPreceedingBlankLine() { return regionUtil.addLeadingWhitespace(importRegion, resource).getOffset() != 0; }