'Create the QTextDocument of an item.
Args:
index: The QModelIndex of the item to draw.'

def _get_textdoc(self, index):

    text_option = QTextOption()
    if (self._opt.features & QStyleOptionViewItem.WrapText):
        text_option.setWrapMode(QTextOption.WordWrap)
    else:
        text_option.setWrapMode(QTextOption.ManualWrap)
    text_option.setTextDirection(self._opt.direction)
    text_option.setAlignment(QStyle.visualAlignment(self._opt.direction, self._opt.displayAlignment))
    if (self._doc is not None):
        self._doc.deleteLater()
    self._doc = QTextDocument(self)
    self._doc.setDefaultFont(self._opt.font)
    self._doc.setDefaultTextOption(text_option)
    self._doc.setDefaultStyleSheet(style.get_stylesheet("\n                                                .highlight    {\n                                                                color:    {{    color['completion.match.fg']    }};\n                                                }\n                                "))
    self._doc.setDocumentMargin(2)
    if index.parent().isValid():
        view = self.parent()
        pattern = view.pattern
        columns_to_filter = index.model().columns_to_filter(index)
        if ((index.column() in columns_to_filter) and pattern):
            repl = '<span    class="highlight">\\g<0></span>'
            text = re.sub(re.escape(pattern).replace('\\    ', '|'), repl, self._opt.text, flags=re.IGNORECASE)
            self._doc.setHtml(text)
        else:
            self._doc.setPlainText(self._opt.text)
    else:
        self._doc.setHtml('<span    style="font:    {};">{}</span>'.format(html.escape(config.get('fonts', 'completion.category')), html.escape(self._opt.text)))
