Class: TkInspect::ClassBrowser::CodeComponent
- Inherits:
-
TkComponent::Base
- Object
- TkComponent::Base
- TkInspect::ClassBrowser::CodeComponent
- Defined in:
- lib/tk_inspect/class_browser/code_component.rb
Constant Summary collapse
- @@lexers =
{}
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#method_line ⇒ Object
Returns the value of attribute method_line.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
Instance Method Summary collapse
- #component_did_build ⇒ Object
- #highlight_code(native_item, code, filename) ⇒ Object
- #render(p, parent_component) ⇒ Object
- #update ⇒ Object
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
31 32 33 |
# File 'lib/tk_inspect/class_browser/code_component.rb', line 31 def code @code end |
#filename ⇒ Object
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_line ⇒ Object
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_name ⇒ Object
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_build ⇒ Object
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 |
#update ⇒ Object
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 |