Search in sources :

Example 1 with DefaultIndentationHandler

use of org.eclipse.xtend.core.richstring.DefaultIndentationHandler 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 DefaultIndentationHandler

use of org.eclipse.xtend.core.richstring.DefaultIndentationHandler in project xtext-xtend by eclipse.

the class RichStringEvaluationTest method assertOutput.

@Override
public void assertOutput(String expectedOutput, String richString) throws Exception {
    RichString parsedString = richString(richString);
    StringBuilderBasedAcceptor acceptor = new StringBuilderBasedAcceptor();
    DefaultIndentationHandler handler = new DefaultIndentationHandler();
    RichStringProcessor processor = new RichStringProcessor();
    processor.process(parsedString, acceptor, handler);
    String actualOutput = acceptor.toString();
    assertEquals(richString, expectedOutput, actualOutput);
}
Also used : DefaultIndentationHandler(org.eclipse.xtend.core.richstring.DefaultIndentationHandler) RichStringProcessor(org.eclipse.xtend.core.richstring.RichStringProcessor) RichString(org.eclipse.xtend.core.xtend.RichString) RichString(org.eclipse.xtend.core.xtend.RichString)

Example 3 with DefaultIndentationHandler

use of org.eclipse.xtend.core.richstring.DefaultIndentationHandler in project xtext-xtend by eclipse.

the class RichStringProcessorTest method recordRichStringProcessorEvents.

protected String recordRichStringProcessorEvents(String string) throws Exception {
    RichString richString = richString(string);
    RichStringProcessor processor = new RichStringProcessor();
    RecordingRichStringPartAcceptor acceptor = new RecordingRichStringPartAcceptor();
    processor.process(richString, acceptor, new DefaultIndentationHandler());
    return acceptor.toString();
}
Also used : DefaultIndentationHandler(org.eclipse.xtend.core.richstring.DefaultIndentationHandler) RichStringProcessor(org.eclipse.xtend.core.richstring.RichStringProcessor) RichString(org.eclipse.xtend.core.xtend.RichString)

Aggregations

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