throws Exception
{
IBuffer buffer = src.getBuffer();
String contents = buffer.getContents();
String delimiter = StubUtility.getLineDelimiterUsed(src);
DefaultCodeFormatter formatter =
new DefaultCodeFormatter(src.getJavaProject().getOptions(true));
// when the eclipse indent settings differ from vim (tabs vs spaces) then
// the inserted method's indent may be a bit off. this is a workaround to
// force reformatting of the code from the start of the line to the start of
// the next set of code following the new method. Doesn't quite fix indent
// formatting of methods in nested classes.
while (offset > 0 &&
!IndentManipulation.isLineDelimiterChar(buffer.getChar(offset - 1)))
{
offset--;
length++;
}
while ((offset + length) < contents.length() &&
IndentManipulation.isLineDelimiterChar(buffer.getChar(offset + length)))
{
length++;
}
TextEdit edits = formatter.format(kind, contents, offset, length, 0, delimiter);
if (edits != null) {
int oldLength = contents.length();
Document document = new Document(contents);
edits.apply(document);