<commit_before>import os
import re

import sublime
import sublime_plugin


class SplitOnCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        selections = list(self.view.sel())
        selections.reverse()
        split_char = kwargs.get('split_char', '')
        if split_char:
            le = '\n'
            for selection in selections:
                current_line = self.view.line(selection)
                padding = ' ' * (selection.begin() - current_line.begin())
                text = self.view.substr(selection)
                elements = [x.strip() for x in text.split(split_char)]
                elements[1:] = [padding + x for x in elements[1:]]
                new_text = (split_char + le).join(elements)
                self.view.replace(edit, selection, new_text)


class Heading(object):
    def __init__(self, line):
        P = r'^(#+)[ \t]*[\d\.]*[ \t]*(.*)$'
        m = re.match(P, line)
        self.level = len(m.group(1))
        self.text = m.group(2)
        if self.level == 1:
            self.format = "{0}.0"
        else:
            self.format = '.'.join(['{{{v}}}'.format(v=i) for i in range(self.level)])
        self.value = ''

    def format_value(self, levels):
        self.value = self.format.format(*levels)

    def __str__(self):
        return ' '.join(['#' * self.level, self.value, self.text])


class MarkdownHeadings(sublime_plugin.TextCommand):

    def run(self, edit, **kwargs):
        regions = self.view.find_all(r'^(#+)[ \t]*[\d\.]*[ \t]*(.*)$')
        headings = []
        for r in regions:
            headings.append(Heading(self.view.substr(r)))
        levels = [0]
        for h in headings:
            if h.level > len(levels):
                levels.append(1)
            elif h.level == len(levels):
                levels[-1] += 1
            else:
                levels[h.level - 1] += 1
                levels[h.level: ] = [0] * len(levels[h.level: ])
            h.format_value(levels[:h.level])
        headings.reverse()
        regions.reverse()
        for i in range(len(regions)):
            self.view.replace(edit, regions[i], str(headings[i]))

    def is_enabled(self):
        return os.path.splitext(self.view.file_name())[-1].strip('.') in ['md', 'markdown']


class MarkdownHeadingsListener(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        view.run_command('markdown_headings')<commit_msg>small efficiency fix for markdown headings<commit_after>import os
import re

import sublime
import sublime_plugin


class SplitOnCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        selections = list(self.view.sel())
        selections.reverse()
        split_char = kwargs.get('split_char', '')
        if split_char:
            le = '\n'
            for selection in selections:
                current_line = self.view.line(selection)
                padding = ' ' * (selection.begin() - current_line.begin())
                text = self.view.substr(selection)
                elements = [x.strip() for x in text.split(split_char)]
                elements[1:] = [padding + x for x in elements[1:]]
                new_text = (split_char + le).join(elements)
                self.view.replace(edit, selection, new_text)


HEADING_P = r'^(#+)[ \t]*[\d\.]*[ \t]*(.*)$'

class Heading(object):
    def __init__(self, line):
        m = re.match(HEADING_P, line)
        self.level = len(m.group(1))
        self.text = m.group(2)
        if self.level == 1:
            self.format = "{0}.0"
        else:
            self.format = '.'.join(['{{{v}}}'.format(v=i) for i in range(self.level)])
        self.value = ''

    def format_value(self, levels):
        if self.level > len(levels):
            levels.append(1)
        elif self.level == len(levels):
            levels[-1] += 1
        else:
            levels[self.level - 1] += 1
            levels[self.level: ] = [0] * len(levels[self.level: ])
        self.value = self.format.format(*levels[:self.level])
        return levels

    def __str__(self):
        return ' '.join(['#' * self.level, self.value, self.text])


class MarkdownHeadings(sublime_plugin.TextCommand):

    def run(self, edit, **kwargs):
        regions = self.view.find_all(HEADING_P)
        headings = []
        levels = [0]
        for r in regions:
            h = Heading(self.view.substr(r))
            levels = h.format_value(levels)
            headings.append(h)            
        headings.reverse()
        regions.reverse()
        for i in range(len(regions)):
            self.view.replace(edit, regions[i], str(headings[i]))

    def is_enabled(self):
        return os.path.splitext(self.view.file_name())[-1].strip('.') in ['md', 'markdown']


class MarkdownHeadingsListener(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        view.run_command('markdown_headings')
{'fraction_non_alphanumeric': 0.15207185139824453, 'fraction_numerical': 0.00612369871402327, 'mean_word_length': 2.0973451327433628, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6824789', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1513, 'n_words': 313}