<commit_before># -*- coding: utf-8 -*-
""" Contains the automatic generic indenter """
import re
from pyqode.core.frontend import Mode
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QTextCursor, QKeyEvent


class AutoIndentMode(Mode):
    """
    Generic indenter mode that indents the text when the user press RETURN.

    You can customize this mode by overriding
    :meth:`pyqode.core.AutoIndentMode._getIndent`
    """
    def __init__(self):
        super(AutoIndentMode, self).__init__()
        self.min_indent = ""

    def _get_indent(self, tc):
        """
        Return the indentation text (a series of spaces or tabs)

        :param tc: QTextCursor

        :returns: Tuple (text before new line, text after new line)
        """
        pos = tc.position()
        # tc.movePosition(QTextCursor.StartOfLine)
        # tc.setPosition(tc.position() - 1)
        tc.movePosition(QTextCursor.StartOfLine)
        tc.select(QTextCursor.LineUnderCursor)
        s = tc.selectedText()
        indent = re.match(r"\s*", s).group()
        tc.setPosition(pos)
        if len(indent) < len(self.min_indent):
            indent = self.min_indent
        return "", indent

    def _on_state_changed(self, state):
        if state is True:
            self.editor.key_pressed.connect(self._on_key_pressed)
        else:
            self.editor.key_pressed.disconnect(self._on_key_pressed)

    def _on_key_pressed(self, event):
        """
        Auto indent if the released key is the return key.
        :param event: the key event
        """
        if event.isAccepted():
            return
        if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
            tc = self.editor.textCursor()
            pre, post = self._get_indent(tc)
            tc.insertText("%s\n%s" % (pre, post))

            # eats possible whitespaces
            tc.movePosition(tc.WordRight, tc.KeepAnchor)
            txt = tc.selectedText()
            if txt.startswith(' '):
                new_txt = txt.replace(" ", '')
                if len(txt) > len(new_txt):
                    tc.insertText(new_txt)

            event.accept()
<commit_msg>Use text api functions<commit_after># -*- coding: utf-8 -*-
""" Contains the automatic generic indenter """
import re
from pyqode.core.frontend import Mode, text
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QTextCursor, QKeyEvent


class AutoIndentMode(Mode):
    """
    Generic indenter mode that indents the text when the user press RETURN.

    You can customize this mode by overriding
    :meth:`pyqode.core.AutoIndentMode._getIndent`
    """
    def __init__(self):
        super(AutoIndentMode, self).__init__()
        self.min_indent = ""

    def _get_indent(self, tc):
        """
        Return the indentation text (a series of spaces or tabs)

        :param tc: QTextCursor

        :returns: Tuple (text before new line, text after new line)
        """
        indent = text.line_indent(self.editor) * ' '
        if len(indent) < len(self.min_indent):
            indent = self.min_indent
        return "", indent

    def _on_state_changed(self, state):
        if state is True:
            self.editor.key_pressed.connect(self._on_key_pressed)
        else:
            self.editor.key_pressed.disconnect(self._on_key_pressed)

    def _on_key_pressed(self, event):
        """
        Auto indent if the released key is the return key.
        :param event: the key event
        """
        if event.isAccepted():
            return
        if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
            tc = self.editor.textCursor()
            pre, post = self._get_indent(tc)
            tc.insertText("%s\n%s" % (pre, post))

            # eats possible whitespaces
            tc.movePosition(tc.WordRight, tc.KeepAnchor)
            txt = tc.selectedText()
            if txt.startswith(' '):
                new_txt = txt.replace(" ", '')
                if len(txt) > len(new_txt):
                    tc.insertText(new_txt)

            event.accept()

{'fraction_non_alphanumeric': 0.10747663551401869, 'fraction_numerical': 0.001721593703885883, 'mean_word_length': 2.116475095785441, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7236231', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1195, 'n_words': 307}