PsiLanguageInjectionHost (@NotNull PsiFile hostPsiFile, @NotNull ASTNode oldRoot, @NotNull ASTNode newRoot, @NotNull PsiLanguageInjectionHost oldInjectionHost, @NotNull Segment newInjectionHostRange) { TextRange oldRootRange = oldRoot.getTextRange(); TextRange newRootRange = newRoot.getTextRange(); PsiElement toLookIn; int startToLook; int endToLook; if (newInjectionHostRange.getEndOffset() <= oldRootRange.getStartOffset()) { // find left of the change toLookIn = hostPsiFile; startToLook = newInjectionHostRange.getStartOffset(); endToLook = newInjectionHostRange.getEndOffset(); } else if (newInjectionHostRange.getStartOffset() >= oldRootRange.getStartOffset() + newRootRange.getLength()) { // find right of the change toLookIn = hostPsiFile; startToLook = newInjectionHostRange.getStartOffset() + newRootRange.getLength() - oldRootRange.getLength(); endToLook = newInjectionHostRange.getEndOffset() + newRootRange.getLength() - oldRootRange.getLength(); } else { // inside toLookIn = newRoot.getPsi(); if (toLookIn instanceof PsiFile) { FileViewProvider viewProvider = ((PsiFile)toLookIn).getViewProvider(); toLookIn = ObjectUtils.notNull(viewProvider.getPsi(hostPsiFile.getLanguage()), viewProvider.getPsi(viewProvider.getBaseLanguage())); } startToLook = newInjectionHostRange.getStartOffset() - oldRootRange.getStartOffset(); endToLook = newInjectionHostRange.getEndOffset() - oldRootRange.getStartOffset(); } try { Identikit.ByType kit = Identikit.fromPsi(oldInjectionHost, hostPsiFile.getLanguage()); return (PsiLanguageInjectionHost)kit.findInside(toLookIn, startToLook, endToLook); } // JavaParserDefinition.create() throws this. // DO not over-generalize this exception type to avoid swallowing meaningful exceptions catch (IllegalArgumentException e) { return null; } }