Search in sources :

Example 1 with ISemanticModification

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

the class CreateMemberQuickfixes method newLocalVariableQuickfix.

protected void newLocalVariableQuickfix(final String variableName, XAbstractFeatureCall call, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
    LightweightTypeReference variableType = getNewMemberType(call);
    final StringBuilderBasedAppendable localVarDescriptionBuilder = new StringBuilderBasedAppendable();
    localVarDescriptionBuilder.append("...").newLine();
    final String defaultValueLiteral = getDefaultValueLiteral(variableType);
    localVarDescriptionBuilder.append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
    localVarDescriptionBuilder.newLine().append("...");
    issueResolutionAcceptor.accept(issue, "Create local variable '" + variableName + "'", localVarDescriptionBuilder.toString(), "fix_local_var.png", new SemanticModificationWrapper(issue.getUriToProblem(), new ISemanticModification() {

        @Override
        public void apply(/* @Nullable */
        final EObject element, /* @Nullable */
        final IModificationContext context) throws Exception {
            if (element != null) {
                XtendMember xtendMember = EcoreUtil2.getContainerOfType(element, XtendMember.class);
                if (xtendMember != null) {
                    int offset = getFirstOffsetOfKeyword(xtendMember, "{");
                    IXtextDocument xtextDocument = context.getXtextDocument();
                    if (offset != -1 && xtextDocument != null) {
                        final ReplacingAppendable appendable = appendableFactory.create(xtextDocument, (XtextResource) element.eResource(), offset, 0, new OptionalParameters() {

                            {
                                baseIndentationLevel = 1;
                            }
                        });
                        appendable.increaseIndentation().newLine().append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
                        appendable.commitChanges();
                    }
                }
            }
        }
    }));
}
Also used : OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) SemanticModificationWrapper(org.eclipse.xtext.ui.editor.model.edit.SemanticModificationWrapper) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 2 with ISemanticModification

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

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

the class XtendQuickfixProvider method implementAbstractMethods.

@Fix(IssueCodes.CLASS_MUST_BE_ABSTRACT)
public void implementAbstractMethods(final Issue issue, IssueResolutionAcceptor acceptor) {
    doOverrideMethods(issue, acceptor, "Add unimplemented methods");
    acceptor.accept(issue, "Make class abstract", "Make class abstract", "fix_indent.gif", new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            internalDoAddAbstractKeyword(element, context);
        }
    });
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 4 with ISemanticModification

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

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

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