<commit_before>module Redcar
  class StripTrailingSpaces
    def self.storage
      @storage ||= begin
        storage = Plugin::Storage.new('strip_trailing_spaces_plugin')
        storage.set_default('enabled', false)
        storage
      end
    end

    def self.menus
      Menu::Builder.build do
        sub_menu "Plugins" do
          sub_menu "Strip Trailing Spaces" do
            if (Redcar::StripTrailingSpaces.storage['enabled'])
              item "Disable Plugin", DisablePlugin
            else
              item "Enable Plugin", EnablePlugin
            end
          end
        end
      end
    end

    def self.before_save(doc)
      if (doc.mirror.is_a?(Redcar::Project::FileMirror) && storage['enabled'])
        # Read cursor position and adjust line offset
        cursor_line = doc.cursor_line
        top_line = doc.smallest_visible_line
        line_offset = doc.cursor_line_offset
        line = doc.get_line(cursor_line)
        line_offset = line.rstrip.size if line_offset > line.rstrip.size

        doc.text = doc.to_s.split("\n").each{|s| s.rstrip!}.join("\n") + "\n"

        # Adjust cursor offset and make visible
        doc.scroll_to_line_at_top(top_line)
        offset=doc.offset_at_line(cursor_line) + line_offset
        doc.cursor_offset=offset
        doc.ensure_visible(offset)
      end
    end

    class EnablePlugin < Redcar::Command
      def execute
        Redcar::StripTrailingSpaces.storage['enabled'] = true
        Redcar.app.refresh_menu!
      end
    end

    class DisablePlugin < Redcar::Command
      def execute
        Redcar::StripTrailingSpaces.storage['enabled'] = false
        Redcar.app.refresh_menu!
      end
    end
  end
end<commit_msg>First attempt at stripping line by line instead of all at once<commit_after>module Redcar
  class StripTrailingSpaces
    def self.storage
      @storage ||= begin
        storage = Plugin::Storage.new('strip_trailing_spaces_plugin')
        storage.set_default('enabled', false)
        storage
      end
    end

    def self.menus
      Menu::Builder.build do
        sub_menu "Plugins" do
          sub_menu "Strip Trailing Spaces" do
            if (Redcar::StripTrailingSpaces.storage['enabled'])
              item "Disable Plugin", DisablePlugin
            else
              item "Enable Plugin", EnablePlugin
            end
          end
        end
      end
    end

    def self.before_save(doc)
      if (doc.mirror.is_a?(Redcar::Project::FileMirror) && storage['enabled'])
        # Read cursor position and adjust line offset
        cursor_line = doc.cursor_line
        top_line = doc.smallest_visible_line
        line_offset = doc.cursor_line_offset
        line = doc.get_line(cursor_line)
        line_offset = line.rstrip.size if line_offset > line.rstrip.size

        doc.line_count.times do |l|
          line_text = doc.get_line(l)
          line_text.rstrip!
          start_offset = doc.offset_at_line(l)
          end_offset = doc.offset_at_inner_end_of_line(l)
          doc.replace(start_offset, end_offset - start_offset, line_text)
        end

        # Adjust cursor offset and make visible
        doc.scroll_to_line_at_top(top_line)
        offset=doc.offset_at_line(cursor_line) + line_offset
        doc.cursor_offset=offset
        doc.ensure_visible(offset)
      end
    end

    class EnablePlugin < Redcar::Command
      def execute
        Redcar::StripTrailingSpaces.storage['enabled'] = true
        Redcar.app.refresh_menu!
      end
    end

    class DisablePlugin < Redcar::Command
      def execute
        Redcar::StripTrailingSpaces.storage['enabled'] = false
        Redcar.app.refresh_menu!
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08075880758807588, 'fraction_numerical': 0.0, 'mean_word_length': 2.27797513321492, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1656390', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1074, 'n_words': 235}