Class: Ace::Docs::Atoms::DiffFilterer

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/docs/atoms/diff_filterer.rb

Overview

Pure functions for filtering diff content

Constant Summary collapse

DEFAULT_EXCLUDE_PATTERNS =

Default patterns to exclude from diffs

[
  %r{^test/},
  %r{^spec/},
  %r{\.test\.},
  %r{\.spec\.},
  %r{^coverage/},
  %r{^tmp/},
  %r{^vendor/},
  %r{^node_modules/},
  %r{^\.git/},
  %r{Gemfile\.lock$},
  %r{package-lock\.json$},
  %r{yarn\.lock$}
].freeze

Class Method Summary collapse

Class Method Details

.count_changes(diff) ⇒ Hash

Count significant changes (additions and deletions)

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Hash)

    Statistics about the diff



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ace/docs/atoms/diff_filterer.rb', line 66

def count_changes(diff)
  return {additions: 0, deletions: 0, files: 0} if diff.nil? || diff.empty?

  additions = 0
  deletions = 0
  files = 0

  diff.split("\n").each do |line|
    if file_header?(line)
      files += 1
    elsif line.start_with?("+") && !line.start_with?("+++")
      additions += 1
    elsif line.start_with?("-") && !line.start_with?("---")
      deletions += 1
    end
  end

  {
    additions: additions,
    deletions: deletions,
    files: files,
    total_changes: additions + deletions
  }
end

.estimate_size(diff) ⇒ Integer

Estimate the size of a diff in lines

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Integer)

    Number of lines



57
58
59
60
61
# File 'lib/ace/docs/atoms/diff_filterer.rb', line 57

def estimate_size(diff)
  return 0 if diff.nil? || diff.empty?

  diff.count("\n") + 1
end

.exceeds_limit?(diff, max_lines = 100_000) ⇒ Boolean

Check if diff is too large for processing

Parameters:

  • diff (String)

    The diff content

  • max_lines (Integer) (defaults to: 100_000)

    Maximum allowed lines

Returns:

  • (Boolean)

    True if diff exceeds limit



95
96
97
# File 'lib/ace/docs/atoms/diff_filterer.rb', line 95

def exceeds_limit?(diff, max_lines = 100_000)
  estimate_size(diff) > max_lines
end

.filter_paths(diff, exclude_patterns = DEFAULT_EXCLUDE_PATTERNS) ⇒ String

Filter paths from diff output

Parameters:

  • diff (String)

    The diff content

  • exclude_patterns (Array<Regexp>) (defaults to: DEFAULT_EXCLUDE_PATTERNS)

    Patterns to exclude

Returns:

  • (String)

    Filtered diff content



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ace/docs/atoms/diff_filterer.rb', line 29

def filter_paths(diff, exclude_patterns = DEFAULT_EXCLUDE_PATTERNS)
  return "" if diff.nil? || diff.empty?

  lines = diff.split("\n")
  filtered_lines = []
  skip_until_next_file = false

  lines.each do |line|
    # Check if this is a file header
    if file_header?(line)
      file_path = extract_file_path(line)
      if should_exclude?(file_path, exclude_patterns)
        skip_until_next_file = true
      else
        skip_until_next_file = false
        filtered_lines << line
      end
    elsif !skip_until_next_file
      filtered_lines << line
    end
  end

  filtered_lines.join("\n")
end