Module: SimpleCov::Formatter::AIFormatter::MarkdownBuilder::SnippetFormatter
- Extended by:
- T::Sig
- Defined in:
- lib/simplecov-ai/markdown_builder/snippet_formatter.rb
Overview
Handles extraction and formatting of source code snippets for the markdown digest.
Constant Summary collapse
- ESTIMATED_CHARS_PER_LINE =
Approximate maximum characters per line for truncation calculation
T.let(80, Integer)
- TRUNCATION_ELLIPSIS =
Suffix added to truncated snippets
T.let('...', String)
- OCCURRENCE_TEMPLATE =
Template for identical snippet occurrences indicator
T.let('(Occurrence %d of %d).', String)
Instance Method Summary collapse
- #calculate_occurrence(line_num, source_lines, node) ⇒ Object
- #count_snippet_occurrences(snippet, target_line_number, source_lines, node) ⇒ Object
- #fetch_snippet_text(line_nums, source_lines) ⇒ Object
- #truncate_snippet(snippet_text, max_snippet_lines) ⇒ Object
Instance Method Details
#calculate_occurrence(line_num, source_lines, node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 54 def calculate_occurrence(line_num, source_lines, node) return '' if node.nil? first_line_of_snippet = source_lines[line_num - 1]&.strip return '' if first_line_of_snippet.nil? || first_line_of_snippet.empty? occurrences, current = count_snippet_occurrences(first_line_of_snippet, line_num, source_lines, node) occurrences > 1 ? Kernel.format(OCCURRENCE_TEMPLATE, current, occurrences) : '' end |
#count_snippet_occurrences(snippet, target_line_number, source_lines, node) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 69 def count_snippet_occurrences(snippet, target_line_number, source_lines, node) occurrences = 0 current_occurrence = 1 (node.start_line..node.end_line).each do |line_number| line_content = source_lines[line_number - 1]&.strip next unless line_content == snippet occurrences += 1 current_occurrence = occurrences if line_number == target_line_number end [occurrences, current_occurrence] end |
#fetch_snippet_text(line_nums, source_lines) ⇒ Object
25 26 27 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 25 def fetch_snippet_text(line_nums, source_lines) line_nums.filter_map { |line_number| source_lines[line_number - 1]&.strip }.reject(&:empty?).join(' ') end |
#truncate_snippet(snippet_text, max_snippet_lines) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 35 def truncate_snippet(snippet_text, max_snippet_lines) max_chars = max_snippet_lines * ESTIMATED_CHARS_PER_LINE if snippet_text.length > max_chars "#{snippet_text[0...max_chars]}#{TRUNCATION_ELLIPSIS}" else snippet_text end end |