Search in sources :

Example 1 with IssueResolutionAcceptor

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

the class DefaultCheckQuickfixProvider method getResolutions.

/**
 * Invokes quickfix methods and creates IssueResolution objects. Both "native" quickfix providers and Check
 * quickfix providers are considered.
 * <p>
 * {@inheritDoc}
 */
@Override
protected List<IssueResolution> getResolutions(final Issue issue, final List<Method> fixMethods) {
    final ICoreIssueResolutionAcceptor coreIssueResolutionAcceptor = coreIssueResolutionAcceptorProvider.get();
    final IssueResolutionAcceptor defaultIssueResolutionAcceptor = issueResolutionAcceptorProvider.get();
    for (Method fixMethod : fixMethods) {
        try {
            fixMethod.setAccessible(true);
            if (Lists.newArrayList(getClass().getMethods()).contains(fixMethod)) {
                if (fixMethod.getAnnotation(CoreFix.class) != null) {
                    fixMethod.invoke(this, issue, coreIssueResolutionAcceptor);
                } else {
                    // Legacy code: executes native quickfixes
                    fixMethod.invoke(this, issue, defaultIssueResolutionAcceptor);
                }
            } else {
                // the fix method
                for (final ICoreQuickfixProvider p : getProviders()) {
                    if (Iterables.contains(methodsForProvider(p, issue.getCode()), fixMethod)) {
                        fixMethod.invoke(p, issue, coreIssueResolutionAcceptor);
                    }
                }
            }
        // CHECKSTYLE:OFF
        } catch (Exception e) {
            // CHECKSTYLE:ON
            // $NON-NLS-1$
            LOGGER.error("Error executing fix method", e);
        }
    }
    final Iterable<IssueResolution> allResolutions = Iterables.concat(Lists.transform(coreIssueResolutionAcceptor.getIssueResolutions(), new Function<CoreIssueResolution, IssueResolution>() {

        @Override
        public IssueResolution apply(final CoreIssueResolution from) {
            return new IssueResolutionWrapper(from);
        }
    }), defaultIssueResolutionAcceptor.getIssueResolutions());
    return Lists.newArrayList(allResolutions);
}
Also used : Function(com.google.common.base.Function) CoreIssueResolution(com.avaloq.tools.ddk.check.runtime.quickfix.CoreIssueResolution) ICoreIssueResolutionAcceptor(com.avaloq.tools.ddk.check.runtime.quickfix.ICoreIssueResolutionAcceptor) CoreFix(com.avaloq.tools.ddk.check.runtime.quickfix.CoreFix) ICoreQuickfixProvider(com.avaloq.tools.ddk.check.runtime.quickfix.ICoreQuickfixProvider) CoreIssueResolution(com.avaloq.tools.ddk.check.runtime.quickfix.CoreIssueResolution) IssueResolution(org.eclipse.xtext.ui.editor.quickfix.IssueResolution) Method(java.lang.reflect.Method) IssueResolutionAcceptor(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor) ICoreIssueResolutionAcceptor(com.avaloq.tools.ddk.check.runtime.quickfix.ICoreIssueResolutionAcceptor)

Example 2 with IssueResolutionAcceptor

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

the class DeclarativeQuickfixProviderTest method testHasResolutions2.

@Test
public void testHasResolutions2() throws Exception {
    AbstractDeclarativeQuickfixProvider provider = new AbstractDeclarativeQuickfixProvider() {

        @Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE)
        @Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE2)
        public void signature(Issue i, IssueResolutionAcceptor acceptor) {
        }
    };
    assertFalse(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE + 1));
    assertTrue(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE));
    assertTrue(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE2));
}
Also used : AbstractDeclarativeQuickfixProvider(org.eclipse.xtext.ui.editor.quickfix.AbstractDeclarativeQuickfixProvider) Issue(org.eclipse.xtext.validation.Issue) IssueResolutionAcceptor(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor) Test(org.junit.Test)

Example 3 with IssueResolutionAcceptor

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

the class DeclarativeQuickfixProviderTest method testHasResolutions3.

@Test
public void testHasResolutions3() throws Exception {
    AbstractDeclarativeQuickfixProvider provider = new AbstractDeclarativeQuickfixProvider() {

        @Fixes({ @Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE), @Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE2) })
        public void signature(Issue i, IssueResolutionAcceptor acceptor) {
        }
    };
    assertFalse(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE + 1));
    assertTrue(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE));
    assertTrue(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE2));
}
Also used : AbstractDeclarativeQuickfixProvider(org.eclipse.xtext.ui.editor.quickfix.AbstractDeclarativeQuickfixProvider) Issue(org.eclipse.xtext.validation.Issue) IssueResolutionAcceptor(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor) Test(org.junit.Test)

Example 4 with IssueResolutionAcceptor

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

the class DeclarativeQuickfixProviderTest method testHasResolutions.

@Test
public void testHasResolutions() throws Exception {
    AbstractDeclarativeQuickfixProvider provider = new AbstractDeclarativeQuickfixProvider() {

        @Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE)
        public void signature(Issue i, IssueResolutionAcceptor acceptor) {
        }
    };
    assertFalse(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE + 1));
    assertTrue(provider.hasResolutionFor(DeclarativeQuickfixProviderTest.DUMMY_CODE));
}
Also used : AbstractDeclarativeQuickfixProvider(org.eclipse.xtext.ui.editor.quickfix.AbstractDeclarativeQuickfixProvider) Issue(org.eclipse.xtext.validation.Issue) IssueResolutionAcceptor(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor) Test(org.junit.Test)

Example 5 with IssueResolutionAcceptor

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

the class XtendQuickfixProvider method removeUnusedPrivateMember.

@Fix(IssueCodes.UNUSED_PRIVATE_MEMBER)
public void removeUnusedPrivateMember(final Issue issue, IssueResolutionAcceptor acceptor) {
    // use the same label, description and image
    // to be able to use the quickfixes (issue resolution) in batch mode
    String label = "Remove member.";
    String description = "Remove the unused private member.";
    String image = "delete_edit.png";
    acceptor.accept(issue, label, description, image, new IModification() {

        @Override
        public void apply(IModificationContext context) throws Exception {
            final IXtextDocument document = context.getXtextDocument();
            document.modify(new IUnitOfWork.Void<XtextResource>() {

                @Override
                public void process(XtextResource state) throws Exception {
                    final EObject element = state.getEObject(issue.getUriToProblem().fragment());
                    final ICompositeNode elementNode = NodeModelUtils.findActualNodeFor(element);
                    final Position elementPosition = new Position(elementNode.getOffset(), elementNode.getLength());
                    document.addPosition(elementPosition);
                    if (element instanceof XtendField) {
                        final JvmField field = associations.getJvmField((XtendField) element);
                        final List<Position> assignments = EcoreUtil2.eAllContentsAsList(element.eResource()).stream().filter(XAssignment.class::isInstance).map(XAssignment.class::cast).filter(assignment -> field == assignment.getFeature()).map(assignment -> {
                            final ICompositeNode assignmentNode = NodeModelUtils.findActualNodeFor(assignment);
                            final int offset = assignmentNode.getOffset();
                            final XExpression assignmentValue = assignment.getValue();
                            int length = assignmentNode.getLength();
                            if (expressionHelper.hasSideEffects(assignmentValue)) {
                                length -= NodeModelUtils.findActualNodeFor(assignmentValue).getLength();
                            }
                            return new Position(offset, length);
                        }).collect(Collectors.toList());
                        for (final Position assignment : assignments) {
                            document.addPosition(assignment);
                        }
                        for (final Position assignment : assignments) {
                            document.replace(assignment.getOffset(), assignment.getLength(), "");
                        }
                    }
                    document.replace(elementPosition.getOffset(), elementPosition.getLength(), "");
                }
            });
            organizeImportsHandler.doOrganizeImports(document);
        }
    });
}
Also used : XtendField(org.eclipse.xtend.core.xtend.XtendField) ITextualMultiModification(org.eclipse.xtext.ui.editor.model.edit.ITextualMultiModification) CoreException(org.eclipse.core.runtime.CoreException) IssueResolutionAcceptor(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) OrganizeImportsHandler(org.eclipse.xtext.xbase.ui.imports.OrganizeImportsHandler) IRenameRefactoringProvider(org.eclipse.xtext.ui.refactoring.IRenameRefactoringProvider) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) PlatformUI(org.eclipse.ui.PlatformUI) Strings(org.eclipse.xtext.util.Strings) Set(java.util.Set) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IssueResolution(org.eclipse.xtext.ui.editor.quickfix.IssueResolution) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) RenameElementProcessor(org.eclipse.xtext.ui.refactoring.impl.RenameElementProcessor) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Singleton(com.google.inject.Singleton) Iterables(com.google.common.collect.Iterables) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) IJavaProjectProvider(org.eclipse.xtext.common.types.access.jdt.IJavaProjectProvider) URI(org.eclipse.emf.common.util.URI) InsertionOffsets(org.eclipse.xtend.ide.codebuilder.InsertionOffsets) StopWatch(org.eclipse.xtext.util.StopWatch) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Diagnostic(org.eclipse.xtext.diagnostics.Diagnostic) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) TypesPackage(org.eclipse.xtext.common.types.TypesPackage) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) Lists(com.google.common.collect.Lists) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) OverrideHelper(org.eclipse.xtext.xbase.typesystem.override.OverrideHelper) XbaseQuickfixProvider(org.eclipse.xtext.xbase.ui.quickfix.XbaseQuickfixProvider) IFile(org.eclipse.core.resources.IFile) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtendJvmAssociations(org.eclipse.xtend.core.jvmmodel.IXtendJvmAssociations) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) EReference(org.eclipse.emf.ecore.EReference) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendPackage(org.eclipse.xtend.core.xtend.XtendPackage) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XAssignment(org.eclipse.xtext.xbase.XAssignment) Junit5LibClasspathAdder(org.eclipse.xtend.ide.buildpath.Junit5LibClasspathAdder) Issue(org.eclipse.xtext.validation.Issue) TextSelection(org.eclipse.jface.text.TextSelection) PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) ResolvedFeatures(org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures) RefactoringCore(org.eclipse.ltk.core.refactoring.RefactoringCore) Inject(com.google.inject.Inject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IssueCodes(org.eclipse.xtend.core.validation.IssueCodes) Logger(org.apache.log4j.Logger) Junit4LibClasspathAdder(org.eclipse.xtend.ide.buildpath.Junit4LibClasspathAdder) IPath(org.eclipse.core.runtime.IPath) IRenameContextFactory(org.eclipse.xtext.ui.refactoring.ui.IRenameContextFactory) Change(org.eclipse.ltk.core.refactoring.Change) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) INode(org.eclipse.xtext.nodemodel.INode) Objects(com.google.common.base.Objects) Position(org.eclipse.jface.text.Position) IJavaProject(org.eclipse.jdt.core.IJavaProject) EObject(org.eclipse.emf.ecore.EObject) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) XtendGrammarAccess(org.eclipse.xtend.core.services.XtendGrammarAccess) Keyword(org.eclipse.xtext.Keyword) List(java.util.List) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) IBatchTypeResolver(org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver) DisplayRunHelper(org.eclipse.xtext.ui.util.DisplayRunHelper) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) TypeReferences(org.eclipse.xtext.common.types.util.TypeReferences) ProjectUtil(org.eclipse.xtext.ui.refactoring.impl.ProjectUtil) XtendLibClasspathAdder(org.eclipse.xtend.ide.buildpath.XtendLibClasspathAdder) XIfExpression(org.eclipse.xtext.xbase.XIfExpression) MemberFromSuperImplementor(org.eclipse.xtend.ide.codebuilder.MemberFromSuperImplementor) JvmType(org.eclipse.xtext.common.types.JvmType) IssueModificationContext(org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) XtextResource(org.eclipse.xtext.resource.XtextResource) ICompositeModification(org.eclipse.xtext.ui.editor.model.edit.ICompositeModification) XExpression(org.eclipse.xtext.xbase.XExpression) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable) JvmField(org.eclipse.xtext.common.types.JvmField) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) EList(org.eclipse.emf.common.util.EList) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITextRegion(org.eclipse.xtext.util.ITextRegion) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IResource(org.eclipse.core.resources.IResource) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix) XtendExpressionHelper(org.eclipse.xtend.core.typing.XtendExpressionHelper) Position(org.eclipse.jface.text.Position) XtextResource(org.eclipse.xtext.resource.XtextResource) 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) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XExpression(org.eclipse.xtext.xbase.XExpression) JvmField(org.eclipse.xtext.common.types.JvmField) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XAssignment(org.eclipse.xtext.xbase.XAssignment) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

IssueResolutionAcceptor (org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor)7 Issue (org.eclipse.xtext.validation.Issue)6 AbstractDeclarativeQuickfixProvider (org.eclipse.xtext.ui.editor.quickfix.AbstractDeclarativeQuickfixProvider)5 Test (org.junit.Test)5 IssueResolution (org.eclipse.xtext.ui.editor.quickfix.IssueResolution)4 Iterables (com.google.common.collect.Iterables)3 List (java.util.List)3 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)3 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)3 Fixes (org.eclipse.xtext.ui.editor.quickfix.Fixes)2 Assert (org.junit.Assert)2 CoreFix (com.avaloq.tools.ddk.check.runtime.quickfix.CoreFix)1 CoreIssueResolution (com.avaloq.tools.ddk.check.runtime.quickfix.CoreIssueResolution)1 ICoreIssueResolutionAcceptor (com.avaloq.tools.ddk.check.runtime.quickfix.ICoreIssueResolutionAcceptor)1 ICoreQuickfixProvider (com.avaloq.tools.ddk.check.runtime.quickfix.ICoreQuickfixProvider)1 Function (com.google.common.base.Function)1 Objects (com.google.common.base.Objects)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1