String (@Nullable Editor editor, boolean selectWordIfFound) { if (editor == null) return null; int caretOffset = editor.getCaretModel().getOffset(); Document document = editor.getDocument(); CharSequence text = document.getCharsSequence(); int start = 0; int end = document.getTextLength(); if (!editor.getSelectionModel().hasSelection()) { for (int i = caretOffset - 1; i >= 0; i--) { char c = text.charAt(i); if (!Character.isJavaIdentifierPart(c)) { start = i + 1; break; } } for (int i = caretOffset; i < document.getTextLength(); i++) { char c = text.charAt(i); if (!Character.isJavaIdentifierPart(c)) { end = i; break; } } if (start < end && selectWordIfFound) { editor.getSelectionModel().setSelection(start, end); EditorSearchSession.logSelectionUpdate(); } } else { start = editor.getSelectionModel().getSelectionStart(); end = editor.getSelectionModel().getSelectionEnd(); } return start < end? text.subSequence(start, end).toString() : null; }