<commit_before># Code adapted from:
#
# http://rowinggolfer.blogspot.de/2010/08/qtextedit-with-autocompletion-using.html
#
# and based on:
#
# http://qt-project.org/doc/qt-4.8/tools-customcompleter.html

from ...external.qt import QtGui, QtCore


__all__ = ["CompletionTextEdit"]


class CompletionTextEdit(QtGui.QTextEdit):

    def __init__(self, parent=None):

        super(CompletionTextEdit, self).__init__(parent)

        self.setMinimumWidth(400)
        self.completer = None
        self.word_list = None

        self.moveCursor(QtGui.QTextCursor.End)

    def set_word_list(self, word_list):
        self.word_list = word_list
        self.set_completer(QtGui.QCompleter(word_list))

    def set_completer(self, completer):

        if self.completer:
            self.disconnect(self.completer, 0, self, 0)
        if not completer:
            return

        completer.setWidget(self)
        completer.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer = completer
        self.connect(self.completer, QtCore.SIGNAL("activated(const QString&)"), self.insert_completion)

    def insert_completion(self, completion):

        tc = self.textCursor()
        tc.select(QtGui.QTextCursor.WordUnderCursor)
        tc.deleteChar()

        completion = completion + " "

        self.setTextCursor(tc)

        self.insertPlainText(completion)


    def text_under_cursor(self):
        tc = self.textCursor()
        tc.select(QtGui.QTextCursor.WordUnderCursor)
        return tc.selectedText()

    # The following methods override methods in QTextEdit and should not be
    # renamed.<commit_msg>Simplify event connection as suggested by @ChrisBeaumont<commit_after># Code adapted from:
#
# http://rowinggolfer.blogspot.de/2010/08/qtextedit-with-autocompletion-using.html
#
# and based on:
#
# http://qt-project.org/doc/qt-4.8/tools-customcompleter.html

from ...external.qt import QtGui, QtCore


__all__ = ["CompletionTextEdit"]


class CompletionTextEdit(QtGui.QTextEdit):

    def __init__(self, parent=None):

        super(CompletionTextEdit, self).__init__(parent)

        self.setMinimumWidth(400)
        self.completer = None
        self.word_list = None

        self.moveCursor(QtGui.QTextCursor.End)

    def set_word_list(self, word_list):
        self.word_list = word_list
        self.set_completer(QtGui.QCompleter(word_list))

    def set_completer(self, completer):

        if self.completer:
            self.disconnect(self.completer, 0, self, 0)
        if not completer:
            return

        self.completer = completer

        self.completer.setWidget(self)
        self.completer.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer.activated.connect(self.insert_completion)

    def insert_completion(self, completion):

        tc = self.textCursor()
        tc.select(QtGui.QTextCursor.WordUnderCursor)
        tc.deleteChar()

        completion = completion + " "

        self.setTextCursor(tc)

        self.insertPlainText(completion)


    def text_under_cursor(self):
        tc = self.textCursor()
        tc.select(QtGui.QTextCursor.WordUnderCursor)
        return tc.selectedText()

    # The following methods override methods in QTextEdit and should not be
    # renamed.
{'fraction_non_alphanumeric': 0.09985358711566618, 'fraction_numerical': 0.007613469985358712, 'mean_word_length': 2.9628770301624128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2960153', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1033, 'n_words': 176}