int (@NotNull Caret caret) { int caretOffset = caret.getOffset(); if (caretOffset == 0) { return caretOffset; } CharSequence text = caret.getEditor().getDocument().getCharsSequence(); char prev = text.charAt(caretOffset - 1); if (caretOffset < text.length() && !Character.isJavaIdentifierPart(text.charAt(caretOffset)) && Character.isJavaIdentifierPart(prev)) { return caretOffset - 1; } if ((caretOffset == text.length() || Character.isWhitespace(text.charAt(caretOffset))) && !Character.isWhitespace(prev)) { return caretOffset - 1; } return caretOffset; }