Class: RackConsole::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_console/source_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_location) ⇒ SourceFile

Returns a new instance of SourceFile.



4
5
6
# File 'lib/rack_console/source_file.rb', line 4

def initialize source_location
  @file, @line = Array(source_location)
end

Instance Attribute Details

#block_begObject (readonly)

Returns the value of attribute block_beg.



9
10
11
# File 'lib/rack_console/source_file.rb', line 9

def block_beg
  @block_beg
end

#block_endObject (readonly)

Returns the value of attribute block_end.



9
10
11
# File 'lib/rack_console/source_file.rb', line 9

def block_end
  @block_end
end

#block_indentObject (readonly)

Returns the value of attribute block_indent.



9
10
11
# File 'lib/rack_console/source_file.rb', line 9

def block_indent
  @block_indent
end

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/rack_console/source_file.rb', line 3

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



3
4
5
# File 'lib/rack_console/source_file.rb', line 3

def line
  @line
end

#linesObject

Returns the value of attribute lines.



8
9
10
# File 'lib/rack_console/source_file.rb', line 8

def lines
  @lines
end

Instance Method Details

#highlight_block!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rack_console/source_file.rb', line 32

def highlight_block!
  block_indent = last_line = nil
  @lines.each do | l |
    indent = (l[:str] =~ /^(\s*)\S/ && $1) || ''
    add_classes = block_finished = nil
    case
    when l[:line] == @line
      @block_beg = l
      add_classes = [ :block, :block_begin ]
      @block_indent = indent
      block_indent = indent.size
    when block_indent && (indent.size  > block_indent || l[:str] =~ /^(\s*)$/)
      add_classes = [ :block, :block_body ]
    when block_indent && (indent.size  < block_indent)
      add_classes = nil
      block_finished = true
    when block_indent &&  indent.size == block_indent
      if l[:str] =~ /^(\s*)(ensure|rescue)\b/
        add_classes = [ :block, :block_body ]
      else
        @block_end = l
        add_classes = [ :block, :block_end ]
        block_indent = nil
        block_finished = true
      end
      add_classes = [ :block, :block_body ]
    end
    if add_classes
      l[:class].concat(add_classes).delete(:unselected_line)
    end
    break if block_finished
  end
  self
end

#load!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack_console/source_file.rb', line 11

def load!
  @lines ||= File.read(@file).split("\n")

  line = 0
  @lines.map! do | l |
    line += 1
    c = [ :source_line ]
    case @line
    when nil
      c << :normal_line
    when line
      c << :selected_line
    else
      c << :unselected_line
    end
    { index: line - 1, line: line, str: l, class: c }
  end
  highlight_block! if @line
  self
end

#narrow_to_block!(context_lines = 2) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rack_console/source_file.rb', line 67

def narrow_to_block! context_lines = 2
  if @block_beg
    lineno = @block_beg[:line] - context_lines
    @lines = @lines.select{|l| l[:line] >= lineno }
  end
  if @block_end
    lineno = @block_end[:line] + context_lines
    @lines = @lines.select{|l| l[:line] <= lineno }
  end
  self
end