Search in sources :

Example 16 with Fix

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

the class XtendQuickfixProvider method refactorTernaryOperatorIntoNormalIf.

@Fix(IssueCodes.TERNARY_EXPRESSION_NOT_ALLOWED)
public void refactorTernaryOperatorIntoNormalIf(final Issue issue, IssueResolutionAcceptor acceptor) {
    String label = "Refactor into inline if-expression.";
    String description = "Refactor ternary expression (cond? a : b) \ninto an inline if-expression (if cond a else b).";
    String image = "delete_edit.png";
    // stop before NullpointerExceptions are thrown
    if (issue == null)
        return;
    // Fix
    acceptor.accept(issue, label, description, image, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            // Collect basic elements
            IXtextDocument xtextDocument = context.getXtextDocument();
            if (xtextDocument == null || element == null || !(element instanceof XExpression))
                return;
            XIfExpression exp = (XIfExpression) element;
            // Collect original ternary expression information
            ICompositeNode expNode = NodeModelUtils.findActualNodeFor(exp);
            if (expNode == null)
                throw new IllegalStateException("Node to refactor may not be null");
            int expOffset = expNode.getTextRegion().getOffset();
            int expLength = expNode.getTextRegion().getLength();
            String ifExpr = this.buildIfExpression(exp, xtextDocument);
            xtextDocument.replace(expOffset, expLength, ifExpr);
        }

        /**
         * Build an inline if expression from the given ternary expression
         * Also apply this fix to nested elements in then or else part
         */
        private String buildIfExpression(XIfExpression expression, IXtextDocument document) throws Exception {
            // Basic elements
            XExpression thenPart = expression.getThen();
            XExpression elsePart = expression.getElse();
            boolean outerBracketsSet = false;
            // Collect String representations
            String ternExpStr = this.getString(NodeModelUtils.findActualNodeFor(expression), document);
            if (ternExpStr.endsWith(")"))
                outerBracketsSet = true;
            String condStr = this.getString(NodeModelUtils.findActualNodeFor(expression.getIf()), document);
            if (!condStr.contains("(")) {
                condStr = "(" + condStr + ")";
            }
            // Check if then or else part is a ternary expression, too,
            // and apply the fix to the inner elements
            String thenStr = "", elseStr = "";
            if (thenPart instanceof XIfExpression) {
                if (((XIfExpression) thenPart).isConditionalExpression()) {
                    thenStr = buildIfExpression((XIfExpression) thenPart, document);
                }
            }
            if (elsePart != null && elsePart instanceof XIfExpression) {
                if (((XIfExpression) elsePart).isConditionalExpression()) {
                    elseStr = buildIfExpression((XIfExpression) elsePart, document);
                }
            }
            // if then/else part does not contain nested ternary expressions
            if (thenStr.isEmpty()) {
                thenStr = this.getString(NodeModelUtils.findActualNodeFor(thenPart), document);
            }
            if (elsePart != null && elseStr.isEmpty()) {
                elseStr = this.getString(NodeModelUtils.findActualNodeFor(elsePart), document);
            }
            // Combine
            String ifExpStr = this.combineIfExpParts(condStr, thenStr, elseStr, outerBracketsSet);
            return ifExpStr;
        }

        /**
         * Stitch together the inline if statement
         */
        private String combineIfExpParts(String condStr, String thenStr, String elseStr, boolean outerBracketsSet) {
            String ifExpr = "if " + condStr + " " + thenStr;
            if (!elseStr.isEmpty())
                ifExpr += " else " + elseStr;
            if (outerBracketsSet)
                ifExpr = "(" + ifExpr + ")";
            return ifExpr;
        }

        /**
         * Get the String from a document Node
         * Attention! The Document will not be updated in the meantime, so offsets and thus
         * the outputString might get messed up, if the document is changed and the String
         * is fetched after that.
         */
        private String getString(ICompositeNode partNode, IXtextDocument doc) throws Exception {
            if (partNode == null)
                throw new IllegalStateException("Node to refactor may not be null");
            ITextRegion partRegion = partNode.getTextRegion();
            int partOffset = partRegion.getOffset();
            int partLength = partRegion.getLength();
            String partString = doc.get(partOffset, partLength);
            return partString;
        }
    });
}
Also used : ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) XIfExpression(org.eclipse.xtext.xbase.XIfExpression) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 17 with Fix

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

the class XtendQuickfixProvider method removeUnnecessaryModifier.

@Fix(IssueCodes.UNNECESSARY_MODIFIER)
public void removeUnnecessaryModifier(final Issue issue, IssueResolutionAcceptor acceptor) {
    String[] issueData = issue.getData();
    if (issueData == null || issueData.length == 0) {
        return;
    }
    // use the same label, description and image
    // to be able to use the quickfixes (issue resolution) in batch mode
    String label = "Remove the unnecessary modifier.";
    String description = "The modifier is unnecessary and could be removed.";
    String image = "fix_indent.gif";
    acceptor.accept(issue, label, description, image, new ITextualMultiModification() {

        @Override
        public void apply(IModificationContext context) throws Exception {
            if (context instanceof IssueModificationContext) {
                Issue theIssue = ((IssueModificationContext) context).getIssue();
                Integer offset = theIssue.getOffset();
                IXtextDocument document = context.getXtextDocument();
                document.replace(offset, theIssue.getLength(), "");
                while (Character.isWhitespace(document.getChar(offset))) {
                    document.replace(offset, 1, "");
                }
            }
        }
    });
}
Also used : IssueModificationContext(org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext) Issue(org.eclipse.xtext.validation.Issue) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ITextualMultiModification(org.eclipse.xtext.ui.editor.model.edit.ITextualMultiModification) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 18 with Fix

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

the class XtendQuickfixProvider method implementAbstractMethods.

@Fix(IssueCodes.CLASS_MUST_BE_ABSTRACT)
public void implementAbstractMethods(final Issue issue, IssueResolutionAcceptor acceptor) {
    doOverrideMethods(issue, acceptor, "Add unimplemented methods");
    acceptor.accept(issue, "Make class abstract", "Make class abstract", "fix_indent.gif", new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            internalDoAddAbstractKeyword(element, context);
        }
    });
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 19 with Fix

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

the class DomainmodelQuickfixProvider method fixFeatureName.

@Fix(IssueCodes.INVALID_FEATURE_NAME)
public void fixFeatureName(final Issue issue, final IssueResolutionAcceptor acceptor) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("Uncapitalize name of \'");
    String _get = issue.getData()[0];
    _builder.append(_get);
    _builder.append("\'");
    final ISemanticModification _function = (EObject element, IModificationContext context) -> {
        ((Feature) element).setName(Strings.toFirstLower(issue.getData()[0]));
    };
    acceptor.accept(issue, "Uncapitalize name", _builder.toString(), "upcase.png", _function);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 20 with Fix

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

the class XbaseQuickfixProvider method fixEqualsWithNull.

@Fix(IssueCodes.EQUALS_WITH_NULL)
public void fixEqualsWithNull(final Issue issue, IssueResolutionAcceptor acceptor) {
    String[] data = issue.getData();
    if (data == null || data.length == 0) {
        return;
    }
    String operator = data[0];
    String message = "Replace '" + operator + "' with '" + operator + "='";
    acceptor.accept(issue, message, message, null, new IModification() {

        @Override
        public void apply(IModificationContext context) throws Exception {
            IXtextDocument document = context.getXtextDocument();
            document.replace(issue.getOffset(), issue.getLength(), operator + "=");
        }
    });
}
Also used : IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) 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

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