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.
Instance Method Summary collapse
- #calculate_occurrence(line_num, source_lines, node) ⇒ Object
- #count_snippet_occurrences(snippet, target_ln, source_lines, node) ⇒ Object
- #fetch_snippet_text(line_nums, source_lines) ⇒ Object
- #truncate_snippet(text, max_snippet_lines) ⇒ Object
Instance Method Details
#calculate_occurrence(line_num, source_lines, node) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 43 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 ? "(Occurrence #{current} of #{occurrences}) " : '' end |
#count_snippet_occurrences(snippet, target_ln, source_lines, node) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 58 def count_snippet_occurrences(snippet, target_ln, source_lines, node) occurrences = 0 current_occurrence = 1 (node.start_line..node.end_line).each do |ln| line_content = source_lines[ln - 1]&.strip next unless line_content == snippet occurrences += 1 current_occurrence = occurrences if ln == target_ln end [occurrences, current_occurrence] end |
#fetch_snippet_text(line_nums, source_lines) ⇒ Object
18 19 20 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 18 def fetch_snippet_text(line_nums, source_lines) line_nums.filter_map { |ln| source_lines[ln - 1]&.strip }.reject(&:empty?).join(' ') end |
#truncate_snippet(text, max_snippet_lines) ⇒ Object
28 29 30 31 |
# File 'lib/simplecov-ai/markdown_builder/snippet_formatter.rb', line 28 def truncate_snippet(text, max_snippet_lines) max_chars = max_snippet_lines * 80 text.length > max_chars ? "#{text[0...max_chars]}..." : text end |