Search in sources :

Example 6 with Function2

use of org.eclipse.xtext.xbase.lib.Functions.Function2 in project xtext-core by eclipse.

the class ContentAssistFragment2 method handleAssignmentOptions.

private StringConcatenationClient handleAssignmentOptions(final Iterable<AbstractElement> terminals) {
    StringConcatenationClient _xblockexpression = null;
    {
        final HashSet<EClass> processedTerminals = CollectionLiterals.<EClass>newHashSet();
        final Function2<ArrayList<AbstractElement>, AbstractElement, ArrayList<AbstractElement>> _function = (ArrayList<AbstractElement> candidates, AbstractElement terminal) -> {
            ArrayList<AbstractElement> _xblockexpression_1 = null;
            {
                boolean _contains = processedTerminals.contains(terminal.eClass());
                boolean _not = (!_contains);
                if (_not) {
                    EClass _eClass = terminal.eClass();
                    processedTerminals.add(_eClass);
                    candidates.add(terminal);
                }
                _xblockexpression_1 = candidates;
            }
            return _xblockexpression_1;
        };
        final ArrayList<AbstractElement> candidates = IterableExtensions.<AbstractElement, ArrayList<AbstractElement>>fold(terminals, CollectionLiterals.<AbstractElement>newArrayList(), _function);
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                {
                    for (final AbstractElement terminal : candidates) {
                        _builder.append("if (assignment.getTerminal() instanceof ");
                        Class<?> _instanceClass = terminal.eClass().getInstanceClass();
                        _builder.append(_instanceClass);
                        _builder.append(") {");
                        _builder.newLineIfNotEmpty();
                        _builder.append("\t");
                        StringConcatenationClient _client = new StringConcatenationClient() {

                            @Override
                            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                _builder.append("assignment.getTerminal()");
                            }
                        };
                        StringConcatenationClient _assignmentTerminal = ContentAssistFragment2.this.assignmentTerminal(terminal, _client);
                        _builder.append(_assignmentTerminal, "\t");
                        _builder.newLineIfNotEmpty();
                        _builder.append("}");
                        _builder.newLine();
                    }
                }
            }
        };
        _xblockexpression = _client;
    }
    return _xblockexpression;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) AbstractElement(org.eclipse.xtext.AbstractElement) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) ArrayList(java.util.ArrayList) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) HashSet(java.util.HashSet)

Example 7 with Function2

use of org.eclipse.xtext.xbase.lib.Functions.Function2 in project xtext-core by eclipse.

the class DefaultEcoreElementFactoryTest method testNullInput.

@Test
public void testNullInput() throws Exception {
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    final ILeafNode node = new LeafNode();
    Function2<String, INode, Object> toValueImpl = new Function2<String, INode, Object>() {

        @Override
        public Object apply(String lexerRule, INode nodeParam) {
            if ("foo".equals(lexerRule) && nodeParam.equals(node)) {
                return null;
            }
            fail();
            return null;
        }
    };
    DefaultEcoreElementFactory factory = new DefaultEcoreElementFactory();
    factory.setConverterService(new MockedConverterService(toValueImpl));
    try {
        factory.set(eClass, "abstract", null, "foo", node);
        fail("Expected ValueConverterException");
    } catch (ValueConverterException ex) {
        assertNull(ex.getCause());
        assertTrue(ex.getMessage().indexOf("ValueConverter returned null for") >= 0);
        assertSame(node, ex.getNode());
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) Test(org.junit.Test)

Example 8 with Function2

use of org.eclipse.xtext.xbase.lib.Functions.Function2 in project xtext-core by eclipse.

the class DefaultEcoreElementFactoryTest method testValueConverterException.

@Test
public void testValueConverterException() throws Exception {
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    final ILeafNode node = new LeafNode();
    final ValueConverterException expected = new ValueConverterException("Foo", node, null);
    Function2<String, INode, Object> toValueImpl = new Function2<String, INode, Object>() {

        @Override
        public Object apply(String lexerRule, INode nodeParam) {
            if ("foo".equals(lexerRule) && node.equals(nodeParam)) {
                throw expected;
            }
            fail();
            return null;
        }
    };
    DefaultEcoreElementFactory factory = new DefaultEcoreElementFactory();
    factory.setConverterService(new MockedConverterService(toValueImpl));
    try {
        factory.set(eClass, "abstract", null, "foo", node);
        fail("Expected ValueConverterException");
    } catch (ValueConverterException ex) {
        assertSame(expected, ex);
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) Test(org.junit.Test)

Example 9 with Function2

use of org.eclipse.xtext.xbase.lib.Functions.Function2 in project xtext-core by eclipse.

the class DefaultEcoreElementFactoryTest method testConvertNullInput.

@Test
public void testConvertNullInput() throws Exception {
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    Function2<String, INode, Object> toValueImpl = new Function2<String, INode, Object>() {

        @Override
        public Object apply(String lexerRule, INode node) {
            if ("foo".equals(lexerRule)) {
                return "FOO";
            }
            return null;
        }
    };
    DefaultEcoreElementFactory factory = new DefaultEcoreElementFactory();
    factory.setConverterService(new MockedConverterService(toValueImpl));
    factory.set(eClass, "name", null, "foo", null);
    assertEquals("FOO", eClass.getName());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) INode(org.eclipse.xtext.nodemodel.INode) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) Test(org.junit.Test)

Aggregations

Function2 (org.eclipse.xtext.xbase.lib.Functions.Function2)9 EClass (org.eclipse.emf.ecore.EClass)4 INode (org.eclipse.xtext.nodemodel.INode)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 URI (org.eclipse.emf.common.util.URI)2 EObject (org.eclipse.emf.ecore.EObject)2 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)2 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)2 Document (org.eclipse.xtext.ide.server.Document)2 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)2 LeafNode (org.eclipse.xtext.nodemodel.impl.LeafNode)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)2 Binder (com.google.inject.Binder)1 AnnotatedBindingBuilder (com.google.inject.binder.AnnotatedBindingBuilder)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Resource (org.eclipse.emf.ecore.resource.Resource)1