Search in sources :

Example 81 with XExpression

use of org.eclipse.xtext.xbase.XExpression 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));
                if ((length >= 0)) {
                    this.setSpace(doc, offset, length, text);
                } else {
                    RuntimeException _runtimeException = new RuntimeException(("Programmatic error: length == " + Integer.valueOf(length)));
                    EcorePlugin.INSTANCE.log(_runtimeException);
                }
            }
        }
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) RichString(org.eclipse.xtend.core.xtend.RichString) RichString(org.eclipse.xtend.core.xtend.RichString) DefaultIndentationHandler(org.eclipse.xtend.core.richstring.DefaultIndentationHandler) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 82 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class XtendValidator method checkImplicitReturn.

@Check
protected void checkImplicitReturn(final XtendFunction method) {
    if (isIgnored(IMPLICIT_RETURN))
        return;
    JvmOperation jvmOperation = associations.getDirectlyInferredOperation(method);
    IResolvedTypes types = batchTypeResolver.resolveTypes(method);
    if (jvmOperation != null && types.getActualType(jvmOperation).isPrimitiveVoid())
        return;
    implicitReturnFinder.findImplicitReturns(method.getExpression(), new Acceptor() {

        @Override
        public void accept(XExpression implicitReturn) {
            if (method.getExpression() == implicitReturn)
                return;
            addIssue("Implicit return", implicitReturn, IMPLICIT_RETURN);
        }
    });
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) ValidationMessageAcceptor(org.eclipse.xtext.validation.ValidationMessageAcceptor) Acceptor(org.eclipse.xtext.xbase.validation.ImplicitReturnFinder.Acceptor) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XExpression(org.eclipse.xtext.xbase.XExpression) Check(org.eclipse.xtext.validation.Check)

Example 83 with XExpression

use of org.eclipse.xtext.xbase.XExpression 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 84 with XExpression

use of org.eclipse.xtext.xbase.XExpression 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 85 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class XtendReentrantTypeResolver method getReturnTypeOfOverriddenOperation.

@Override
protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ResolvedTypes resolvedTypes, IFeatureScopeSession session) {
    if (operation.getVisibility() == JvmVisibility.PRIVATE)
        return null;
    if (InferredTypeIndicator.isInferred(operation.getReturnType())) {
        LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType());
        if (declaringType == null) {
            throw new IllegalStateException("Cannot determine declaring type of operation: " + operation);
        }
        BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, declaringType, overrideTester);
        List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
        if (overriddenMethods.isEmpty())
            return null;
        IResolvedOperation overriddenMethod = overriddenMethods.get(0);
        JvmOperation declaration = overriddenMethod.getDeclaration();
        XExpression inferredFrom = getInferredFrom(declaration.getReturnType());
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=439535
        if (inferredFrom != null && (inferredFrom == getInferredFrom(operation.getReturnType()) || isHandled(inferredFrom))) {
            return null;
        }
        LightweightTypeReference result = overriddenMethod.getResolvedReturnType();
        return result;
    }
    return null;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) BottomResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Aggregations

XExpression (org.eclipse.xtext.xbase.XExpression)266 Test (org.junit.Test)134 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)97 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)86 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)85 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)84 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)83 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)64 EObject (org.eclipse.emf.ecore.EObject)40 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)37 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)29 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)28 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)25 XtendField (org.eclipse.xtend.core.xtend.XtendField)24 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)23 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)22 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)22 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)21 XtextResource (org.eclipse.xtext.resource.XtextResource)18 XClosure (org.eclipse.xtext.xbase.XClosure)18