PsiFile (@NotNull PsiLanguageInjectionHost host, @NotNull PsiFile hostFileCopy, @NotNull Language injectedLanguage) { InjectedLanguageManager injectionManager = InjectedLanguageManager.getInstance(hostFileCopy.getProject()); PsiLanguageInjectionHost hostCopy = PsiTreeUtil.findSameElementInCopy(host, hostFileCopy); var visitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() { private final Language origLanguage = injectedLanguage; PsiFile injectedFileCopy = null; @Override public void visit(@NotNull PsiFile injectedPsi, @NotNull List<? extends PsiLanguageInjectionHost.Shred> places) { if (injectedPsi.getLanguage() == origLanguage) { injectedFileCopy = injectedPsi; } } }; injectionManager.enumerate(hostCopy, visitor); return Objects.requireNonNull(visitor.injectedFileCopy); }