Bookmark (@NotNull Editor editor, boolean isWrapped, boolean next) { VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument()); if (file == null) return null; List<Bookmark> bookmarksForDocument = new ArrayList<>(myBookmarks.get(file)); if (bookmarksForDocument.isEmpty()) return null; int sign = next ? 1 : -1; bookmarksForDocument.sort((o1, o2) -> sign * (o1.getLine() - o2.getLine())); int caretLine = editor.getCaretModel().getLogicalPosition().line; for (Bookmark bookmark : bookmarksForDocument) { if (next && bookmark.getLine() > caretLine) return bookmark; if (!next && bookmark.getLine() < caretLine) return bookmark; } return isWrapped && !bookmarksForDocument.isEmpty() ? bookmarksForDocument.get(0) : null; }