Search in sources :

Example 1 with OffsetRange

use of blue.ui.editor.support.OffsetRange in project blue by kunstmusik.

the class ClojureLexUtilities method findBwd.

/**
 * Search backwards in the token sequence until a token of type <code>up</code> is found
 */
public static OffsetRange findBwd(BaseDocument doc, TokenSequence<? extends ClojureTokenId> ts, char up, char down) {
    int balance = 0;
    while (ts.movePrevious()) {
        Token<? extends ClojureTokenId> token = ts.token();
        TokenId id = token.id();
        if (textEquals(token.text(), up)) {
            if (balance == 0) {
                return new OffsetRange(ts.offset(), ts.offset() + token.length());
            }
            balance++;
        } else if (textEquals(token.text(), down)) {
            balance--;
        }
    }
    return OffsetRange.NONE;
}
Also used : OffsetRange(blue.ui.editor.support.OffsetRange) TokenId(org.netbeans.api.lexer.TokenId)

Example 2 with OffsetRange

use of blue.ui.editor.support.OffsetRange in project blue by kunstmusik.

the class ClojureLexUtilities method findBwd.

/**
 * Search forwards in the token sequence until a matching closing token is found
 * so keeps track of nested pairs of up-down eg (()) is ignored if we're
 * searching for a )
 * @param ts the TokenSequence set to the position after an up
 * @param up the opening token eg { or [
 * @param down the closing token eg } or ]
 * @return the Range of closing token in our case 1 char
 */
public static OffsetRange findBwd(TokenSequence<? extends ClojureTokenId> ts, int up, int down) {
    int balance = 0;
    while (ts.movePrevious()) {
        Token<? extends ClojureTokenId> token = ts.token();
        TokenId id = token.id();
        if (token.id().ordinal() == up) {
            if (balance == 0) {
                return new OffsetRange(ts.offset(), ts.offset() + token.length());
            }
            balance++;
        } else if (token.id().ordinal() == down) {
            balance--;
        }
    }
    return OffsetRange.NONE;
}
Also used : OffsetRange(blue.ui.editor.support.OffsetRange) TokenId(org.netbeans.api.lexer.TokenId)

Example 3 with OffsetRange

use of blue.ui.editor.support.OffsetRange in project blue by kunstmusik.

the class ClojureBracesMatcher method findMatches.

@Override
public int[] findMatches() throws InterruptedException, BadLocationException {
    int[] ret = null;
    ((AbstractDocument) context.getDocument()).readLock();
    try {
        BaseDocument doc = (BaseDocument) context.getDocument();
        int offset = context.getSearchOffset();
        TokenSequence<? extends ClojureTokenId> ts = ClojureLexUtilities.getClojureTokenSequence(doc, offset);
        if (ts != null) {
            ts.move(offset);
            if (ts.moveNext()) {
                Token<? extends ClojureTokenId> token = ts.token();
                if (token != null) {
                    TokenId id = token.id();
                    int ordinal = id.ordinal();
                    OffsetRange r;
                    for (BracePair bp : bracePairs) {
                        if (ordinal == bp.open) {
                            r = ClojureLexUtilities.findFwd(ts, bp.open, bp.close);
                            ret = new int[] { r.getStart(), r.getEnd() };
                            break;
                        } else if (ordinal == bp.close) {
                            r = ClojureLexUtilities.findBwd(ts, bp.open, bp.close);
                            ret = new int[] { r.getStart(), r.getEnd() };
                            break;
                        }
                    }
                }
            }
        }
    } finally {
        ((AbstractDocument) context.getDocument()).readUnlock();
    }
    return ret;
}
Also used : OffsetRange(blue.ui.editor.support.OffsetRange) AbstractDocument(javax.swing.text.AbstractDocument) BaseDocument(org.netbeans.editor.BaseDocument) TokenId(org.netbeans.api.lexer.TokenId)

Aggregations

OffsetRange (blue.ui.editor.support.OffsetRange)3 TokenId (org.netbeans.api.lexer.TokenId)3 AbstractDocument (javax.swing.text.AbstractDocument)1 BaseDocument (org.netbeans.editor.BaseDocument)1