Class: RubyReactor::Utils::CodeExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_reactor/utils/code_extractor.rb

Class Method Summary collapse

Class Method Details

.extract(file_path, line_number, radius: 2) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_reactor/utils/code_extractor.rb', line 6

def self.extract(file_path, line_number, radius: 2)
  return nil unless file_path && line_number && File.exist?(file_path)

  lines = File.readlines(file_path)
  total_lines = lines.size
  target_index = line_number - 1

  return nil if target_index.negative? || target_index >= total_lines

  start_index = [0, target_index - radius].max
  end_index = [total_lines - 1, target_index + radius].min

  (start_index..end_index).map do |i|
    {
      line_number: i + 1,
      content: lines[i].chomp,
      target: i == target_index
    }
  end
rescue StandardError
  # Fail gracefully if file reading fails
  nil
end