Usage (@NotNull UsageSearcher usageSearcher, @NotNull FileSearchScope dir, int startOffset, @NotNull AtomicBoolean usagesWereFound, @NotNull Editor editor) { if (editor.getUserData(KEY_START_USAGE_AGAIN) != null) { dir = dir == FileSearchScope.AFTER_CARET ? FileSearchScope.FROM_START : FileSearchScope.FROM_END; } FileSearchScope direction = dir; AtomicReference<Usage> foundUsage = new AtomicReference<>(); usageSearcher.generate(usage -> { usagesWereFound.set(true); int usageOffset = usage instanceof UsageInfo2UsageAdapter ? ((UsageInfo2UsageAdapter)usage).getNavigationRange().getStartOffset() : 0; switch (direction) { case FROM_START -> { foundUsage.compareAndSet(null, usage); return false; } case FROM_END -> foundUsage.set(usage); case AFTER_CARET -> { if (usageOffset > startOffset) { foundUsage.set(usage); return false; } } case BEFORE_CARET -> { if (usageOffset >= startOffset) { return false; } while (true) { Usage found = foundUsage.get(); if (found == null) { if (foundUsage.compareAndSet(null, usage)) break; } else { int foundOffset = found instanceof UsageInfo2UsageAdapter ? ((UsageInfo2UsageAdapter)found).getNavigationRange().getStartOffset() : 0; if (foundOffset < usageOffset && foundUsage.compareAndSet(found, usage)) break; } } } } return true; }); editor.putUserData(KEY_START_USAGE_AGAIN, null); return foundUsage.get(); }