Search in sources :

Example 6 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method addThrowsDeclaration.

@Fix(org.eclipse.xtext.xbase.validation.IssueCodes.UNHANDLED_EXCEPTION)
public void addThrowsDeclaration(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null && issue.getData().length > 0)
        acceptor.accept(issue, "Add throws declaration", "Add throws declaration", "fix_indent.gif", new ISemanticModification() {

            @Override
            public void apply(EObject element, IModificationContext context) throws Exception {
                String[] issueData = issue.getData();
                XtendExecutable xtendExecutable = EcoreUtil2.getContainerOfType(element, XtendExecutable.class);
                XtextResource xtextResource = (XtextResource) xtendExecutable.eResource();
                List<JvmType> exceptions = getExceptions(issueData, xtextResource);
                if (exceptions.size() > 0) {
                    int insertPosition;
                    if (xtendExecutable.getExpression() == null) {
                        ICompositeNode functionNode = NodeModelUtils.findActualNodeFor(xtendExecutable);
                        if (functionNode == null)
                            throw new IllegalStateException("functionNode may not be null");
                        insertPosition = functionNode.getEndOffset();
                    } else {
                        ICompositeNode expressionNode = NodeModelUtils.findActualNodeFor(xtendExecutable.getExpression());
                        if (expressionNode == null)
                            throw new IllegalStateException("expressionNode may not be null");
                        insertPosition = expressionNode.getOffset();
                    }
                    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) xtendExecutable.eResource(), insertPosition, 0);
                    if (xtendExecutable.getExpression() == null)
                        appendable.append(" ");
                    EList<JvmTypeReference> thrownExceptions = xtendExecutable.getExceptions();
                    if (thrownExceptions.isEmpty())
                        appendable.append("throws ");
                    else
                        appendable.append(", ");
                    for (int i = 0; i < exceptions.size(); i++) {
                        appendable.append(exceptions.get(i));
                        if (i != exceptions.size() - 1) {
                            appendable.append(", ");
                        }
                    }
                    if (xtendExecutable.getExpression() != null)
                        appendable.append(" ");
                    appendable.commitChanges();
                }
            }
        });
}
Also used : ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) XtextResource(org.eclipse.xtext.resource.XtextResource) JvmType(org.eclipse.xtext.common.types.JvmType) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 7 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method surroundWithTryCatch.

@Fix(org.eclipse.xtext.xbase.validation.IssueCodes.UNHANDLED_EXCEPTION)
public void surroundWithTryCatch(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() == null || issue.getData().length <= 1) {
        return;
    }
    IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
    IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument == null) {
        return;
    }
    if (isJvmConstructorCall(xtextDocument, issue)) {
        return;
    }
    acceptor.accept(issue, "Surround with try/catch block", "Surround with try/catch block", "fix_indent.gif", new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            String[] issueData = issue.getData();
            URI childURI = URI.createURI(issueData[issueData.length - 1]);
            XtextResource xtextResource = (XtextResource) element.eResource();
            List<JvmType> exceptions = getExceptions(issueData, xtextResource);
            if (exceptions.size() > 0) {
                EObject childThrowingException = xtextResource.getResourceSet().getEObject(childURI, true);
                XExpression toBeSurrounded = findContainerExpressionInBlockExpression(childThrowingException);
                IXtextDocument xtextDocument = context.getXtextDocument();
                if (toBeSurrounded != null) {
                    ICompositeNode toBeSurroundedNode = NodeModelUtils.findActualNodeFor(toBeSurrounded);
                    if (toBeSurroundedNode == null)
                        throw new IllegalStateException("toBeSurroundedNode may not be null");
                    ITextRegion toBeSurroundedRegion = toBeSurroundedNode.getTextRegion();
                    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) childThrowingException.eResource(), toBeSurroundedRegion.getOffset(), toBeSurroundedRegion.getLength());
                    appendable.append("try {").increaseIndentation().newLine().append(xtextDocument.get(toBeSurroundedRegion.getOffset(), toBeSurroundedRegion.getLength())).decreaseIndentation().newLine();
                    for (JvmType exceptionType : exceptions) {
                        appendable.append("} catch (").append(exceptionType).append(" exc) {").increaseIndentation().newLine().append("throw new RuntimeException(\"auto-generated try/catch\", exc)").decreaseIndentation().newLine();
                    }
                    appendable.append("}");
                    appendable.commitChanges();
                }
            }
        }
    });
}
Also used : ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) JvmType(org.eclipse.xtext.common.types.JvmType) URI(org.eclipse.emf.common.util.URI) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 8 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable 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 9 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable 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)

Example 10 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.

the class ImplementSuperMemberAssistTest method testExistingStaticImport.

@Test
public void testExistingStaticImport() throws Exception {
    ICompletionProposal[] proposals = newBuilder().append("import static java.util.Collection.*" + "class SpecialList extends java.util.ArrayList { removeAll").computeCompletionProposals();
    assertEquals(1, proposals.length);
    ImportOrganizingProposal proposal = (ImportOrganizingProposal) proposals[0];
    ReplacingAppendable appendable = proposal.getAppendable();
    RewritableImportSection importSection = appendable.getImportSection();
    List<ReplaceRegion> imports = importSection.rewrite();
    assertEquals(1, imports.size());
    assertEquals("import java.util.Collection", imports.get(0).getText().trim());
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) ImportOrganizingProposal(org.eclipse.xtext.xbase.ui.contentassist.ImportOrganizingProposal) RewritableImportSection(org.eclipse.xtext.xbase.imports.RewritableImportSection) Test(org.junit.Test)

Aggregations

ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)11 XtextResource (org.eclipse.xtext.resource.XtextResource)8 EObject (org.eclipse.emf.ecore.EObject)7 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)6 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)6 CoreException (org.eclipse.core.runtime.CoreException)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)4 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)4 URI (org.eclipse.emf.common.util.URI)3 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)2 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)2 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)2 JvmType (org.eclipse.xtext.common.types.JvmType)2 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)2 ResolvedFeatures (org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2