Search in sources :

Example 1 with IFormattableDocument

use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-xtend by eclipse.

the class RichStringFormatter method setNewLines.

protected void setNewLines(final IFormattableDocument doc, final int offset, final int length, final int indentationIncrease, final int indentationDecrease, final int newLines) {
    IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
    final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
        it.setIndentationIncrease(Integer.valueOf(indentationIncrease));
        it.setIndentationDecrease(Integer.valueOf(indentationDecrease));
        it.setNewLinesMin(Integer.valueOf(newLines));
        it.setNewLinesDefault(Integer.valueOf(newLines));
        it.setNewLinesMax(Integer.valueOf(newLines));
    };
    final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
    AbstractFormatter2 _formatter = doc.getFormatter();
    ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
    TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
    final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
    doc.addReplacer(replacer);
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)

Example 2 with IFormattableDocument

use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-xtend by eclipse.

the class RichStringFormatter method _format.

protected void _format(final RichString richString, final IFormattableDocument doc) {
    RichString _containerOfType = EcoreUtil2.<RichString>getContainerOfType(richString.eContainer(), RichString.class);
    boolean _tripleNotEquals = (_containerOfType != null);
    if (_tripleNotEquals) {
        return;
    }
    boolean _hasSyntaxError = this.hasSyntaxError(this._iTextRegionExtensions.regionForEObject(richString));
    if (_hasSyntaxError) {
        return;
    }
    ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
    final RichStringToLineModel impl = new RichStringToLineModel(_textRegionAccess, richString);
    DefaultIndentationHandler _defaultIndentationHandler = new DefaultIndentationHandler();
    this.factory.richStringProcessor.process(richString, impl, _defaultIndentationHandler);
    impl.finish();
    EList<XExpression> _expressions = richString.getExpressions();
    for (final XExpression e : _expressions) {
        this.format(e, doc);
    }
    final List<Line> lines = impl.getModel().getLines();
    final boolean canIndent = ((!lines.isEmpty()) && StringExtensions.isNullOrEmpty(IterableExtensions.<Line>last(lines).getContent()));
    for (final Line line : lines) {
        int _rootIndentLenght = impl.getModel().getRootIndentLenght();
        boolean _greaterThan = (_rootIndentLenght > 0);
        if (_greaterThan) {
            int _xifexpression = (int) 0;
            if ((canIndent && Objects.equal(line, IterableExtensions.<Line>head(lines)))) {
                _xifexpression = 1;
            } else {
                _xifexpression = 0;
            }
            final int increaseIndentationChange = _xifexpression;
            int _xifexpression_1 = (int) 0;
            if ((canIndent && Objects.equal(line, IterableExtensions.<Line>last(lines)))) {
                _xifexpression_1 = 1;
            } else {
                _xifexpression_1 = 0;
            }
            final int decraseIndentationChange = _xifexpression_1;
            int _xifexpression_2 = (int) 0;
            boolean _isLeadingSemanticNewLine = line.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine) {
                int _offset = line.getOffset();
                int _newLineCharCount = line.getNewLineCharCount();
                _xifexpression_2 = (_offset + _newLineCharCount);
            } else {
                _xifexpression_2 = line.getOffset();
            }
            final int nloffset = _xifexpression_2;
            final int i = Math.min(line.getIndentLength(), impl.getModel().getRootIndentLenght());
            int _xifexpression_3 = (int) 0;
            boolean _isLeadingSemanticNewLine_1 = line.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine_1) {
                _xifexpression_3 = i;
            } else {
                int _newLineCharCount_1 = line.getNewLineCharCount();
                _xifexpression_3 = (_newLineCharCount_1 + i);
            }
            final int nllength = _xifexpression_3;
            boolean _isLeadingSemanticNewLine_2 = line.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine_2) {
                this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 0);
            } else {
                this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 1);
            }
            boolean _isEmpty = line.getChunks().isEmpty();
            boolean _not = (!_isEmpty);
            if (_not) {
                final int offset = (nloffset + nllength);
                int _indentLength = line.getIndentLength();
                int _rootIndentLenght_1 = impl.getModel().getRootIndentLenght();
                final int length = (_indentLength - _rootIndentLenght_1);
                final Function1<Chunk, CharSequence> _function = (Chunk chunk) -> {
                    CharSequence _switchResult = null;
                    boolean _matched = false;
                    if (chunk instanceof SemanticWhitespace) {
                        _matched = true;
                        _switchResult = ((SemanticWhitespace) chunk).getText();
                    }
                    if (!_matched) {
                        if (chunk instanceof TemplateWhitespace) {
                            _matched = true;
                            _switchResult = doc.getFormatter().<String>getPreference(FormatterPreferenceKeys.indentation);
                        }
                    }
                    return _switchResult;
                };
                final String text = IterableExtensions.join(ListExtensions.<Chunk, CharSequence>map(line.getChunks(), _function));
                this.setSpace(doc, offset, length, text);
            }
        }
    }
}
Also used : RichStringToLineModel(org.eclipse.xtend.core.formatting2.RichStringToLineModel) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) RichString(org.eclipse.xtend.core.xtend.RichString) TemplateWhitespace(org.eclipse.xtend.core.formatting2.TemplateWhitespace) RichString(org.eclipse.xtend.core.xtend.RichString) Chunk(org.eclipse.xtend.core.formatting2.Chunk) DefaultIndentationHandler(org.eclipse.xtend.core.richstring.DefaultIndentationHandler) Line(org.eclipse.xtend.core.formatting2.Line) XExpression(org.eclipse.xtext.xbase.XExpression) SemanticWhitespace(org.eclipse.xtend.core.formatting2.SemanticWhitespace)

Example 3 with IFormattableDocument

use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-xtend by eclipse.

the class XtendFormatter method _format.

protected void _format(final XtendEnum enumeration, @Extension final IFormattableDocument format) {
    this.formatAnnotations(enumeration, format, XbaseFormatterPreferenceKeys.newLineAfterClassAnnotations);
    this.formatModifiers(enumeration, format);
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    format.append(this.textRegionExtensions.regionFor(enumeration).keyword("enum"), _function);
    final ISemanticRegion open = this.textRegionExtensions.regionFor(enumeration).keyword("{");
    final ISemanticRegion close = this.textRegionExtensions.regionFor(enumeration).keyword("}");
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.indent();
    };
    format.<ISemanticRegion, ISemanticRegion>interior(open, close, _function_1);
    format.prepend(open, XbaseFormatterPreferenceKeys.bracesInNewLine);
    boolean _isEmpty = enumeration.getMembers().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
        format.append(open, XtendFormatterPreferenceKeys.blankLinesBeforeFirstMember);
        int _size = enumeration.getMembers().size();
        int _minus = (_size - 1);
        IntegerRange _upTo = new IntegerRange(0, _minus);
        for (final Integer i : _upTo) {
            {
                final XtendMember current = enumeration.getMembers().get((i).intValue());
                format.<XtendMember>format(current);
                int _size_1 = enumeration.getMembers().size();
                int _minus_1 = (_size_1 - 1);
                boolean _lessThan = ((i).intValue() < _minus_1);
                if (_lessThan) {
                    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
                        it.noSpace();
                    };
                    format.append(format.prepend(this.textRegionExtensions.immediatelyFollowing(current).keyword(","), _function_2), XtendFormatterPreferenceKeys.blankLinesBetweenEnumLiterals);
                } else {
                    format.<XtendMember>append(current, XtendFormatterPreferenceKeys.blankLinesAfterLastMember);
                }
            }
        }
    } else {
        final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
            it.newLine();
        };
        format.append(open, _function_2);
    }
}
Also used : IntegerRange(org.eclipse.xtext.xbase.lib.IntegerRange) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 4 with IFormattableDocument

use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-xtend by eclipse.

the class XtendFormatter method _format.

protected void _format(final XtendField field, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    document.append(this.textRegionExtensions.regionFor(field).keyword("extension"), _function);
    this.formatAnnotations(field, document, XbaseFormatterPreferenceKeys.newLineAfterFieldAnnotations);
    this.formatModifiers(field, document);
    String _name = field.getName();
    boolean _tripleNotEquals = (_name != null);
    if (_tripleNotEquals) {
        final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
            it.oneSpace();
        };
        document.<JvmTypeReference>append(field.getType(), _function_1);
    }
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    final Procedure1<IHiddenRegionFormatter> _function_3 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    document.append(document.prepend(this.textRegionExtensions.regionFor(field).keyword("="), _function_2), _function_3);
    document.<JvmTypeReference>format(field.getType());
    document.<XExpression>format(field.getInitialValue());
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString)

Example 5 with IFormattableDocument

use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-xtend by eclipse.

the class XtendFormatter method _format.

protected void _format(final XtendInterface interfaze, @Extension final IFormattableDocument format) {
    this.formatAnnotations(interfaze, format, XbaseFormatterPreferenceKeys.newLineAfterClassAnnotations);
    this.formatModifiers(interfaze, format);
    this.formatTypeParameters(interfaze, interfaze.getTypeParameters(), format);
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    format.append(this.textRegionExtensions.regionFor(interfaze).keyword("interface"), _function);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    format.surround(this.textRegionExtensions.regionFor(interfaze).keyword("extends"), _function_1);
    EList<JvmTypeReference> _extends = interfaze.getExtends();
    for (final JvmTypeReference imp : _extends) {
        {
            final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
                it.noSpace();
            };
            final Procedure1<IHiddenRegionFormatter> _function_3 = (IHiddenRegionFormatter it) -> {
                it.oneSpace();
            };
            format.append(format.prepend(this.textRegionExtensions.immediatelyFollowing(imp).keyword(","), _function_2), _function_3);
            format.<JvmTypeReference>format(imp);
        }
    }
    this.formatBody(interfaze, format);
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1)

Aggregations

IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)34 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)28 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)22 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)22 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)22 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)22 Test (org.junit.Test)22 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)20 IDList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList)13 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)12 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)8 KWList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.KWList)7 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)5 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)4 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)4 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)4 XExpression (org.eclipse.xtext.xbase.XExpression)4 Extension (org.eclipse.xtext.xbase.lib.Extension)4 Collection (java.util.Collection)3 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)3