Search in sources :

Example 1 with ImploderAttachment

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

the class CategorizerService method sortConsCategory.

private ICategory sortConsCategory(StylerFacet facet, IStrategoTerm term) {
    final ImploderAttachment imploderAttachment = ImploderAttachment.get(term);
    final String sort = imploderAttachment.getSort();
    if (sort == null) {
        return null;
    }
    // LEGACY: for some reason, when using concrete syntax extensions, all sorts are appended with _sort.
    final String massagedSort = sort.replace("_sort", "");
    if (term.getTermType() == IStrategoTerm.APPL) {
        final String cons = ((IStrategoAppl) term).getConstructor().getName();
        if (facet.hasSortConsStyle(massagedSort, cons)) {
            return new SortConsCategory(massagedSort, cons);
        } else if (facet.hasConsStyle(cons)) {
            return new ConsCategory(cons);
        } else if (facet.hasSortStyle(massagedSort)) {
            return new SortCategory(massagedSort);
        }
        return null;
    }
    if (facet.hasSortStyle(massagedSort)) {
        return new SortCategory(massagedSort);
    }
    return null;
}
Also used : ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment)

Example 2 with ImploderAttachment

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

the class JSGLRCompletionService method createCompletionInsertAtEnd.

private ICompletion createCompletionInsertAtEnd(String name, String text, String additionalInfo, StrategoAppl change, boolean blankLineCompletion) {
    final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
    final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
    // expected two lists
    if (change.getSubtermCount() != 2 || !(oldNode instanceof IStrategoList) || !(newNode instanceof IStrategoList)) {
        return null;
    }
    final String sort = ImploderAttachment.getElementSort(oldNode);
    int insertionPoint, suffixPoint;
    ITokens tokenizer = ImploderAttachment.getTokenizer(oldNode);
    final ImploderAttachment oldListIA = oldNode.getAttachment(ImploderAttachment.TYPE);
    int tokenPosition;
    // node
    if (oldNode.getSubtermCount() == 0) {
        tokenPosition = oldListIA.getLeftToken().getIndex() - 1 > 0 ? oldListIA.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 list is not empty
        // insert after at end offset of the rightmost token of the element before the
        // completion
        StrategoTerm elementBefore = (StrategoTerm) oldNode.getSubterm(oldNode.getAllSubterms().length - 1);
        int leftIdx = elementBefore.getAttachment(ImploderAttachment.TYPE).getLeftToken().getIndex();
        int rightIdx = elementBefore.getAttachment(ImploderAttachment.TYPE).getRightToken().getIndex();
        while ((tokenizer.getTokenAt(rightIdx).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(rightIdx).getLength() == 0) && rightIdx > leftIdx) {
            rightIdx--;
        }
        insertionPoint = tokenizer.getTokenAt(rightIdx).getEndOffset();
        tokenPosition = rightIdx;
    }
    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;
                }
            }
        }
    }
    return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, CompletionKind.expansion);
}
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) 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 3 with ImploderAttachment

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

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

the class JSGLRCompletionService method findNestedCompletionTerm.

private Collection<IStrategoTerm> findNestedCompletionTerm(final StrategoTerm ast, final boolean excludeIdTerm) {
    final Collection<IStrategoTerm> completionTerms = Lists.newLinkedList();
    final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {

        @Override
        public boolean visit(IStrategoTerm term) {
            ImploderAttachment ia = term.getAttachment(ImploderAttachment.TYPE);
            if (excludeIdTerm && term.equals(ast)) {
                return true;
            }
            if (ia.isNestedCompletion()) {
                completionTerms.add(term);
                return false;
            }
            return true;
        }
    };
    StrategoTermVisitee.topdown(visitor, ast);
    return completionTerms;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) AStrategoTermVisitor(org.spoofax.terms.visitor.AStrategoTermVisitor) IStrategoTermVisitor(org.spoofax.terms.visitor.IStrategoTermVisitor) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment)

Example 5 with ImploderAttachment

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

the class JSGLRCompletionService method findCompletionTerm.

private Collection<IStrategoTerm> findCompletionTerm(StrategoTerm ast) {
    final Collection<IStrategoTerm> completionTerms = Lists.newLinkedList();
    final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {

        @Override
        public boolean visit(IStrategoTerm term) {
            ImploderAttachment ia = term.getAttachment(ImploderAttachment.TYPE);
            if (ia.isNestedCompletion()) {
                return false;
            }
            if (ia.isCompletion()) {
                completionTerms.add(term);
                return false;
            }
            return true;
        }
    };
    StrategoTermVisitee.topdown(visitor, ast);
    return completionTerms;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) AStrategoTermVisitor(org.spoofax.terms.visitor.AStrategoTermVisitor) IStrategoTermVisitor(org.spoofax.terms.visitor.IStrategoTermVisitor) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment)

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