Java JTextComponent Word getPartialWordBeforePos(JTextComponent editor, int iPos)

Here you can find the source of getPartialWordBeforePos(JTextComponent editor, int iPos)

Description

get Partial Word Before Pos

License

Apache License

Declaration

public static String getPartialWordBeforePos(JTextComponent editor, int iPos) 

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 getPartialWordBeforePos(JTextComponent editor, int iPos) {
        try {/*ww w  . j  a v  a 2s .  c  om*/
            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;
    }

    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;
    }

    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;
    }
}

Related

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