Search in sources :

Example 6 with IModificationContext

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

the class CodeBuilderQuickfix method getXtendModification.

protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
    final IModification _function = (IModificationContext it) -> {
        final XtendTypeDeclaration xtendClass = builder.getXtendType();
        final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
        if ((!(editor instanceof XtextEditor))) {
            return;
        }
        final XtextEditor xtextEditor = ((XtextEditor) editor);
        final IXtextDocument document = xtextEditor.getDocument();
        final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
        final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
            ReplacingAppendable _xblockexpression = null;
            {
                int offset = builder.getInsertOffset(resource);
                wrapper.set(Integer.valueOf(offset));
                final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
                DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
                final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
                    int _indentationLevel = builder.getIndentationLevel();
                    int _plus = (_indentationLevel + typeIndentation);
                    it_1.baseIndentationLevel = _plus;
                    it_1.ensureEmptyLinesAround = true;
                };
                DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
                _xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
            }
            return _xblockexpression;
        };
        final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
        Integer offset = wrapper.get();
        builder.build(appendable);
        appendable.commitChanges();
        xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
    };
    return _function;
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) IEditorPart(org.eclipse.ui.IEditorPart) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) DocumentSourceAppender(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 7 with IModificationContext

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

the class CodeBuilderQuickfix method getJavaModification.

protected IModification getJavaModification(final ICodeBuilder.Java builder) {
    final IModification _function = (IModificationContext it) -> {
        final IType type = builder.getIType();
        char _charAt = ".".charAt(0);
        final ImportManager importManager = new ImportManager(true, _charAt);
        final StringBuilderBasedAppendable content = new StringBuilderBasedAppendable(importManager);
        builder.build(content);
        final Consumer<String> _function_1 = (String it_1) -> {
            try {
                ICompilationUnit _compilationUnit = type.getCompilationUnit();
                NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                _compilationUnit.createImport(it_1, null, _nullProgressMonitor);
            } catch (Throwable _e) {
                throw Exceptions.sneakyThrow(_e);
            }
        };
        importManager.getImports().forEach(_function_1);
        Object _switchResult = null;
        boolean _matched = false;
        if (builder instanceof JavaFieldBuilder) {
            _matched = true;
            String _string = content.toString();
            NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
            _switchResult = type.createField(_string, null, true, _nullProgressMonitor);
        }
        if (!_matched) {
            if (builder instanceof JavaConstructorBuilder) {
                _matched = true;
                String _string = content.toString();
                NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                _switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
            }
        }
        if (!_matched) {
            if (builder instanceof JavaMethodBuilder) {
                _matched = true;
                String _string = content.toString();
                NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                _switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
            }
        }
        if (!_matched) {
            _switchResult = null;
        }
        final Object element = ((Object) _switchResult);
        if ((element != null)) {
            JdtHyperlink _jdtHyperlink = new JdtHyperlink();
            final Procedure1<JdtHyperlink> _function_2 = (JdtHyperlink it_1) -> {
                it_1.setJavaElement(((IMember) element));
                it_1.open();
            };
            ObjectExtensions.<JdtHyperlink>operator_doubleArrow(_jdtHyperlink, _function_2);
        }
    };
    return _function;
}
Also used : ImportManager(org.eclipse.xtext.xbase.compiler.ImportManager) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaMethodBuilder(org.eclipse.xtend.ide.codebuilder.JavaMethodBuilder) JavaConstructorBuilder(org.eclipse.xtend.ide.codebuilder.JavaConstructorBuilder) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) Consumer(java.util.function.Consumer) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) EObject(org.eclipse.emf.ecore.EObject) JavaFieldBuilder(org.eclipse.xtend.ide.codebuilder.JavaFieldBuilder) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification)

Example 8 with IModificationContext

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

the class CreateXtendTypeQuickfixes method newXtendAnnotationQuickfix.

protected void newXtendAnnotationQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
    String packageDescription = getPackageDescription(explicitPackage);
    issueResolutionAcceptor.accept(issue, "Create Xtend annotation '" + typeName + "'" + packageDescription, "Opens the new Xtend annotation wizard to create the type '" + typeName + "'" + packageDescription, "xtend_file.png", new IModification() {

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

                @Override
                public void run() {
                    NewElementWizard newXtendAnnotationWizard = newXtendAnnotationWizardProvider.get();
                    WizardDialog dialog = createWizardDialog(newXtendAnnotationWizard);
                    NewXtendAnnotationWizardPage page = (NewXtendAnnotationWizardPage) newXtendAnnotationWizard.getStartingPage();
                    configureWizardPage(page, resource.getURI(), typeName, explicitPackage);
                    dialog.open();
                }
            });
        }
    });
}
Also used : NewElementWizard(org.eclipse.jdt.internal.ui.wizards.NewElementWizard) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) NewXtendAnnotationWizardPage(org.eclipse.xtend.ide.wizards.NewXtendAnnotationWizardPage) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification)

Example 9 with IModificationContext

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

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

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