Search in sources :

Example 26 with Pair

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

the class AbstractSemanticRegionsFinder method keywordPairs.

@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(String kw1, String kw2) {
    Preconditions.checkNotNull(kw1);
    Preconditions.checkNotNull(kw2);
    Preconditions.checkArgument(!kw1.equals(kw2));
    Predicate<ISemanticRegion> p1 = new KeywordPredicate(kw1);
    Predicate<ISemanticRegion> p2 = new KeywordPredicate(kw2);
    List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
    Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
    LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
    for (ISemanticRegion region : all) {
        if (p1.apply(region))
            stack.push(region);
        else {
            AbstractRule regionRule = GrammarUtil.containingRule(region.getGrammarElement());
            while (!stack.isEmpty()) {
                ISemanticRegion candidate = stack.pop();
                if (region.getSemanticElement() == candidate.getSemanticElement()) {
                    AbstractRule candidateRule = GrammarUtil.containingRule(candidate.getGrammarElement());
                    if (regionRule == candidateRule) {
                        result.add(Pair.of(candidate, region));
                        break;
                    }
                }
            }
        }
    }
    return result.build();
}
Also used : ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) AbstractRule(org.eclipse.xtext.AbstractRule) LinkedList(java.util.LinkedList) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 27 with Pair

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

the class SerializerFragment2 method genGetUnassignedRuleCallTokens.

private StringConcatenationClient genGetUnassignedRuleCallTokens() {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("protected String getUnassignedRuleCallToken(");
            _builder.append(EObject.class);
            _builder.append(" semanticObject, ");
            _builder.append(RuleCall.class);
            _builder.append(" ruleCall, ");
            _builder.append(INode.class);
            _builder.append(" node) {");
            _builder.newLineIfNotEmpty();
            {
                Iterable<Pair<Integer, AbstractRule>> _indexed = IterableExtensions.<AbstractRule>indexed(SerializerFragment2.this.unassignedCalledTokenRules());
                for (final Pair<Integer, AbstractRule> rule : _indexed) {
                    _builder.append("\t");
                    {
                        Integer _key = rule.getKey();
                        boolean _greaterThan = ((_key).intValue() > 0);
                        if (_greaterThan) {
                            _builder.append("else ");
                        }
                    }
                    _builder.append("if (ruleCall.getRule() == grammarAccess.");
                    String _gaAccessor = SerializerFragment2.this._grammarAccessExtensions.gaAccessor(rule.getValue());
                    _builder.append(_gaAccessor, "\t");
                    _builder.append(")");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return ");
                    CharSequence _unassignedCalledTokenRuleName = SerializerFragment2.this.unassignedCalledTokenRuleName(rule.getValue());
                    _builder.append(_unassignedCalledTokenRuleName, "\t\t");
                    _builder.append("(semanticObject, ruleCall, node);");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("return \"\";");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
        }
    };
    return _client;
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) AbstractRule(org.eclipse.xtext.AbstractRule) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 28 with Pair

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

the class XtextAntlrGeneratorFragment2 method initNameMappings.

/**
 * @since 2.14
 */
protected StringConcatenationClient initNameMappings(final Grammar it) {
    StringConcatenationClient _xblockexpression = null;
    {
        final RuleFilter filter = new RuleFilter();
        filter.setDiscardUnreachableRules(this.getOptions().isSkipUnusedRules());
        final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
        final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
        final Set<AbstractElement> seenElements = CollectionLiterals.<AbstractElement>newHashSet();
        Collection<? extends AbstractElement> _allAlternatives = GrammarUtil.getAllAlternatives(flattened);
        Collection<? extends AbstractElement> _allGroups = GrammarUtil.getAllGroups(flattened);
        Iterable<AbstractElement> _plus = Iterables.<AbstractElement>concat(_allAlternatives, _allGroups);
        Collection<? extends AbstractElement> _allAssignments = GrammarUtil.getAllAssignments(flattened);
        Iterable<AbstractElement> _plus_1 = Iterables.<AbstractElement>concat(_plus, _allAssignments);
        Collection<? extends AbstractElement> _allUnorderedGroups = GrammarUtil.getAllUnorderedGroups(flattened);
        final Function1<AbstractElement, Boolean> _function = (AbstractElement it_1) -> {
            return Boolean.valueOf(seenElements.add(AntlrGrammarGenUtil.<AbstractElement>getOriginalElement(it_1)));
        };
        final List<AbstractElement> elements = IterableExtensions.<AbstractElement>toList(IterableExtensions.<AbstractElement>filter(Iterables.<AbstractElement>filter((Iterables.<AbstractElement>concat(_plus_1, _allUnorderedGroups)), AbstractElement.class), _function));
        final List<List<AbstractElement>> partitions = Lists.<AbstractElement>partition(elements, 2500);
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("@");
                _builder.append(Singleton.class);
                _builder.newLineIfNotEmpty();
                _builder.append("public static final class NameMappings {");
                _builder.newLine();
                _builder.append("\t");
                _builder.newLine();
                {
                    int _size = partitions.size();
                    boolean _greaterThan = (_size > 1);
                    if (_greaterThan) {
                        {
                            Iterable<Pair<Integer, List<AbstractElement>>> _indexed = IterableExtensions.<List<AbstractElement>>indexed(partitions);
                            for (final Pair<Integer, List<AbstractElement>> partition : _indexed) {
                                _builder.append("\t");
                                _builder.append("private static final class Init");
                                Integer _key = partition.getKey();
                                _builder.append(_key, "\t");
                                _builder.append(" {");
                                _builder.newLineIfNotEmpty();
                                _builder.append("\t");
                                _builder.append("\t");
                                _builder.append("private static void doInit(");
                                _builder.append(ImmutableMap.class, "\t\t");
                                _builder.append(".Builder<");
                                _builder.append(AbstractElement.class, "\t\t");
                                _builder.append(", ");
                                _builder.append(String.class, "\t\t");
                                _builder.append("> builder, ");
                                TypeReference _grammarAccess = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
                                _builder.append(_grammarAccess, "\t\t");
                                _builder.append(" grammarAccess) {");
                                _builder.newLineIfNotEmpty();
                                _builder.append("\t");
                                _builder.append("\t\t");
                                StringConcatenationClient _initNameMappings = XtextAntlrGeneratorFragment2.this.initNameMappings(partition.getValue());
                                _builder.append(_initNameMappings, "\t\t\t");
                                _builder.newLineIfNotEmpty();
                                _builder.append("\t");
                                _builder.append("\t");
                                _builder.append("}");
                                _builder.newLine();
                                _builder.append("\t");
                                _builder.append("}");
                                _builder.newLine();
                                _builder.append("\t");
                                _builder.newLine();
                            }
                        }
                    }
                }
                _builder.append("\t");
                _builder.append("private final ");
                _builder.append(Map.class, "\t");
                _builder.append("<");
                _builder.append(AbstractElement.class, "\t");
                _builder.append(", ");
                _builder.append(String.class, "\t");
                _builder.append("> mappings;");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@");
                _builder.append(Inject.class, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("public NameMappings(");
                TypeReference _grammarAccess_1 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
                _builder.append(_grammarAccess_1, "\t");
                _builder.append(" grammarAccess) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append(ImmutableMap.class, "\t\t");
                _builder.append(".Builder<");
                _builder.append(AbstractElement.class, "\t\t");
                _builder.append(", ");
                _builder.append(String.class, "\t\t");
                _builder.append("> builder = ");
                _builder.append(ImmutableMap.class, "\t\t");
                _builder.append(".builder();");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("init(builder, grammarAccess);");
                _builder.newLine();
                _builder.append("\t\t");
                _builder.append("this.mappings = builder.build();");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("public ");
                _builder.append(String.class, "\t");
                _builder.append(" getRuleName(");
                _builder.append(AbstractElement.class, "\t");
                _builder.append(" element) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("return mappings.get(element);");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("private static void init(");
                _builder.append(ImmutableMap.class, "\t");
                _builder.append(".Builder<");
                _builder.append(AbstractElement.class, "\t");
                _builder.append(", ");
                _builder.append(String.class, "\t");
                _builder.append("> builder, ");
                TypeReference _grammarAccess_2 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
                _builder.append(_grammarAccess_2, "\t");
                _builder.append(" grammarAccess) {");
                _builder.newLineIfNotEmpty();
                {
                    int _size_1 = partitions.size();
                    boolean _greaterThan_1 = (_size_1 > 1);
                    if (_greaterThan_1) {
                        {
                            Iterable<Pair<Integer, List<AbstractElement>>> _indexed_1 = IterableExtensions.<List<AbstractElement>>indexed(partitions);
                            for (final Pair<Integer, List<AbstractElement>> partition_1 : _indexed_1) {
                                _builder.append("\t\t");
                                _builder.append("Init");
                                Integer _key_1 = partition_1.getKey();
                                _builder.append(_key_1, "\t\t");
                                _builder.append(".doInit(builder, grammarAccess);");
                                _builder.newLineIfNotEmpty();
                            }
                        }
                    } else {
                        {
                            for (final List<AbstractElement> partition_2 : partitions) {
                                _builder.append("\t\t");
                                StringConcatenationClient _initNameMappings_1 = XtextAntlrGeneratorFragment2.this.initNameMappings(partition_2);
                                _builder.append(_initNameMappings_1, "\t\t");
                                _builder.newLineIfNotEmpty();
                            }
                        }
                    }
                }
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
                _builder.newLine();
                _builder.append("@");
                _builder.append(Inject.class);
                _builder.newLineIfNotEmpty();
                _builder.append("private NameMappings nameMappings;");
                _builder.newLine();
            }
        };
        _xblockexpression = _client;
    }
    return _xblockexpression;
}
Also used : Inject(com.google.inject.Inject) Set(java.util.Set) RuleNames(org.eclipse.xtext.xtext.RuleNames) AbstractElement(org.eclipse.xtext.AbstractElement) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) ImmutableMap(com.google.common.collect.ImmutableMap) Singleton(com.google.inject.Singleton) Collection(java.util.Collection) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 29 with Pair

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

the class SerializerFragment2 method genEmitUnassignedTokens.

private StringConcatenationClient genEmitUnassignedTokens() {
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("protected void emitUnassignedTokens(");
            _builder.append(EObject.class);
            _builder.append(" semanticObject, ");
            _builder.append(ISyntacticSequencerPDAProvider.ISynTransition.class);
            _builder.append(" transition, ");
            _builder.append(INode.class);
            _builder.append(" fromNode, ");
            _builder.append(INode.class);
            _builder.append(" toNode) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("if (transition.getAmbiguousSyntaxes().isEmpty()) return;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append(List.class, "\t");
            _builder.append("<");
            _builder.append(INode.class, "\t");
            _builder.append("> transitionNodes = collectNodes(fromNode, toNode);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("for (");
            _builder.append(GrammarAlias.AbstractElementAlias.class, "\t");
            _builder.append(" syntax : transition.getAmbiguousSyntaxes()) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append(List.class, "\t\t");
            _builder.append("<");
            _builder.append(INode.class, "\t\t");
            _builder.append("> syntaxNodes = getNodesFor(transitionNodes, syntax);");
            _builder.newLineIfNotEmpty();
            {
                Iterable<Pair<Integer, EqualAmbiguousTransitions>> _indexed = IterableExtensions.<EqualAmbiguousTransitions>indexed(SerializerFragment2.this._syntacticSequencerExtensions.getAllAmbiguousTransitionsBySyntax());
                for (final Pair<Integer, EqualAmbiguousTransitions> group : _indexed) {
                    _builder.append("\t\t");
                    {
                        Integer _key = group.getKey();
                        boolean _greaterThan = ((_key).intValue() > 0);
                        if (_greaterThan) {
                            _builder.append("else ");
                        }
                    }
                    _builder.append("if (match_");
                    String _identifier = group.getValue().getIdentifier();
                    _builder.append(_identifier, "\t\t");
                    _builder.append(".equals(syntax))");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("\t");
                    _builder.append("emit_");
                    String _identifier_1 = group.getValue().getIdentifier();
                    _builder.append(_identifier_1, "\t\t\t");
                    _builder.append("(semanticObject, getLastNavigableState(), syntaxNodes);");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t\t");
            {
                boolean _isEmpty = SerializerFragment2.this._syntacticSequencerExtensions.getAllAmbiguousTransitionsBySyntax().isEmpty();
                boolean _not = (!_isEmpty);
                if (_not) {
                    _builder.append("else ");
                }
            }
            _builder.append("acceptNodes(getLastNavigableState(), syntaxNodes);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
        }
    };
    return _client;
}
Also used : ISyntacticSequencerPDAProvider(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) GrammarAlias(org.eclipse.xtext.serializer.analysis.GrammarAlias) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 30 with Pair

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

the class HoverTest method testHover_05.

@Test
public void testHover_05() {
    final Procedure1<HoverTestConfiguration> _function = (HoverTestConfiguration it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("/**");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("* Some documentation.");
        _builder.newLine();
        _builder.append(" ");
        _builder.append("*/");
        _builder.newLine();
        _builder.append("type Foo {");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        Pair<String, String> _mappedTo = Pair.<String, String>of(("MyModel2." + this.fileExtension), _builder.toString());
        it.setFilesInScope(Collections.<String, CharSequence>unmodifiableMap(CollectionLiterals.<String, CharSequence>newHashMap(_mappedTo)));
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("type Bar extends Foo {");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        it.setModel(_builder_1.toString());
        it.setColumn("type Bar extends F".length());
        StringConcatenation _builder_2 = new StringConcatenation();
        _builder_2.append("[[0, 17] .. [0, 20]]");
        _builder_2.newLine();
        _builder_2.append("Some documentation.");
        _builder_2.newLine();
        it.setExpectedHover(_builder_2.toString());
    };
    this.testHover(_function);
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) HoverTestConfiguration(org.eclipse.xtext.testing.HoverTestConfiguration) Pair(org.eclipse.xtext.xbase.lib.Pair) Test(org.junit.Test) AbstractTestLangLanguageServerTest(org.eclipse.xtext.ide.tests.server.AbstractTestLangLanguageServerTest)

Aggregations

Pair (org.eclipse.xtext.xbase.lib.Pair)67 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)22 Test (org.junit.Test)15 Collection (java.util.Collection)11 List (java.util.List)11 ArrayList (java.util.ArrayList)9 AbstractHierarchyBuilderTest (org.eclipse.xtext.junit4.ide.AbstractHierarchyBuilderTest)9 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)9 Map (java.util.Map)6 EObject (org.eclipse.emf.ecore.EObject)6 Resource (org.eclipse.emf.ecore.resource.Resource)6 AbstractElement (org.eclipse.xtext.AbstractElement)6 HashMap (java.util.HashMap)5 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)5 Type (org.eclipse.n4js.ts.types.Type)5 EList (org.eclipse.emf.common.util.EList)4 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)4 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3