JavaCodeFragment (JavaCodeFragment codeFragment) { GlobalSearchScope originalResolveScope = codeFragment.getResolveScope(); codeFragment.forceResolveScope(new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(codeFragment.getProject())) { final Comparator<VirtualFile> myScopeComparator = Comparator.comparing(originalResolveScope::contains).thenComparing(super::compare); @Override public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) { // prefer files from the original resolve scope return myScopeComparator.compare(file1, file2); } }); return codeFragment; }