本文整理汇总了Java中org.netbeans.api.lexer.Token.offset方法的典型用法代码示例。如果您正苦于以下问题:Java Token.offset方法的具体用法?Java Token.offset怎么用?Java Token.offset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.netbeans.api.lexer.Token
的用法示例。
在下文中一共展示了Token.offset方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNamespaceInsertionOffset
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
/**
* Finds the namespace insertion offset in the xml document.
*/
public static int getNamespaceInsertionOffset(Document document) {
int offset = 0;
((AbstractDocument)document).readLock();
try {
TokenHierarchy th = TokenHierarchy.get(document);
TokenSequence ts = th.tokenSequence();
while(ts.moveNext()) {
Token nextToken = ts.token();
if(nextToken.id() == XMLTokenId.TAG && nextToken.text().toString().equals(">")) {
offset = nextToken.offset(th);
break;
}
}
} finally {
((AbstractDocument)document).readUnlock();
}
return offset;
}
示例2: findIdentifierSpan
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
public static int[] findIdentifierSpan( final TreePath decl, final CompilationInfo info, final Document doc) {
final int[] result = new int[] {-1, -1};
Runnable r = new Runnable() {
public void run() {
Token<JavaTokenId> t = findIdentifierSpan(info, doc, decl);
if (t != null) {
result[0] = t.offset(null);
result[1] = t.offset(null) + t.length();
}
}
};
if (doc != null) {
doc.render(r);
} else {
r.run();
}
return result;
}
示例3: runWithSequence
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
/**
* Exeutes user code with {@link TokenSequence} positioned at a particular token.
* This convenience method works much like {@link #runWithSequence(int, org.netbeans.modules.xml.text.api.dom.XMLSyntaxSupport.SequenceCallable)},
* except that Token (its starting offset) is used to position the sequence instead of raw offset value.
*
* @param <T>
* @param startFrom token to start from
* @param userCode user code to execute
* @return user-defined value
* @throws BadLocationException if the user code throws BadLocationException
*/
public <T> T runWithSequence(Token<XMLTokenId> startFrom, SequenceCallable<T> userCode) throws BadLocationException {
T result;
TokenSequence old = null;
try {
((AbstractDocument)document).readLock();
old = cachedSequence.get();
cachedSequence.remove();
TokenHierarchy th = TokenHierarchy.get(((AbstractDocument)document));
TokenSequence ts = th.tokenSequence();
if (ts == null) {
throw new BadLocationException("No sequence for position", startFrom.offset(null)); // NOI18N
}
cachedSequence.set(ts);
synchronized (ts) {
ts.move(startFrom.offset(th));
ts.moveNext();
result = userCode.call(ts);
}
} finally {
cachedSequence.set(old);
((AbstractDocument)document).readUnlock();
}
return result;
}
示例4: tokenOffset
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
@Override
public int tokenOffset(int index) {
Token<?> token = existingToken(index);
if (token.isFlyweight()) {
int offset = 0;
while (--index >= 0) {
token = existingToken(index);
offset += token.length();
if (!token.isFlyweight()) {
// Return from here instead of break; - see code after while()
return offset + token.offset(null);
}
}
// might remove token sequence starting with flyweight
return removedTokensStartOffset + offset;
} else { // non-flyweight offset
return token.offset(null);
}
}
示例5: scanToken
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
private FoldMarkInfo scanToken(Token token) throws BadLocationException {
// ignore any token that is not comment
if (token.id().primaryCategory() != null && token.id().primaryCategory().startsWith("comment")) { //NOI18N
Matcher matcher = pattern.matcher(token.text());
if (matcher.find()) {
if (matcher.group(1) != null) { // fold's start mark found
boolean state;
if (matcher.group(3) != null) {
state = "collapsed".equals(matcher.group(3)); // remember the defaultstate // NOI18N
} else {
state = "collapsed".equals(matcher.group(5));
}
if (matcher.group(2) != null) { // fold's id exists
Boolean collapsed = (Boolean)customFoldId.get(matcher.group(2));
if (collapsed != null)
state = collapsed.booleanValue(); // fold's state is already known from the past
else
customFoldId.put(matcher.group(2), Boolean.valueOf(state));
}
return new FoldMarkInfo(true, token.offset(null), matcher.end(0), matcher.group(2), state, matcher.group(4)); // NOI18N
} else { // fold's end mark found
return new FoldMarkInfo(false, token.offset(null), matcher.end(0), null, false, null);
}
}
}
return null;
}
示例6: isIn
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
private boolean isIn(int caretPosition, Token span) {
// System.err.println("caretPosition = " + caretPosition );
// System.err.println("span[0]= " + span[0]);
// System.err.println("span[1]= " + span[1]);
if (span == null)
return false;
return span.offset(null) <= caretPosition && caretPosition <= span.offset(null) + span.length();
}
示例7: findUnresolvedElementSpan
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
private static int[] findUnresolvedElementSpan(CompilationInfo info, int offset) throws IOException {
Token t = findUnresolvedElementToken(info, offset);
if (t != null) {
return new int[] {
t.offset(null),
t.offset(null) + t.length()
};
}
return null;
}
示例8: assertTokenEquals
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
private static void assertTokenEquals(String message, TokenSequence<?> ts, TokenId id, String text, int offset, int length) {
message = messagePrefix(message);
Token<?> t = ts.token();
TestCase.assertNotNull("Token is null", t);
TokenId tId = t.id();
TestCase.assertEquals(message + "Invalid token.id() for text=\"" + debugTextOrNull(t.text()) + '"', id, tId);
if (length != -1) {
TestCase.assertEquals("Invalid token length", length, t.length());
}
if (text != null) {
CharSequence tText = t.text();
assertTextEquals(message + "Invalid token.text() for id=" + LexerUtilsConstants.idToString(id), text, tText);
TestCase.assertEquals(message + "Invalid token.length()", text.length(), t.length());
}
if (offset != -1) {
int tsOffset = ts.offset();
TestCase.assertEquals(message + "Invalid tokenSequence.offset()", offset, tsOffset);
// It should also be true that if the token is non-flyweight then
// ts.offset() == t.offset()
// and if it's flyweight then t.offset() == -1
int tOffset = t.offset(null);
assertTokenOffsetMinusOneForFlyweight(t.isFlyweight(), tOffset);
if (!t.isFlyweight()) {
assertTokenOffsetsEqual(message, tOffset, offset);
}
}
}
示例9: scanToken
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
private FoldMarkInfo scanToken(Token token) throws BadLocationException {
// ignore any token that is not comment
if (token.id().primaryCategory() != null && token.id().primaryCategory().startsWith(tokenId)) { //NOI18N
Matcher matcher = pattern.matcher(token.text());
if (matcher.find()) {
if (matcher.group(1) != null) { // fold's start mark found
boolean state;
if (matcher.group(3) != null) {
state = "collapsed".equals(matcher.group(3)); // remember the defaultstate // NOI18N
} else {
state = "collapsed".equals(matcher.group(5));
}
if (matcher.group(2) != null) { // fold's id exists
Boolean collapsed = (Boolean)customFoldId.get(matcher.group(2));
if (collapsed != null)
state = collapsed.booleanValue(); // fold's state is already known from the past
else
customFoldId.put(matcher.group(2), Boolean.valueOf(state));
}
return new FoldMarkInfo(true, token.offset(null), matcher.end(0), matcher.group(2), state, matcher.group(4)); // NOI18N
} else { // fold's end mark found
return new FoldMarkInfo(false, token.offset(null), matcher.end(0), null, false, null);
}
}
}
return null;
}
示例10: getEndPos
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
private int getEndPos(Token<JavaTokenId> comment) {
return comment.offset(null) + comment.length();
}
示例11: HighlightImpl
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
public HighlightImpl(Document doc, Token token, Collection<ColoringAttributes> colorings) {
this.doc = doc;
this.start = token.offset(null);
this.end = token.offset(null) + token.text().length();
this.colorings = colorings;
}
示例12: getUnderlineSpan
import org.netbeans.api.lexer.Token; //导入方法依赖的package包/类
/**
* This method returns the part of the syntax tree to be highlighted.
* It will be usually the class/method/variable identifier.
*/
public static TextSpan getUnderlineSpan(CompilationInfo info, Tree tree){
SourcePositions srcPos = info.getTrees().getSourcePositions();
int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree);
int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree);
Tree startSearchingForNameIndentifierBehindThisTree = null;
if(tree != null) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())){
startSearchingForNameIndentifierBehindThisTree = ((ClassTree)tree).getModifiers();
} else if (tree.getKind() == Tree.Kind.METHOD){
startSearchingForNameIndentifierBehindThisTree = ((MethodTree)tree).getReturnType();
} else if (tree.getKind() == Tree.Kind.VARIABLE){
startSearchingForNameIndentifierBehindThisTree = ((VariableTree)tree).getType();
}
if (startSearchingForNameIndentifierBehindThisTree != null){
int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(),
startSearchingForNameIndentifierBehindThisTree);
TokenSequence tokenSequence = info.getTreeUtilities().tokensFor(tree);
if (tokenSequence != null){
boolean eob = false;
tokenSequence.move(searchStart);
do{
eob = !tokenSequence.moveNext();
}
while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER);
if (!eob){
Token identifier = tokenSequence.token();
startOffset = identifier.offset(info.getTokenHierarchy());
endOffset = startOffset + identifier.length();
}
}
}
}
return new TextSpan(startOffset, endOffset);
}