<commit_before>module Textbringer
  class ProgrammingMode < FundamentalMode
    # abstract mode
    undefine_command(:programming_mode)

    define_generic_command :indent_line
    define_generic_command :reindent_then_newline_and_indent
    define_generic_command :indent_region
    define_generic_command :forward_definition
    define_generic_command :backward_definition
    define_generic_command :compile
    define_generic_command :toggle_test

    PROGRAMMING_MODE_MAP = Keymap.new
    PROGRAMMING_MODE_MAP.define_key("\t", :indent_line_command)
    PROGRAMMING_MODE_MAP.define_key("\C-m",
                                    :reindent_then_newline_and_indent_command)
    PROGRAMMING_MODE_MAP.define_key("\e\C-\\", :indent_region_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-n", :forward_definition_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-p", :backward_definition_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-c", :compile_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-t", :toggle_test_command)

    def initialize(buffer)
      super(buffer)
      buffer.keymap = PROGRAMMING_MODE_MAP
    end

    # Return true if modified.
    def indent_line
      result = false
      level = calculate_indentation
      return result if level.nil?
      @buffer.save_excursion do
        @buffer.beginning_of_line
        @buffer.composite_edit do
          if @buffer.looking_at?(/[ \t]+/)
            s = @buffer.match_string(0)
            break if /\t/ !~ s && s.size == level
            @buffer.delete_region(@buffer.match_beginning(0),
                                  @buffer.match_end(0))
          else
            break if level == 0
          end
          @buffer.indent_to(level)
        end
        result = true
      end
      pos = @buffer.point
      @buffer.beginning_of_line
      @buffer.forward_char while /[ \t]/ =~ @buffer.char_after
      if @buffer.point < pos
        @buffer.goto_char(pos)
      end
      result
    end

    def reindent_then_newline_and_indent
      @buffer.composite_edit do<commit_msg>Avoid errors when calcuated indentation is negative<commit_after>module Textbringer
  class ProgrammingMode < FundamentalMode
    # abstract mode
    undefine_command(:programming_mode)

    define_generic_command :indent_line
    define_generic_command :reindent_then_newline_and_indent
    define_generic_command :indent_region
    define_generic_command :forward_definition
    define_generic_command :backward_definition
    define_generic_command :compile
    define_generic_command :toggle_test

    PROGRAMMING_MODE_MAP = Keymap.new
    PROGRAMMING_MODE_MAP.define_key("\t", :indent_line_command)
    PROGRAMMING_MODE_MAP.define_key("\C-m",
                                    :reindent_then_newline_and_indent_command)
    PROGRAMMING_MODE_MAP.define_key("\e\C-\\", :indent_region_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-n", :forward_definition_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-p", :backward_definition_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-c", :compile_command)
    PROGRAMMING_MODE_MAP.define_key("\C-c\C-t", :toggle_test_command)

    def initialize(buffer)
      super(buffer)
      buffer.keymap = PROGRAMMING_MODE_MAP
    end

    # Return true if modified.
    def indent_line
      result = false
      level = calculate_indentation
      return result if level.nil? || level < 0
      @buffer.save_excursion do
        @buffer.beginning_of_line
        @buffer.composite_edit do
          if @buffer.looking_at?(/[ \t]+/)
            s = @buffer.match_string(0)
            break if /\t/ !~ s && s.size == level
            @buffer.delete_region(@buffer.match_beginning(0),
                                  @buffer.match_end(0))
          else
            break if level == 0
          end
          @buffer.indent_to(level)
        end
        result = true
      end
      pos = @buffer.point
      @buffer.beginning_of_line
      @buffer.forward_char while /[ \t]/ =~ @buffer.char_after
      if @buffer.point < pos
        @buffer.goto_char(pos)
      end
      result
    end

    def reindent_then_newline_and_indent
      @buffer.composite_edit do
{'fraction_non_alphanumeric': 0.08617808546192408, 'fraction_numerical': 0.0021484841250895203, 'mean_word_length': 2.685136323658751, 'pattern_counts': {'":': 0, '<': 8, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4879999', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1309, 'n_words': 217}