Class: CodeArea

Inherits:
Object
  • Object
show all
Includes:
Glimmer::LibUI::CustomControl
Defined in:
lib/glimmer/libui/custom_control/code_area.rb

Constant Summary collapse

REGEX_COLOR_HEX6 =
/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/

Instance Attribute Summary

Attributes included from Glimmer::LibUI::CustomControl

#args, #body_root, #content, #keyword, #libui, #options, #parent, #parent_proxy

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Glimmer::LibUI::CustomControl

add_custom_control_namespaces_for, after_body, before_body, body, #can_handle_listener?, custom_control_namespaces, def_option_attr_accessors, flyweight_custom_control_classes, for, #handle_listener, #has_instance_method?, #initialize, keyword, #method_missing, namespaces_for_class, #observer_registrations, option, options, #post_add_content, #post_initialize_child, reset_custom_control_namespaces, #respond_to?, shortcut_keyword

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::LibUI::CustomControl

Class Method Details

.languagesObject



3
4
5
6
# File 'lib/glimmer/libui/custom_control/code_area.rb', line 3

def languages
  require 'rouge'
  Rouge::Lexer.all.map {|lexer| lexer.tag}.sort
end

.lexersObject



8
9
10
11
# File 'lib/glimmer/libui/custom_control/code_area.rb', line 8

def lexers
  require 'rouge'
  Rouge::Lexer.all.sort_by(&:title)
end

Instance Method Details

#lexerObject



42
43
44
45
46
47
48
# File 'lib/glimmer/libui/custom_control/code_area.rb', line 42

def lexer
  require 'rouge'
  require 'glimmer-dsl-libui/ext/rouge/theme/glimmer'
  # 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(language)
  @lexer ||= Rouge::Lexer.find_fancy('ruby') # default to Ruby if no lexer is found
end

#syntax_highlighting(text) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/glimmer/libui/custom_control/code_area.rb', line 50

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