Search in sources :

Example 26 with RichString

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

the class ExtractMethodRefactoring method createMethodDeclarationEdit.

protected void createMethodDeclarationEdit(DocumentRewriter.Section declarationSection, int expressionIndentLevel, ITextRegion expressionsRegion) throws BadLocationException {
    String expressionsAsString = getExtractedMethodBody(expressionsRegion);
    declarationSection.newLine().newLine();
    appendMethodSignature(declarationSection);
    if (firstExpression.eContainer() instanceof RichString && !isRichStringXExpressionOrVarDeclaration()) {
        declarationSection.increaseIndentation().newLine().append("'''");
    } else {
        declarationSection.append(" {").increaseIndentation().newLine();
    }
    declarationSection.append(expressionsAsString, Math.min(0, -expressionIndentLevel));
    if (isNeedsReturnExpression())
        declarationSection.newLine().append(((XFeatureCall) returnExpression).getFeature().getSimpleName());
    if (firstExpression.eContainer() instanceof RichString && !isRichStringXExpressionOrVarDeclaration()) {
        declarationSection.append("'''").decreaseIndentation().newLine();
    } else {
        declarationSection.decreaseIndentation().newLine().append("}");
    }
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) RichString(org.eclipse.xtend.core.xtend.RichString)

Example 27 with RichString

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

the class XtendTypeComputer method _computeTypes.

protected void _computeTypes(RichString object, ITypeComputationState state) {
    List<XExpression> expressions = object.getExpressions();
    if (!expressions.isEmpty()) {
        for (XExpression expression : expressions) {
            ITypeComputationState expressionState = state.withoutExpectation();
            expressionState.computeTypes(expression);
            if (expression instanceof XVariableDeclaration) {
                addLocalToCurrentScope((XVariableDeclaration) expression, state);
            }
        }
    }
    for (ITypeExpectation expectation : state.getExpectations()) {
        LightweightTypeReference expectedType = expectation.getExpectedType();
        if (expectedType != null && expectedType.isType(StringConcatenation.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        } else if (expectedType != null && expectedType.isType(StringConcatenationClient.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        } else if (expectedType != null && expectedType.isType(String.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        // TODO this special treatment here should become obsolete as soon as the expectations are properly propagated
        } else if (!(object.eContainer() instanceof XCastedExpression) && object.eContainingFeature() != XbasePackage.Literals.XMEMBER_FEATURE_CALL__MEMBER_CALL_TARGET && (expectedType != null && !expectedType.isResolved() || expectedType == null && !expectation.isVoidTypeAllowed())) {
            LightweightTypeReference type = getRawTypeForName(String.class, state);
            expectation.acceptActualType(type, ConformanceFlags.UNCHECKED | ConformanceFlags.DEMAND_CONVERSION);
        } else {
            LightweightTypeReference type = getRawTypeForName(CharSequence.class, state);
            expectation.acceptActualType(type, ConformanceFlags.UNCHECKED);
        }
    }
}
Also used : ITypeComputationState(org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState) XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XCastedExpression(org.eclipse.xtext.xbase.XCastedExpression) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString) ITypeExpectation(org.eclipse.xtext.xbase.typesystem.computation.ITypeExpectation)

Example 28 with RichString

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

the class ExtractMethodRefactoring method createMethodCallEdit.

protected void createMethodCallEdit(DocumentRewriter.Section methodCallSection, ITextRegion expressionRegion) throws BadLocationException {
    if (firstExpression.eContainer() instanceof RichString) {
        methodCallSection.append("�");
    }
    if (isNeedsReturnExpression()) {
        JvmIdentifiableElement returnFeature = ((XFeatureCall) returnExpression).getFeature();
        if (isFinalFeature(returnFeature))
            methodCallSection.append("val ");
        else
            methodCallSection.append("var ");
        methodCallSection.append(returnFeature.getSimpleName()).append(" = ");
    }
    boolean needsSurroundingParentheses = false;
    if (firstExpression.eContainer() instanceof XMemberFeatureCall) {
        if (((XMemberFeatureCall) firstExpression.eContainer()).getMemberCallArguments().size() == 1) {
            String expressionExpanded = document.get(expressionRegion.getOffset() - 1, expressionRegion.getLength() + 2);
            if (!expressionExpanded.startsWith("(") || !expressionExpanded.endsWith(")")) {
                needsSurroundingParentheses = true;
                methodCallSection.append("(");
            }
        }
    }
    methodCallSection.append(methodName).append("(");
    boolean isFirst = true;
    for (ParameterInfo parameterInfo : getParameterInfos()) {
        if (!isFirst)
            methodCallSection.append(", ");
        isFirst = false;
        methodCallSection.append(parameterInfo.getOldName());
    }
    methodCallSection.append(")");
    if (needsSurroundingParentheses)
        methodCallSection.append(")");
    if (lastExpression.eContainer() instanceof RichString) {
        methodCallSection.append("�");
    }
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 29 with RichString

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

the class JavaConverterTest method testRichStringSpecialCase3.

@Test
public void testRichStringSpecialCase3() throws Exception {
    XtendClass clazz = this.toValidXtendClass("class Z {String richTxt = \"x(p1)} def dispatch x(int s) {\'int\'} def dispatch x(boolean s)\"+\" {\'boolean\'} def dispatch x(double s) {\'double\'\";}");
    Assert.assertNotNull(clazz);
    XtendMember _get = clazz.getMembers().get(0);
    XtendField xtendMember = ((XtendField) _get);
    Assert.assertEquals("richTxt", xtendMember.getName());
    XExpression _initialValue = xtendMember.getInitialValue();
    Assert.assertTrue((_initialValue instanceof RichString));
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 30 with RichString

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

the class ParserTest method testRichString_00.

@Test
public void testRichString_00() throws Exception {
    XtendFunction function = function("def foo() ''' foo '''");
    assertTrue(function.getExpression() instanceof RichString);
    RichString richString = (RichString) function.getExpression();
    assertEquals(1, richString.getExpressions().size());
    RichStringLiteral stringLiteral = (RichStringLiteral) richString.getExpressions().get(0);
    assertEquals(" foo ", stringLiteral.getValue());
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) RichString(org.eclipse.xtend.core.xtend.RichString) RichStringLiteral(org.eclipse.xtend.core.xtend.RichStringLiteral) Test(org.junit.Test)

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