Sha256: 00c0823137b37b633c0a732c02c74e43a5f0bf8d1ea160f85c86f1d2c5f0b155

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

module Redcar
  class AutoPairer
    module PairsForScope
      def self.autopair_rules
        @autopair_rules ||= begin
          rules = Hash.new {|h, k| h[k] = {}}
          @autopair_default = nil
          start = Time.now
          pair_settings = Textmate.all_settings.select do |setting|
            setting.is_a?(Textmate::SmartTypingPairsSetting)
          end
          pair_settings.each do |setting|
            if setting.scope
              rules[setting.scope] = Hash[*setting.pairs.flatten]
            else
              @autopair_default = Hash[*setting.pairs.flatten]
            end
          end
          if @autopair_default
            @autopair_default1 = @autopair_default.invert
          end
          puts "loaded autopair rules in #{Time.now - start}s"
          rules.default = nil
          rules
        end
      end
      
      def self.autopair_default
        @autopair_default ||= begin
          autopair_rules
          @autopair_default
        end
      end

      def self.cache
        @cache ||= {}
      end

      def self.pairs_for_scope(current_scope)
        cache[current_scope] ||= begin
          rules = nil
          if current_scope
            matches = autopair_rules.map do |scope_name, value|
              if match = JavaMateView::ScopeMatcher.get_match(scope_name, current_scope)
                [scope_name, match, value]
              end
            end.compact
            best_match = matches.sort do |a, b|
              JavaMateView::ScopeMatcher.compare_match(current_scope, a[1], b[1])
            end.last
            if best_match
              rules = best_match[2]
            end
          end
          rules = autopair_default unless rules
          rules
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redcar-0.3.4.3 plugins/auto_pairer/lib/auto_pairer/pairs_for_scope.rb
redcar-0.3.4.2 plugins/auto_pairer/lib/auto_pairer/pairs_for_scope.rb
redcar-0.3.4.1 plugins/auto_pairer/lib/auto_pairer/pairs_for_scope.rb
redcar-0.3.4 plugins/auto_pairer/lib/auto_pairer/pairs_for_scope.rb