Search in sources :

Example 51 with ReplaceRegion

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;
}
Also used : Position(org.eclipse.jface.text.Position) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TemplateException(org.eclipse.jface.text.templates.TemplateException) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) IPositionUpdater(org.eclipse.jface.text.IPositionUpdater) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 52 with ReplaceRegion

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());
            }
        }
    });
}
Also used : XCastedExpression(org.eclipse.xtext.xbase.XCastedExpression) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)52 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)20 Test (org.junit.Test)19 XtextResource (org.eclipse.xtext.resource.XtextResource)18 Resource (org.eclipse.emf.ecore.resource.Resource)13 INode (org.eclipse.xtext.nodemodel.INode)11 IParseResult (org.eclipse.xtext.parser.IParseResult)10 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)9 EObject (org.eclipse.emf.ecore.EObject)7 LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)6 StringReader (java.io.StringReader)5 SyntheticCompositeNode (org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 List (java.util.List)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TextEdit (org.eclipse.text.edits.TextEdit)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 PartialParsingPointers (org.eclipse.xtext.parser.impl.PartialParsingPointers)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)3 ArrayList (java.util.ArrayList)2