Search in sources :

Example 6 with ImploderAttachment

use of org.spoofax.jsglr.client.imploder.ImploderAttachment 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 7 with ImploderAttachment

use of org.spoofax.jsglr.client.imploder.ImploderAttachment in project spoofax by metaborg.

the class GetSortNamePrimitive method call.

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) {
    final ImploderAttachment attachment = tvars[0].getAttachment(ImploderAttachment.TYPE);
    if (attachment == null) {
        return false;
    }
    final String sort = attachment.getElementSort();
    if (sort == null) {
        return false;
    }
    env.setCurrent(env.getFactory().makeString(sort));
    return true;
}
Also used : ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment)

Example 8 with ImploderAttachment

use of org.spoofax.jsglr.client.imploder.ImploderAttachment 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 9 with ImploderAttachment

use of org.spoofax.jsglr.client.imploder.ImploderAttachment in project spoofax by metaborg.

the class JSGLRCompletionService method optionalCompletions.

public Collection<ICompletion> optionalCompletions(Iterable<IStrategoTerm> optionals, boolean blankLineCompletion, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    for (IStrategoTerm optional : optionals) {
        ImploderAttachment attachment = optional.getAttachment(ImploderAttachment.TYPE);
        String sort = attachment.getSort().substring(0, attachment.getSort().length());
        String placeholderName = sort + "-Plhdr";
        IStrategoAppl optionalPlaceholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
        final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), optional, optionalPlaceholder);
        // call Stratego part of the framework to compute change
        final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
        final IStrategoTerm proposalsOptional = strategoCommon.invoke(runtime, strategoInput, "get-proposals-optional-" + languageName);
        if (proposalsOptional == null) {
            logger.error("Getting proposals for {} failed", strategoInput);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposalsOptional) {
            if (!(proposalTerm instanceof IStrategoTuple)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 4 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final String name = Tools.asJavaString(tuple.getSubterm(0));
            final String text = Tools.asJavaString(tuple.getSubterm(1));
            final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
            final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
            if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, blankLineCompletion, "", "");
                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) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Example 10 with ImploderAttachment

use of org.spoofax.jsglr.client.imploder.ImploderAttachment 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)

Aggregations

ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)15 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)13 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)12 ICompletion (org.metaborg.core.completion.ICompletion)8 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)8 ITokens (org.spoofax.jsglr.client.imploder.ITokens)8 StrategoTerm (org.spoofax.terms.StrategoTerm)8 Completion (org.metaborg.core.completion.Completion)7 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)7 CompletionKind (org.metaborg.core.completion.CompletionKind)5 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)4 IToken (org.spoofax.jsglr.client.imploder.IToken)4 AStrategoTermVisitor (org.spoofax.terms.visitor.AStrategoTermVisitor)3 IStrategoTermVisitor (org.spoofax.terms.visitor.IStrategoTermVisitor)3 ISourceRegion (org.metaborg.core.source.ISourceRegion)1 ICategory (org.metaborg.core.style.ICategory)1 IRegionCategory (org.metaborg.core.style.IRegionCategory)1 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)1 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)1 StrategoAppl (org.spoofax.terms.StrategoAppl)1