Search in sources :

Example 16 with ReplacingAppendable

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

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

the class XbaseQuickfixProvider method fixRedundantCase.

@Fix(IssueCodes.REDUNDANT_CASE)
public void fixRedundantCase(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove redundant case.", "Remove redundant case.", null, new ReplaceModification(issue, ""));
    acceptor.accept(issue, "Assign empty expression.", "Assign empty expression.", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
            if (switchExpression == null) {
                return;
            }
            XCasePart casePart = IterableExtensions.last(switchExpression.getCases());
            if (casePart == null || !(casePart.isFallThrough() && casePart.getThen() == null)) {
                return;
            }
            List<INode> nodes = NodeModelUtils.findNodesForFeature(casePart, XbasePackage.Literals.XCASE_PART__FALL_THROUGH);
            if (nodes.isEmpty()) {
                return;
            }
            INode firstNode = IterableExtensions.head(nodes);
            INode lastNode = IterableExtensions.last(nodes);
            int offset = firstNode.getOffset();
            int length = lastNode.getEndOffset() - offset;
            ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), offset, length);
            appendable.append(": {");
            appendable.increaseIndentation().newLine();
            appendable.decreaseIndentation().newLine().append("}");
            appendable.commitChanges();
        }
    });
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceModification(org.eclipse.xtext.ui.editor.quickfix.ReplaceModification) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XCasePart(org.eclipse.xtext.xbase.XCasePart) XSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

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