<commit_before>module Listen
  module Adapter
    # Adapter implementation for Windows `wdm`.
    #
    class Windows < Base
      OS_REGEXP = /mswin|mingw|cygwin/i

      BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
        Please add the following to your Gemfile to avoid polling for changes:
          require 'rbconfig'
          if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
            gem 'wdm', '>= 0.1.0'
          end
      EOS

      def self.usable?
        return false unless super
        require 'wdm'
        true
      rescue LoadError
        _log :debug, "wdm - load failed: #{$!}:#{$@.join("\n")}"
        Kernel.warn BUNDLER_DECLARE_GEM
        false
      end

      private

      def _configure
        _log :debug, 'wdm - starting...'
        @worker = WDM::Monitor.new
        _directories.each do |path|
          @worker.watch_recursively(path.to_s, :files, &_file_callback)
          @worker.watch_recursively(path.to_s, :directories, &_dir_callback)
          @worker.watch_recursively(path.to_s, :attributes, :last_write,
                                    &_attr_callback)
        end
      end

      def _run
        @worker.run!
      end

      def _file_callback
        lambda do |change|
          begin
            path = _path(change.path)
            _log :debug, "wdm - FILE callback: #{change.inspect}"
            options = { change: _change(change.type) }
            _notify_change(:file, path, options)
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _attr_callback
        lambda do |change|
          begin
            path = _path(change.path)
            return if path.directory?

            _log :debug, "wdm - ATTR callback: #{change.inspect}"
            options = { change: _change(change.type) }
            _notify_change(:file, _path(change.path), options)
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _dir_callback
        lambda do |change|
          begin
            path = _path(change.path)
            _log :debug, "wdm - DIR callback: #{change.inspect}"
            if change.type == :removed
              _notify_change(:dir, path.dirname)
            elsif change.type == :added
              _notify_change(:dir, path)
            else
              # do nothing - changed directory means either:
              #   - removed subdirs (handled above)
              #   - added subdirs (handled above)
              #   - removed files (handled by _file_callback)
              #   - added files (handled by _file_callback)
              # so what's left?
            end
          rescue
            _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
            raise
          end
        end
      end

      def _path(path)
        Pathname.new(path)
      end

      def _change(type)
        { modified: [:modified, :attrib], # TODO: is attrib really passed?
          added:    [:added, :renamed_new_file],
          removed:  [:removed, :renamed_old_file] }.each do |change, types|<commit_msg>update WDM adapter to use dir separation<commit_after>module Listen
  module Adapter
    # Adapter implementation for Windows `wdm`.
    #
    class Windows < Base
      OS_REGEXP = /mswin|mingw|cygwin/i

      BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
        Please add the following to your Gemfile to avoid polling for changes:
          require 'rbconfig'
          if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
            gem 'wdm', '>= 0.1.0'
          end
      EOS

      def self.usable?
        return false unless super
        require 'wdm'
        true
      rescue LoadError
        _log :debug, "wdm - load failed: #{$!}:#{$@.join("\n")}"
        Kernel.warn BUNDLER_DECLARE_GEM
        false
      end

      private

      def _configure(dir, &callback)
        require 'wdm'
        _log :debug, 'wdm - starting...'
        @worker ||= WDM::Monitor.new
        @worker.watch_recursively(dir.to_s, :files) do |change|
          callback.call([:file, change])
        end

        @worker.watch_recursively(dir.to_s, :directories) do |change|
          callback.call([:dir, change])
        end

        events = [:attributes, :last_write]
        @worker.watch_recursively(dir.to_s, *events) do |change|
          callback.call([:attr, change])
        end
      end

      def _run
        @worker.run!
      end

      def _process_event(directory, event, new_changes)
        _log :debug, "wdm - callback: #{event.inspect}"

        type, change = event

        rel_path = Pathname(change.path).relative_from(directory)

        options = { change: _change(change.type) }

        case type
        when :file
          new_changes << [:file, rel_path, options]
        when :attr
          unless path.directory?
            new_changes << [:file, rel_path, options]
          end
        when :dir
          if change.type == :removed
            new_changes << [:dir, rel_path.dirname]
          elsif change.type == :added
            new_changes << [:dir, rel_path]
          else
            # do nothing - changed directory means either:
            #   - removed subdirs (handled above)
            #   - added subdirs (handled above)
            #   - removed files (handled by _file_callback)
            #   - added files (handled by _file_callback)
            # so what's left?
          end
        end
      rescue
        details = event.inspect
        _log :error, "wdm - callback (#{details}): #{$!}:#{$@.join("\n")}"
        raise
      end

      def _change(type)
        { modified: [:modified, :attrib], # TODO: is attrib really passed?
          added:    [:added, :renamed_new_file],
          removed:  [:removed, :renamed_old_file] }.each do |change, types|
{'fraction_non_alphanumeric': 0.11142905234808954, 'fraction_numerical': 0.0013465746507322, 'mean_word_length': 1.70953032375741, 'pattern_counts': {'":': 0, '<': 17, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1336553', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1757, 'n_words': 456}