Java JTextComponent Word getWordAtCaret(JTextComponent editor)

Here you can find the source of getWordAtCaret(JTextComponent editor)

Description

get Word At Caret

License

Apache License

Declaration

public static String getWordAtCaret(JTextComponent editor) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.text.BadLocationException;

import javax.swing.text.JTextComponent;

public class Main {
    public static String getWordAtCaret(JTextComponent editor) {
        try {/*  w  ww  .  j a va  2 s.  c  o  m*/
            int iCaretPos = editor.getCaretPosition();
            int iStart = getWordStart(editor, iCaretPos);
            int iEnd = getWordEnd(editor, iCaretPos);
            return editor.getText(iStart, iEnd - iStart);
        } catch (BadLocationException e) {
            return getPartialWordBeforeCaret(editor);
        }
    }

    public static int getWordStart(JTextComponent editor, int iOffset) throws BadLocationException {
        String text = editor.getText();
        iOffset = maybeAdjustOffsetToNextWord(text, iOffset);

        int iStart = iOffset;
        for (; iStart > 0 && Character.isJavaIdentifierPart(text.charAt(iStart)); iStart--)
            ;
        if (iStart != iOffset) {
            iStart++;
        }
        return iStart;
    }

    public static int getWordEnd(JTextComponent editor, int iOffset) throws BadLocationException {
        String text = editor.getText();
        iOffset = maybeAdjustOffsetToNextWord(text, iOffset);

        int iEnd = iOffset;
        for (; iEnd < text.length() && Character.isJavaIdentifierPart(text.charAt(iEnd)); iEnd++)
            ;
        if (iEnd == iOffset && !Character.isWhitespace(text.charAt(iEnd))) {
            // the word is a single, non-identifier character
            iEnd++;
        }
        return iEnd;
    }

    public static String getPartialWordBeforeCaret(JTextComponent editor) {
        return getPartialWordBeforePos(editor, editor.getCaretPosition());
    }

    private static int maybeAdjustOffsetToNextWord(String text, int iOffset) throws BadLocationException {
        if (text.length() < iOffset) {
            throw new BadLocationException("Index out of bounds. Offset: " + iOffset + "  Length: " + text.length(),
                    iOffset);
        }

        if (iOffset > 0 && !text.isEmpty()
                && (text.length() == iOffset || Character.isWhitespace(text.charAt(iOffset)))) {
            if (Character.isWhitespace(text.charAt(iOffset - 1))) {
                while (iOffset < text.length()) {
                    if (!Character.isWhitespace(text.charAt(iOffset))) {
                        return iOffset;
                    }
                    iOffset++;
                }
            }
            iOffset--;
        }
        return iOffset;
    }

    public static String getPartialWordBeforePos(JTextComponent editor, int iPos) {
        try {
            int iNonWhitespace = findNonWhitespacePositionBefore(editor.getText(), iPos - 1);
            int iStart = getWordStart(editor, iNonWhitespace);
            return editor.getText(iStart, iPos - iStart).trim();
        } catch (BadLocationException e) {
            return "";
        }
    }

    /**
     * @return the first non-whitespace position in the given script before the given position
     */
    public static int findNonWhitespacePositionBefore(String script, int position) {
        if (position > script.length() - 1) {
            return position;
        }

        while (position > 0) {
            if (!Character.isWhitespace(script.charAt(position))) {
                break;
            }
            position--;
        }
        return position;
    }
}

Related

  1. getNextWord(JTextComponent c, int offs)
  2. getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first)
  3. getPartialWordBeforePos(JTextComponent editor, int iPos)
  4. getPreviousWord(JTextComponent editor, int iOffset)
  5. getWordDimensionAtCaret(JTextComponent editor)
  6. getWordDimensionBeforeCaret(JTextComponent editor)
  7. getWordEnd(JTextComponent c, int offs)
  8. getWordEnd(JTextComponent editor, int iOffset)