Search in sources :

Example 1 with ContentAssistContext

use of org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext in project xtext-eclipse by eclipse.

the class UiToIdeContentProposalProvider method getIdeContext.

private ContentAssistContext getIdeContext(final org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext c) {
    final ContentAssistContext.Builder builder = this.builderProvider.get();
    final Region replaceRegion = c.getReplaceRegion();
    ContentAssistContext.Builder _setOffset = builder.setPrefix(c.getPrefix()).setSelectedText(c.getSelectedText()).setRootModel(c.getRootModel()).setRootNode(c.getRootNode()).setCurrentModel(c.getCurrentModel()).setPreviousModel(c.getPreviousModel()).setCurrentNode(c.getCurrentNode()).setLastCompleteNode(c.getLastCompleteNode()).setOffset(c.getOffset());
    int _offset = replaceRegion.getOffset();
    int _length = replaceRegion.getLength();
    TextRegion _textRegion = new TextRegion(_offset, _length);
    _setOffset.setReplaceRegion(_textRegion).setResource(c.getResource());
    ImmutableList<AbstractElement> _firstSetGrammarElements = c.getFirstSetGrammarElements();
    for (final AbstractElement grammarElement : _firstSetGrammarElements) {
        builder.accept(grammarElement);
    }
    return builder.toContext();
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) AbstractElement(org.eclipse.xtext.AbstractElement) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) Region(org.eclipse.jface.text.Region) TextRegion(org.eclipse.xtext.util.TextRegion)

Example 2 with ContentAssistContext

use of org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext in project xtext-eclipse by eclipse.

the class UiToIdeContentProposalProvider method createProposals.

@Override
public void createProposals(final org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    final ArrayList<Pair<ContentAssistEntry, Integer>> entries = new ArrayList<Pair<ContentAssistEntry, Integer>>();
    final IIdeContentProposalAcceptor ideAcceptor = new IIdeContentProposalAcceptor() {

        @Override
        public void accept(final ContentAssistEntry entry, final int priority) {
            if ((entry != null)) {
                Pair<ContentAssistEntry, Integer> _mappedTo = Pair.<ContentAssistEntry, Integer>of(entry, Integer.valueOf(priority));
                entries.add(_mappedTo);
            }
        }

        @Override
        public boolean canAcceptMoreProposals() {
            int _size = entries.size();
            int _maxProposals = UiToIdeContentProposalProvider.this.getMaxProposals();
            return (_size < _maxProposals);
        }
    };
    ContentAssistContext _ideContext = this.getIdeContext(context);
    this.ideProvider.createProposals(Collections.<ContentAssistContext>unmodifiableList(CollectionLiterals.<ContentAssistContext>newArrayList(_ideContext)), ideAcceptor);
    final AbstractContentProposalProvider.NullSafeCompletionProposalAcceptor uiAcceptor = new AbstractContentProposalProvider.NullSafeCompletionProposalAcceptor(acceptor);
    final Procedure2<Pair<ContentAssistEntry, Integer>, Integer> _function = (Pair<ContentAssistEntry, Integer> p, Integer idx) -> {
        final ContentAssistEntry entry = p.getKey();
        final ConfigurableCompletionProposal proposal = this.doCreateProposal(entry.getProposal(), this.getDisplayString(entry), this.getImage(entry), (p.getValue()).intValue(), context);
        uiAcceptor.accept(proposal);
    };
    IterableExtensions.<Pair<ContentAssistEntry, Integer>>forEach(entries, _function);
}
Also used : ArrayList(java.util.ArrayList) IIdeContentProposalAcceptor(org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor) ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) AbstractContentProposalProvider(org.eclipse.xtext.ui.editor.contentassist.AbstractContentProposalProvider) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 3 with ContentAssistContext

use of org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext in project xtext-core by eclipse.

the class ContentAssistContextTestHelper method firstSetGrammarElementsToString.

public String firstSetGrammarElementsToString(ContentAssistContextFactory factory) throws Exception {
    int offset = document.indexOf(cursor);
    Preconditions.checkArgument(offset >= 0, "you forgot to provide a cursor");
    String doc = document.replace(cursor, "");
    XtextResource res = parse(doc);
    factory.setPool(Executors.newCachedThreadPool());
    ContentAssistContext[] ctxs = factory.create(doc, new TextRegion(0, 0), offset, res);
    GrammarElementTitleSwitch f = new GrammarElementTitleSwitch().showAssignments().showQualified().showRule();
    Iterable<String> entries = Iterables.transform(IterableExtensions.indexed(Arrays.asList(ctxs)), ctx -> {
        String elements = Joiner.on("").join(Lists.transform(ctx.getValue().getFirstSetGrammarElements(), ele -> "	" + ele.eClass().getName() + ": " + f.apply(ele) + "\n"));
        String entry = "context" + ctx.getKey() + " {\n" + elements + "}\n";
        return entry;
    });
    String result = Joiner.on("").join(entries);
    return result;
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) StringInputStream(org.eclipse.xtext.util.StringInputStream) Arrays(java.util.Arrays) ContentAssistContextFactory(org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory) Iterables(com.google.common.collect.Iterables) FileExtensionProvider(org.eclipse.xtext.resource.FileExtensionProvider) URI(org.eclipse.emf.common.util.URI) IResourceFactory(org.eclipse.xtext.resource.IResourceFactory) Inject(com.google.inject.Inject) TextRegion(org.eclipse.xtext.util.TextRegion) Executors(java.util.concurrent.Executors) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) Lists(com.google.common.collect.Lists) ValidationTestHelper(org.eclipse.xtext.testing.validation.ValidationTestHelper) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) ParserRule(org.eclipse.xtext.ParserRule) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Joiner(com.google.common.base.Joiner) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) TextRegion(org.eclipse.xtext.util.TextRegion) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 4 with ContentAssistContext

use of org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext in project xtext-core by eclipse.

the class ContentAssistService method createProposals.

protected void createProposals(String document, TextRegion selection, int caretOffset, XtextResource resource, IIdeContentProposalAcceptor acceptor) {
    if (caretOffset > document.length()) {
        return;
    }
    ContentAssistContextFactory contextFactory = contextFactoryProvider.get();
    contextFactory.setPool(executorService);
    ContentAssistContext[] contexts = contextFactory.create(document, selection, caretOffset, resource);
    proposalProvider.createProposals(Arrays.asList(contexts), acceptor);
}
Also used : ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) ContentAssistContextFactory(org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory)

Example 5 with ContentAssistContext

use of org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext in project xtext-core by eclipse.

the class ContentAssistContextFactory method isLikelyToBeValidProposal.

protected boolean isLikelyToBeValidProposal(INode lastCompleteNode, Iterable<ContentAssistContext> contexts) {
    for (ContentAssistContext context : contexts) {
        for (AbstractElement element : context.getFirstSetGrammarElements()) {
            if (element instanceof Keyword) {
                String keywordValue = ((Keyword) element).getValue();
                String lastText = ((ILeafNode) lastCompleteNode).getText();
                if (keywordValue.equals(lastText))
                    return true;
            }
        }
    }
    return false;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext)

Aggregations

ContentAssistContext (org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext)5 AbstractElement (org.eclipse.xtext.AbstractElement)2 ContentAssistContextFactory (org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory)2 TextRegion (org.eclipse.xtext.util.TextRegion)2 Joiner (com.google.common.base.Joiner)1 Preconditions (com.google.common.base.Preconditions)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Inject (com.google.inject.Inject)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Executors (java.util.concurrent.Executors)1 URI (org.eclipse.emf.common.util.URI)1 Region (org.eclipse.jface.text.Region)1 Keyword (org.eclipse.xtext.Keyword)1 ParserRule (org.eclipse.xtext.ParserRule)1 GrammarElementTitleSwitch (org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch)1 ContentAssistEntry (org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry)1 IIdeContentProposalAcceptor (org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor)1