Search in sources :

Example 6 with ICompletion

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

the class JSGLRCompletionService method createCompletionInsertionTermFixing.

private ICompletion createCompletionInsertionTermFixing(String name, String text, String additionalInfo, String prefix, String suffix, StrategoAppl change, String completionKind) {
    final StrategoTerm newNode = (StrategoTerm) change.getSubterm(0);
    if (change.getSubtermCount() != 1 || !(newNode instanceof IStrategoAppl)) {
        return null;
    }
    final String sort = ImploderAttachment.getSort(newNode);
    int insertionPoint, suffixPoint;
    ITokens tokenizer = ImploderAttachment.getTokenizer(newNode);
    final StrategoTerm topMostAmb = findTopMostAmbNode(newNode);
    final ImploderAttachment topMostAmbIA = topMostAmb.getAttachment(ImploderAttachment.TYPE);
    // get the last non-layout token before the topmost ambiguity
    int tokenPosition = topMostAmbIA.getLeftToken().getIndex() - 1 > 0 ? topMostAmbIA.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();
    if (topMostAmbIA.getRightToken().getEndOffset() < topMostAmbIA.getRightToken().getStartOffset()) {
        // keep all the characters after the last non-layout token if completion ends with a
        // placeholder
        tokenPosition = topMostAmbIA.getRightToken().getIndex();
        while (tokenPosition > 0 && (tokenizer.getTokenAt(tokenPosition).getEndOffset() < tokenizer.getTokenAt(tokenPosition).getStartOffset() || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT)) tokenPosition--;
        suffixPoint = tokenizer.getTokenAt(tokenPosition).getEndOffset() + 1;
    } else {
        // skip all the (erroneous) characters that were in the text already
        suffixPoint = topMostAmbIA.getRightToken().getEndOffset() + 1;
    }
    CompletionKind kind;
    if (completionKind.equals("recovery")) {
        kind = CompletionKind.recovery;
    } else if (completionKind.equals("expansionEditing")) {
        kind = CompletionKind.expansionEditing;
    } 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 7 with ICompletion

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

the class JSGLRCompletionService method createCompletionReplaceTerm.

private ICompletion createCompletionReplaceTerm(String name, String text, String additionalInfo, StrategoAppl change, boolean blankLineCompletion, String prefix, String suffix) {
    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 (tokenPosition > 0 && (tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_ERROR)) tokenPosition--;
        insertionPoint = tokenizer.getTokenAt(tokenPosition).getEndOffset();
        // if completion does not spam multiple lines preserve everything starting at the first non-layout char
        if (!additionalInfo.contains("\n")) {
            tokenPosition = oldNodeIA.getLeftToken().getIndex() + 1 < tokenizer.getTokenCount() ? oldNodeIA.getLeftToken().getIndex() + 1 : tokenizer.getTokenCount() - 1;
            while (tokenPosition < tokenizer.getTokenCount() && (tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_ERROR)) tokenPosition++;
            suffixPoint = tokenizer.getTokenAt(tokenPosition).getStartOffset();
        } else {
            // if completion spams multiple lines keep the lines
            suffixPoint = insertionPoint + 1;
        }
        // if completion is triggered in an empty line, consume that line
        IToken checkToken;
        boolean blankLine = false;
        if (blankLineCompletion) {
            for (; tokenPosition < tokenizer.getTokenCount(); tokenPosition++) {
                checkToken = tokenizer.getTokenAt(tokenPosition);
                if (tokenizer.toString(checkToken, checkToken).contains("\n")) {
                    suffixPoint = checkToken.getEndOffset();
                    if (!blankLine) {
                        blankLine = true;
                    } else {
                        break;
                    }
                }
            }
        }
    } else {
        // if not, do a regular replacement
        insertionPoint = oldNodeIA.getLeftToken().getStartOffset() - 1;
        suffixPoint = oldNodeIA.getRightToken().getEndOffset() + 1;
    }
    CompletionKind kind;
    if (prefix.equals("") && suffix.equals("")) {
        kind = CompletionKind.expansion;
    } else {
        kind = CompletionKind.expansionEditing;
    }
    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) IToken(org.spoofax.jsglr.client.imploder.IToken) 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 8 with ICompletion

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

the class JSGLRCompletionService method completionCorrectPrograms.

public Collection<ICompletion> completionCorrectPrograms(int position, boolean blankLineCompletion, ISpoofaxParseUnit parseResult) throws MetaborgException {
    Collection<ICompletion> completions = Sets.newHashSet();
    final FileObject location = parseResult.source();
    final ILanguageImpl language = parseResult.input().langImpl();
    final String languageName = language.belongsTo().name();
    for (ILanguageComponent component : language.components()) {
        final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
        final ITermFactory termFactory = termFactoryService.get(component, null, false);
        final Map<IStrategoTerm, Boolean> leftRecursiveTerms = new HashMap<IStrategoTerm, Boolean>();
        final Map<IStrategoTerm, Boolean> rightRecursiveTerms = new HashMap<IStrategoTerm, Boolean>();
        final Iterable<IStrategoTerm> terms = tracingTermsCompletions(position, parseResult.ast(), new SourceRegion(position), runtime, termFactory, languageName, leftRecursiveTerms, rightRecursiveTerms);
        final IStrategoAppl placeholder = getPlaceholder(position, terms);
        final Iterable<IStrategoList> lists = getLists(terms, leftRecursiveTerms, rightRecursiveTerms);
        final Iterable<IStrategoTerm> optionals = getOptionals(terms, leftRecursiveTerms, rightRecursiveTerms);
        final Iterable<IStrategoTerm> leftRecursive = getLeftRecursiveTerms(position, terms, leftRecursiveTerms);
        final Iterable<IStrategoTerm> rightRecursive = getRightRecursiveTerms(position, terms, rightRecursiveTerms);
        if (placeholder != null) {
            completions.addAll(placeholderCompletions(placeholder, languageName, component, location));
        } else {
            if (Iterables.size(lists) != 0) {
                completions.addAll(listsCompletions(position, blankLineCompletion, lists, languageName, component, location));
            }
            if (Iterables.size(optionals) != 0) {
                completions.addAll(optionalCompletions(optionals, blankLineCompletion, languageName, component, location));
            }
        // TODO Improve recursive completions
        // if(Iterables.size(leftRecursive) != 0 || Iterables.size(rightRecursive) != 0) {
        // completions
        // .addAll(recursiveCompletions(leftRecursive, rightRecursive, languageName, component, location));
        // }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) HashMap(java.util.HashMap) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) SourceRegion(org.metaborg.core.source.SourceRegion) ISourceRegion(org.metaborg.core.source.ISourceRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 9 with ICompletion

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

the class JSGLRCompletionService method createCompletionInsertBefore.

private ICompletion createCompletionInsertBefore(String name, String text, String additionalInfo, StrategoAppl change) {
    final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
    final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
    // expect two terms and 1st should be an element of a list
    final StrategoTerm oldList = (StrategoTerm) ParentAttachment.getParent(oldNode);
    if (change.getSubtermCount() != 2 || !(oldNode instanceof IStrategoAppl) || !(newNode instanceof IStrategoList) || !(oldList instanceof IStrategoList)) {
        return null;
    }
    final String sort = ImploderAttachment.getSort(oldNode);
    int insertionPoint, suffixPoint;
    ITokens tokenizer = ImploderAttachment.getTokenizer(oldNode);
    IStrategoTerm[] subterms = oldList.getAllSubterms();
    int indexOfElement;
    for (indexOfElement = 0; indexOfElement < subterms.length; indexOfElement++) {
        if (subterms[indexOfElement] == oldNode)
            break;
    }
    // if inserted element is first (only two elements)
    if (indexOfElement == 0) {
        // insert after the first non-layout token before the leftmost token of the
        // completion node
        final ImploderAttachment oldNodeIA = oldNode.getAttachment(ImploderAttachment.TYPE);
        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 inserted element is not first
        // insert after at end offset of the rightmost token of the element before the
        // completion
        StrategoTerm elementBefore = (StrategoTerm) oldList.getSubterm(indexOfElement - 1);
        insertionPoint = elementBefore.getAttachment(ImploderAttachment.TYPE).getRightToken().getEndOffset();
    }
    // if completion is separated by a newline, preserve indentation of the subsequent node
    // else separation follows from the grammar
    String separator = "";
    for (int i = text.length() - 1; i >= 0; i--) {
        if (text.charAt(i) == additionalInfo.charAt(additionalInfo.length() - 1)) {
            break;
        }
        separator = text.charAt(i) + separator;
    }
    IToken checkToken = oldNode.getAttachment(ImploderAttachment.TYPE).getLeftToken();
    int checkTokenIdx = oldNode.getAttachment(ImploderAttachment.TYPE).getLeftToken().getIndex();
    suffixPoint = insertionPoint;
    if (separator.contains("\n")) {
        for (; checkTokenIdx >= 0; checkTokenIdx--) {
            checkToken = tokenizer.getTokenAt(checkTokenIdx);
            if (tokenizer.toString(checkToken, checkToken).contains("\n")) {
                break;
            }
            suffixPoint = checkToken.getStartOffset();
        }
    } else {
        suffixPoint = checkToken.getStartOffset();
    }
    return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, CompletionKind.expansion);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ICompletion(org.metaborg.core.completion.ICompletion) Completion(org.metaborg.core.completion.Completion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) ITokens(org.spoofax.jsglr.client.imploder.ITokens)

Example 10 with ICompletion

use of org.metaborg.core.completion.ICompletion 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)

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