Class: DEBUGGER__::SourceRepository

Inherits:
Object
  • Object
show all
Includes:
Color
Defined in:
lib/debug/source_repository.rb

Defined Under Namespace

Classes: SrcInfo

Instance Method Summary collapse

Methods included from Color

#color_pp, #colored_inspect, #colorize, #colorize_blue, #colorize_code, #colorize_cyan, #colorize_dim, #colorize_magenta, #irb_colorize, #with_inspection_error_guard

Constructor Details

#initializeSourceRepository

Returns a new instance of SourceRepository.



30
31
32
33
34
# File 'lib/debug/source_repository.rb', line 30

def initialize
  # cache
  @cmap = ObjectSpace::WeakMap.new
  @loaded_file_map = {} # path => nil
end

Instance Method Details

#add(iseq, src) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/debug/source_repository.rb', line 36

def add iseq, src
  # only manage loaded file names
  if (path = (iseq.absolute_path || iseq.path)) && File.exist?(path)
    if @loaded_file_map.has_key? path
      return path, true # reloaded
    else
      @loaded_file_map[path] = path
      return path, false
    end
  end
end

#file_src(iseq) ⇒ Object



9
10
11
12
13
# File 'lib/debug/source_repository.rb', line 9

def file_src iseq
  if (path = (iseq.absolute_path || iseq.path)) && File.exist?(path)
    File.readlines(path, chomp: true)
  end
end

#get(iseq) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/debug/source_repository.rb', line 15

def get iseq
  return unless iseq

  if CONFIG[:show_evaledsrc]
    orig_src(iseq) || file_src(iseq)
  else
    file_src(iseq) || orig_src(iseq)
  end
end

#get_colored(iseq) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/debug/source_repository.rb', line 58

def get_colored iseq
  if lines = @cmap[iseq]
    lines
  else
    if src_lines = get(iseq)
      @cmap[iseq] = colorize_code(src_lines.join("\n")).lines
    else
      nil
    end
  end
end

#orig_src(iseq) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/debug/source_repository.rb', line 48

def orig_src iseq
  lines = iseq.script_lines&.map(&:chomp)
  line = iseq.first_line
  if line > 1
    [*([''] * (line - 1)), *lines]
  else
    lines
  end
end