PsiFile () { final PsiFile file = getFile(); int offsetWithRegexp = file.getText().indexOf("Pattern.compile(\"") + "Pattern.compile(\"".length(); final PsiElement stringLiteralLeaf = file.findElementAt(offsetWithRegexp); assertNotNull(stringLiteralLeaf); assertNotNull(stringLiteralLeaf.getParent()); assertTrue(stringLiteralLeaf.getParent() instanceof PsiLanguageInjectionHost); final PsiLanguageInjectionHost elementWithInjection = ((PsiLanguageInjectionHost)stringLiteralLeaf.getParent()); InjectedLanguageManager.getInstance(file.getProject()).enumerateEx(elementWithInjection, file, false, (injectedPsi, places) -> { }); assertTrue(InjectedLanguageUtil.hasInjections(elementWithInjection)); final PsiElement elementInInjected = InjectedLanguageUtil.findElementInInjected(elementWithInjection, offsetWithRegexp); final PsiFile regexpFile = PsiTreeUtil.getParentOfType(elementInInjected, PsiFile.class); assertNotNull(regexpFile); return regexpFile; }