Search in sources :

Example 41 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix 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)

Example 42 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixRedundantCase.

@Fix(IssueCodes.REDUNDANT_CASE)
public void fixRedundantCase(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove redundant case.", "Remove redundant case.", null, new ReplaceModification(issue, ""));
    acceptor.accept(issue, "Assign empty expression.", "Assign empty expression.", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
            if (switchExpression == null) {
                return;
            }
            XCasePart casePart = IterableExtensions.last(switchExpression.getCases());
            if (casePart == null || !(casePart.isFallThrough() && casePart.getThen() == null)) {
                return;
            }
            List<INode> nodes = NodeModelUtils.findNodesForFeature(casePart, XbasePackage.Literals.XCASE_PART__FALL_THROUGH);
            if (nodes.isEmpty()) {
                return;
            }
            INode firstNode = IterableExtensions.head(nodes);
            INode lastNode = IterableExtensions.last(nodes);
            int offset = firstNode.getOffset();
            int length = lastNode.getEndOffset() - offset;
            ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), offset, length);
            appendable.append(": {");
            appendable.increaseIndentation().newLine();
            appendable.decreaseIndentation().newLine().append("}");
            appendable.commitChanges();
        }
    });
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceModification(org.eclipse.xtext.ui.editor.quickfix.ReplaceModification) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XCasePart(org.eclipse.xtext.xbase.XCasePart) XSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 43 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.

the class DomainmodelQuickfixProvider method fixTypeName.

@Fix(IssueCodes.INVALID_TYPE_NAME)
public void fixTypeName(Issue issue, IssueResolutionAcceptor acceptor) {
    // exemplary textual modification
    acceptor.accept(// exemplary textual modification
    issue, // exemplary textual modification
    "Capitalize name", // exemplary textual modification
    "Capitalize name of '" + issue.getData()[0] + "'", // exemplary textual modification
    "upcase.png", (IModificationContext context) -> {
        IXtextDocument xtextDocument = context.getXtextDocument();
        String firstLetter = xtextDocument.get(issue.getOffset(), 1);
        xtextDocument.replace(issue.getOffset(), 1, Strings.toFirstUpper(firstLetter));
    });
}
Also used : IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)43 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)35 BadLocationException (org.eclipse.jface.text.BadLocationException)29 EObject (org.eclipse.emf.ecore.EObject)25 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)18 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)17 CoreException (org.eclipse.core.runtime.CoreException)16 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)16 XtextResource (org.eclipse.xtext.resource.XtextResource)13 List (java.util.List)9 WrappedException (org.eclipse.emf.common.util.WrappedException)9 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)9 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)7 ArrayList (java.util.ArrayList)5 EList (org.eclipse.emf.common.util.EList)5 URI (org.eclipse.emf.common.util.URI)5 INode (org.eclipse.xtext.nodemodel.INode)5 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4