Java Code Examples for org.netbeans.api.lexer.Token#offset()

The following examples show how to use org.netbeans.api.lexer.Token#offset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: XMLSyntaxSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: CompletionUtil.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: RemovedTokenList.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 5
Source File: TplBracesMatching.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean beforeCommentTag(TokenHierarchy<Document> th, TokenSequence<TplTopTokenId> ts, int[] delimsLength, int searchOffset) {
    if (ts.moveNext()) {
        Token<TplTopTokenId> nextToken = ts.token();
        if (nextToken != null) {
            if (nextToken.id() == TplTopTokenId.T_COMMENT) {
                ts.movePrevious();
                return nextToken.offset(th) - searchOffset <= delimsLength[0];
            } else if (nextToken.id() == TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
                if (ts.moveNext()) {
                    Token<TplTopTokenId> nextNextToken = ts.token();
                    ts.movePrevious();
                    if (nextNextToken != null) {
                        if (nextNextToken.id() == TplTopTokenId.T_COMMENT) {
                            ts.movePrevious();
                            return nextToken.offset(th) - searchOffset <= delimsLength[0];
                        }
                    }
                }
                ts.movePrevious();
            }
        }
    }
    return false;
}
 
Example 6
Source File: GspBracesMatcher.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Iterates through the <code>tokenSequence</code> backwards and return the first
 * match with <code>tokenID</code>. If you would like to search forwards, please
 * use {@link #findForwards(org.netbeans.api.lexer.TokenSequence, org.netbeans.modules.groovy.gsp.lexer.GspTokenId)}
 * method instead.
 *
 * @param tokenSequence we are iterating through
 * @param tokenID token we are looking for
 * @return offset of the first match token or zero if nothing has been found
 *         or if the task was canceled
 */
private int findBackwards(TokenSequence<GspTokenId> tokenSequence, GspTokenId tokenID) {
    while (true) {
        if (MatcherContext.isTaskCanceled()) {
            break;
        }

        Token<GspTokenId> nextGspToken = tokenSequence.token();
        if (nextGspToken.id() == tokenID) {
            return nextGspToken.offset(TokenHierarchy.get(document));
        }
        if (!tokenSequence.movePrevious()) {
            break;
        }
    }
    return 0;
}
 
Example 7
Source File: GspBracesMatcher.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Iterates through the <code>tokenSequence</code> forwards and return the first
 * match with <code>tokenID</code>. If you would like to search backwards, please
 * use {@link #findBackwards(org.netbeans.api.lexer.TokenSequence, org.netbeans.modules.groovy.gsp.lexer.GspTokenId)}
 * method instead.
 *
 * @param tokenSequence we are iterating through
 * @param tokenID token we are looking for
 * @return offset of the first match token or zero if nothing has been found
 *         or if the task was canceled
 */
private int findForwards(TokenSequence<GspTokenId> tokenSequence, GspTokenId tokenID) {
    while (true) {
        if (MatcherContext.isTaskCanceled()) {
            break;
        }

        Token<GspTokenId> nextToken = tokenSequence.token();
        if (nextToken.id() == tokenID) {
            return nextToken.offset(TokenHierarchy.get(document)) + nextToken.length();
        }
        if (!tokenSequence.moveNext()) {
            break;
        }
    }
    return 0;
}
 
Example 8
Source File: MarkOccurrencesHighlighterBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
    }
 
Example 9
Source File: CreateQualifier.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private 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;
}
 
Example 10
Source File: LexerTestUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 11
Source File: TplBracesMatching.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean afterCommentTag(TokenHierarchy<Document> th, TokenSequence<TplTopTokenId> ts, int[] delimsLength, int searchOffset) {
    if (ts.movePrevious()) {
        Token<TplTopTokenId> prevToken = ts.token();
        if (prevToken != null) {
            ts.moveNext();
            return searchOffset - prevToken.offset(th) <= delimsLength[1] + 1;
        }
    }
    return false;
}
 
Example 12
Source File: CustomFoldManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: CustomFoldManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: ImplementAbstractMethodsHintError.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static int getClassDeclarationOffset(TokenHierarchy<?> th, int classNameOffset) {
    TokenSequence<PHPTokenId> ts = LexUtilities.getPHPTokenSequence(th, classNameOffset);
    ts.move(classNameOffset);
    ts.movePrevious();
    Token<? extends PHPTokenId> previousToken = LexUtilities.findPreviousToken(ts, Collections.<PHPTokenId>singletonList(PHPTokenId.PHP_CLASS));
    return previousToken.offset(th);
}
 
Example 15
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * 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 (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<JavaTokenId> 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<JavaTokenId> identifier = tokenSequence.token();
                startOffset = identifier.offset(info.getTokenHierarchy());
                endOffset = startOffset + identifier.length();
            }
        }
    }

    return new TextSpan(startOffset, endOffset);
}
 
Example 16
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: JspLexerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String getTokenInfo(Token token, TokenHierarchy tokenHierarchy) {
    return "TOKEN[text=\"" + token.text() + "\"; tokenId=" + token.id().name() + "; offset=" + token.offset(tokenHierarchy) + "]";
}
 
Example 18
Source File: AssignComments.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private int getEndPos(Token<JavaTokenId> comment) {
    return comment.offset(null) + comment.length();
}
 
Example 19
Source File: CdiEditorAnalysisFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static List<Integer> getElementPosition(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 startTree = null;
    
    if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())){
        startTree = ((ClassTree)tree).getModifiers();
        
    } else if (tree.getKind() == Tree.Kind.METHOD){
        startTree = ((MethodTree)tree).getReturnType();
    } else if (tree.getKind() == Tree.Kind.VARIABLE){
        startTree = ((VariableTree)tree).getType();
    }
    
    if (startTree != null){
        int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(),
                startTree);
        
        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();
            }
        }
    }
    
    List<Integer> result = new ArrayList<Integer>(2);
    result.add(startOffset);
    result.add(endOffset );
    return result;
}
 
Example 20
Source File: BeanValidationConfigImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public HashMap<FileObject, ConstraintMapping> getConstraintMappings() {
    HashMap<FileObject, ConstraintMapping> constraintMap = new HashMap<FileObject, ConstraintMapping>();
    getDocument();
    insertOffset = 0;
    if (document != null) {
        try {
            document.readLock();
            TokenHierarchy hi = TokenHierarchy.get(document);
            TokenSequence ts = hi.tokenSequence();
            boolean started = false;
            boolean ended = false;
            boolean findInsertPoint = false;
            int startOffset = 0;
            int endOffset = 0;
            FileObject constraintFile = null;
            while (ts.moveNext()) {
                Token t = ts.token();

                if (t.id() == XMLTokenId.TAG && t.text().equals("<validation-config")) {    //NOI18N
                    findInsertPoint = true;
                }
                if (findInsertPoint && t.id() == XMLTokenId.TAG && t.text().equals(">")) {    //NOI18N
                    insertOffset = t.offset(hi) +t.length();
                    findInsertPoint = false;
                }
                if (!started && t.id() == XMLTokenId.TAG && t.text().equals("<constraint-mapping")) {    //NOI18N
                    startOffset = t.offset(hi);
                    endOffset = startOffset +t.length();
                    started = true;
                    ended = true;
                }
                if (started && t.id() == XMLTokenId.TEXT) {
                    endOffset += t.length();
                    String value = t.text().toString();
                    WebModule wm = WebModule.getWebModule(configFile);
                    constraintFile = wm.getDocumentBase().getFileObject(value);
                }
                if (started && t.id() == XMLTokenId.TAG && t.text().equals("</constraint-mapping")) {    //NOI18N
                    endOffset += t.length()+1;
                    ended = true;
                    started = false;
                }

                if (ended && t.id() == XMLTokenId.TAG && t.text().equals(">")) {    //NOI18N
                    endOffset +=t.length();
                    ended = false;
                }
                
                if (!started && !ended && constraintFile != null) {
                    ConstraintMapping constraintMapping = new ConstraintMappingImpl(constraintFile, startOffset, endOffset);
                    constraintMap.put(constraintFile, constraintMapping);
                    constraintFile = null;
                }
            }
        }finally{
            document.readUnlock();
        }
    }
    return constraintMap;
}