class FastIgnore::GitignoreRuleBuilder

Public Class Methods

new(rule, file_path) click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 5
def initialize(rule, file_path)
  @re = ::FastIgnore::GitignoreRuleRegexpBuilder.new
  @s = ::FastIgnore::GitignoreRuleScanner.new(rule)

  @file_path = file_path
  @negation = false
  @anchored = false
  @dir_only = false
end

Public Instance Methods

anchored!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 31
def anchored!
  @anchored ||= true
end
anchored_or_file_path() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 166
def anchored_or_file_path
  @anchored || @file_path
end
blank!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 19
def blank!
  throw :abort_build, []
end
break!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 15
def break!
  throw :break
end
build() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 170
def build
  catch :abort_build do
    blank! if @s.hash?
    negated! if @s.exclamation_mark?
    process_rule

    @anchored = false if @anchored == :never

    build_rule
  end
end
build_rule() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 161
def build_rule
  @re.prepend(prefix)
  ::FastIgnore::Rule.new(@re.to_regexp, @negation, anchored_or_file_path, @dir_only)
end
dir_only!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 39
def dir_only!
  @dir_only = true
end
emit_dir() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 47
def emit_dir
  anchored!
  @re.append_dir
end
emit_end() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 52
def emit_end
  @re.append_end_anchor
  break!
end
negated!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 27
def negated!
  @negation = true
end
never_anchored!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 35
def never_anchored!
  @anchored = :never
end
nothing_emitted?() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 43
def nothing_emitted?
  @re.empty?
end
prefix() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 146
def prefix # rubocop:disable Metrics/MethodLength
  out = ::FastIgnore::GitignoreRuleRegexpBuilder.new
  if @file_path
    out.append_start_anchor.append(@file_path.escaped)
    out.append_any_dir unless @anchored
  else
    if @anchored
      out.append_start_anchor
    else
      out.append_start_dir_or_anchor
    end
  end
  out
end
process_backslash() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 57
def process_backslash
  return unless @s.backslash?

  @re.append_escaped(@s.next_character) || unmatchable_rule!
end
process_character_class() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 103
def process_character_class # rubocop:disable Metrics/MethodLength
  return unless @s.character_class_start?

  @re.append_character_class_open
  @re.append_character_class_negation if @s.character_class_negation?
  unmatchable_rule! if @s.character_class_end?

  until @s.character_class_end?
    next if process_backslash
    next @re.append_character_class_dash if @s.dash?
    next if @re.append_escaped(@s.character_class_literal)

    unmatchable_rule!
  end

  @re.append_character_class_close
end
process_end() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 121
def process_end
  blank! if nothing_emitted?

  emit_end
end
process_rule() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 127
def process_rule # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  anchored! if @s.slash?

  catch :break do
    loop do
      next if process_backslash
      next if process_slash
      next if process_two_stars
      next @re.append_any_non_dir if @s.star?
      next @re.append_one_non_dir if @s.question_mark?
      next if process_character_class
      next if @re.append_escaped(@s.literal)
      next if @re.append_escaped(@s.significant_whitespace)

      process_end
    end
  end
end
process_slash() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 70
def process_slash
  return unless @s.slash?
  return dir_only! if @s.end?
  return unmatchable_rule! if @s.slash?

  emit_dir
  process_star_end_after_slash
end
process_star_end_after_slash() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 63
def process_star_end_after_slash
  return true unless @s.star_end?

  @re.append_many_non_dir
  emit_end
end
process_two_stars() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 79
def process_two_stars # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  return unless @s.two_stars?
  return break! if @s.end?

  if @s.slash?
    if @s.end?
      @re.append_any_non_dir
      dir_only!
    elsif @s.slash?
      unmatchable_rule!
    else
      if nothing_emitted?
        never_anchored!
      else
        @re.append_any_dir
        anchored!
      end
      process_star_end_after_slash
    end
  else
    @re.append_any_non_dir
  end
end
unmatchable_rule!() click to toggle source
# File lib/fast_ignore/gitignore_rule_builder.rb, line 23
def unmatchable_rule!
  throw :abort_build, []
end