Thanks for your help, but I've solved this myself since I posted the thread. I used the following code:
Code:
// Actually edits the document via the AST, using ASTRewrite.
ASTRewrite rewriter = ASTRewrite.create(ast);
ASTHandler handler = new ASTHandler();
ASTNode currentNode = handler.descend(root,
CalciteCompletionProposalComputer
.getIdentifierBeforeOffset(context));
ASTNode parent = currentNode.getParent();
//ITrackedNodePosition parentLocation = rewriter.track(currentNode.getParent());
ListRewrite lrw;
try {
if (parent instanceof Block)
lrw = rewriter
.getListRewrite(parent, Block.STATEMENTS_PROPERTY);
else
lrw = rewriter.getListRewrite(parent,
((AbstractTypeDeclaration) parent)
.getBodyDeclarationsProperty());
} catch (Throwable e) {
return;
}
for (ASTNode prior : preStatements) {
lrw.insertBefore(prior, currentNode, null);
}
lrw = rewriter.getListRewrite(root, CompilationUnit.IMPORTS_PROPERTY);
for (ImportDeclaration imp : importStatements)
lrw.insertLast(imp, null);
TextEdit edits = rewriter.rewriteAST(document, null);
// Sets cursor position and applies the primary edit
int cursorDelta = super.getReplacementString().indexOf(',');
if (cursorDelta == -1)
cursorDelta = regexParseDisplay(super.getReplacementString());
if (preStatements.size() > 0)
cursorDelta += COMMENT_LENGTH;
cursorDelta += getMultiEditLength(edits);
super.setCursorPosition(cursorDelta);
super.apply(document, trigger, offset);
try {
// Preserves the undo stack.
edits.apply(document);
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
If anyone who reads this needs more help, please just reply to this thread.