PsiBuilder (@NotNull final ASTNode chameleon, @NotNull Function<PsiElement, LanguageLevel> languageLevelFunction, @NotNull Function<LanguageLevel, BasicJavaLexer> lexerFunction, @NotNull Function<PsiFile, TokenList> psiAsLexer) { final PsiElement psi = chameleon.getPsi(); assert psi != null : chameleon; final Project project = psi.getProject(); CharSequence indexedText = psi.getUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY); CharSequence text; if (TreeUtil.isCollapsedChameleon(chameleon)) { text = chameleon.getChars(); } else { text = indexedText; if (text == null) text = chameleon.getChars(); } LanguageLevel level = languageLevelFunction.apply(psi); Lexer lexer = psi instanceof PsiFile && indexedText != null ? psiAsLexer.apply((PsiFile)psi).asLexer() : lexerFunction.apply(level); Language language = psi.getLanguage(); if (!language.isKindOf(JavaLanguage.INSTANCE)) language = JavaLanguage.INSTANCE; PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, lexer, language, text); setLanguageLevel(builder, level); return builder; }