Search in sources :

Example 6 with StrategoTerm

use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.

the class JSGLRCompletionService method completionErroneousProgramsNested.

public Collection<? extends ICompletion> completionErroneousProgramsNested(int cursorPosition, Collection<IStrategoTerm> nestedCompletionTerms, 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();
    IStrategoTerm completionAst = completionParseResult.ast();
    for (ILanguageComponent component : language.components()) {
        final ITermFactory termFactory = termFactoryService.get(component, null, false);
        for (IStrategoTerm nestedCompletionTerm : nestedCompletionTerms) {
            final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
            Collection<IStrategoTerm> inputsStrategoNested = Lists.newLinkedList();
            // calculate direct proposals
            inputsStrategoNested.addAll(calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime));
            // calculate inner nested proposals
            Collection<IStrategoTerm> innerNestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
            for (IStrategoTerm innerNestedCompletionTerm : innerNestedCompletionTerms) {
                inputsStrategoNested.addAll(calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime));
            }
            for (IStrategoTerm inputStrategoNested : inputsStrategoNested) {
                final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
                if (proposalTermNested == null) {
                    logger.error("Getting proposals for {} failed", inputStrategoNested);
                    continue;
                }
                final String name = Tools.asJavaString(proposalTermNested.getSubterm(0));
                final String text = Tools.asJavaString(proposalTermNested.getSubterm(1));
                final String additionalInfo = Tools.asJavaString(proposalTermNested.getSubterm(2));
                final StrategoAppl change = (StrategoAppl) proposalTermNested.getSubterm(3);
                final StrategoTerm completionTerm = (StrategoTerm) proposalTermNested.getSubterm(4);
                String prefix = calculatePrefix(cursorPosition, completionTerm);
                String suffix = calculateSuffix(cursorPosition, completionTerm);
                String completionKind = "recovery";
                // 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", proposalTermNested);
                        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", proposalTermNested);
                        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", proposalTermNested);
                        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", proposalTermNested);
                        continue;
                    }
                    completions.add(completion);
                }
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) HybridInterpreter(org.strategoxt.HybridInterpreter) FileObject(org.apache.commons.vfs2.FileObject) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Example 7 with StrategoTerm

use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.

the class JSGLRCompletionService method calculateDirectCompletionProposals.

private Collection<IStrategoTerm> calculateDirectCompletionProposals(IStrategoTerm nestedCompletionTerm, ITermFactory termFactory, IStrategoTerm completionAst, String languageName, HybridInterpreter runtime) throws MetaborgException {
    Collection<IStrategoTerm> inputsStratego = Lists.newLinkedList();
    Collection<IStrategoTerm> completionTerms = findCompletionTermInsideNested((StrategoTerm) nestedCompletionTerm);
    for (IStrategoTerm completionTerm : completionTerms) {
        final StrategoTerm topMostCompletionTerm = findTopMostCompletionNode((StrategoTerm) completionTerm);
        final StrategoTerm topMostAmb = findTopMostAmbNode(topMostCompletionTerm);
        IStrategoTerm parenthesized = parenthesizeTerm(topMostCompletionTerm, termFactory);
        final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized);
        final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-" + languageName);
        if (proposalTerm == null) {
            logger.error("Getting proposals for {} failed", inputStratego);
            continue;
        }
        final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTerm.getSubterm(1));
        IStrategoTerm parenthesizedNested = parenthesizeTerm(nestedCompletionTerm, termFactory);
        final IStrategoTerm inputStrategoNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesizedNested)), completionAst, nestedCompletionTerm, proposalTerm.getSubterm(0), replaceTermText, parenthesizedNested);
        inputsStratego.add(inputStrategoNested);
    }
    return inputsStratego;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) StrategoConstructor(org.spoofax.terms.StrategoConstructor)

Example 8 with StrategoTerm

use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.

the class JSGLRCompletionService method createCompletionReplaceTermFixing.

private ICompletion createCompletionReplaceTermFixing(String name, String text, String additionalInfo, String prefix, String suffix, StrategoAppl change, String completionKind) {
    final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
    final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
    if (change.getSubtermCount() != 2 || !(newNode instanceof IStrategoAppl) || !(oldNode instanceof IStrategoAppl)) {
        return null;
    }
    final String sort = ImploderAttachment.getSort(oldNode);
    int insertionPoint, suffixPoint;
    final ImploderAttachment oldNodeIA = oldNode.getAttachment(ImploderAttachment.TYPE);
    ITokens tokenizer = ImploderAttachment.getTokenizer(oldNode);
    // check if it's an empty node
    if (oldNodeIA.getLeftToken().getStartOffset() > oldNodeIA.getRightToken().getEndOffset()) {
        // get the last non-layout token before the new node
        int tokenPosition = oldNodeIA.getLeftToken().getIndex() - 1 > 0 ? oldNodeIA.getLeftToken().getIndex() - 1 : 0;
        while ((tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_ERROR) && tokenPosition > 0) tokenPosition--;
        insertionPoint = tokenizer.getTokenAt(tokenPosition).getEndOffset();
    } else {
        // if not, do a regular replacement
        insertionPoint = oldNodeIA.getLeftToken().getStartOffset() - 1;
    }
    // insert after the first non-layout token
    int tokenPositionEnd = oldNodeIA.getRightToken().getIndex();
    while ((tokenizer.getTokenAt(tokenPositionEnd).getEndOffset() < tokenizer.getTokenAt(tokenPositionEnd).getStartOffset() || tokenizer.getTokenAt(tokenPositionEnd).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPositionEnd).getKind() == IToken.TK_ERROR) && tokenPositionEnd > 0) tokenPositionEnd--;
    suffixPoint = tokenizer.getTokenAt(tokenPositionEnd).getEndOffset() + 1;
    CompletionKind kind;
    if (completionKind.equals("recovery")) {
        kind = CompletionKind.recovery;
    } else {
        kind = CompletionKind.expansion;
    }
    return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, kind, prefix, suffix);
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) Completion(org.metaborg.core.completion.Completion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) CompletionKind(org.metaborg.core.completion.CompletionKind) ITokens(org.spoofax.jsglr.client.imploder.ITokens)

Example 9 with StrategoTerm

use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.

the class JSGLRCompletionService method findTopMostCompletionNode.

private StrategoTerm findTopMostCompletionNode(StrategoTerm newNode) {
    StrategoTerm parent = (StrategoTerm) ParentAttachment.getParent(newNode);
    if (parent == null) {
        return newNode;
    }
    ImploderAttachment ia = ImploderAttachment.get(parent);
    if (ia.getSort() == null || ia.isNestedCompletion()) {
        return newNode;
    }
    return findTopMostCompletionNode(parent);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment)

Example 10 with StrategoTerm

use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.

the class JSGLRCompletionService method calculateNestedCompletionProposals.

private Collection<IStrategoTerm> calculateNestedCompletionProposals(IStrategoTerm mainNestedCompletionTerm, IStrategoTerm nestedCompletionTerm, ITermFactory termFactory, IStrategoTerm completionAst, String languageName, HybridInterpreter runtime) throws MetaborgException {
    Collection<IStrategoTerm> inputsStratego = Lists.newLinkedList();
    Collection<IStrategoTerm> nestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
    for (IStrategoTerm innerNestedCompletionTerm : nestedCompletionTerms) {
        Collection<IStrategoTerm> inputsStrategoInnerNested = calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime);
        for (IStrategoTerm inputStrategoNested : inputsStrategoInnerNested) {
            final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
            if (proposalTermNested == null) {
                logger.error("Getting proposals for {} failed", inputStrategoNested);
                continue;
            }
            final StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) nestedCompletionTerm);
            final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTermNested.getSubterm(1));
            IStrategoTerm parenthesized = parenthesizeTerm(mainNestedCompletionTerm, termFactory);
            final IStrategoTerm inputStrategoInnerNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, mainNestedCompletionTerm, proposalTermNested.getSubterm(0), replaceTermText, parenthesized);
            inputsStratego.add(inputStrategoInnerNested);
        }
    }
    Collection<IStrategoTerm> inputsStrategoInner = calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime);
    for (IStrategoTerm inputStrategoNested : inputsStrategoInner) {
        final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
        if (proposalTermNested == null) {
            logger.error("Getting proposals for {} failed", inputStrategoNested);
            continue;
        }
        final StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) nestedCompletionTerm);
        final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTermNested.getSubterm(1));
        IStrategoTerm parenthesized = parenthesizeTerm(mainNestedCompletionTerm, termFactory);
        final IStrategoTerm inputStrategoInnerNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, mainNestedCompletionTerm, proposalTermNested.getSubterm(0), replaceTermText, parenthesized);
        inputsStratego.add(inputStrategoInnerNested);
    }
    return inputsStratego;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) StrategoConstructor(org.spoofax.terms.StrategoConstructor)

Aggregations

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