PsiFile (@NotNull PsiElement hostContext, @NotNull PsiFile hostFile, @NotNull TextRange rangeInHostFile) { PsiDocumentManagerBase docManager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(getProject()); PsiFile[] result = {null}; PsiLanguageInjectionHost.InjectedPsiVisitor visitor = (injectedPsi, places) -> { Document document = docManager.getDocument(injectedPsi); if (document instanceof DocumentWindow) { DocumentWindow window = (DocumentWindow)docManager.getLastCommittedDocument(document); TextRange hostRange = window.injectedToHost(new TextRange(0, injectedPsi.getTextLength())); if (hostRange.contains(rangeInHostFile)) { result[0] = injectedPsi; } } }; PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject()); Document document = documentManager.getDocument(hostFile); InjectedLanguageManager injectionManager = InjectedLanguageManager.getInstance(getProject()); if (document != null && documentManager.isUncommited(document)) { List<DocumentWindow> documents = injectionManager.getCachedInjectedDocumentsInRange(hostFile, rangeInHostFile); Set<PsiFile> injectedFiles = ContainerUtil.map2SetNotNull(documents, d -> documentManager.getPsiFile(d)); for (PsiFile injectedFile : injectedFiles) { visitor.visit(injectedFile, ContainerUtil.emptyList()); } } else { List<Pair<PsiElement, TextRange>> injected = injectionManager.getInjectedPsiFiles(hostContext); if (injected != null) { Set<PsiFile> injectedFiles = ContainerUtil.map2SetNotNull(injected, pair -> pair.first.getContainingFile()); for (PsiFile injectedFile : injectedFiles) { visitor.visit(injectedFile, ContainerUtil.emptyList()); } } } return result[0]; }