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

The following examples show how to use org.netbeans.api.lexer.TokenSequence#movePrevious() . 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: PhpTypedBreakInterceptor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean isPartOfHereOrNowDoc(TokenSequence<? extends PHPTokenId> ts) {
    boolean result = false;
    int originalOffset = ts.offset();
    Token<? extends PHPTokenId> token = ts.token();
    if (token != null && TypingHooksUtils.isStringToken(token)) {
        while (ts.movePrevious()) {
            token = ts.token();
            if (token != null) {
                if (!TypingHooksUtils.isStringToken(token)) {
                    PHPTokenId tokenId = token.id();
                    if (tokenId == PHPTokenId.PHP_HEREDOC_TAG_START || tokenId == PHPTokenId.PHP_NOWDOC_TAG_START) {
                        result = true;
                        break;
                    } else if (tokenId == PHPTokenId.PHP_HEREDOC_TAG_END || tokenId == PHPTokenId.PHP_NOWDOC_TAG_END) {
                        break;
                    }
                }
            }
        }
    }
    ts.move(originalOffset);
    ts.moveNext();
    return result;
}
 
Example 2
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 3
Source File: TreeUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static int[] ignoreWhitespaces(CompilationInfo ci, int start, int end) {
    TokenSequence<JavaTokenId> ts = ci.getTokenHierarchy().tokenSequence(JavaTokenId.language());
    if (ts == null) {
        return new int[]{start, end};
    }
    ts.move(start);
    if (ts.moveNext()) {
        boolean wasMoveNext = true;
        while (WHITESPACES.contains(ts.token().id()) && (wasMoveNext = ts.moveNext())) {
            ;
        }
        if (wasMoveNext && ts.offset() > start) {
            start = ts.offset();
        }
    }
    ts.move(end);
    while (ts.movePrevious() && WHITESPACES.contains(ts.token().id()) && ts.offset() < end) {
        end = ts.offset();
    }
    return new int[]{start, end};
}
 
Example 4
Source File: TwigDeletedTextInterceptor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean beforeRemove(Context context) throws BadLocationException {
    if (OptionsUtils.autoCompletionSmartDelimiters()) {
        int dotPos = context.getOffset();
        Document document = context.getDocument();
        TokenSequence<? extends TwigTopTokenId> ts = TwigLexerUtils.getTwigTokenSequence(document, dotPos);
        // now support {{}} and {%%}, should also check whitespaces?
        if (ts != null) {
            ts.move(dotPos);
            if (ts.movePrevious()) {
                if (ts.token().id() == getOpeningId()
                        && ts.offset() == dotPos - 2) {
                    ts.move(dotPos);
                    if (ts.moveNext()) {
                        if (ts.token().id() == getClosingId()
                                && ts.offset() == dotPos) {
                            document.remove(dotPos - 2, 4); // {{}} or {%%}
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 5
Source File: PhpDeletedTextInterceptor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void remove(Context context) throws BadLocationException  {
    BaseDocument doc = (BaseDocument) context.getDocument();
    int dotPos = context.getOffset() - 1;
    if (OptionsUtils.autoCompletionSmartQuotes()) {
        TokenSequence<? extends PHPTokenId> tokenSequence = LexUtilities.getPHPTokenSequence(doc, dotPos);
        if (tokenSequence != null) {
            tokenSequence.move(dotPos);
            if ((tokenSequence.moveNext() || tokenSequence.movePrevious())
                    && (tokenSequence.token().id() == PHPTokenId.PHP_ENCAPSED_AND_WHITESPACE
                        || tokenSequence.token().id() == PHPTokenId.PHP_CONSTANT_ENCAPSED_STRING)) {
                char[] precedingChars = doc.getChars(dotPos - 1, 1);
                if (precedingChars.length > 0 && precedingChars[0] == '\\') {
                    doc.remove(dotPos - 1, 1);
                    return;
                }
            }
        }
        char[] match = doc.getChars(dotPos, 1);
        if ((match != null) && (match[0] == getQuote())) {
            doc.remove(dotPos, 1);
        }
    }
}
 
Example 6
Source File: CodeCompletionUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static boolean inVariableModifiers(Document doc, int caretOffset) {
    TokenHierarchy tokenHierarchy = TokenHierarchy.get(doc);
    TokenSequence tokenSequence = tokenHierarchy.tokenSequence();

    tokenSequence.move(caretOffset);
    tokenSequence.movePrevious(); tokenSequence.moveNext();
    while (!tokenSequence.isEmpty()) {
        if (tokenSequence.token().id() == TplTopTokenId.T_SMARTY_OPEN_DELIMITER
                || tokenSequence.token().id() == TplTopTokenId.T_HTML
                && isDefaultOpenDelimOnPreviousPosition(doc, caretOffset)) {
            return false;
        } else if (tokenSequence.token().id() == TplTopTokenId.T_SMARTY) {
            if (tokenSequence.token().text().toString().contains("|")) {
                return true;
            }
        }
        tokenSequence.movePrevious();
    }
    return false;
}
 
Example 7
Source File: IndentationCounter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean semicolonProceededByBreak(TokenSequence ts) {
    boolean retunValue = false;

    if (ts.token().id() == PHPTokenId.PHP_BREAK) {
        retunValue = true;
    } else if (ts.token().id() == PHPTokenId.PHP_NUMBER) {
        int origOffset = ts.offset();

        if (ts.movePrevious()) {
            if (ts.token().id() == PHPTokenId.WHITESPACE) {
                if (ts.movePrevious()) {
                    if (ts.token().id() == PHPTokenId.PHP_BREAK) {
                        retunValue = true;
                    }
                }
            }
        }

        ts.move(origOffset);
        ts.moveNext();
    }

    return retunValue;
}
 
Example 8
Source File: ModifiersCheckHintError.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static int getStartOffset(final BaseDocument doc, final int elementOffset) {
    int retval = 0;
    doc.readLock();
    try {
        TokenSequence<? extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, elementOffset);
        if (ts != null) {
            ts.move(elementOffset);
            TokenId lastTokenId = null;
            while (ts.movePrevious()) {
                Token t = ts.token();
                if (t.id() != PHPTokenId.PHP_PUBLIC && t.id() != PHPTokenId.PHP_PROTECTED && t.id() != PHPTokenId.PHP_PRIVATE
                        && t.id() != PHPTokenId.PHP_STATIC && t.id() != PHPTokenId.PHP_FINAL && t.id() != PHPTokenId.PHP_ABSTRACT
                        && t.id() != PHPTokenId.PHP_FUNCTION && t.id() != PHPTokenId.WHITESPACE && t.id() != PHPTokenId.PHP_CLASS
                        && t.id() != PHPTokenId.PHP_CONST) {
                    ts.moveNext();
                    if (lastTokenId == PHPTokenId.WHITESPACE) {
                        ts.moveNext();
                    }
                    retval = ts.offset();
                    break;
                }
                lastTokenId = t.id();
            }
        }
    } finally {
        doc.readUnlock();
    }
    return retval;
}
 
Example 9
Source File: CompletionContextFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static synchronized boolean isUnderInterfaceTokenId(final TokenSequence tokenSequence) {
    boolean retval = false;
    int curlyBalance = -1;
    int orgOffset = tokenSequence.offset();
    try {
        while (tokenSequence.movePrevious()) {
            Token token = tokenSequence.token();
            TokenId id = token.id();
            if (id.equals(PHPTokenId.PHP_INTERFACE) && curlyBalance == 0) {
                retval = true;
                break;
            } else if (id.equals(PHPTokenId.PHP_CURLY_OPEN)) {
                curlyBalance++;
            } else if (id.equals(PHPTokenId.PHP_CURLY_CLOSE)) {
                curlyBalance--;
            } else if (id.equals(PHPTokenId.PHP_CLASS) || id.equals(PHPTokenId.PHP_WHILE)
                    || id.equals(PHPTokenId.PHP_IF) || id.equals(PHPTokenId.PHP_FOR)
                    || id.equals(PHPTokenId.PHP_FOREACH) || id.equals(PHPTokenId.PHP_TRY)
                    || id.equals(PHPTokenId.PHP_CATCH) || id.equals(PHPTokenId.PHP_FUNCTION)) {
                // here could be more tokens which can interrupt interface scope, but theese are good enough
                retval = false;
                break;
            }
        }
    } finally {
        tokenSequence.move(orgOffset);
        tokenSequence.moveNext();
    }
    return retval;
}
 
Example 10
Source File: HintTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Compares the result with the golden string but ignores *all* whitespaces. In order to keep the nice IDE tools
 * to work, if a inconsistency is found, the prefix of the golden file will be forged from the actual result, so the
 * first reported inconsistency will occur at the right place.
 * The lax comparison can be switched off by -Dorg.netbeans.test.hints.strictOutputCompare=true
 * @param result
 * @param golden 
 */
private void assertSameOutput(String result, String golden) throws Exception {
    Language lng = Language.find("text/x-java");
    if (lng == null || Boolean.getBoolean("org.netbeans.test.hints.strictOutputCompare")) {
        assertEquals("The output code does not match the expected code.", reduceWhitespaces(golden), 
                reduceWhitespaces(result));
        return;
    }
    TokenHierarchy h1 = TokenHierarchy.create(result, lng);
    TokenHierarchy h2 = TokenHierarchy.create(golden, lng);
    TokenSequence s1 = h1.tokenSequence();
    TokenSequence s2 = h2.tokenSequence();
    while (s2.moveNext()) {
        Token gt = s2.token();
        boolean wh = gt.id() == JavaTokenId.WHITESPACE;
        if (s1.moveNext()) {
            Token rt;
            do {
                rt = s1.token();
                if (!wh) {
                    if (!rt.text().toString().equals(gt.text().toString())) {
                        failNotSame(result, golden, s1.offset(), s2.offset());
                    }
                } else if (!isWH(rt)) {
                    s1.movePrevious();
                    break;
                }
            } while (isWH(rt) && s1.moveNext());
        } else if (!wh) {
            failNotSame(result, golden, s1.offset(), s2.offset());
        }
    }
    
    s1.movePrevious();
    s2.movePrevious();
    if (s1.moveNext() != s2.moveNext()) {
        failNotSame(result, golden, s1.offset(), s2.offset());
    }
}
 
Example 11
Source File: JavadocBracesMatcher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * simple check whether selected token is type parameter {@code @param <T>}
 * @param seq token sequence with selected token
 * @return {@code true} when the token should not be interpreted.
 */
private static boolean isTypeParameterTag(TokenSequence<? extends TokenId> seq) {
    int index = seq.index();
    try {
        if (!seq.movePrevious() || seq.token().id() != JavadocTokenId.OTHER_TEXT) {
            return false;
        }

        return seq.movePrevious() && seq.token().id() == JavadocTokenId.TAG
                && "@param".contentEquals(seq.token().text()); // NOI18N
    } finally {
        seq.moveIndex(index);
        seq.moveNext();
    }
}
 
Example 12
Source File: TypingCompletion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve whether pairing right curly should be added automatically
 * at the caret position or not.
 * <br>
 * There must be only whitespace or line comment or block comment
 * between the caret position
 * and the left brace and the left brace must be on the same line
 * where the caret is located.
 * <br>
 * The caret must not be "contained" in the opened block comment token.
 *
 * @param doc document in which to operate.
 * @param caretOffset offset of the caret.
 * @return true if a right brace '}' should be added
 *  or false if not.
 */
static boolean isAddRightBrace(BaseDocument doc, int caretOffset) throws BadLocationException {
    if (tokenBalance(doc, JavaTokenId.LBRACE) <= 0) {
        return false;
    }
    int caretRowStartOffset = org.netbeans.editor.Utilities.getRowStart(doc, caretOffset);
    TokenSequence<JavaTokenId> ts = javaTokenSequence(doc, caretOffset, true);
    if (ts == null) {
        return false;
    }
    boolean first = true;
    do {
        if (ts.offset() < caretRowStartOffset) {
            return false;
        }
        switch (ts.token().id()) {
            case WHITESPACE:
            case LINE_COMMENT:
                break;
            case BLOCK_COMMENT:
            case JAVADOC_COMMENT:
                if (first && caretOffset > ts.offset() && caretOffset < ts.offset() + ts.token().length()) {
                    // Caret contained within block comment -> do not add anything
                    return false;
                }
                break; // Skip
            case LBRACE:
                return true;
        }
        first = false;
    } while (ts.movePrevious());
    return false;
}
 
Example 13
Source File: PositionEstimator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static int moveBackToToken(TokenSequence<JavaTokenId> tokenSequence, 
        final int pos,
        JavaTokenId id)
{
    tokenSequence.move(pos);
    tokenSequence.moveNext(); // Assumes the pos is located within input bounds
    while (!id.equals(tokenSequence.token().id())) {
        if (!tokenSequence.movePrevious())
            return -1;
    }
    return tokenSequence.offset();
}
 
Example 14
Source File: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean uncommentIt(TokenSequence<TplTopTokenId> ts) {
    if (ts.token().id() == TplTopTokenId.T_COMMENT) {
        return true;
    } else if (ts.token().id() == TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
        return ts.moveNext() && ts.token().id() == TplTopTokenId.T_COMMENT;
    } else if (ts.token().id() == TplTopTokenId.T_SMARTY_CLOSE_DELIMITER) {
        return ts.movePrevious() && ts.token().id() == TplTopTokenId.T_COMMENT;
    }
    return false;
}
 
Example 15
Source File: ExtJsCodeCompletion.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompletionProposal> complete(CodeCompletionContext ccContext, CompletionContext jsCompletionContext, String prefix) {
    if (jsCompletionContext != CompletionContext.OBJECT_PROPERTY_NAME) {
        return Collections.<CompletionProposal>emptyList();
    }
    // find the object that can be configured
    TokenHierarchy<?> th = ccContext.getParserResult().getSnapshot().getTokenHierarchy();
    if (th == null) {
        return Collections.<CompletionProposal>emptyList();
    }
    int carretOffset  = ccContext.getCaretOffset();
    int eOffset = ccContext.getParserResult().getSnapshot().getEmbeddedOffset(carretOffset);
    TokenSequence<? extends JsTokenId> ts = LexUtilities.getJsTokenSequence(th, eOffset);
    if (ts == null) {
        return Collections.<CompletionProposal>emptyList();
    }
    
    ts.move(eOffset);
    
    if (!ts.moveNext() && !ts.movePrevious()){
        return Collections.<CompletionProposal>emptyList();
    }
    
    Token<? extends JsTokenId> token = null;
    JsTokenId tokenId;
    //find the begining of the object literal
    int balance = 1;
    while (ts.movePrevious() && balance > 0) {
        token = ts.token();
        tokenId = token.id();
        if (tokenId == JsTokenId.BRACKET_RIGHT_CURLY) {
            balance++;
        } else if (tokenId == JsTokenId.BRACKET_LEFT_CURLY) {
            balance--;
        }
    }
    if (token == null || balance != 0) {
        return Collections.<CompletionProposal>emptyList();
    }
    
    // now we should be at the beginning of the object literal. 
    token = LexUtilities.findPreviousToken(ts, Arrays.asList(JsTokenId.IDENTIFIER));
    tokenId = token.id();
    StringBuilder sb = new StringBuilder(token.text());
    while ((tokenId == JsTokenId.IDENTIFIER || tokenId == JsTokenId.OPERATOR_DOT) && ts.movePrevious()) {
        token = ts.token(); tokenId = token.id();
        if (tokenId == JsTokenId.OPERATOR_DOT) {
            sb.insert(0, '.'); // NOI18N
        } else if (tokenId == JsTokenId.IDENTIFIER) {
            sb.insert(0, token.text());
        }
    }
    
    String fqn = sb.toString();
    Map<String, Collection<ExtJsDataItem>> data = getData();
    Collection<ExtJsDataItem> items = data.get(fqn);
    int anchorOffset = eOffset - ccContext.getPrefix().length();
    if (items != null) {
        List<CompletionProposal> result = new ArrayList<CompletionProposal>();
        for (ExtJsDataItem item : items) {
            if (item.getName().startsWith(prefix)) {
                result.add(ExtJsCompletionItem.createExtJsItem(item, anchorOffset));
            }
        }
        return result;
    }
    return Collections.<CompletionProposal>emptyList();
}
 
Example 16
Source File: AddUnderscores.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@TriggerTreeKind({Kind.INT_LITERAL, Kind.LONG_LITERAL})
public static ErrorDescription hint(HintContext ctx) {
    TreePath tp = ctx.getPath();
    int end = (int) ctx.getInfo().getTrees().getSourcePositions().getEndPosition(tp.getCompilationUnit(), tp.getLeaf());
    int start = (int) ctx.getInfo().getTrees().getSourcePositions().getStartPosition(tp.getCompilationUnit(), tp.getLeaf());
    TokenSequence<?> ts = ctx.getInfo().getTokenHierarchy().tokenSequence();
    ts.move(end);
    if (!ts.movePrevious()) return null;
    String literal = ts.token().text().toString();
    StringBuilder tokenPrefix = new StringBuilder();
    
    while (ts.offset() > start) {
        if (!ts.movePrevious()) {
            break;
        }
        if (ts.offset() == start) {
            tokenPrefix.append(ts.token().text().toString());
            break;
        }
    }
    if (!isReplaceLiteralsWithUnderscores(ctx.getPreferences()) && literal.contains("_")) return null;
    RadixInfo info = radixInfo(literal);
    if (info.radix == 8) return null;//octals ignored for now
    String normalized = info.constant.replaceAll(Pattern.quote("_"), "");
    int separateCount = getSizeForRadix(ctx.getPreferences(), info.radix);
    StringBuilder split = new StringBuilder();
    int count = separateCount + 1;

    for (int i = normalized.length(); i > 0; i--) {
        if (--count == 0) {
            split.append("_");
            count = separateCount;
        }
        split.append(normalized.charAt(i - 1));
    }

    split.reverse();

    String result = info.prefix + split.toString() + info.suffix;

    if (result.equals(literal)) return null;

    String displayName = NbBundle.getMessage(AddUnderscores.class, "ERR_" + ID);
    Fix f = new FixImpl(ctx.getInfo(), tp, tokenPrefix.toString() + result).toEditorFix();

    return ErrorDescriptionFactory.forTree(ctx, tp, displayName, f);
}
 
Example 17
Source File: LatteLexerUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static List<OffsetRange> findBackwardMatching(
        TokenSequence<? extends LatteTopTokenId> topTs,
        LatteTokenText start,
        LatteTokenText end,
        List<LatteTokenText> middle) {
    List<OffsetRange> result = new ArrayList<>();
    topTs.movePrevious();
    int originalOffset = topTs.offset();
    int balance = 1;
    while (topTs.movePrevious()) {
        Token<? extends LatteTopTokenId> token = topTs.token();
        if (token != null && (token.id() == LatteTopTokenId.T_LATTE)) {
            TokenSequence<LatteMarkupTokenId> markupTs = topTs.embedded(LatteMarkupTokenId.language());
            if (markupTs != null) {
                markupTs.moveEnd();
                while (markupTs.movePrevious()) {
                    Token<? extends LatteMarkupTokenId> markupToken = markupTs.token();
                    if (start.matches(markupToken)) {
                        balance++;
                    } else if (end.matches(markupToken)) {
                        balance--;
                        if (balance == 0) {
                            result.add(new OffsetRange(markupTs.offset(), markupTs.offset() + markupToken.length()));
                            break;
                        }
                    } else if (matchesToken(middle, markupToken)) {
                        if (balance == 1) {
                            result.add(new OffsetRange(markupTs.offset(), markupTs.offset() + markupToken.length()));
                            break;
                        }
                    }
                }
                if (balance == 0) {
                    break;
                }
            }
        }
    }
    topTs.move(originalOffset);
    return result;
}
 
Example 18
Source File: JsConventionRule.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages("AssignmentCondition=Expected a conditional expression and instead saw an assignment.")
private void checkAssignmentInCondition(Node condition) {
    if (assignmentInCondition == null) {
        return;
    }
    if (condition instanceof BinaryNode) {
        BinaryNode binaryNode = (BinaryNode)condition;
        if (binaryNode.isAssignment()) {
            TokenSequence<? extends JsTokenId> ts = LexUtilities.getJsTokenSequence(context.parserResult.getSnapshot(), condition.getStart());
            if (ts == null) {
                return;
            }
            ts.move(condition.getStart());
            int parenBalance = 0;
            if (ts.moveNext()) {
                JsTokenId id = ts.token().id();
                
                while ( id != JsTokenId.KEYWORD_IF && id != JsTokenId.KEYWORD_FOR && id != JsTokenId.KEYWORD_WHILE && ts.movePrevious()) {
                    id = ts.token().id();
                    if (id == JsTokenId.BRACKET_RIGHT_PAREN) {
                        parenBalance--;
                    } else if (id == JsTokenId.BRACKET_LEFT_PAREN) {
                        parenBalance++;
                    }
                }
            }
            if (parenBalance == 1) {
                // 1 -> if ( a = b ) -> hint is valid
                // > 1 -> if ((a=b)) -> hint is not valid - see https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/if...else
                hints.add(new Hint(assignmentInCondition, Bundle.AssignmentCondition(),
                        context.getJsParserResult().getSnapshot().getSource().getFileObject(),
                        ModelUtils.documentOffsetRange(context.getJsParserResult(), condition.getStart(), condition.getFinish()), null, 500));
            }
        }
        if (binaryNode.lhs() instanceof BinaryNode) {
            checkAssignmentInCondition(binaryNode.lhs());
        }
        if (binaryNode.rhs() instanceof BinaryNode) {
            checkAssignmentInCondition(binaryNode.rhs());
        }
    }
}
 
Example 19
Source File: LexUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
     * Tries to skip parenthesis
     */
    public static boolean skipParenthesis(TokenSequence<?extends JsTokenId> ts, boolean back) {
        int balance = 0;

        Token<?extends JsTokenId> token = ts.token();
        if (token == null) {
            return false;
        }

        TokenId id = token.id();

//        // skip whitespaces
//        if (id == JsTokenId.WHITESPACE) {
//            while (ts.moveNext() && ts.token().id() == JsTokenId.WHITESPACE) {}
//        }
        if (id == JsTokenId.WHITESPACE || id == JsTokenId.EOL) {
            while ((back ? ts.movePrevious() : ts.moveNext()) && (ts.token().id() == JsTokenId.WHITESPACE || ts.token().id() == JsTokenId.EOL)) {}
        }

        // if current token is not left parenthesis
        if (ts.token().id() != (back ? JsTokenId.BRACKET_RIGHT_PAREN : JsTokenId.BRACKET_LEFT_PAREN)) {
            return false;
        }

        do {
            token = ts.token();
            id = token.id();

            if (id == (back ? JsTokenId.BRACKET_RIGHT_PAREN : JsTokenId.BRACKET_LEFT_PAREN)) {
                balance++;
            } else if (id == (back ? JsTokenId.BRACKET_LEFT_PAREN : JsTokenId.BRACKET_RIGHT_PAREN)) {
                if (balance == 0) {
                    return false;
                } else if (balance == 1) {
                    //int length = ts.offset() + token.length();
                    if (back) {
                        ts.movePrevious();
                    } else {
                        ts.moveNext();
                    }
                    return true;
                }

                balance--;
            }
        } while (back ? ts.movePrevious() : ts.moveNext());

        return false;
    }
 
Example 20
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];
}