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); }
public RewritableImportSection( XtextResource resource, IImportsConfiguration importsConfiguration, XImportSection originalImportSection, String lineSeparator, ImportSectionRegionUtil regionUtil, IValueConverter<String> nameConverter) { this.importsConfiguration = importsConfiguration; this.resource = resource; this.lineSeparator = lineSeparator; this.regionUtil = regionUtil; this.nameValueConverter = nameConverter; this.implicitlyImportedPackages = importsConfiguration.getImplicitlyImportedPackages(resource); this.importRegion = regionUtil.computeRegion(resource); if (originalImportSection != null) { for (XImportDeclaration originalImportDeclaration : originalImportSection.getImportDeclarations()) { this.originalImportDeclarations.add(originalImportDeclaration); JvmDeclaredType importedType = originalImportDeclaration.getImportedType(); if (originalImportDeclaration.isStatic()) { String memberName = originalImportDeclaration.getMemberName(); if (originalImportDeclaration.isExtension()) { Maps2.putIntoSetMap(importedType, memberName, staticExtensionImports); } else { Maps2.putIntoSetMap(importedType, memberName, staticImports); } } else if (importedType != null) { Maps2.putIntoListMap(importedType.getSimpleName(), importedType, plainImports); } } } }
protected boolean needsPreceedingBlankLine() { return regionUtil.addLeadingWhitespace(importRegion, resource).getOffset() != 0; }