Java Code Examples for org.netbeans.editor.Utilities#getFirstNonWhiteFwd()

The following examples show how to use org.netbeans.editor.Utilities#getFirstNonWhiteFwd() . 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: 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 3
Source File: CompletionContext.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether this completion request was issued behind an import statement.
 * In such cases we are typically in context of completing packages/types within
 * an import statement. Few examples:
 * <br/><br/>
 * 
 * {@code import java.^}<br/>
 * {@code import java.lan^}<br/>
 * {@code import java.lang.In^}<br/>
 *
 * @return true if we are on the line that starts with an import keyword, false otherwise
 */
public boolean isBehindImportStatement() {
    int rowStart = 0;
    int nonWhite = 0;

    try {
        rowStart = Utilities.getRowStart(doc, lexOffset);
        nonWhite = Utilities.getFirstNonWhiteFwd(doc, rowStart);

    } catch (BadLocationException ex) {
    }

    Token<GroovyTokenId> importToken = LexUtilities.getToken(doc, nonWhite);

    if (importToken != null && importToken.id() == GroovyTokenId.LITERAL_import) {
        return true;
    }

    return false;
}
 
Example 4
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void markCurrentLanguageLines(BaseDocument doc, TextBounds languageBounds, EmbeddingType[] embeddingType) throws BadLocationException {
    if (languageBounds.getStartPos() == -1){
        return; // only white spaces
    }
    
    int firstLineOfTheLanguageBlock = languageBounds.getStartLine();

    int lineStart = Utilities.getRowStartFromLineOffset(doc, firstLineOfTheLanguageBlock);

    if (Utilities.getFirstNonWhiteFwd(doc, lineStart) < languageBounds.getStartPos()) {
        firstLineOfTheLanguageBlock++;
    }

    for (int i = firstLineOfTheLanguageBlock; i <= languageBounds.getEndLine(); i++) {
        embeddingType[i] = EmbeddingType.CURRENT_LANG;
    }
}
 
Example 5
Source File: ExtSyntaxSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Is the identifier at the position a function call?
* It first checks whether there is a identifier under
* the cursor and then it searches for the function call
* character - usually '('. Note: Java 1.5 annotations are not
* taken as function calls.
* @param identifierBlock int[2] block delimiting the identifier
* @return int[2] block or null if there's no function call
*/
public int[] getFunctionBlock(int[] identifierBlock) throws BadLocationException {
    if (identifierBlock != null) {
        int nwPos = Utilities.getFirstNonWhiteFwd(getDocument(), identifierBlock[1]);
        if ((nwPos >= 0) && (getDocument().getChars(nwPos, 1)[0] == '(')) {
            return new int[] { identifierBlock[0], nwPos + 1 };
        }
    }
    return null;
}
 
Example 6
Source File: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int findCommentStart(BaseDocument doc, CommentHandler handler, int offsetFrom, int offsetTo) throws BadLocationException {
    int from = Utilities.getFirstNonWhiteFwd(doc, offsetFrom, offsetTo);
    if (from == -1) {
        return offsetFrom;
    }
    String startDelim = handler.getCommentStartDelimiter();
    if (CharSequenceUtilities.equals(
        DocumentUtilities.getText(doc).subSequence(
            from, Math.min(offsetTo, from + startDelim.length())),
        startDelim)) {
        return from;
    }
    
    return offsetFrom;
}
 
Example 7
Source File: TagBasedFormatter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int getInitialIndentFromNextLine(final BaseDocument doc, final int line) throws BadLocationException {
    
    // get initial indent from the next line
    int initialIndent = 0;
    
    int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
    int lineEnd = Utilities.getRowEnd(doc, lineStart);
    int nextNonWhiteLineStart = Utilities.getFirstNonWhiteFwd(doc, lineEnd);
    
    if (nextNonWhiteLineStart > 0){
        initialIndent = Utilities.getRowIndent(doc, nextNonWhiteLineStart, true);
    }
    
    return initialIndent;
}
 
Example 8
Source File: ErrorAnnotationImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Transforms ErrosInfo to Annotation
 */
private Collection getAnnotations(ErrorInfo[] errors, StyledDocument document) {
    BaseDocument doc = (BaseDocument) document;
    HashMap map = new HashMap(errors.length);
    for (int i = 0; i < errors.length; i ++) {
        ErrorInfo err = errors[i];
        int line = err.getLine();
        int column = err.getColumn();

        if (line<0){
            // place error annotation on the 1st non-empty line
            try {
                int firstNonWS = Utilities.getFirstNonWhiteFwd(doc, 0);
                line = Utilities.getLineOffset(doc, firstNonWS) + 1;
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        
        if (column < 0){
            column = 0;
        }
        
        String message = err.getDescription();
        LineSetAnnotation ann;
        switch (err.getType()){
            case ErrorInfo.JSP_ERROR:
                ann = new JspParserErrorAnnotation(line, column, message, (NbEditorDocument)document);
                break;
            default:
                ann = new JspParserErrorAnnotation(line, column, message, (NbEditorDocument)document);
                break;
        }
       

        // This is trying to ensure that annotations on the same
        // line are "chained" (so we get a single annotation for
        // multiple errors on a line).
        // If we knew the errors were sorted by file & line number,
        // this would be easy (and we wouldn't need to do the hashmap
        // "sort"
        Integer lineInt = new Integer(line);
        /*LineSetAnnotation prev = (LineSetAnnotation)map.get(lineInt);
        if (prev != null) {
            prev.chain(ann);
        } else if (map.size() < maxErrors) {*/
        map.put(lineInt, ann);
        //}
    }
    return map.values();
}
 
Example 9
Source File: LineBreakHook.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void insert(MutableContext context) throws BadLocationException {
    if (!(context.getDocument() instanceof BaseDocument)) {
        return;
    }
    BaseDocument doc = (BaseDocument)context.getDocument();

    int insertPos = context.getCaretOffset();
    int caretPos = context.getComponent().getCaretPosition();
    int lineStartPos = Utilities.getRowStart(doc, insertPos);

    TokenHierarchy h = TokenHierarchy.get(doc);
    TokenSequence seq = h.tokenSequence();
    // check the actual tokens
    seq.move(context.getCaretOffset());
    int openOffset = followsOpeningTag(seq);
    
    int nonWhiteBefore = Utilities.getFirstNonWhiteBwd(doc, insertPos, lineStartPos);

    int lineEndPos = Utilities.getRowEnd(doc, caretPos);
    int nonWhiteAfter = Utilities.getFirstNonWhiteFwd(doc, caretPos, lineEndPos);

    // there is a opening tag preceding on the line && something following the insertion point
    if (nonWhiteBefore != -1 && nonWhiteAfter != -1 && openOffset >= 0) {
        // check that the following token (after whitespace(s)) is a 
        // opening tag
        seq.move(nonWhiteAfter);
        // now we need to position the caret at the END of the line immediately 
        // preceding the closing tag. Assuming it's already indented
        if (precedesClosingTag(seq)) {
            int startClosingLine = Utilities.getRowStart(doc, nonWhiteAfter);
            int nextLineStart = Utilities.getRowStart(doc, insertPos, 1);
            if (nextLineStart >= startClosingLine - 1) {
                insertBlankBetweenTabs(context, openOffset);
            }
            return;
        }
    }
    // if the rest of the line is blank, we must insert newline + indent, so the cursor
    // appears at the correct place
    if (nonWhiteAfter != -1) {
        // will be handled by the formatter automatically
        return;
    }

    int desiredIndent;

    if (openOffset != Integer.MIN_VALUE) {
        desiredIndent = IndentUtils.lineIndent(doc, Utilities.getRowStart(doc, Math.abs(openOffset)));
        if (openOffset >= 0) {
            desiredIndent += IndentUtils.indentLevelSize(doc);
        }
    } else {
        // align with the current line
        desiredIndent = IndentUtils.lineIndent(doc, lineStartPos);
    }
    String blankLine = "\n" + 
        IndentUtils.createIndentString(doc, desiredIndent);
    context.setText(blankLine, -1, blankLine.length(), 1, blankLine.length());
}