Class: TkInspect::ClassBrowser::CodeComponent

Inherits:
TkComponent::Base
  • Object
show all
Defined in:
lib/tk_inspect/class_browser/code_component.rb

Constant Summary collapse

@@lexers =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code.



31
32
33
# File 'lib/tk_inspect/class_browser/code_component.rb', line 31

def code
  @code
end

#filenameObject

Returns the value of attribute filename.



32
33
34
# File 'lib/tk_inspect/class_browser/code_component.rb', line 32

def filename
  @filename
end

#method_lineObject

Returns the value of attribute method_line.



34
35
36
# File 'lib/tk_inspect/class_browser/code_component.rb', line 34

def method_line
  @method_line
end

#method_nameObject

Returns the value of attribute method_name.



33
34
35
# File 'lib/tk_inspect/class_browser/code_component.rb', line 33

def method_name
  @method_name
end

Instance Method Details

#component_did_buildObject



43
44
45
46
47
# File 'lib/tk_inspect/class_browser/code_component.rb', line 43

def component_did_build
  TAG_STYLES.each do |k,v|
    @code_text.native_item.tag_configure(k.to_s, v)
  end
end

#highlight_code(native_item, code, filename) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/tk_inspect/class_browser/code_component.rb', line 63

def highlight_code(native_item, code, filename)
  lexer = (@@lexers[filename] ||= Rouge::Lexers::Ruby.lex(code))
  native_item.replace('1.0', 'end', '')
  lexer.each do |token, chunk|
    tag = TOKEN_TAGS[token.to_s]
    native_item.insert('end', chunk, tag.to_s)
  end
end

#render(p, parent_component) ⇒ Object



36
37
38
39
40
41
# File 'lib/tk_inspect/class_browser/code_component.rb', line 36

def render(p, parent_component)
  p.vframe(padding: "0 0 0 0", sticky: 'nsew', x_flex: 1, y_flex: 1) do |vf|
    @filename_label = vf.label(font: 'TkSmallCaptionFont', sticky: 'ewn', x_flex: 1, y_flex: 0)
    @code_text = vf.text(sticky: 'nswe', x_flex: 1, y_flex: 1, wrap: 'none', scrollers: 'xy')
  end
end

#updateObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tk_inspect/class_browser/code_component.rb', line 49

def update
  if @code && @method_line
    highlight_code(@code_text.native_item, @code, @filename)
    @code_text.tk_item.select_range("#{@method_line}.0", "#{@method_line}.end")
    @code_text.native_item.see("#{@method_line}.0")
    @filename_label.native_item.text(@filename)
  else
    @filename_label.native_item.text('')
    @code_text.tk_item.value = "Source code not available for #{@method_name}"
  end
end