Search in sources :

Example 6 with ISemanticModification

use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification 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 7 with ISemanticModification

use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification 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 8 with ISemanticModification

use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification 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

EObject (org.eclipse.emf.ecore.EObject)8 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)8 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)8 CoreException (org.eclipse.core.runtime.CoreException)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)6 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)6 XtextResource (org.eclipse.xtext.resource.XtextResource)5 URI (org.eclipse.emf.common.util.URI)3 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)3 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)3 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)2 JvmType (org.eclipse.xtext.common.types.JvmType)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 ResolvedFeatures (org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EList (org.eclipse.emf.common.util.EList)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1