/** First of all, change the public static String getWord(JTextComponent c, int offset) function into public static WordBlock getWordBlock(JTextComponent c, int offset) as shown below*/ /** create an inner class which will be the return type of our function*/ class WordBlock { public String word = null; public int startPos = 0; public int endPos = 0; } /** now change the getWOrd(...) into getWordBlock(...) */ public static WordBlock getWordBlock(JTextComponent c, int offset) throws BadLocationException { String id = null; int[] res = null; WordBlock wb = null; Document doc = c.getDocument(); int idStart = javax.swing.text.Utilities.getWordStart(c, offset); if (idStart >= 0) { int idEnd = javax.swing.text.Utilities.getWordEnd(c, idStart); if (idEnd >= 0) { id = doc.getText(idStart, idEnd - idStart); ret = new int[] { idStart, idEnd }; if (id.trim().length() == 0) { int prevWordStart = javax.swing.text.Utilities.getPreviousWord(c, offset); if (offset == javax.swing.text.Utilities.getWordEnd(c,prevWordStart )) { id = doc.getText(prevWordStart, offset - prevWordStart); ret = new int[] { prevWordStart, offset }; } } else if ((id != null) && (id.length() != 0) && (id.indexOf(".") != -1)){ int index = offset - idStart; int begin = id.substring(0, index).lastIndexOf("."); begin = (begin == -1) ? 0 : begin + 1; //first index after the dot, if exists int end = id.indexOf(".", index); end = (end == -1) ? id.length() : end; id = id.substring(begin, end); ret = new int[] { idStart+begin, idStart+end }; } } } if (id != null) { wb = new WordBlock(); wb.word = id; wb.startPos = res[0]; wb.endPos = res[1]; } return wb; } /** Then modify getSelectionOrIdentifierBlock */ public static int[] getSelectionOrIdentifierBlock(JTextComponent c, int offset) throws BadLocationException { Document doc = (Document)c.getDocument(); Caret caret = c.getCaret(); int[] ret; if (caret.isSelectionVisible()) { ret = new int[] { c.getSelectionStart(), c.getSelectionEnd() }; } else if !(doc instanceof BaseDocument){ WordBlock wb = getWordBlock(c, offset); if (wb != null) ret = new int[] {wb.startPos, wb.endPos}; } else { ret = getIdentifierBlock((BaseDocument)doc, caret.getDot()); } return ret; } /** Also modify the function below which was using getWord(JTextComponent c, int offset) to reflect the changes*/ public static String getSelectionOrIdentifier(JTextComponent c, int offset) throws BadLocationException { Document doc = c.getDocument(); Caret caret = c.getCaret(); String ret; if (caret.isSelectionVisible()) { ret = c.getSelectedText(); if (ret != null) return ret; } if (doc instanceof BaseDocument){ ret = getIdentifier((BaseDocument) doc, caret.getDot()); } else { WordBlock wb = getWordBlock(c, offset); if (wb != null) ret = wb.word; } return ret; }