void (@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { Comparator<Integer> ordering = myDirection.ordering; int caretOffset = editor.getCaretModel().getOffset(); int startOffset = file.getTextRange().getStartOffset(); int endOffset = file.getTextRange().getEndOffset(); Ref<Integer> first = new Ref<>(); Ref<Integer> next = new Ref<>(); DaemonCodeAnalyzerEx.processHighlights(((MarkupModelEx)editor.getMarkupModel()), project, null, startOffset, endOffset, info -> { if (HighlightInfoType.ELEMENT_UNDER_CARET_READ.equals(info.type) || HighlightInfoType.ELEMENT_UNDER_CARET_WRITE.equals(info.type)) { if (ordering.compare(info.startOffset, caretOffset) > 0 && ordering.compare(info.endOffset, caretOffset) > 0) { if (next.isNull() || ordering.compare(next.get(), info.startOffset) > 0) { next.set(info.startOffset); } } if (first.isNull() || ordering.compare(first.get(), info.startOffset) > 0) { first.set(info.startOffset); } } return true; }); if (!next.isNull()) { moveCaret(editor, next.get()); } else if (!first.isNull()) { moveCaret(editor, first.get()); } else { // It's ok, do nothing. } }