Search in sources :

Example 1 with RichString

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

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

the class InitialTemplateIndentationComputer method caseRichString.

@Override
public String caseRichString(RichString object) {
    String result = null;
    List<XExpression> elements = object.getExpressions();
    for (int i = 0; i < elements.size(); ) {
        XExpression element = elements.get(i);
        String elementResult = null;
        int nextIndex = i + 1;
        if (element instanceof RichStringLiteral) {
            RichStringLiteral literal = (RichStringLiteral) element;
            if (nextIndex == elements.size()) {
                // last one
                elementResult = getLeadingWhitespace(literal.getValue(), literal);
            } else if (!(elements.get(nextIndex) instanceof RichStringLiteral)) {
                elementResult = getLeadingWhitespace(literal.getValue(), literal);
            } else {
                StringBuilder run = new StringBuilder(Strings.emptyIfNull(literal.getValue()));
                RichStringLiteral next = null;
                do {
                    next = (RichStringLiteral) elements.get(nextIndex);
                    run.append(next.getValue());
                    nextIndex++;
                } while (nextIndex < elements.size() && elements.get(nextIndex) instanceof RichStringLiteral);
                elementResult = getLeadingWhitespace(run.toString(), next);
            }
        }
        if (elementResult == null && i == 0)
            return initial;
        result = getBetterString(result, elementResult);
        if (Strings.isEmpty(result))
            return result;
        i = nextIndex;
    }
    return result;
}
Also used : RichStringLiteral(org.eclipse.xtend.core.xtend.RichStringLiteral) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString)

Example 3 with RichString

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

the class InitialTemplateIndentationComputer method getLeadingWhitespace.

private String getLeadingWhitespace(String value, RichStringLiteral lastLiteral) {
    List<TextLine> lines = TextLines.splitString(value);
    // no line breaks or immediately closed string literal => no initial indentation
    if (lines.size() <= 1) {
        return null;
    }
    TextLine firstLine = lines.get(0);
    // first line has content == no initial indentation
    if (!firstLine.containsOnlyWhitespace()) {
        return null;
    }
    String result = null;
    boolean emptyLineSeen = false;
    for (int i = 1; i < lines.size(); i++) {
        TextLine line = lines.get(i);
        CharSequence leadingWS = line.getLeadingWhiteSpace();
        // line is not empty
        if (leadingWS.length() != line.length()) {
            if (leadingWS.length() == 0)
                return "";
            result = getBetterString(result, leadingWS.toString());
        } else {
            // some tools tend to right trim text files by default (e.g. git)
            // that's why we ignore empty lines
            RichString completeString = (RichString) lastLiteral.eContainer();
            List<XExpression> siblings = completeString.getExpressions();
            if (siblings.get(siblings.size() - 1) != lastLiteral) {
                if (leadingWS.length() == 0) {
                    // empty line
                    emptyLineSeen = true;
                } else {
                    result = getBetterString(result, leadingWS.toString());
                }
            }
        }
    }
    if (emptyLineSeen && result == null)
        return "";
    return result;
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString)

Example 4 with RichString

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

the class XtendHighlightingCalculator method highlightRichStrings.

protected void highlightRichStrings(XExpression expression, IHighlightedPositionAcceptor acceptor) {
    if (expression != null) {
        TreeIterator<EObject> iterator = EcoreUtil2.eAll(expression);
        while (iterator.hasNext()) {
            EObject object = iterator.next();
            if (object instanceof RichString) {
                RichStringHighlighter highlighter = createRichStringHighlighter(acceptor);
                processor.process((RichString) object, highlighter, indentationHandlerProvider.get());
                iterator.prune();
            }
        }
    }
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) EObject(org.eclipse.emf.ecore.EObject)

Example 5 with RichString

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

the class XtendCompiler method _toJavaStatement.

public void _toJavaStatement(RichString richString, ITreeAppendable b, boolean isReferenced) {
    LightweightTypeReference actualType = getLightweightType(richString);
    b = b.trace(richString);
    if (actualType.isType(StringConcatenationClient.class)) {
        String resultVariableName = b.declareSyntheticVariable(richString, "_client");
        b.newLine();
        b.append(actualType);
        b.append(" ");
        b.append(resultVariableName);
        b.append(" = new ");
        b.append(actualType);
        b.append("() {");
        b.openScope();
        reassignThisInClosure(b, actualType.getType());
        b.increaseIndentation().newLine();
        b.append("@");
        b.append(Override.class);
        b.newLine().append("protected void appendTo(");
        b.append(StringConcatenationClient.TargetStringConcatenation.class);
        String variableName = b.declareSyntheticVariable(richString, "_builder");
        b.append(" ").append(variableName).append(") {");
        b.increaseIndentation();
        RichStringPrepareCompiler compiler = new RichStringPrepareCompiler(b, variableName, richString);
        richStringProcessor.process(richString, compiler, indentationHandler.get());
        b.closeScope();
        b.decreaseIndentation().newLine().append("}").decreaseIndentation().newLine().append("};");
    } else {
        // declare variable
        String variableName = b.declareSyntheticVariable(richString, "_builder");
        b.newLine();
        b.append(StringConcatenation.class);
        b.append(" ");
        b.append(variableName);
        b.append(" = new ");
        b.append(StringConcatenation.class);
        b.append("();");
        RichStringPrepareCompiler compiler = new RichStringPrepareCompiler(b, variableName, richString);
        richStringProcessor.process(richString, compiler, indentationHandler.get());
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) RichString(org.eclipse.xtend.core.xtend.RichString)

Aggregations

RichString (org.eclipse.xtend.core.xtend.RichString)38 Test (org.junit.Test)18 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)15 RichStringLiteral (org.eclipse.xtend.core.xtend.RichStringLiteral)11 XExpression (org.eclipse.xtext.xbase.XExpression)11 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)8 XtendField (org.eclipse.xtend.core.xtend.XtendField)6 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)6 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)5 XBinaryOperation (org.eclipse.xtext.xbase.XBinaryOperation)4 DefaultIndentationHandler (org.eclipse.xtend.core.richstring.DefaultIndentationHandler)3 RichStringForLoop (org.eclipse.xtend.core.xtend.RichStringForLoop)3 XBooleanLiteral (org.eclipse.xtext.xbase.XBooleanLiteral)3 XStringLiteral (org.eclipse.xtext.xbase.XStringLiteral)3 EObject (org.eclipse.emf.ecore.EObject)2 RichStringProcessor (org.eclipse.xtend.core.richstring.RichStringProcessor)2 AbstractRichStringTest (org.eclipse.xtend.core.tests.richstring.AbstractRichStringTest)2 RichStringIf (org.eclipse.xtend.core.xtend.RichStringIf)2 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)2 XtendInterface (org.eclipse.xtend.core.xtend.XtendInterface)2