Class: CodeBuddy::StackFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/code_buddy/stack_frame.rb

Constant Summary collapse

CODE_WINDOW =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception_string) ⇒ StackFrame

Returns a new instance of StackFrame.



9
10
11
12
13
14
15
16
17
18
# File 'lib/code_buddy/stack_frame.rb', line 9

def initialize(exception_string)
  if exception_string =~ /\s*([^:\s]+):([0-9]+)\s*/
    @path = $1
    @line = $2.to_i
  else
    @path = exception_string
    @line = 0
  end
  code
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



7
8
9
# File 'lib/code_buddy/stack_frame.rb', line 7

def line
  @line
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/code_buddy/stack_frame.rb', line 6

def path
  @path
end

Instance Method Details

#codeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/code_buddy/stack_frame.rb', line 20

def code
  @code ||= begin
    html_args = { :line_numbers => :inline, :wrap => :span }
    lines_of_code = File.new(path).readlines

    first_line_to_show      = [1, line-CODE_WINDOW].max
    last_line_to_show       = [lines_of_code.length, line + 1 + CODE_WINDOW].min
    code_to_show            = lines_of_code[first_line_to_show-1 .. last_line_to_show-1]
    formatted_lines = CodeRay.scan(code_to_show.join,   :ruby).
                                   html(:line_numbers      => :inline,
                                        :wrap              => :span,
                                        :bold_every        => false,
                                        :line_number_start => first_line_to_show)
    
    highlighted_line = line - first_line_to_show + 1
    formatted_lines_array = formatted_lines.split("\n")
    formatted_lines_array[highlighted_line-1] = "<span class='container selected'>#{formatted_lines_array[highlighted_line-1]}<span class='overlay'></span></span>"
    formatted_lines_array.join("\n")
  rescue => exception
    "<span class=\"coderay\">Unable to read file:\n&nbsp;\"#{@path}\"</span>"
  end
end

#open_in_editorObject

different syntaxes for opening to a line number for different editors



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/code_buddy/stack_frame.rb', line 44

def open_in_editor
  editor = ENV['CODE_BUDDY_EDITOR'] || ENV['EDITOR']
  case editor
  when 'mate'
    `mate #{path} -l #{line}`
  when /(mvim|emacsclient)/
    `#{$1} +#{line} #{path}`
  when 'netbeans'
    `netbeans #{path}:#{line}`
  else
    puts "Sorry unable to open the file for editing.  Please set your environment variable to either mate or mvim 'export CODE_BUDDY_EDITOR=mate' and restart the server"
  end
end