Class: Glimmer::SWT::Custom::CodeText
- Inherits:
-
Object
- Object
- Glimmer::SWT::Custom::CodeText
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
- REGEX_COLOR_HEX6 =
/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/
- FONT_NAMES_PREFERRED =
['Consolas', 'Courier', 'Monospace', 'Liberation Mono']
- SHORTCUT_KEY_COMMAND =
OS.mac? ? :command : :ctrl
Instance Attribute Summary collapse
#body_root, #options, #parent, #parent_proxy, #swt_style, #swt_widget
Class Method Summary
collapse
Instance Method Summary
collapse
add_custom_widget_namespaces_for, after_body, #async_exec, #attribute_setter, before_body, body, #children_owner, #content, custom_widget_namespaces, def_option_attr_accessors, #disposed?, flyweight_custom_widget_classes, for, #get_attribute, #has_attribute?, #has_style?, #initialize, keyword, #local_respond_to?, namespaces_for_class, #observer_registrations, option, options, #post_add_content, #post_initialize_child, reset_custom_widget_namespaces, #set_attribute, #shell_proxy, shortcut_keyword, #sync_exec, #timer_exec
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 63
def method_missing(method_name, *args, &block)
dsl_mode = @dsl_mode || args.last.is_a?(Hash) && args.last[:dsl]
if dsl_mode
args.pop if args.last.is_a?(Hash) && args.last[:dsl]
super(method_name, *args, &block)
elsif @styled_text_proxy&.respond_to?(method_name, *args, &block)
@line_numbers_styled_text_proxy&.send(method_name, *args, &block) if method_name.to_s == 'font='
@styled_text_proxy&.send(method_name, *args, &block)
else
super
end
end
|
Instance Attribute Details
#line_numbers_styled_text_proxy ⇒ Object
Returns the value of attribute line_numbers_styled_text_proxy.
61
62
63
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 61
def line_numbers_styled_text_proxy
@line_numbers_styled_text_proxy
end
|
#lines_width ⇒ Object
Returns the value of attribute lines_width.
61
62
63
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 61
def lines_width
@lines_width
end
|
#styled_text_proxy ⇒ Object
Returns the value of attribute styled_text_proxy.
61
62
63
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 61
def styled_text_proxy
@styled_text_proxy
end
|
#styled_text_proxy_text ⇒ Object
Returns the value of attribute styled_text_proxy_text.
60
61
62
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 60
def styled_text_proxy_text
@styled_text_proxy_text
end
|
#styled_text_proxy_top_pixel ⇒ Object
Returns the value of attribute styled_text_proxy_top_pixel.
60
61
62
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 60
def styled_text_proxy_top_pixel
@styled_text_proxy_top_pixel
end
|
Class Method Details
.languages ⇒ Object
32
33
34
35
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 32
def languages
require 'rouge'
Rouge::Lexer.all.map {|lexer| lexer.tag}.sort
end
|
.lexers ⇒ Object
37
38
39
40
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 37
def lexers
require 'rouge'
Rouge::Lexer.all.sort_by(&:title)
end
|
Instance Method Details
#add_observer(observer, attribute_name) ⇒ Object
94
95
96
97
98
99
100
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 94
def add_observer(observer, attribute_name)
if @styled_text_proxy&.can_add_observer?(attribute_name)
@styled_text_proxy.add_observer(observer, attribute_name)
else
super
end
end
|
#bump_font_height_down ⇒ Object
350
351
352
353
354
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 350
def bump_font_height_down
@original_font_height ||= font_datum.height
new_font_height = (font_datum.height - 1) == 0 ? font_datum.height : (font_datum.height - 1)
update_font_height(new_font_height)
end
|
#bump_font_height_up ⇒ Object
344
345
346
347
348
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 344
def bump_font_height_up
@original_font_height ||= font_datum.height
new_font_height = font_datum.height + 1
update_font_height(new_font_height)
end
|
#can_add_observer?(attribute_name) ⇒ Boolean
90
91
92
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 90
def can_add_observer?(attribute_name)
@styled_text_proxy&.can_add_observer?(attribute_name) || super
end
|
#can_handle_observation_request?(observation_request) ⇒ Boolean
102
103
104
105
106
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 102
def can_handle_observation_request?(observation_request)
@styled_text_proxy&.can_handle_observation_request?(observation_request) || super
rescue
super
end
|
#code_text_widget ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 188
def code_text_widget
@styled_text_proxy = styled_text(swt_style) {
layout_data :fill, :fill, true, true if lines
text <=> [self, :styled_text_proxy_text] if lines
top_pixel <=> [self, :styled_text_proxy_top_pixel] if lines
font @font_options
background :black if @dark_mode
foreground rgb(75, 75, 75)
left_margin 5
top_margin 5
right_margin 5
bottom_margin 5
tabs 2
if default_behavior
on_key_pressed { |event|
character = event.keyCode.chr rescue nil
case [event.stateMask, character]
when [swt(SHORTCUT_KEY_COMMAND), 'a']
@styled_text_proxy.selectAll
when [(swt(:ctrl) if OS.mac?), 'a']
jump_to_beginning_of_line
when [(swt(:ctrl) if OS.mac?), 'e']
jump_to_end_of_line
when [swt(SHORTCUT_KEY_COMMAND), '=']
bump_font_height_up
when [swt(SHORTCUT_KEY_COMMAND), '-']
bump_font_height_down
when [swt(SHORTCUT_KEY_COMMAND), '0']
restore_font_height
end
}
on_verify_text { |verify_event|
if verify_event.text == "\n"
line_index = verify_event.widget.get_line_at_offset(verify_event.widget.get_caret_offset)
line = verify_event.widget.get_line(line_index)
line_indent = line.match(/^([ ]*)/)[1].to_s.size
verify_event.text += ' '*line_indent
verify_event.text += ' '*2 if line.strip.end_with?('{') || line.strip.match(/do([ ]*[|][^|]*[|])?$/) || line.start_with?('class') || line.start_with?('module') || line.strip.start_with?('def')
end
}
end
on_modify_text do |event|
new_text = event.data
async_exec do
unless @syntax_highlighting.nil?
lines = new_text.to_s.split("\n")
line_diff = @syntax_highlighting.keys - lines
line_diff.each do |line|
@syntax_highlighting.delete(line)
end
end
end
end
on_line_get_style do |line_style_event|
begin
styles = []
style_data = nil
syntax_highlighting(line_style_event.lineText).to_a.each do |token_hash|
start_index = token_hash[:token_index]
size = token_hash[:token_text].size
style_data = Rouge::Theme.find(theme).new.style_for(token_hash[:token_type])
foreground_color = hex_color_to_swt_color(style_data[:fg], [@dark_mode ? :white : :black])
background_color = hex_color_to_swt_color(style_data[:bg], [@dark_mode ? :black : :white])
font_styles = []
font_styles << :bold if style_data[:bold]
font_styles << :italic if style_data[:italic]
font_style = SWTProxy[*font_styles]
styles << StyleRange.new(line_style_event.lineOffset + start_index, size, foreground_color, background_color, font_style)
end
line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
rescue => e
Glimmer::Config.logger.error {"Error encountered with style data: #{style_data}"}
Glimmer::Config.logger.error {e.message}
Glimmer::Config.logger.error {e.full_message}
end
end
}
end
|
#font_datum ⇒ Object
370
371
372
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 370
def font_datum
@styled_text_proxy.font.font_data.first
end
|
#handle_observation_request(observation_request, &block) ⇒ Object
108
109
110
111
112
113
114
115
116
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 108
def handle_observation_request(observation_request, &block)
if @styled_text_proxy&.can_handle_observation_request?(observation_request)
@styled_text_proxy.handle_observation_request(observation_request, &block)
else
super
end
rescue
super
end
|
#has_instance_method?(method_name) ⇒ Boolean
86
87
88
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 86
def has_instance_method?(method_name)
respond_to?(method_name)
end
|
#hex_color_to_swt_color(color_data, default_color) ⇒ Object
TODO extract this to ColorProxy
297
298
299
300
301
302
303
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 297
def hex_color_to_swt_color(color_data, default_color)
color_data = "##{color_data.chars.drop(1).map {|c| c*2}.join}" if color_data.is_a?(String) && color_data.start_with?('#') && color_data&.size == 4
color_data = color_data.match(REGEX_COLOR_HEX6).to_a.drop(1).map {|c| "0x#{c}".hex}.to_a if color_data.is_a?(String) && color_data.start_with?('#')
color_data = [color_data] unless color_data.nil? || color_data.empty? || color_data.is_a?(Array)
color_data = default_color if color_data.nil? || color_data.empty?
color(*color_data).swt_color
end
|
#jump_to_beginning_of_line ⇒ Object
305
306
307
308
309
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 305
def jump_to_beginning_of_line
current_line_index = @styled_text_proxy.getLineAtOffset(@styled_text_proxy.getCaretOffset)
beginning_of_current_line_offset = @styled_text_proxy.getOffsetAtLine(current_line_index)
@styled_text_proxy.setSelection(beginning_of_current_line_offset, beginning_of_current_line_offset)
end
|
#jump_to_end_of_line ⇒ Object
311
312
313
314
315
316
317
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 311
def jump_to_end_of_line
current_line_index = @styled_text_proxy.getLineAtOffset(@styled_text_proxy.getCaretOffset)
current_line = @styled_text_proxy.getLine(current_line_index)
beginning_of_current_line_offset = @styled_text_proxy.getOffsetAtLine(current_line_index)
new_offset = beginning_of_current_line_offset + current_line.size
@styled_text_proxy.setSelection(new_offset, new_offset)
end
|
#lexer ⇒ Object
289
290
291
292
293
294
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 289
def lexer
require 'rouge'
@lexer ||= Rouge::Lexer.find_fancy(language)
@lexer ||= Rouge::Lexer.find_fancy('ruby')
end
|
#line_numbers_block=(block) ⇒ Object
122
123
124
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 122
def line_numbers_block=(block)
@line_numbers_styled_text_proxy.content(&block)
end
|
#line_numbers_text_from(text_value) ⇒ Object
319
320
321
322
323
324
325
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 319
def line_numbers_text_from(text_value)
line_count = "#{text_value} ".split("\n").count
line_count = 1 if line_count == 0
lines_text_size = [line_count.to_s.size, @lines_width].max
@lines_width = lines_text_size if lines_text_size > @lines_width
line_count.times.map {|n| (' ' * (lines_text_size - (n+1).to_s.size)) + (n+1).to_s }.join("\n") + "\n"
end
|
#respond_to?(method_name, *args, &block) ⇒ Boolean
76
77
78
79
80
81
82
83
84
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 76
def respond_to?(method_name, *args, &block)
dsl_mode = @dsl_mode || args.last.is_a?(Hash) && args.last[:dsl]
if dsl_mode
args = args[0...-1] if args.last.is_a?(Hash) && args.last[:dsl]
super(method_name, *args, &block)
else
super || @styled_text_proxy&.respond_to?(method_name, *args, &block)
end
end
|
#restore_font_height ⇒ Object
356
357
358
359
360
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 356
def restore_font_height
return if @original_font_height.nil?
update_font_height(@original_font_height)
@original_font_height = nil
end
|
#root_block=(block) ⇒ Object
118
119
120
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 118
def root_block=(block)
body_root.content(&block)
end
|
#select_best_font ⇒ Object
327
328
329
330
331
332
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 327
def select_best_font
select_best_font_name
@font_options = {height: OS.mac? ? 15 : 12}
@font_options.merge!(name: @font_name) if @font_name
@font_options
end
|
#select_best_font_name ⇒ Object
334
335
336
337
338
339
340
341
342
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 334
def select_best_font_name
all_font_names = display.get_font_list(nil, true).map(&:name)
@font_name = 'Courier' if OS.mac?
FONT_NAMES_PREFERRED.each do |font_name|
@font_name ||= font_name if all_font_names.include?(font_name)
end
@font_name ||= all_font_names.find {|font_name| font_name.downcase.include?('mono')}
@font_name
end
|
#syntax_highlighting(text) ⇒ Object
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 273
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
|
#update_font_height(new_font_height) ⇒ Object
362
363
364
365
366
367
368
|
# File 'lib/glimmer/swt/custom/code_text.rb', line 362
def update_font_height(new_font_height)
return if new_font_height.nil?
@styled_text_proxy.font = {name: font_datum.name, height: new_font_height, style: font_datum.style}
@line_numbers_styled_text_proxy&.font = {name: font_datum.name, height: new_font_height, style: font_datum.style}
@body_root.shell_proxy.layout(true, true)
@body_root.shell_proxy.pack_same_size
end
|