use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class XbaseTemplateContext method evaluate.
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
IXtextDocument xDocument = xtextDocumentUtil.getXtextDocument(getDocument());
// Ensure clean state before evaluation starts
imports.clear();
TemplateBuffer resolvedContent = super.evaluate(template);
Position position = new Position(getCompletionOffset(), 0);
List<ReplaceRegion> rewrite = createImports(imports, xDocument);
if (rewrite.size() > 0 && !isReadOnly()) {
// Remember the completion offset before performing doc changes
// $NON-NLS-1$
final String category = "__template_position_import_section" + System.currentTimeMillis();
IPositionUpdater updater = new DefaultPositionUpdater(category);
xDocument.addPositionCategory(category);
xDocument.addPositionUpdater(updater);
xDocument.addPosition(position);
try {
replaceConverter.convertToTextEdit(rewrite).apply(xDocument);
// restore CompletionOffset
setCompletionOffset(position.getOffset());
} catch (BadLocationException e) {
throw new TemplateException(e);
} finally {
xDocument.removePosition(position);
xDocument.removePositionUpdater(updater);
try {
xDocument.removePositionCategory(category);
} catch (BadPositionCategoryException e) {
throw new TemplateException(e);
}
}
}
return resolvedContent;
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixObsoletCast.
@Fix(IssueCodes.OBSOLETE_CAST)
public void fixObsoletCast(final Issue issue, IssueResolutionAcceptor acceptor) {
String fixup = "Remove unnecessary cast";
acceptor.accept(issue, fixup, fixup, null, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
final IXtextDocument document = context.getXtextDocument();
ReplaceRegion replacement = document.tryReadOnly(new IUnitOfWork<ReplaceRegion, XtextResource>() {
@Override
public ReplaceRegion exec(XtextResource state) throws Exception {
EObject type = state.getEObject(issue.getUriToProblem().fragment());
XCastedExpression cast = EcoreUtil2.getContainerOfType(type, XCastedExpression.class);
INode castNode = NodeModelUtils.findActualNodeFor(cast);
INode targetNode = NodeModelUtils.findActualNodeFor(cast.getTarget());
return new ReplaceRegion(castNode.getTotalTextRegion(), targetNode.getText());
}
});
if (replacement != null) {
document.replace(replacement.getOffset(), replacement.getLength(), replacement.getText());
}
}
});
}
Aggregations