Search in sources :

Example 11 with IModificationContext

use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method fixPackageName.

@Fix(IssueCodes.WRONG_PACKAGE)
public void fixPackageName(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null && issue.getData().length == 1) {
        final String expectedPackage = issue.getData()[0];
        acceptor.accept(issue, "Change package declaration to '" + expectedPackage + "'", "Change package declaration to '" + expectedPackage + "'", "package_obj.gif", new ISemanticModification() {

            @Override
            public void apply(EObject element, IModificationContext context) throws Exception {
                XtendFile file = (XtendFile) element;
                String newPackageName = isEmpty(expectedPackage) ? null : expectedPackage;
                String oldPackageName = file.getPackage();
                for (EObject obj : file.eResource().getContents()) {
                    if (obj instanceof JvmDeclaredType) {
                        JvmDeclaredType type = (JvmDeclaredType) obj;
                        String typePackage = type.getPackageName();
                        if (Objects.equal(typePackage, oldPackageName) || typePackage != null && typePackage.startsWith(oldPackageName + ".")) {
                            type.internalSetIdentifier(null);
                            type.setPackageName(newPackageName);
                        }
                    }
                }
                file.setPackage(newPackageName);
            }
        });
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 12 with IModificationContext

use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method fixFileName.

@Fix(IssueCodes.WRONG_FILE)
public void fixFileName(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null && issue.getData().length == 1) {
        final String expectedFileName = issue.getData()[0];
        final IFile iFile = projectUtil.findFileStorage(issue.getUriToProblem(), true);
        final IPath pathToMoveTo = iFile.getParent().getFullPath().append(expectedFileName).addFileExtension(iFile.getFileExtension());
        if (!iFile.getWorkspace().getRoot().exists(pathToMoveTo)) {
            final String label = "Rename file to '" + expectedFileName + ".xtend'";
            acceptor.accept(issue, label, label, "xtend_file.png", new IModification() {

                @Override
                public void apply(IModificationContext context) throws Exception {
                    runAsyncInDisplayThread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                iFile.move(pathToMoveTo, IResource.KEEP_HISTORY, null);
                            } catch (CoreException e) {
                                logger.error(e);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 13 with IModificationContext

use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method doOverrideMethods.

protected void doOverrideMethods(final Issue issue, IssueResolutionAcceptor acceptor, String label, final String[] operationUris) {
    acceptor.accept(issue, label, label, "fix_indent.gif", new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XtendTypeDeclaration clazz = (XtendTypeDeclaration) element;
            JvmGenericType inferredType = (JvmGenericType) associations.getInferredType(clazz);
            ResolvedFeatures resolvedOperations = overrideHelper.getResolvedFeatures(inferredType);
            IXtextDocument document = context.getXtextDocument();
            final int offset = insertionOffsets.getNewMethodInsertOffset(null, clazz);
            int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, (XtextResource) clazz.eResource());
            final int indentationToUse = clazz.getMembers().isEmpty() ? currentIndentation + 1 : currentIndentation;
            ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) clazz.eResource(), offset, 0, new OptionalParameters() {

                {
                    ensureEmptyLinesAround = true;
                    baseIndentationLevel = indentationToUse;
                }
            });
            boolean isFirst = true;
            for (String operationUriAsString : operationUris) {
                URI operationURI = URI.createURI(operationUriAsString);
                EObject overridden = clazz.eResource().getResourceSet().getEObject(operationURI, true);
                if (overridden instanceof JvmOperation) {
                    if (!isFirst)
                        appendable.newLine().newLine();
                    isFirst = false;
                    superMemberImplementor.appendOverrideFunction(clazz, resolvedOperations.getResolvedOperation((JvmOperation) overridden), appendable);
                }
            }
            appendable.commitChanges();
        }
    });
}
Also used : ResolvedFeatures(org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) URI(org.eclipse.emf.common.util.URI) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 14 with IModificationContext

use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method addConstuctorFromSuper.

@Fix(IssueCodes.MISSING_CONSTRUCTOR)
public void addConstuctorFromSuper(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null) {
        for (int i = 0; i < issue.getData().length; i += 2) {
            final URI constructorURI = URI.createURI(issue.getData()[i]);
            String javaSignature = issue.getData()[i + 1];
            String xtendSignature = "new" + javaSignature.substring(javaSignature.indexOf('('));
            acceptor.accept(issue, "Add constructor " + xtendSignature, "Add constructor " + xtendSignature, "fix_indent.gif", new ISemanticModification() {

                @Override
                public void apply(EObject element, IModificationContext context) throws Exception {
                    XtendClass clazz = (XtendClass) element;
                    JvmGenericType inferredType = associations.getInferredType(clazz);
                    ResolvedFeatures features = overrideHelper.getResolvedFeatures(inferredType);
                    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) clazz.eResource(), insertionOffsets.getNewConstructorInsertOffset(null, clazz), 0, new OptionalParameters() {

                        {
                            ensureEmptyLinesAround = true;
                            baseIndentationLevel = 1;
                        }
                    });
                    EObject constructor = clazz.eResource().getResourceSet().getEObject(constructorURI, true);
                    if (constructor instanceof JvmConstructor) {
                        superMemberImplementor.appendConstructorFromSuper(clazz, new ResolvedConstructor((JvmConstructor) constructor, features.getType()), appendable);
                    }
                    appendable.commitChanges();
                }
            });
        }
    }
}
Also used : ResolvedFeatures(org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) URI(org.eclipse.emf.common.util.URI) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)14 EObject (org.eclipse.emf.ecore.EObject)9 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)8 CoreException (org.eclipse.core.runtime.CoreException)7 BadLocationException (org.eclipse.jface.text.BadLocationException)7 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)7 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)7 XtextResource (org.eclipse.xtext.resource.XtextResource)6 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)6 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)4 URI (org.eclipse.emf.common.util.URI)3 NewElementWizard (org.eclipse.jdt.internal.ui.wizards.NewElementWizard)3 WizardDialog (org.eclipse.jface.wizard.WizardDialog)3 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)3 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)2 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)2 JvmType (org.eclipse.xtext.common.types.JvmType)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)2 ResolvedFeatures (org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures)2