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 appendSubsection( StringBuilder builder, Iterable<XImportDeclaration> subSection, boolean needsNewline) { if (!isEmpty(subSection)) { if (needsNewline) builder.append(lineSeparator); for (XImportDeclaration declaration : isSort() ? sort(subSection) : subSection) { appendImport(builder, declaration); } return true; } return needsNewline; }
protected String serializeImports(List<XImportDeclaration> allDeclarations) { StringBuilder builder = new StringBuilder(); if (needsPreceedingBlankLine()) builder.append(lineSeparator).append(lineSeparator); boolean needNewline = appendSubsection( builder, filter( allDeclarations, new Predicate<XImportDeclaration>() { @Override public boolean apply(XImportDeclaration input) { return !input.isStatic(); } }), false); needNewline = appendSubsection( builder, filter( allDeclarations, new Predicate<XImportDeclaration>() { @Override public boolean apply(XImportDeclaration input) { return input.isStatic() && !input.isExtension(); } }), needNewline); appendSubsection( builder, filter( allDeclarations, new Predicate<XImportDeclaration>() { @Override public boolean apply(XImportDeclaration input) { return input.isStatic() && input.isExtension(); } }), needNewline); if (!isEmpty(allDeclarations)) builder.append(lineSeparator); return builder.toString(); }
protected void appendImport(StringBuilder builder, XImportDeclaration newImportDeclaration) { builder.append("import "); if (newImportDeclaration.isStatic()) { builder.append("static "); if (newImportDeclaration.isExtension()) { builder.append("extension "); } } String qualifiedTypeName = newImportDeclaration.getImportedNamespace(); if (newImportDeclaration.getImportedType() != null) { qualifiedTypeName = serializeType(newImportDeclaration.getImportedType()); } String escapedTypeName = nameValueConverter.toString(qualifiedTypeName); builder.append(escapedTypeName); if (newImportDeclaration.isStatic()) { builder.append("."); if (newImportDeclaration.isWildcard()) { builder.append("*"); } else { builder.append(newImportDeclaration.getMemberName()); } } builder.append(lineSeparator); }