RangeMarker (Caret caret) { final int offset = caret.getOffset(); final Document document = myEditor.getDocument(); final CharSequence sequence = document.getCharsSequence(); int start = -1; boolean seenColon = false; for (int i = offset - 1; i >= Math.max(0, offset - 50); i--) { try { final char c = sequence.charAt(i); if (c == '<' || c == '/' && i > 0 && sequence.charAt(i - 1) == '<') { start = i + 1; break; } if (!isValidTagNameChar(c)) break; seenColon |= c == ':'; } catch (IndexOutOfBoundsException e) { LOG.error("incorrect offset:" + i + ", initial: " + offset, new Attachment("document.txt", sequence.toString())); return null; } } if (start < 0) return null; int end = -1; for (int i = offset; i < Math.min(document.getTextLength(), offset + 50); i++) { final char c = sequence.charAt(i); if (!isValidTagNameChar(c) || seenColon && c == ':') { end = i; break; } seenColon |= c == ':'; } if (end < 0 || start > end) return null; return document.createRangeMarker(start, end, true); }