<commit_before>module Pvim
  class Plugin < Thor
    include Thor::Actions

    class_option  :pvim,
                  :aliases => 'p',
                  :default => File.expand_path(Dir.pwd),
                  :desc    => 'Your current pvim director'

    option :sort, :aliases => '-s', :type => :boolean, :default => false, :desc => 'Sort installed plugins by name'
    desc 'list', 'List all installed vim plugins'
    def list
      inside pvim do
        plugins = load_plugins
        plugins = plugins.values
        if plugins.any?
          plugins.sort_by!{ |(name, _, _)| name.downcase } if options[:sort]
          say "Installed plugins:", Color::GREEN
          say
          plugins.unshift [ 'Name', 'Repository URL', 'Installed at' ]
          print_table plugins
          say
          say "Total: #{plugins.size - 1}"
        else
          say "No installed plugins."
        end
      end
    end

    desc 'add URL', 'Add vim plugins'
    def add(url)
      check_url(url)
      inside pvim do
        empty_directory bundle_dir
        run "git submodule add #{url.inspect} #{installed_dir(url).inspect}"
      end
    end

    desc 'remove URL', 'Remove vim plugins'
    def remove(url)
      check_url(url)
      if plugin = find_plugin(url)
        inside pvim do
          empty_directory bundle_dir
          path = plugin.last
          submodule = ['submodule', path].join('.')
          run "git submodule deinit -f #{path.inspect}"
          run "git rm -rf #{path.inspect}"
          run "git config -f .gitmodules --remove-section #{submodule.inspect}"
          remove_file File.join('.git', 'modules', path)
        end
      else
        say "#{url} is not installed", Color::RED
      end
    end

    desc 'update', 'Update all vim plugins to latest versions'
    def update
      inside pvim do
        empty_directory bundle_dir
        run "git submodule update"
      end
    end

    private
      def pvim
        @pvim ||= File.expand_path(options[:pvim] || Dir.pwd)
      end

      def bundle_dir
        @bundle_dir ||= File.join(pvim, 'bundle')
      end

      def installed_dir(url)
        repo = url.gsub(/^https?\:\/\/github\.com\//, '') # <user>/<project>
        File.join(bundle_dir.gsub(pvim+'/', ''), repo.gsub('/', '-'))
      end

      def gitmodules
        @gitmodules ||= File.join(pvim, '.gitmodules')
      end

      def git_config
        @git_config ||= File.join(pvim, '.git', 'config')
      end

      def load_plugins
        return @plugins if @plugins

        unless File.exists?(gitmodules)
          say ".gitmodules does not exist.", Color::RED
          exit(1)
        end

        @plugins = {}
        lines = File.readlines(gitmodules)
        lines.map!(&:chomp)
        lines.delete_if(&:empty?)
        lines.each_slice(3) do |(_, path, url)|
          path = value(path)
          url  = value(url)
          name = repo_name(url)
          @plugins[url] = [name, url, path]
        end

        @plugins
      end

      def check_url(url)
        unless is_github_url?(url)
          say "URL must be a valid github url"
          exit(1)
        end
      end

      def find_plugin(url)
        load_plugins[url]
      end

      def is_github_url?(url)
        !!url.match(/^https?\:\/\/github\.com\/[a-z0-9\-\._]+\/[a-z0-9\-\._]+$/)<commit_msg>Add "https://github.com" automatically if url is in format "<user>/<project>"<commit_after>module Pvim
  class Plugin < Thor
    include Thor::Actions

    class_option  :pvim,
                  :aliases => 'p',
                  :default => File.expand_path(Dir.pwd),
                  :desc    => 'Your current pvim director'

    option :sort, :aliases => '-s', :type => :boolean, :default => false, :desc => 'Sort installed plugins by name'
    desc 'list', 'List all installed vim plugins'
    def list
      inside pvim do
        plugins = load_plugins
        plugins = plugins.values
        if plugins.any?
          plugins.sort_by!{ |(name, _, _)| name.downcase } if options[:sort]
          say "Installed plugins:", Color::GREEN
          say
          plugins.unshift [ 'Name', 'Repository URL', 'Installed at' ]
          print_table plugins
          say
          say "Total: #{plugins.size - 1}"
        else
          say "No installed plugins."
        end
      end
    end

    desc 'add URL', 'Add vim plugins'
    def add(url)
      url = add_github_prefix(url)
      check_url(url)
      inside pvim do
        empty_directory bundle_dir
        run "git submodule add #{url.inspect} #{installed_dir(url).inspect}"
      end
    end

    desc 'remove URL', 'Remove vim plugins'
    def remove(url)
      url = add_github_prefix(url)
      check_url(url)
      if plugin = find_plugin(url)
        inside pvim do
          empty_directory bundle_dir
          path = plugin.last
          submodule = ['submodule', path].join('.')
          run "git submodule deinit -f #{path.inspect}"
          run "git rm -rf #{path.inspect}"
          run "git config -f .gitmodules --remove-section #{submodule.inspect}"
          remove_file File.join('.git', 'modules', path)
        end
      else
        say "#{url} is not installed", Color::RED
      end
    end

    desc 'update', 'Update all vim plugins to latest versions'
    def update
      inside pvim do
        empty_directory bundle_dir
        run "git submodule update"
      end
    end

    private
      def pvim
        @pvim ||= File.expand_path(options[:pvim] || Dir.pwd)
      end

      def bundle_dir
        @bundle_dir ||= File.join(pvim, 'bundle')
      end

      def installed_dir(url)
        repo = url.gsub(/^https?\:\/\/github\.com\//, '') # <user>/<project>
        File.join(bundle_dir.gsub(pvim+'/', ''), repo.gsub('/', '-'))
      end

      def gitmodules
        @gitmodules ||= File.join(pvim, '.gitmodules')
      end

      def git_config
        @git_config ||= File.join(pvim, '.git', 'config')
      end

      def load_plugins
        return @plugins if @plugins

        unless File.exists?(gitmodules)
          say ".gitmodules does not exist.", Color::RED
          exit(1)
        end

        @plugins = {}
        lines = File.readlines(gitmodules)
        lines.map!(&:chomp)
        lines.delete_if(&:empty?)
        lines.each_slice(3) do |(_, path, url)|
          path = value(path)
          url  = value(url)
          name = repo_name(url)
          @plugins[url] = [name, url, path]
        end

        @plugins
      end

      def add_github_prefix(url)
        return "https://github.com/#{url}" unless url.match(/^https?\:\/\/github\.com/)
      end

      def check_url(url)
        unless is_github_url?(url)
          say "URL must be a valid github url"
          exit(1)
        end
      end

      def find_plugin(url)
        load_plugins[url]
      end

      def is_github_url?(url)
        !!url.match(/^https?\:\/\/github\.com\/[a-z0-9\-\._]+\/[a-z0-9\-\._]+$/)
{'fraction_non_alphanumeric': 0.1269064748201439, 'fraction_numerical': 0.002302158273381295, 'mean_word_length': 1.829059829059829, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '1955600', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2104, 'n_words': 564}