Search in sources :

Example 16 with ICompletion

use of org.metaborg.core.completion.ICompletion in project spoofax by metaborg.

the class JSGLRCompletionService method completionErroneousPrograms.

public Collection<ICompletion> completionErroneousPrograms(int cursorPosition, Iterable<IStrategoTerm> completionTerms, ISpoofaxParseUnit completionParseResult) throws MetaborgException {
    final FileObject location = completionParseResult.source();
    final ILanguageImpl language = completionParseResult.input().langImpl();
    final String languageName = language.belongsTo().name();
    final Collection<ICompletion> completions = Lists.newLinkedList();
    final Collection<IStrategoTerm> proposalsTerm = Lists.newLinkedList();
    for (ILanguageComponent component : language.components()) {
        final ITermFactory termFactory = termFactoryService.get(component, null, false);
        for (IStrategoTerm completionTerm : completionTerms) {
            IStrategoTerm completionAst = completionParseResult.ast();
            final StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) completionTerm);
            if (ImploderAttachment.get(completionTerm).isSinglePlaceholderCompletion()) {
                Collection<IStrategoTerm> placeholders = Lists.newLinkedList();
                placeholders.addAll(findPlaceholderTerms(completionTerm));
                if (placeholders.size() != 1) {
                    logger.error("Getting proposals for {} failed", completionTerm);
                    continue;
                }
                IStrategoAppl placeholderTerm = (IStrategoAppl) Iterables.get(placeholders, 0);
                IStrategoAppl placeholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderTerm.getConstructor().getName(), 0));
                IStrategoTerm parenthesized = parenthesizeTerm(completionTerm, termFactory);
                final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized, placeholder, placeholderTerm);
                final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
                final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-single-placeholder-" + languageName);
                if (proposalTerm == null || !(proposalTerm instanceof IStrategoList)) {
                    logger.error("Getting proposals for {} failed", completionTerm);
                    continue;
                }
                for (IStrategoTerm proposalPlaceholder : proposalTerm) {
                    proposalsTerm.add(proposalPlaceholder);
                }
            } else {
                IStrategoTerm parenthesized = parenthesizeTerm(completionTerm, termFactory);
                final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized);
                final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
                final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-" + languageName);
                if (proposalTerm == null) {
                    logger.error("Getting proposals for {} failed", completionTerm);
                    continue;
                }
                proposalsTerm.add(proposalTerm);
            }
        }
        for (IStrategoTerm proposalTerm : proposalsTerm) {
            if (!(proposalTerm instanceof IStrategoTuple)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 6 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || (tuple.getSubterm(4) == null) || !(tuple.getSubterm(5) instanceof IStrategoString)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final String name = Tools.asJavaString(tuple.getSubterm(0));
            String text = Tools.asJavaString(tuple.getSubterm(1));
            String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
            final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
            final StrategoTerm completionTerm = (StrategoTerm) tuple.getSubterm(4);
            final String completionKind = Tools.asJavaString(tuple.getSubterm(5));
            String prefix = calculatePrefix(cursorPosition, completionTerm);
            String suffix = calculateSuffix(cursorPosition, completionTerm);
            // if the change is inserting at the end of a list
            if (change.getConstructor().getName().contains("INSERT_AT_END")) {
                // calls a different method because now, the program has errors that should be fixed
                final ICompletion completion = createCompletionInsertAtEndFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            } else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
                final ICompletion completion = createCompletionInsertBeforeFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            } else if (change.getConstructor().getName().contains("INSERTION_TERM")) {
                final ICompletion completion = createCompletionInsertionTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            } else if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                final ICompletion completion = createCompletionReplaceTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

ICompletion (org.metaborg.core.completion.ICompletion)16 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)16 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)16 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)14 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)9 StrategoTerm (org.spoofax.terms.StrategoTerm)9 Completion (org.metaborg.core.completion.Completion)8 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)8 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)8 HybridInterpreter (org.strategoxt.HybridInterpreter)8 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)7 ITokens (org.spoofax.jsglr.client.imploder.ITokens)7 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 StrategoAppl (org.spoofax.terms.StrategoAppl)6 CompletionKind (org.metaborg.core.completion.CompletionKind)5 FileObject (org.apache.commons.vfs2.FileObject)4 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)4 IToken (org.spoofax.jsglr.client.imploder.IToken)3 HashMap (java.util.HashMap)1