if (op != null){
String lineDelim = src.findRecommendedLineSeparator();
IImportDeclaration[] imports = src.getImports();
int importsEnd = -1;
if (imports.length > 0){
ISourceRange last = imports[imports.length - 1].getSourceRange();
importsEnd = last.getOffset() + last.getLength() + lineDelim.length();
}
op.run(null);
// an op.getResultingEdit() would be nice here, but we'll make do w/ what
// we got and caculate our own edit offset/length combo so we can format
// the new code.
int offset = pos != -1 ? pos : (len - 1 - lineDelim.length());
int newLen = src.getBuffer().getLength();
int length = newLen - len - 1;
// a more accurate length estimate can be found by locating the
// sibling at its new position and taking the difference.
// this prevents the formatting from screwing up the sibling's formatting
final IJavaElement[] newSiblings = sibling == null ?
null : src.findElements(sibling);
if (newSiblings != null && newSiblings.length == 1) {
// not sure what it would mean if there were more than one...
length = getOffset(newSiblings[0]) - offset;
}
// the change in length may include newly added imports, so handle that as
// best we can
int importLenChange = 0;
imports = src.getImports();
if (importsEnd != -1){
ISourceRange last = imports[imports.length - 1].getSourceRange();
importLenChange = last.getOffset() + last.getLength() +
lineDelim.length() - importsEnd;
}else if(imports.length > 0){
ISourceRange first = imports[0].getSourceRange();
ISourceRange last = imports[imports.length - 1].getSourceRange();
importLenChange = last.getOffset() + last.getLength() +
(lineDelim.length() * 2) - first.getOffset();
}
offset += importLenChange;
length -= importLenChange;