Search in sources :

Example 11 with Fix

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

the class FormatQuickfixProvider method removeOverride.

/**
 * Semantic quickfix removing the override flag for a rule.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(FormatJavaValidator.OVERRIDE_ILLEGAL_CODE)
public void removeOverride(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove override", "Remove override.", null, new IModification() {

        @Override
        public void apply(final IModificationContext context) throws BadLocationException {
            context.getXtextDocument().modify(new IUnitOfWork<Void, XtextResource>() {

                @Override
                public java.lang.Void exec(final XtextResource state) {
                    Rule rule = (Rule) state.getEObject(issue.getUriToProblem().fragment());
                    rule.setOverride(false);
                    return null;
                }
            });
        }
    });
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 12 with Fix

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

the class CheckCfgQuickfixProvider method fixSeverityToMaxSeverity.

/**
 * Fix severity by setting it to a legal value as is defined by severity range of referenced check. Legal
 * severities are passed as issue data (org.eclipse.xtext.validation.Issue#getData()).
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.SEVERITY_NOT_ALLOWED)
public void fixSeverityToMaxSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null) {
        for (final String severityProposal : issue.getData()) {
            final String label = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_LABEL, severityProposal);
            final String descn = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_DESCN, severityProposal);
            acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {

                public void apply(final IModificationContext context) throws BadLocationException {
                    IXtextDocument xtextDocument = context.getXtextDocument();
                    xtextDocument.replace(issue.getOffset(), issue.getLength(), severityProposal);
                }
            });
        }
    }
}
Also used : IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) 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 13 with Fix

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

the class XtendQuickfixProvider method fixJunitNotOnClasspath.

@Fix(Diagnostic.LINKING_DIAGNOSTIC)
public void fixJunitNotOnClasspath(final Issue issue, final IssueResolutionAcceptor acceptor) {
    if (issue.getData().length != 0 && issue.getData()[0] != null) {
        String type = issue.getData()[0];
        if (type.startsWith("org.junit.jupiter.")) {
            acceptor.accept(issue, "Add JUnit 5 lib to classpath", "Add JUnit 5 lib to classpath", "julaunch.gif", new ISemanticModification() {

                @Override
                public void apply(EObject element, IModificationContext context) throws Exception {
                    ResourceSet resourceSet = element.eResource().getResourceSet();
                    IJavaProject javaProject = projectProvider.getJavaProject(resourceSet);
                    junit5LibAdder.addLibsToClasspath(javaProject, new NullProgressMonitor());
                }
            });
        } else if (type.startsWith("org.junit.")) {
            acceptor.accept(issue, "Add JUnit 4 lib to classpath", "Add JUnit 4 lib to classpath", "julaunch.gif", new ISemanticModification() {

                @Override
                public void apply(EObject element, IModificationContext context) throws Exception {
                    ResourceSet resourceSet = element.eResource().getResourceSet();
                    IJavaProject javaProject = projectProvider.getJavaProject(resourceSet);
                    junit4LibAdder.addLibsToClasspath(javaProject, new NullProgressMonitor());
                }
            });
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 14 with Fix

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

the class XtendQuickfixProvider method specifyTypeExplicitly.

@Fix(IssueCodes.API_TYPE_INFERENCE)
public void specifyTypeExplicitly(Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Infer type", "Infer type", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            EStructuralFeature featureAfterType = null;
            JvmIdentifiableElement jvmElement = null;
            if (element instanceof XtendFunction) {
                XtendFunction function = (XtendFunction) element;
                if (function.getCreateExtensionInfo() == null) {
                    featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__NAME;
                } else {
                    featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__CREATE_EXTENSION_INFO;
                }
                jvmElement = associations.getDirectlyInferredOperation((XtendFunction) element);
            } else if (element instanceof XtendField) {
                featureAfterType = XtendPackage.Literals.XTEND_FIELD__NAME;
                jvmElement = associations.getJvmField((XtendField) element);
            }
            if (jvmElement != null) {
                LightweightTypeReference type = batchTypeResolver.resolveTypes(element).getActualType(jvmElement);
                INode node = Iterables.getFirst(NodeModelUtils.findNodesForFeature(element, featureAfterType), null);
                if (node == null) {
                    throw new IllegalStateException("Could not determine node for " + element);
                }
                if (type == null) {
                    throw new IllegalStateException("Could not determine type for " + element);
                }
                ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), node.getOffset(), 0);
                appendable.append(type);
                appendable.append(" ");
                appendable.commitChanges();
            }
        }
    });
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) INode(org.eclipse.xtext.nodemodel.INode) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) XtendField(org.eclipse.xtend.core.xtend.XtendField) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 15 with Fix

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

the class XtendQuickfixProvider method fixWrongFileRenameClass.

@Fix(IssueCodes.WRONG_FILE)
public void fixWrongFileRenameClass(final Issue issue, final IssueResolutionAcceptor acceptor) {
    URI uri = issue.getUriToProblem();
    String className = uri.trimFileExtension().lastSegment();
    String label = String.format("Rename class to '%s'", className);
    acceptor.accept(issue, label, label, null, (element, context) -> {
        context.getXtextDocument().modify(resource -> {
            IRenameElementContext renameContext = renameContextFactory.createRenameElementContext(element, null, new TextSelection(context.getXtextDocument(), issue.getOffset(), issue.getLength()), resource);
            final ProcessorBasedRefactoring refactoring = renameRefactoringProvider.getRenameRefactoring(renameContext);
            ((RenameElementProcessor) refactoring.getProcessor()).setNewName(className);
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, monitor -> {
                try {
                    if (!refactoring.checkFinalConditions(monitor).isOK())
                        return;
                    Change change = refactoring.createChange(monitor);
                    change.initializeValidationData(monitor);
                    PerformChangeOperation performChangeOperation = new PerformChangeOperation(change);
                    performChangeOperation.setUndoManager(RefactoringCore.getUndoManager(), refactoring.getName());
                    performChangeOperation.setSchedulingRule(ResourcesPlugin.getWorkspace().getRoot());
                    performChangeOperation.run(monitor);
                } catch (CoreException e) {
                    logger.error(e);
                }
            });
            return null;
        });
    });
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) CoreException(org.eclipse.core.runtime.CoreException) TextSelection(org.eclipse.jface.text.TextSelection) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) Change(org.eclipse.ltk.core.refactoring.Change) URI(org.eclipse.emf.common.util.URI) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) RenameElementProcessor(org.eclipse.xtext.ui.refactoring.impl.RenameElementProcessor) 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