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;
}
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;
}
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;
}
Aggregations