Class: Glimmer::SWT::Custom::CodeText

Inherits:
Object
  • Object
show all
Includes:
UI::CustomWidget
Defined in:
lib/glimmer/swt/custom/code_text.rb

Overview

CodeText is a customization of StyledText with support for Ruby Syntax Highlighting

Constant Summary collapse

SYNTAX_COLOR_MAP =
{
   Builtin:     [215,58,73],
   Class:       [3,47,98],
   Constant:    [0,92,197],
   Double:      [0,92,197],
   Escape:      [:red],
   Function:    [:blue],
   Instance:    [227,98,9],
   Integer:     [:blue],
   Interpol:    [:blue],
   Keyword:     [:blue],
   Name:        [111,66,193], #purple
   Operator:    [:red],
   Pseudo:      [:dark_red],
   Punctuation: [:blue],
   Single:      [106,115,125], # Also, Comments
   Symbol:      [:dark_green],
   Text:        [75, 75, 75],
}

Instance Attribute Summary

Attributes included from UI::CustomWidget

#body_root, #options, #parent, #swt_style, #swt_widget

Instance Method Summary collapse

Methods included from UI::CustomWidget

add_custom_widget_namespaces_for, #add_observer, after_body, #async_exec, #attribute_setter, before_body, body, #can_add_observer?, #can_handle_observation_request?, #content, custom_widget_namespaces, def_option_attr_accessors, for, #get_attribute, #handle_observation_request, #has_attribute?, #has_instance_method?, #has_style?, #initialize, #local_respond_to?, #method_missing, namespaces_for_class, option, options, #post_initialize_child, reset_custom_widget_namespaces, #respond_to?, #set_attribute, #sync_exec

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::UI::CustomWidget

Instance Method Details

#lexerObject



57
58
59
60
# File 'lib/glimmer/swt/custom/code_text.rb', line 57

def lexer
  # TODO Try to use Rouge::Lexer.find_fancy('guess', code) in the future to guess the language or otherwise detect it from file extension
  @lexer ||= Rouge::Lexer.find_fancy('ruby')
end

#syntax_highlighting(text) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/glimmer/swt/custom/code_text.rb', line 41

def syntax_highlighting(text)
  return [] if text.to_s.strip.empty?
  @syntax_highlighting ||= {}
  unless @syntax_highlighting.keys.include?(text)
    lex = lexer.lex(text).to_a
    text_size = 0
    @syntax_highlighting[text] = lex.map do |pair|
      {token_type: pair.first, token_text: pair.last}
    end.each do |hash|
      hash[:token_index] = text_size
      text_size += hash[:token_text].size
    end
  end
  @syntax_highlighting[text]
end

#textObject



37
38
39
# File 'lib/glimmer/swt/custom/code_text.rb', line 37

def text
  swt_widget&.text
end

#text=(value) ⇒ Object

TODO support ‘option :language` TODO support auto language detection



33
34
35
# File 'lib/glimmer/swt/custom/code_text.rb', line 33

def text=(value)
  swt_widget&.text = value
end