ASTNode (@NotNull ASTNode chameleon, @NotNull PsiElement psi) { final Project project = psi.getProject(); final Lexer lexer = new MarkdownMergingLexer(); final CharSequence chars = new StringUtil.BombedCharSequence(chameleon.getChars()) { @Override protected void checkCanceled() { ProgressManager.checkCanceled(); } }; MarkdownFlavourDescriptor flavour = psi.getContainingFile().getUserData(MarkdownParserManager.FLAVOUR_DESCRIPTION); if (flavour == null) { LOG.error("Markdown flavour doesn't set for " + psi.getContainingFile()); flavour = MarkdownParserManager.FLAVOUR; } final var parser = new MarkdownParser(flavour, true); final var nodeType = MarkdownElementType.markdownType(chameleon.getElementType()); final var node = parser.parseInline(nodeType, chars, 0, chars.length()); final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, lexer, getLanguage(), chars); PsiBuilder.Marker rootMarker = builder.mark(); //Flatten type is used to solve problem with trailing whitespaces new PsiBuilderFillingVisitor(builder, false).visitNode(node); assert builder.eof(); rootMarker.done(this); final var tree = builder.getTreeBuilt(); final var actualElement = tree.getFirstChildNode().getFirstChildNode(); //System.out.println("Expanded tree:\n" + DebugKt.astToString(tree)); return actualElement; }