Java Code Examples for org.netbeans.api.lexer.TokenSequence#move()

The following examples show how to use org.netbeans.api.lexer.TokenSequence#move() . 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: CompletionContext.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Skips to and reads the root Element. Reads root element's attributes,
 * so we can detect whether required namespace(s) are present.
 * 
 * This MUST be called prior to readCurrentContent().
 * @param s 
 */
private void readRootElement(TokenSequence<XMLTokenId> seq) {
    seq.move(0);
    while (seq.moveNext()) {
        Token<XMLTokenId> t = seq.token();
        XMLTokenId id = t.id();
        if (id == XMLTokenId.TAG && t.length() > 1) {
            int startOffset = seq.offset();
            readTagContent(seq);
            // reassign stuff:
            copyToRoot();
            rootTagStartOffset = startOffset;
            rootAttrInsertOffset = startOffset + t.length();
            if (t.text().charAt(t.length() - 1) == '>') {
                rootAttrInsertOffset--;
                if (t.length() > 2 && t.text().charAt(t.length() - 2) == '/') {
                    rootAttrInsertOffset--;
                }
            }
            findRootInsertionPoint();
            return;
        }
    }
}
 
Example 2
Source File: TokenList.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static List<TokenSequence<?>> embeddedTokenSequences(TokenHierarchy<Document> th, int offset) {
    TokenSequence<?> embedded = th.tokenSequence();
    List<TokenSequence<?>> sequences = new ArrayList<TokenSequence<?>>();

    do {
        TokenSequence<?> seq = embedded;
        embedded = null;

        seq.move(offset);
        if (seq.moveNext()) {
            sequences.add(seq);
            embedded = seq.embedded();
        }
    } while (embedded != null);
    
    return sequences;
}
 
Example 3
Source File: LexerEmbeddingAdapter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void defineEmbeddings(TokenSequence seq, ConsoleModel model, ConsoleSection s) {
    F: for (Rng r : s.getPartRanges()) {
        seq.move(r.start);
        Token<JShellToken> tukac;
        
        W: while (seq.moveNext() && seq.offset() < r.end) {
            tukac = seq.token();
            switch (tukac.id()) {
                
                case JAVA:
                    seq.createEmbedding(JavaTokenId.language(), 0, 0);
                    
                    // fall through
                case WHITESPACE: 
                    break;
                    
                default:
                    break W;
            }
        }
    }
}
 
Example 4
Source File: ELHyperlinkProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected static int[] getELIdentifierSpan(Document doc, int offset) {
    TokenSequence<?> elTokenSequence = LexerUtils.getTokenSequence(doc, offset, ELTokenId.language(), false);
    if (elTokenSequence == null) {
        return null;
    }

    elTokenSequence.move(offset);
    if (!elTokenSequence.moveNext()) {
        return null; //no token
    }

    if (elTokenSequence.token().id() == ELTokenId.IDENTIFIER
            || elTokenSequence.token().id() == ELTokenId.STRING_LITERAL) { // string for bundle keys

        return new int[]{elTokenSequence.offset(), elTokenSequence.offset() + elTokenSequence.token().length()};
    }

    return null;
}
 
Example 5
Source File: HyperlinkProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    TokenHierarchy th = TokenHierarchy.get(document);
    TokenSequence<XMLTokenId> xml = th.tokenSequence(XMLTokenId.language());
    xml.move(offset);
    xml.moveNext();
    Token<XMLTokenId> token = xml.token();

    // when it's not a value -> do nothing.
    if (token == null) {
        return;
    }

    if (token.id() == XMLTokenId.TEXT) {
        hyperLinkInfo.calculateInfo(token, xml);
    }
}
 
Example 6
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean indexWithinCurrentLanguage(BaseDocument doc, int index) throws BadLocationException{
    TokenHierarchy tokenHierarchy = TokenHierarchy.get(doc);
    TokenSequence[] tokenSequences = (TokenSequence[]) tokenHierarchy.tokenSequenceList(supportedLanguagePath(), 0, Integer.MAX_VALUE).toArray(new TokenSequence[0]);
    
    for (TokenSequence tokenSequence: tokenSequences){
        TextBounds languageBounds = findTokenSequenceBounds(doc, tokenSequence);
        
        if (languageBounds.getAbsoluteStart() <= index && languageBounds.getAbsoluteEnd() >= index){
            tokenSequence.move(index);
            
            if (tokenSequence.moveNext()){
                // the newly entered \n character may or may not
                // form a separate token - work it around
                if (isWSToken(tokenSequence.token())){
                    tokenSequence.movePrevious(); 
                }
                
                return tokenSequence.embedded() == null && !isWSToken(tokenSequence.token());
            }
        }
    }
    
    return false;
}
 
Example 7
Source File: JQueryCodeCompletion.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String findFunctionName(ParserResult parserResult, int offset) {
    TokenSequence<? extends JsTokenId> ts = LexUtilities.getJsTokenSequence(parserResult.getSnapshot().getTokenHierarchy(), offset);
    if (ts == null) {
        return null;
    }
    ts.move(offset);
    if (!(ts.moveNext() && ts.movePrevious())) {
        return null;
    }
    Token<? extends JsTokenId> token = LexUtilities.findNext(ts, Arrays.asList(JsTokenId.WHITESPACE));
    while(token.id() != JsTokenId.EOL && token.id() != JsTokenId.BRACKET_LEFT_PAREN) {
        if (token.id() == JsTokenId.OPERATOR_DOT) {
            // we are not inside ()
            return null;
        }
        token = LexUtilities.findNext(ts, Arrays.asList(JsTokenId.WHITESPACE));
    }
    if (token.id() == JsTokenId.BRACKET_LEFT_PAREN && ts.movePrevious()) {
        token = LexUtilities.findNext(ts, Arrays.asList(JsTokenId.WHITESPACE));
        if (token.id() == JsTokenId.IDENTIFIER){
            return token.text().toString();
        }
    }
    return null;
}
 
Example 8
Source File: OJETUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static int getPrefixOffset(OJETContext ojContext, Document document, int offset) {
    TokenHierarchy th = TokenHierarchy.get(document);
    int result = offset;
    switch (ojContext) {
        case DATA_BINDING:
            TokenSequence<KODataBindTokenId> ts = LexerUtils.getTokenSequence(th, offset, KODataBindTokenId.language(), false);
            if (ts != null) {
                int diff = ts.move(offset);
                if (diff == 0 && ts.movePrevious() || ts.moveNext()) {
                    //we are on a token of ko-data-bind token sequence
                    Token<KODataBindTokenId> etoken = ts.token();
                    if (etoken.id() == KODataBindTokenId.KEY) {
                        //ke
                        return ts.offset();
                    }
                }
                break;
            } 
    }
    return result;
}
 
Example 9
Source File: CompletionUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean noCompletion(JTextComponent target) {
    if (target == null || target.getCaret() == null) {
        return false;
    }
    int offset = target.getCaret().getDot();
    if (offset < 0) {
        return false;
    }
    //no completion inside CDATA or comment section
    BaseDocument document = (BaseDocument) target.getDocument();
    ((AbstractDocument) document).readLock();
    try {
        TokenHierarchy th = TokenHierarchy.get(document);
        TokenSequence ts = th.tokenSequence();
        if (ts == null) {
            return false;
        }
        ts.move(offset);
        Token token = ts.token();
        if (token == null) {
            ts.moveNext();
            token = ts.token();
            if (token == null) {
                return false;
            }
        }
        if (token.id() == XMLTokenId.CDATA_SECTION || token.id() == XMLTokenId.BLOCK_COMMENT || token.id() == XMLTokenId.PI_START || token.id() == XMLTokenId.PI_END || token.id() == XMLTokenId.PI_CONTENT || token.id() == XMLTokenId.PI_TARGET) {
            return true;
        }
    } finally {
        ((AbstractDocument) document).readUnlock();
    }
    return false;
}
 
Example 10
Source File: IndentationCounter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isInTernaryOperatorStatement(TokenSequence<? extends PHPTokenId> ts) {
    boolean result = false;
    int originalOffset = ts.offset();
    ts.movePrevious();
    Token<? extends PHPTokenId> previousToken = LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_TOKEN));
    if (previousToken != null && previousToken.id() == PHPTokenId.PHP_TOKEN && previousToken.text().charAt(0) == '?') {
        result = true;
    }
    ts.move(originalOffset);
    ts.moveNext();
    return result;
}
 
Example 11
Source File: JavaElementFoldVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void handleJavadoc(Tree t) throws BadLocationException, ConcurrentModificationException {
    int start = (int) sp.getStartPosition(cu, t);

    if (start == (-1))
        return ;

    if (start < initialCommentStopPos)
        initialCommentStopPos = start;

    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId>  ts = th.tokenSequence(JavaTokenId.language());

    if (ts.move(start) == Integer.MAX_VALUE) {
        return;//nothing
    }

    while (ts.movePrevious()) {
        Token<JavaTokenId> token = ts.token();

        if (token.id() == JavaTokenId.JAVADOC_COMMENT) {
            int startOffset = ts.offset();
            addFold(creator.createJavadocFold(startOffset, startOffset + token.length()), startOffset);
            if (startOffset < initialCommentStopPos)
                initialCommentStopPos = startOffset;
        }
        if (   token.id() != JavaTokenId.WHITESPACE
            && token.id() != JavaTokenId.BLOCK_COMMENT
            && token.id() != JavaTokenId.LINE_COMMENT)
            break;
    }
}
 
Example 12
Source File: FXMLHyperlinkProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static int[] getIdentifierSpan(Document doc, int offset) {
        FileObject fo = getFileObject(doc);
        if (fo == null) {
            //do nothing if FO is not attached to the document - the goto would not work anyway:
            return null;
        }
        Project prj = FileOwnerQuery.getOwner(fo);
        if (prj == null) {
            return null;
        }

//        NbModuleProvider module = prj.getLookup().lookup(NbModuleProvider.class);
//        if (module == null) {
//            return null;
//        }

        TokenHierarchy<?> th = TokenHierarchy.get(doc);
        TokenSequence ts = th.tokenSequence(Language.find(JavaFXEditorUtils.FXML_MIME_TYPE));
        if (ts == null) {
            return null;
        }

        ts.move(offset);
        if (!ts.moveNext()) {
            return null;
        }
        Token t = ts.token();
        if (findFile(fo, t.text().toString()) != null) {
            return new int[]{ts.offset() + 1, ts.offset() + t.length() - 1};
        }
        return null;

    }
 
Example 13
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static TokenSequence getTokenSequence (Document document, int offset) {
    TokenHierarchy tokenHierarchy = TokenHierarchy.get (document);
    if (tokenHierarchy == null) return null;
    TokenSequence tokenSequence = tokenHierarchy.tokenSequence ();
    if (tokenSequence == null) return null;
    while (true) {
        tokenSequence.move (offset);
        if (!tokenSequence.moveNext ()) return tokenSequence;
        TokenSequence tokenSequence2 = tokenSequence.embedded ();
        if (tokenSequence2 == null) return tokenSequence;
        tokenSequence = tokenSequence2;
    }
}
 
Example 14
Source File: JsStructureScanner.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isNotAnonymousFunction(TokenSequence ts, int functionKeywordPosition) {
    // expect that the ts in on "{"
    int position = ts.offset();
    boolean value = false;
    // find the function keyword
    ts.move(functionKeywordPosition);
    ts.moveNext();
    Token<? extends JsTokenId> token = LexUtilities.findPrevious(ts, Arrays.asList(JsTokenId.WHITESPACE));
    if ((token.id() == JsTokenId.OPERATOR_ASSIGNMENT || token.id() == JsTokenId.OPERATOR_COLON) && ts.movePrevious()) {
        token = LexUtilities.findPrevious(ts, Arrays.asList(JsTokenId.WHITESPACE));
        if (token.id() == JsTokenId.IDENTIFIER) {
            // it's:
            // name : function() ...
            // name = function() ...
            value = true;
        }
    }
    if (!value) {
        ts.move(functionKeywordPosition);
        ts.moveNext(); ts.moveNext();
        token = LexUtilities.findNext(ts, Arrays.asList(JsTokenId.WHITESPACE));
        if (token.id() == JsTokenId.IDENTIFIER) {
            value = true;
        }
    }
    ts.move(position);
    ts.moveNext();
    return value;
}
 
Example 15
Source File: JavaReference.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param jdts token sequence to analyze
 * @param offset offset of the first token to resolve
 * @return reference
 */
public static JavaReference resolve(TokenSequence<JavadocTokenId> jdts, int offset, int tagEndPosition) {
    JavaReference ref = new JavaReference();
    ref.tagEndPosition = tagEndPosition;
    jdts.move(offset);
    ref.insideFQN(jdts);
    return ref;
}
 
Example 16
Source File: JQueryUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isInJQuerySelector(ParserResult parserResult, int offset) {
        if (isJQuery(parserResult, offset)) {
            TokenSequence<? extends JsTokenId> ts = LexUtilities.getTokenSequence(parserResult.getSnapshot().getTokenHierarchy(), offset, JsTokenId.javascriptLanguage());
            if (ts == null) {
                return false;
            }
            ts.move(offset);
            if (!(ts.moveNext() && ts.movePrevious())) {
                return false;
            }
            return ts.token().id() == JsTokenId.STRING || ts.token().id() == JsTokenId.STRING_BEGIN;
//            boolean leftBracket = false;
//            while (!isEndToken(ts.token().id()) && ts.token().id() != JsTokenId.BRACKET_RIGHT_PAREN && ts.movePrevious()) {
//                if (ts.token().id() == JsTokenId.BRACKET_LEFT_PAREN) {
//                    leftBracket = true;
//                    break;
//                }
//            }
//            if (!leftBracket) {
//                return false;
//            } else {
//                ts.move(offset);
//                if (!(ts.moveNext() && ts.movePrevious())) {
//                    return false;
//                }
//            }
//            while (!isEndToken(ts.token().id()) && ts.token().id() != JsTokenId.BRACKET_LEFT_PAREN && ts.moveNext()) {
//                if (ts.token().id() == JsTokenId.BRACKET_RIGHT_PAREN) {
//                    return true;
//                }
//            }
        }
        return false;
    }
 
Example 17
Source File: TplBracesMatching.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public int[] findMatches() throws InterruptedException, BadLocationException {
    int[] delims = new int[]{1, 1};
    final Source source = Source.create(context.getDocument());
    final int searchOffset = context.getSearchOffset();

    ((AbstractDocument) context.getDocument()).readLock();
    try {
        if (!testMode && MatcherContext.isTaskCanceled()) {
            return null;
        }
        if (source == null) {
            return null;
        }

        // comments - do not color them as errors
        TokenSequence<TplTopTokenId> ts = LexerUtils.getTplTopTokenSequence(context.getDocument(), searchOffset);
        
        if (ts != null && ts.language() == TplTopTokenId.language()) {
            delims = findDelimsLength(ts);
            ts.move(searchOffset);
            ts.moveNext();
            ts.movePrevious();
            if (ts.token().id() == TplTopTokenId.T_COMMENT
                    || atCommentTag(TokenHierarchy.get(context.getDocument()), ts, delims, searchOffset)) {
                return new int[]{searchOffset, searchOffset};
            }
        }
    } finally {
        ((AbstractDocument) context.getDocument()).readUnlock();
    }
    final int[] delimiterLengths = delims;
    final int[][] ret = new int[1][];
    try {
        ParserManager.parse(Collections.singleton(source), new UserTask() {
            @Override
            public void run(ResultIterator resultIterator) throws Exception {
                if (!testMode && MatcherContext.isTaskCanceled()
                        || !source.getMimeType().equals(TplDataLoader.MIME_TYPE)) {
                    return;
                }

                if (resultIterator == null) {
                    ret[0] = new int[]{searchOffset, searchOffset};
                    return;
                }

                TplParserResult parserResult = (TplParserResult) resultIterator.getParserResult();
                if (parserResult == null) {
                    return;
                }
                int searchOffsetLocal = searchOffset;
                while (searchOffsetLocal != context.getLimitOffset()) {
                    int searched = parserResult.getSnapshot().getEmbeddedOffset(searchOffsetLocal);
                    Block block = getBlockForOffset(parserResult, searched, context.isSearchingBackward(), delimiterLengths);
                    if (block == null) {
                        return;
                    }
                    if (block.getSections().size() == 1) {
                        //just simple tag - was found by findOrigin()
                        ret[0] = new int[]{searchOffset, searchOffset};
                        return;
                    }

                    List<Integer> result = new ArrayList<>();
                    TplParserResult.Section lastSection = null;
                    for (TplParserResult.Section section : block.getSections()) {
                        OffsetRange or = section.getOffset();
                        or = new OffsetRange(or.getStart() - delimiterLengths[0], or.getEnd() + delimiterLengths[1]);
                        if (!or.containsInclusive(searchOffset)) {
                            insertMatchingSection(result, section, delimiterLengths);
                        } else {
                            if (lastSection == null) {
                                lastSection = section;
                            } else {
                                if ((section.getOffset().getStart() < lastSection.getOffset().getStart() && context.isSearchingBackward())
                                        || section.getOffset().getStart() > lastSection.getOffset().getStart() && !context.isSearchingBackward()) {
                                    insertMatchingSection(result, lastSection, delimiterLengths);
                                    lastSection = section;
                                } else {
                                    insertMatchingSection(result, section, delimiterLengths);
                                }
                            }
                        }
                    }
                    ret[0] = convertToIntegers(result);
                    searchOffsetLocal = searchOffsetLocal + (context.isSearchingBackward() ? -1 : +1);
                }
            }
        });

    } catch (ParseException ex) {
        Exceptions.printStackTrace(ex);
    }

    return ret[0];
}
 
Example 18
Source File: FxTreeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public int[] findAttributePos(FxNode node, String uri, String name, boolean value) {
    NodeInfo ni = accessor.i(node);
    if (!ni.isElement()) {
        throw new IllegalArgumentException();
    }
    TokenSequence<XMLTokenId> seq = hierarchy.tokenSequence();
    seq.move(ni.getStart());
    
    int state = 0;
    
    while (seq.moveNext()) {
        Token<XMLTokenId> t = seq.token();
        if (ni.isDefined(TextPositions.Position.ContentStart) &&
                seq.offset() >= ni.getContentStart()) {
            return null;
        }
        XMLTokenId id = t.id();
        switch (id) {
            case TAG:
                if (t.text().charAt(0) == '>' || seq.offset() != ni.getStart()) {
                    // broken tag or something
                    return new int[] { ni.getStart(), ni.getContentStart() };
                }
                break;
                
            case ARGUMENT: {
                String n = t.text().toString();
                int pos = n.indexOf(':');
                // HACK HACK, FIXME - the namespace must be translated into
                // the prefix, but I don't have prefixes in the model at this moment.
                if (uri != null && pos == -1) {
                    break;
                }
                if (pos != -1) {
                    n = n.substring(pos + 1);
                }
                if (name.equals(n)) {
                    if (!value) {
                        return new int[] {
                            seq.offset(),
                            seq.offset() + t.length()
                        };
                    }
                    state = 1;
                }
                break;
            }
                
            case VALUE:
                if (state != 1) {
                    break;
                }
                return new int[] {
                    seq.offset() + 1,
                    seq.offset() + t.length() + 1
                };
        }
    }
    return null;
}
 
Example 19
Source File: JavaBracesMatcher.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public int[] findOrigin() throws BadLocationException, InterruptedException {
    ((AbstractDocument) context.getDocument()).readLock();
    try {
        int [] origin = BracesMatcherSupport.findChar(
            context.getDocument(), 
            getSearchOffset(), 
            context.getLimitOffset(), 
            PAIRS
        );

        if (origin != null) {
            originOffset = origin[0];
            originChar = PAIRS[origin[1]];
            matchingChar = PAIRS[origin[1] + origin[2]];
            backward = origin[2] < 0;

            // Filter out block and line comments
            TokenHierarchy<Document> th = TokenHierarchy.get(context.getDocument());
            sequences = getEmbeddedTokenSequences(th, originOffset, backward, JavaTokenId.language());

            if (!sequences.isEmpty()) {
                // Check special tokens
                TokenSequence<?> seq = sequences.get(sequences.size() - 1);
                seq.move(originOffset);
                if (seq.moveNext()) {
                    if (seq.token().id() == JavaTokenId.BLOCK_COMMENT ||
                        seq.token().id() == JavaTokenId.LINE_COMMENT
                    ) {
                        return null;
                    }
                }
            }

            return new int [] { originOffset, originOffset + 1 };
        } else {
            return null;
        }
    } finally {
        ((AbstractDocument) context.getDocument()).readUnlock();
    }
}
 
Example 20
Source File: ControllerGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static void addControllerAttribute(BaseDocument doc, int from, int to, String controllerClassName) throws BadLocationException {
    doc.extWriteLock();
    try {
        TokenHierarchy h = TokenHierarchy.get(doc);

        TokenSequence<XMLTokenId> seq = h.tokenSequence();
        seq.move(from);

        int lastWsPos = -1;
        int start = -1;

        while (seq.moveNext()) {
            Token<XMLTokenId> t = seq.token();
            XMLTokenId id = t.id();

            if (seq.offset() >= to) {
                start = seq.offset();
                break;
            }
            if (id == XMLTokenId.WS) {
                lastWsPos = seq.offset() + 1;
            } else if (id == XMLTokenId.TAG) {
                if (t.text().length() > 0) {
                    if (t.text().charAt(t.text().length() - 1) == '>') {
                        start = seq.offset();
                        break;
                    }
                }
            } else {
                lastWsPos = -1;
            }
        }

        String toInsert = "fx:controller=\"" + controllerClassName + "\"";
        if (lastWsPos != -1) {
            start = lastWsPos;
        } else {
            toInsert = " " + toInsert;
        }
        doc.insertString(start, toInsert, null);
    } finally {
        doc.extWriteUnlock();
    }
}