Search in sources :

Example 6 with Fix

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

the class XbaseQuickfixProvider method fixIncompleteCasesOnEnum.

@Fix(IssueCodes.INCOMPLETE_CASES_ON_ENUM)
public void fixIncompleteCasesOnEnum(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Add 'default' case", "Add 'default' case", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
            if (switchExpression == null) {
                return;
            }
            int insertOffset = getInsertOffset(switchExpression);
            IXtextDocument document = context.getXtextDocument();
            ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) element.eResource(), insertOffset, 0);
            if (switchExpression.getCases().isEmpty()) {
                appendable.increaseIndentation();
            }
            appendable.newLine();
            appendable.append("default: {");
            appendable.newLine().append("}");
            appendable.commitChanges();
        }
    });
    acceptor.accept(issue, "Add missing cases", "Add missing cases", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
            if (switchExpression == null) {
                return;
            }
            int insertOffset = getInsertOffset(switchExpression);
            IXtextDocument document = context.getXtextDocument();
            ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) element.eResource(), insertOffset, 0);
            if (switchExpression.getCases().isEmpty()) {
                appendable.increaseIndentation();
            }
            for (String expectedEnumerationLiteral : issue.getData()) {
                appendable.newLine().append("case ").append(expectedEnumerationLiteral).append(": {");
                appendable.newLine().append("}");
            }
            appendable.commitChanges();
        }
    });
}
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) XSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression) 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) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 7 with Fix

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

the class QuickfixCrossrefTestLanguageQuickfixProvider method fixBadNames.

@SuppressWarnings("unchecked")
@Fix(QuickfixCrossrefTestLanguageValidator.BAD_NAME_IN_SUBELEMENTS)
public void fixBadNames(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.acceptMulti(issue, "Fix Bad Names", "Fix Bad Names", null, (Element main, ICompositeModificationContext<Element> ctx) -> {
        ctx.addModification(main.eContainer(), c -> {
            List<Element> siblings = (List<Element>) main.eContainer().eGet(main.eContainmentFeature());
            int index = siblings.indexOf(main);
            Element newEle = QuickfixCrossrefFactory.eINSTANCE.createElement();
            newEle.setName("newElement");
            siblings.add(index, newEle);
        });
        for (String s : issue.getData()[0].split(";")) {
            EObject ele = main.eResource().getEObject(s);
            Assert.assertTrue(ele instanceof Element);
            ctx.addModification((Element) ele, e -> {
                e.setName("goodname");
            });
        }
    });
}
Also used : ICompositeModificationContext(org.eclipse.xtext.ui.editor.model.edit.ICompositeModificationContext) Element(org.eclipse.xtext.ui.tests.quickfix.quickfixCrossref.Element) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 8 with Fix

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

the class XtextGrammarQuickfixProvider method fixImportedPackageFromSuperGrammar.

@Fix(INVALID_PACKAGE_REFERENCE_INHERITED)
public void fixImportedPackageFromSuperGrammar(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData().length == 1)
        acceptor.accept(issue, "Change to '" + issue.getData()[0] + "'", "Fix the bogus package import\n" + "import '" + issue.getData()[0] + "'", NULL_QUICKFIX_IMAGE, new IModification() {

            @Override
            public void apply(IModificationContext context) throws BadLocationException {
                String replaceString = valueConverterService.toString(issue.getData()[0], "STRING");
                IXtextDocument document = context.getXtextDocument();
                String delimiter = document.get(issue.getOffset(), 1);
                if (!replaceString.startsWith(delimiter)) {
                    replaceString = delimiter + replaceString.substring(1, replaceString.length() - 1) + delimiter;
                }
                document.replace(issue.getOffset(), issue.getLength(), replaceString);
            }
        });
}
Also used : IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) 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 9 with Fix

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

the class XtextGrammarQuickfixProvider method fixEmptyEnumLiteral.

@Fix(EMPTY_ENUM_LITERAL)
public void fixEmptyEnumLiteral(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.acceptMulti(issue, "Fix empty enum literal", "Fix empty enum literal", NULL_QUICKFIX_IMAGE, (EObject element) -> {
        EnumLiteralDeclaration enumLiteralDeclaration = (EnumLiteralDeclaration) element;
        Keyword keyword = XtextFactory.eINSTANCE.createKeyword();
        keyword.setValue(enumLiteralDeclaration.getEnumLiteral().getName().toLowerCase());
        enumLiteralDeclaration.setLiteral(keyword);
    });
}
Also used : EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 10 with Fix

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

the class CheckQuickfixProvider method addIdToCheck.

/**
 * Add explicit ID to a check, autogenerated from its label.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.MISSING_ID_ON_CHECK)
public void addIdToCheck(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, Messages.CheckQuickfixProvider_ADD_ID_LABEL, NLS.bind(Messages.CheckQuickfixProvider_ADD_ID_DESCN, CHECK), NO_IMAGE, (final EObject element, final IModificationContext context) -> {
        final Check check = EcoreUtil2.getContainerOfType(element, Check.class);
        if (check != null) {
            final String label = check.getLabel();
            final String id = CheckUtil.toIdentifier(label);
            check.setId(id);
        }
    });
}
Also used : EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) Check(com.avaloq.tools.ddk.check.check.Check) 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