Search in sources :

Example 1 with Line

use of org.eclipse.xtend.core.formatting2.Line 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 2 with Line

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

the class RichStringToLineModel method acceptLineBreak.

public void acceptLineBreak(final int charCount, final boolean semantic, final boolean startNewLine) {
    this.startContent();
    if ((this.contentStartOffset > 0)) {
        final String lastLinesContent = this.document.substring(this.contentStartOffset, this.offset);
        boolean _isEmpty = this.model.getLines().isEmpty();
        if (_isEmpty) {
            this.model.setLeadingText(lastLinesContent);
            this.contentStartColumn = 0;
        } else {
            final Line lastLine = IterableExtensions.<Line>last(this.model.getLines());
            lastLine.setContent(lastLinesContent);
            int _offset = lastLine.getOffset();
            int _newLineCharCount = lastLine.getNewLineCharCount();
            int _plus = (_offset + _newLineCharCount);
            final int newContentStartColumn = (this.contentStartOffset - _plus);
            boolean _isLeadingSemanticNewLine = lastLine.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine) {
                if ((newContentStartColumn > this.contentStartColumn)) {
                    final int length = (newContentStartColumn - this.contentStartColumn);
                    final String text = this.document.substring((this.contentStartOffset - length), this.contentStartOffset);
                    SemanticWhitespace _semanticWhitespace = new SemanticWhitespace(text, newContentStartColumn);
                    this.indentationStack.push(_semanticWhitespace);
                }
            }
            if ((newContentStartColumn < this.contentStartColumn)) {
                List<SemanticWhitespace> _list = IterableExtensions.<SemanticWhitespace>toList(Iterables.<SemanticWhitespace>filter(this.indentationStack, SemanticWhitespace.class));
                for (final SemanticWhitespace ws : _list) {
                    int _column = ws.getColumn();
                    boolean _greaterThan = (_column > newContentStartColumn);
                    if (_greaterThan) {
                        this.indentationStack.remove(ws);
                    }
                }
                final Chunk lastWs = IterableExtensions.<Chunk>last(this.indentationStack);
                int _xifexpression = (int) 0;
                if ((lastWs == null)) {
                    int _rootIndentLenght = this.model.getRootIndentLenght();
                    _xifexpression = (newContentStartColumn - _rootIndentLenght);
                } else {
                    int _xifexpression_1 = (int) 0;
                    if ((lastWs instanceof SemanticWhitespace)) {
                        int _column_1 = ((SemanticWhitespace) lastWs).getColumn();
                        _xifexpression_1 = (newContentStartColumn - _column_1);
                    } else {
                        _xifexpression_1 = 0;
                    }
                    _xifexpression = _xifexpression_1;
                }
                final int length_1 = _xifexpression;
                if ((length_1 > 0)) {
                    final String text_1 = this.document.substring((this.contentStartOffset - length_1), this.contentStartOffset);
                    SemanticWhitespace _semanticWhitespace_1 = new SemanticWhitespace(text_1, newContentStartColumn);
                    this.indentationStack.push(_semanticWhitespace_1);
                }
            }
            if (this._outdentThisLine) {
                boolean _empty = this.indentationStack.empty();
                boolean _not = (!_empty);
                if (_not) {
                    this.indentationStack.pop();
                }
                this._outdentThisLine = false;
            }
            lastLine.setIndentLength(newContentStartColumn);
            if ((newContentStartColumn != 0)) {
                this.contentStartColumn = newContentStartColumn;
            }
            List<Chunk> _chunks = IterableExtensions.<Line>last(this.model.getLines()).getChunks();
            Iterables.<Chunk>addAll(_chunks, this.indentationStack);
        }
    }
    if (this.indentNextLine) {
        TemplateWhitespace _templateWhitespace = new TemplateWhitespace("");
        this.indentationStack.push(_templateWhitespace);
        this.indentNextLine = false;
    }
    this.contentStartOffset = (-1);
    this.content = false;
    if (startNewLine) {
        List<Line> _lines = this.model.getLines();
        Line _line = new Line(this.offset, semantic, charCount);
        _lines.add(_line);
    }
}
Also used : Line(org.eclipse.xtend.core.formatting2.Line) TemplateWhitespace(org.eclipse.xtend.core.formatting2.TemplateWhitespace) RichString(org.eclipse.xtend.core.xtend.RichString) Chunk(org.eclipse.xtend.core.formatting2.Chunk) SemanticWhitespace(org.eclipse.xtend.core.formatting2.SemanticWhitespace)

Aggregations

Chunk (org.eclipse.xtend.core.formatting2.Chunk)2 Line (org.eclipse.xtend.core.formatting2.Line)2 SemanticWhitespace (org.eclipse.xtend.core.formatting2.SemanticWhitespace)2 TemplateWhitespace (org.eclipse.xtend.core.formatting2.TemplateWhitespace)2 RichString (org.eclipse.xtend.core.xtend.RichString)2 RichStringToLineModel (org.eclipse.xtend.core.formatting2.RichStringToLineModel)1 DefaultIndentationHandler (org.eclipse.xtend.core.richstring.DefaultIndentationHandler)1 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)1 XExpression (org.eclipse.xtext.xbase.XExpression)1