Java Code Examples for org.netbeans.editor.BaseDocument#getChars()

The following examples show how to use org.netbeans.editor.BaseDocument#getChars() . 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: JsTypedTextInterceptor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isQuoteCompletablePosition(BaseDocument doc, int dotPos)
    throws BadLocationException {
    if (dotPos == doc.getLength()) { // there's no other character to test

        return true;
    } else {
        // test that we are in front of ) , " or ' ... etc.
        int eol = Utilities.getRowEnd(doc, dotPos);

        if ((dotPos == eol) || (eol == -1)) {
            return false;
        }

        int firstNonWhiteFwd = Utilities.getFirstNonWhiteFwd(doc, dotPos, eol);

        if (firstNonWhiteFwd == -1) {
            return false;
        }

        char chr = doc.getChars(firstNonWhiteFwd, 1)[0];

        return ((chr == ')') || (chr == ',') || (chr == '+') || (chr == '}') || (chr == ';') ||
           (chr == ']'));
    }
}
 
Example 2
Source File: BasicColorValuesCompletionItem.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public CompletionDocumentation resolveLink(String link) {
    if ("picker".equals(link)) {
        Component component = findComponentUnderMouse();
        if (component != null) {
            Color color = JColorChooser.showDialog(component, "Color Picker", decodeAlfa(value));
            if (color != null) {
                try {
                    BaseDocument document = (BaseDocument) this.document;
                    int startPosition = caretOffset - 1;
                    while ('\"' != (document.getChars(startPosition, 1)[0])) {
                        startPosition--;
                    }
                    startPosition++;
                    document.replace(startPosition, caretOffset - startPosition, getHTMLColorString(color), null);
                    Completion.get().hideAll();
                    RankingProvider.inserted(completionText.hashCode());
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);

                }
            }
        }
    }
    return null;
}
 
Example 3
Source File: BasicValuesCompletionItem.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public void defaultAction(JTextComponent component) {
    if (component != null) {
        try {
            BaseDocument document = (BaseDocument) component.getDocument();
            int caretPosition = component.getCaretPosition();
            int startPosition = caretPosition - 1;
            while ('\"' != (document.getChars(startPosition, 1)[0])) {
                startPosition--;
            }
            startPosition++;
            document.replace(startPosition, caretPosition - startPosition, completionText, null);
            Completion.get().hideAll();
            RankingProvider.inserted(completionText.hashCode());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);

        }
    }

}
 
Example 4
Source File: CompletionImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** 
 * Consumes identifier part of text behind caret upto first non-identifier
 * char.
 */
private void consumeIdentifier() {
    JTextComponent comp = getActiveComponent();
    BaseDocument doc = (BaseDocument) comp.getDocument();
    int initCarPos = comp.getCaretPosition();
    int carPos = initCarPos;
    boolean nonChar = false;
    char c;
    try {
        while(nonChar == false) {
            c = doc.getChars(carPos, 1)[0];
            if(!Character.isJavaIdentifierPart(c)) {
                nonChar = true;
            }
            carPos++;
        }
        doc.remove(initCarPos, carPos - initCarPos -1);
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 5
Source File: FunctionScopeImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static int getWhitespacesBehindASTErrorExpression(Scope scope, int endOffset) {
    FileObject fileObject = scope.getFileObject();
    int wsCount = 0;
    if (fileObject != null) {
        BaseDocument document = GsfUtilities.getDocument(fileObject, true);
        Scope inScope = scope.getInScope();
        if (document != null && inScope != null) {
            int length = inScope.getBlockRange().getEnd() - endOffset;
            if (length > 0) {
                try {
                    char[] chars = document.getChars(endOffset, length);
                    for (char c : chars) {
                        if (c == ' ' || c == '\t') {
                            wsCount++;
                        } else {
                            break;
                        }
                    }
                } catch (BadLocationException ex) {
                    LOGGER.log(Level.WARNING, "Invalid offset: " + ex.offsetRequested(), ex); // NOI18N
                }
            }
        }
    }
    return wsCount;
}
 
Example 6
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 7
Source File: GroovyTypedTextInterceptor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isQuoteCompletablePosition(BaseDocument doc, int dotPos)
    throws BadLocationException {
    if (dotPos == doc.getLength()) { // there's no other character to test

        return true;
    } else {
        // test that we are in front of ) , " or ' ... etc.
        int eol = Utilities.getRowEnd(doc, dotPos);

        if ((dotPos == eol) || (eol == -1)) {
            return false;
        }

        int firstNonWhiteFwd = Utilities.getFirstNonWhiteFwd(doc, dotPos, eol);

        if (firstNonWhiteFwd == -1) {
            return false;
        }

        char chr = doc.getChars(firstNonWhiteFwd, 1)[0];

        return ((chr == ')') || (chr == ',') || (chr == '+') || (chr == '}') || (chr == ';') ||
           (chr == ']'));
    }
}
 
Example 8
Source File: GroovyTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isEscapeSequence(BaseDocument doc, int dotPos)
    throws BadLocationException {
    if (dotPos <= 0) {
        return false;
    }

    char previousChar = doc.getChars(dotPos - 1, 1)[0];

    return previousChar == '\\';
}
 
Example 9
Source File: PhpTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isEscapeSequence(BaseDocument doc, int dotPos) throws BadLocationException {
    if (dotPos <= 0) {
        return false;
    }
    char previousChar = doc.getChars(dotPos - 1, 1)[0];
    return previousChar == '\\';
}
 
Example 10
Source File: PhpTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether dotPos is a position at which bracket and quote completion
 * is performed. Brackets and quotes are not completed everywhere but just
 * at suitable places .
 *
 * @param doc the document
 * @param dotPos position to be tested
 */
private boolean isCompletablePosition(BaseDocument doc, int dotPos) throws BadLocationException {
    if (dotPos == doc.getLength()) { // there's no other character to test
        return true;
    } else {
        // test that we are in front of ) , " or '
        char chr = doc.getChars(dotPos, 1)[0];
        return ((chr == ')') || (chr == ',') || (chr == '\"') || (chr == '\'') || (chr == ' ')
                || (chr == ']') || (chr == '}') || (chr == '\n') || (chr == '\t') || (chr == ';'));
    }
}
 
Example 11
Source File: GroovyTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether dotPos is a position at which bracket and quote
 * completion is performed. Brackets and quotes are not completed
 * everywhere but just at suitable places .
 * @param doc the document
 * @param dotPos position to be tested
 */
private boolean isCompletablePosition(BaseDocument doc, int dotPos)
    throws BadLocationException {
    if (dotPos == doc.getLength()) { // there's no other character to test

        return true;
    } else {
        // test that we are in front of ) , " or '
        char chr = doc.getChars(dotPos, 1)[0];

        return ((chr == ')') || (chr == ',') || (chr == '\"') || (chr == '\'') || (chr == ' ') ||
        (chr == ']') || (chr == '}') || (chr == '\n') || (chr == '\t') || (chr == ';'));
    }
}
 
Example 12
Source File: TwigDeletedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(Context context) throws BadLocationException {
    if (OptionsUtils.autoCompletionSmartQuotes()) {
        BaseDocument doc = (BaseDocument) context.getDocument();
        int dotPos = context.getOffset() - 1;
        TokenSequence<? extends TokenId> ts = TwigLexerUtils.getTwigMarkupTokenSequence(doc, dotPos);
        // check escape sequence
        // e.g. "\"" '\''
        if (ts != null) {
            ts.move(dotPos);
            if ((ts.moveNext() || ts.movePrevious())) {
                TokenId id = ts.token().id();
                if (id == TwigBlockTokenId.T_TWIG_STRING || id == TwigVariableTokenId.T_TWIG_STRING) {
                    if (TypingHooksUtils.isEscapeSequence(doc, dotPos)) {
                        doc.remove(dotPos - 1, 1);
                        return;
                    }
                }
            }
        }

        char[] match = doc.getChars(dotPos, 1);
        if ((match != null) && (match[0] == getQuote())) {
            doc.remove(dotPos, 1);
        }
    }
}
 
Example 13
Source File: TypingHooksUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean sameAsExistingChar(BaseDocument doc, char c, int dotPos) throws BadLocationException {
    if (dotPos <= 0 || doc.getLength() <= dotPos) {
        return false;
    }
    char[] nextChars = doc.getChars(dotPos, 1);
    return nextChars.length > 0 && nextChars[0] == c;
}
 
Example 14
Source File: TypingHooksUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isEscapeSequence(BaseDocument doc, int dotPos) throws BadLocationException {
    if (dotPos <= 0) {
        return false;
    }
    char[] previousChars = doc.getChars(dotPos - 1, 1);
    return previousChars.length > 0 && previousChars[0] == '\\';
}
 
Example 15
Source File: GroovyDeletedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(Context context) throws BadLocationException {
    final BaseDocument doc = (BaseDocument) context.getDocument();
    final char ch = context.getText().charAt(0);
    final int dotPos = context.getOffset() - 1;

    switch (ch) {

        case '{':
        case '(':
        case '[': { // and '{' via fallthrough
            char tokenAtDot = LexUtilities.getTokenChar(doc, dotPos);

            if (((tokenAtDot == ']')
                    && (LexUtilities.getTokenBalance(doc, GroovyTokenId.LBRACKET, GroovyTokenId.RBRACKET, dotPos) != 0))
                    || ((tokenAtDot == ')')
                    && (LexUtilities.getTokenBalance(doc, GroovyTokenId.LPAREN, GroovyTokenId.RPAREN, dotPos) != 0))
                    || ((tokenAtDot == '}')
                    && (LexUtilities.getTokenBalance(doc, GroovyTokenId.LBRACE, GroovyTokenId.RBRACE, dotPos) != 0))) {
                doc.remove(dotPos, 1);
            }
            break;
        }

        case '|':
        case '\"':
        case '\'':
            char[] match = doc.getChars(dotPos, 1);

            if ((match != null) && (match[0] == ch)) {
                doc.remove(dotPos, 1);
            }
    }
}
 
Example 16
Source File: JsTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isEscapeSequence(BaseDocument doc, int dotPos)
    throws BadLocationException {
    if (dotPos <= 0) {
        return false;
    }

    char previousChar = doc.getChars(dotPos - 1, 1)[0];

    return previousChar == '\\';
}
 
Example 17
Source File: JsTypedTextInterceptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether dotPos is a position at which bracket and quote
 * completion is performed. Brackets and quotes are not completed
 * everywhere but just at suitable places .
 * @param doc the document
 * @param dotPos position to be tested
 */
private boolean isCompletablePosition(BaseDocument doc, int dotPos)
    throws BadLocationException {
    if (dotPos == doc.getLength()) { // there's no other character to test

        return true;
    } else {
        // test that we are in front of ) , " or '
        char chr = doc.getChars(dotPos, 1)[0];

        return ((chr == ')') || (chr == ',') || (chr == '\"') || (chr == '\'') || (chr == '`') || (chr == ' ') ||
        (chr == ']') || (chr == '}') || (chr == '\n') || (chr == '\t') || (chr == ';'));
    }
}
 
Example 18
Source File: StructureAnalyzer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void addFolds(BaseDocument doc, List<? extends ASTElement> elements,
        Map<String,List<OffsetRange>> folds, List<OffsetRange> codeblocks) throws BadLocationException {
    for (ASTElement element : elements) {
        ElementKind kind = element.getKind();
        switch (kind) {
        case FIELD:
        case METHOD:
        case CONSTRUCTOR:
        case CLASS:
        case MODULE:
            ASTNode node = element.getNode();
            OffsetRange range = ASTUtils.getRangeFull(node, doc);

            // beware of synthetic elements
            if ((kind == ElementKind.METHOD && !((MethodNode) node).isSynthetic())
                    || (kind == ElementKind.CONSTRUCTOR && !((ConstructorNode) node).isSynthetic())
                    || (kind == ElementKind.FIELD
                        && ((FieldNode) node).getInitialExpression() instanceof ClosureExpression)
                    // Only make nested classes/modules foldable, similar to what the java editor is doing
                    || (range.getStart() > Utilities.getRowStart(doc, range.getStart())) && kind != ElementKind.FIELD) {

                int start = range.getStart();
                // Start the fold at the END of the line behind last non-whitespace, remove curly brace, if any
                start = Utilities.getRowLastNonWhite(doc, start);
                if (start >= 0 && doc.getChars(start, 1)[0] != '{') {
                    start++;
                }
                int end = range.getEnd();
                if (start != (-1) && end != (-1) && start < end && end <= doc.getLength()) {
                    range = new OffsetRange(start, end);
                    codeblocks.add(range);
                }
            }
            break;
        }

        List<? extends ASTElement> children = element.getChildren();

        if (children != null && children.size() > 0) {
            addFolds(doc, children, folds, codeblocks);
        }
    }
}