glimmer-dsl-swt 4.18.3.2 → 4.18.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -0
  3. data/README.md +674 -323
  4. data/VERSION +1 -1
  5. data/glimmer-dsl-swt.gemspec +7 -3
  6. data/lib/ext/rouge/themes/glimmer.rb +29 -0
  7. data/lib/glimmer-dsl-swt.rb +0 -1
  8. data/lib/glimmer/data_binding/table_items_binding.rb +8 -5
  9. data/lib/glimmer/data_binding/widget_binding.rb +9 -1
  10. data/lib/glimmer/dsl/swt/image_expression.rb +14 -6
  11. data/lib/glimmer/dsl/swt/layout_data_expression.rb +4 -4
  12. data/lib/glimmer/dsl/swt/layout_expression.rb +5 -3
  13. data/lib/glimmer/swt/custom/code_text.rb +196 -51
  14. data/lib/glimmer/swt/custom/drawable.rb +4 -6
  15. data/lib/glimmer/swt/custom/shape.rb +69 -12
  16. data/lib/glimmer/swt/date_time_proxy.rb +1 -3
  17. data/lib/glimmer/swt/display_proxy.rb +12 -1
  18. data/lib/glimmer/swt/font_proxy.rb +1 -0
  19. data/lib/glimmer/swt/image_proxy.rb +79 -1
  20. data/lib/glimmer/swt/layout_proxy.rb +4 -1
  21. data/lib/glimmer/swt/shell_proxy.rb +10 -1
  22. data/lib/glimmer/swt/table_proxy.rb +27 -14
  23. data/lib/glimmer/swt/widget_proxy.rb +12 -2
  24. data/lib/glimmer/ui/custom_widget.rb +6 -2
  25. data/samples/elaborate/meta_sample.rb +5 -2
  26. data/samples/elaborate/meta_sample/meta_sample_logo.png +0 -0
  27. data/samples/elaborate/tetris.rb +60 -7
  28. data/samples/elaborate/tetris/model/game.rb +36 -7
  29. data/samples/elaborate/tetris/model/past_game.rb +14 -1
  30. data/samples/elaborate/tetris/model/tetromino.rb +18 -5
  31. data/samples/elaborate/tetris/view/block.rb +8 -13
  32. data/samples/elaborate/tetris/view/high_score_dialog.rb +23 -6
  33. data/samples/elaborate/tetris/view/playfield.rb +1 -1
  34. data/samples/elaborate/tetris/view/tetris_menu_bar.rb +22 -6
  35. data/samples/hello/hello_canvas.rb +10 -9
  36. data/samples/hello/hello_canvas_animation.rb +5 -5
  37. data/samples/hello/hello_code_text.rb +104 -0
  38. data/samples/hello/hello_table.rb +6 -4
  39. data/samples/hello/hello_table/baseball_park.png +0 -0
  40. metadata +6 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.18.3.2
1
+ 4.18.4.1
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: glimmer-dsl-swt 4.18.3.2 ruby lib
5
+ # stub: glimmer-dsl-swt 4.18.4.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "glimmer-dsl-swt".freeze
9
- s.version = "4.18.3.2"
9
+ s.version = "4.18.4.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["AndyMaleh".freeze]
14
- s.date = "2021-01-28"
14
+ s.date = "2021-02-03"
15
15
  s.description = "Glimmer DSL for SWT (JRuby Desktop Development GUI Framework) is a native-GUI cross-platform desktop development library written in JRuby, an OS-threaded faster JVM version of Ruby. Glimmer's main innovation is a declarative Ruby DSL that enables productive and efficient authoring of desktop application user-interfaces by relying on the robust Eclipse SWT library. Glimmer additionally innovates by having built-in data-binding support, which greatly facilitates synchronizing the GUI with domain models, thus achieving true decoupling of object oriented components and enabling developers to solve business problems (test-first) without worrying about GUI concerns, or alternatively drive development GUI-first, and then write clean business models (test-first) afterwards. Not only does Glimmer provide a large set of GUI widgets, but it also supports drawing Canvas Graphics like Shapes and Animations. To get started quickly, Glimmer offers scaffolding options for Apps, Gems, and Custom Widgets. Glimmer also includes native-executable packaging support, sorely lacking in other libraries, thus enabling the delivery of desktop apps written in Ruby as truly native DMG/PKG/APP files on the Mac + App Store, MSI/EXE files on Windows, and Gem Packaged Shell Scripts on Linux.".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.executables = ["glimmer".freeze, "girb".freeze]
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "icons/scaffold_app.png",
35
35
  "lib/ext/glimmer.rb",
36
36
  "lib/ext/glimmer/config.rb",
37
+ "lib/ext/rouge/themes/glimmer.rb",
37
38
  "lib/glimmer-dsl-swt.rb",
38
39
  "lib/glimmer/Rakefile",
39
40
  "lib/glimmer/data_binding/list_selection_binding.rb",
@@ -135,6 +136,7 @@ Gem::Specification.new do |s|
135
136
  "samples/elaborate/contact_manager/contact_repository.rb",
136
137
  "samples/elaborate/login.rb",
137
138
  "samples/elaborate/meta_sample.rb",
139
+ "samples/elaborate/meta_sample/meta_sample_logo.png",
138
140
  "samples/elaborate/tetris.rb",
139
141
  "samples/elaborate/tetris/model/block.rb",
140
142
  "samples/elaborate/tetris/model/game.rb",
@@ -156,6 +158,7 @@ Gem::Specification.new do |s|
156
158
  "samples/hello/hello_canvas_transform.rb",
157
159
  "samples/hello/hello_checkbox.rb",
158
160
  "samples/hello/hello_checkbox_group.rb",
161
+ "samples/hello/hello_code_text.rb",
159
162
  "samples/hello/hello_combo.rb",
160
163
  "samples/hello/hello_computed.rb",
161
164
  "samples/hello/hello_computed/contact.rb",
@@ -181,6 +184,7 @@ Gem::Specification.new do |s|
181
184
  "samples/hello/hello_styled_text.rb",
182
185
  "samples/hello/hello_tab.rb",
183
186
  "samples/hello/hello_table.rb",
187
+ "samples/hello/hello_table/baseball_park.png",
184
188
  "samples/hello/hello_world.rb",
185
189
  "vendor/swt/linux/swt.jar",
186
190
  "vendor/swt/mac/swt.jar",
@@ -0,0 +1,29 @@
1
+ module Rouge
2
+ module Themes
3
+ # A port of the pastie style from Pygments.
4
+ # See https://bitbucket.org/birkenfeld/pygments-main/src/default/pygments/styles/pastie.py
5
+ class Glimmer < Github
6
+ name 'glimmer'
7
+ style Comment::Single, fg: [106,115,125], italic: true # Also, Comments
8
+ style Keyword::Pseudo, fg: [:dark_red]
9
+ style Keyword, fg: [:blue]
10
+ style Literal::String::Single, fg: [106,115,125] # Also, Comments
11
+ style Literal::String::Double, fg: [0,92,197]
12
+ style Literal::String::Escape, fg: [:red]
13
+ style Literal::Number::Integer, fg: [:blue]
14
+ style Literal::String::Interpol, fg: [:blue]
15
+ style Literal::String::Symbol, fg: [:dark_green]
16
+ style Literal::String, fg: [:dark_blue]
17
+ style Name::Builtin, fg: [215,58,73]
18
+ style Name::Class, fg: [3,47,98]
19
+ style Name::Namespace, fg: [3,47,98]
20
+ style Name::Constant, fg: [0,92,197]
21
+ style Name::Function, fg: [:blue]
22
+ style Name::Variable::Instance, fg: [227,98,9]
23
+ style Name, fg: [111,66,193] #purple
24
+ style Operator, fg: [:red]
25
+ style Punctuation, fg: [:blue]
26
+ style Text, fg: [75, 75, 75]
27
+ end
28
+ end
29
+ end
@@ -34,7 +34,6 @@ require 'glimmer'
34
34
  require 'logging'
35
35
  require 'nested_inherited_jruby_include_package'
36
36
  require 'super_module'
37
- require 'rouge'
38
37
  require 'date'
39
38
  require 'facets/string/capitalized'
40
39
  require 'facets/hash/symbolize_keys'
@@ -36,7 +36,9 @@ module Glimmer
36
36
  def initialize(parent, model_binding, column_properties)
37
37
  @table = parent
38
38
  @model_binding = model_binding
39
+ @read_only_sort = @model_binding.binding_options[:read_only_sort]
39
40
  @table.swt_widget.data = @model_binding
41
+ @table.swt_widget.set_data('table_items_binding', self)
40
42
  @column_properties = column_properties
41
43
  @table.on_widget_disposed do |dispose_event|
42
44
  unregister_all_observables
@@ -50,8 +52,8 @@ module Glimmer
50
52
  call
51
53
  end
52
54
 
53
- def call(new_model_collection=nil)
54
- new_model_collection = @model_binding.evaluate_property # this ensures applying converters (e.g. :on_read)
55
+ def call(new_model_collection=nil, internal_sort: false)
56
+ new_model_collection = model_binding_evaluated_property = @model_binding.evaluate_property unless internal_sort # this ensures applying converters (e.g. :on_read)
55
57
  table_cells = @table.swt_widget.items.map {|item| @table.column_properties.size.times.map {|i| item.get_text(i)} }
56
58
  model_cells = new_model_collection.to_a.map {|m| @table.cells_for(m)}
57
59
  return if table_cells == model_cells
@@ -61,10 +63,10 @@ module Glimmer
61
63
  add_dependent(@table_observer_registration => @table_items_observer_registration)
62
64
  @model_collection = new_model_collection
63
65
  end
64
- populate_table(@model_collection, @table, @column_properties)
66
+ populate_table(@model_collection, @table, @column_properties, internal_sort: internal_sort)
65
67
  end
66
68
 
67
- def populate_table(model_collection, parent, column_properties)
69
+ def populate_table(model_collection, parent, column_properties, internal_sort: false)
68
70
  selected_table_item_models = parent.swt_widget.getSelection.map(&:get_data)
69
71
  parent.finish_edit!
70
72
  parent.swt_widget.items.each(&:dispose)
@@ -78,7 +80,8 @@ module Glimmer
78
80
  end
79
81
  selected_table_items = parent.search {|item| selected_table_item_models.include?(item.get_data) }
80
82
  parent.swt_widget.setSelection(selected_table_items)
81
- parent.sort!
83
+ sorted_model_collection = parent.sort!(internal_sort: internal_sort)
84
+ call(sorted_model_collection, internal_sort: true) if @read_only_sort && !internal_sort && !sorted_model_collection.nil?
82
85
  parent.swt_widget.redraw if parent&.swt_widget&.respond_to?(:redraw)
83
86
  end
84
87
  end
@@ -37,7 +37,7 @@ module Glimmer
37
37
  @property = property
38
38
  @translator = translator || proc {|value| value} #TODO check on this it doesn't seem used
39
39
 
40
- if @widget.respond_to?(:dispose)
40
+ if @widget.respond_to?(:on_widget_disposed)
41
41
  @widget.on_widget_disposed do |dispose_event|
42
42
  unregister_all_observables
43
43
  end
@@ -48,6 +48,10 @@ module Glimmer
48
48
  converted_value = translated_value = @translator.call(value)
49
49
 
50
50
  update_operation = lambda do
51
+ if @widget.respond_to?(:disposed?) && @widget.disposed?
52
+ unregister_all_observables
53
+ return
54
+ end
51
55
  @widget.set_attribute(@property, converted_value) unless evaluate_property == converted_value
52
56
  end
53
57
  if Config.auto_sync_exec? && Config.require_sync_exec?
@@ -58,6 +62,10 @@ module Glimmer
58
62
  end
59
63
 
60
64
  def evaluate_property
65
+ if @widget.respond_to?(:disposed?) && @widget.disposed?
66
+ unregister_all_observables
67
+ return
68
+ end
61
69
  @widget.get_attribute(@property)
62
70
  end
63
71
  end
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2007-2021 Andy Maleh
2
- #
2
+ #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
5
5
  # "Software"), to deal in the Software without restriction, including
@@ -7,10 +7,10 @@
7
7
  # distribute, sublicense, and/or sell copies of the Software, and to
8
8
  # permit persons to whom the Software is furnished to do so, subject to
9
9
  # the following conditions:
10
- #
10
+ #
11
11
  # The above copyright notice and this permission notice shall be
12
12
  # included in all copies or substantial portions of the Software.
13
- #
13
+ #
14
14
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
15
  # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
16
  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -28,13 +28,21 @@ module Glimmer
28
28
  # image expression
29
29
  # Note: Cannot be a static expression because it clashes with image property expression
30
30
  class ImageExpression < Expression
31
+ include ParentExpression
32
+
31
33
  def can_interpret?(parent, keyword, *args, &block)
32
- keyword.to_s == 'image' and
33
- (parent.nil? or !parent.respond_to?('image'))
34
+ (keyword == 'image') and
35
+ (parent.nil? or parent.respond_to?('image='))
34
36
  end
35
37
 
36
38
  def interpret(parent, keyword, *args, &block)
37
- Glimmer::SWT::ImageProxy.new(*args)
39
+ args.unshift(parent) unless parent.nil?
40
+ Glimmer::SWT::ImageProxy.new(*args, &block)
41
+ end
42
+
43
+ def add_content(parent, &block)
44
+ super
45
+ parent.post_add_content
38
46
  end
39
47
  end
40
48
  end
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2007-2021 Andy Maleh
2
- #
2
+ #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
5
5
  # "Software"), to deal in the Software without restriction, including
@@ -7,10 +7,10 @@
7
7
  # distribute, sublicense, and/or sell copies of the Software, and to
8
8
  # permit persons to whom the Software is furnished to do so, subject to
9
9
  # the following conditions:
10
- #
10
+ #
11
11
  # The above copyright notice and this permission notice shall be
12
12
  # included in all copies or substantial portions of the Software.
13
- #
13
+ #
14
14
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
15
  # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
16
  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -33,7 +33,7 @@ module Glimmer
33
33
  include ParentExpression
34
34
 
35
35
  def can_interpret?(parent, keyword, *args, &block)
36
- keyword == 'layout_data' and
36
+ super and
37
37
  parent.respond_to?(:swt_widget)
38
38
  end
39
39
 
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2007-2021 Andy Maleh
2
- #
2
+ #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
5
5
  # "Software"), to deal in the Software without restriction, including
@@ -7,10 +7,10 @@
7
7
  # distribute, sublicense, and/or sell copies of the Software, and to
8
8
  # permit persons to whom the Software is furnished to do so, subject to
9
9
  # the following conditions:
10
- #
10
+ #
11
11
  # The above copyright notice and this permission notice shall be
12
12
  # included in all copies or substantial portions of the Software.
13
- #
13
+ #
14
14
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
15
  # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
16
  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -23,6 +23,7 @@ require 'glimmer'
23
23
  require 'glimmer/dsl/expression'
24
24
  require 'glimmer/dsl/parent_expression'
25
25
  require 'glimmer/swt/layout_proxy'
26
+ require 'glimmer/swt/image_proxy'
26
27
 
27
28
  module Glimmer
28
29
  module DSL
@@ -31,6 +32,7 @@ module Glimmer
31
32
  include ParentExpression
32
33
 
33
34
  include_package 'org.eclipse.swt.widgets'
35
+ include_package 'org.eclipse.swt.graphics'
34
36
 
35
37
  def can_interpret?(parent, keyword, *args, &block)
36
38
  keyword.to_s.end_with?('_layout') and
@@ -7,65 +7,139 @@ module Glimmer
7
7
  class CodeText
8
8
  include Glimmer::UI::CustomWidget
9
9
 
10
- SYNTAX_COLOR_MAP = {
11
- Builtin: [215,58,73],
12
- Class: [3,47,98],
13
- Constant: [0,92,197],
14
- Double: [0,92,197],
15
- Escape: [:red],
16
- Function: [:blue],
17
- Instance: [227,98,9],
18
- Integer: [:blue],
19
- Interpol: [:blue],
20
- Keyword: [:blue],
21
- Name: [111,66,193], #purple
22
- Operator: [:red],
23
- Pseudo: [:dark_red],
24
- Punctuation: [:blue],
25
- Single: [106,115,125], # Also, Comments
26
- Symbol: [:dark_green],
27
- Text: [75, 75, 75],
28
- }
10
+ class << self
11
+ def languages
12
+ require 'rouge'
13
+ Rouge::Lexer.all.map {|lexer| lexer.tag}.sort
14
+ end
15
+
16
+ def lexers
17
+ require 'rouge'
18
+ Rouge::Lexer.all.sort_by(&:title)
19
+ end
20
+ end
21
+
22
+ REGEX_COLOR_HEX6 = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/
29
23
 
30
- # TODO support `option :language`
31
24
  # TODO support auto language detection
25
+ # TODO support end of line via CMD+E and beginning of line via CMD+A
26
+ # TODO support select all via CMD+A
27
+
28
+ option :language, default: 'ruby'
29
+ # TODO consider supporting data-binding of language
30
+ # TODO support switcher of language that automatically updates the lexer
31
+ # TODO support method for redrawing the syntax highlighting
32
+ option :theme, default: 'glimmer'
33
+ option :lines, default: false
32
34
 
33
- def text=(value)
34
- swt_widget&.text = value
35
- end
35
+ alias lines? lines
36
+ attr_accessor :styled_text_proxy_text, :styled_text_proxy_top_pixel
37
+ attr_reader :styled_text_proxy, :lines_width, :line_numbers_styled_text_proxy
36
38
 
37
- def text
38
- swt_widget&.text
39
+ def method_missing(method_name, *args, &block)
40
+ dsl_mode = @dsl_mode || args.last.is_a?(Hash) && args.last[:dsl]
41
+ if dsl_mode
42
+ args.pop if args.last.is_a?(Hash) && args.last[:dsl]
43
+ super(method_name, *args, &block)
44
+ elsif @styled_text_proxy&.respond_to?(method_name, *args, &block)
45
+ @styled_text_proxy&.send(method_name, *args, &block)
46
+ else
47
+ super
48
+ end
39
49
  end
40
50
 
41
- def syntax_highlighting(text)
42
- return [] if text.to_s.strip.empty?
43
- @syntax_highlighting ||= {}
44
- unless @syntax_highlighting.keys.include?(text)
45
- lex = lexer.lex(text).to_a
46
- text_size = 0
47
- @syntax_highlighting[text] = lex.map do |pair|
48
- {token_type: pair.first, token_text: pair.last}
49
- end.each do |hash|
50
- hash[:token_index] = text_size
51
- text_size += hash[:token_text].size
52
- end
51
+ def respond_to?(method_name, *args, &block)
52
+ dsl_mode = @dsl_mode || args.last.is_a?(Hash) && args.last[:dsl]
53
+ if dsl_mode
54
+ args = args[0...-1] if args.last.is_a?(Hash) && args.last[:dsl]
55
+ super(method_name, *args, &block)
56
+ else
57
+ super || @styled_text_proxy&.respond_to?(method_name, *args, &block)
53
58
  end
54
- @syntax_highlighting[text]
55
59
  end
56
60
 
57
- def lexer
58
- # TODO Try to use Rouge::Lexer.find_fancy('guess', code) in the future to guess the language or otherwise detect it from file extension
59
- @lexer ||= Rouge::Lexer.find_fancy('ruby')
61
+ def has_instance_method?(method_name)
62
+ respond_to?(method_name)
63
+ end
64
+
65
+ def root_block=(block)
66
+ body_root.content(&block)
67
+ end
68
+
69
+ def line_numbers_block=(block)
70
+ @line_numbers_styled_text_proxy.content(&block)
60
71
  end
61
72
 
62
73
  before_body {
74
+ require 'rouge'
75
+ require 'ext/rouge/themes/glimmer'
63
76
  @swt_style = swt_style == 0 ? [:border, :multi, :v_scroll, :h_scroll] : swt_style
64
77
  @font_name = display.get_font_list(nil, true).map(&:name).include?('Consolas') ? 'Consolas' : 'Courier'
78
+ if lines == true
79
+ @lines_width = 4
80
+ elsif lines.is_a?(Hash)
81
+ @lines_width = lines[:width]
82
+ end
83
+ @dsl_mode = true
84
+ }
85
+
86
+ after_body {
87
+ @dsl_mode = nil
65
88
  }
66
89
 
67
90
  body {
68
- styled_text(swt_style) {
91
+ # TODO enable this once fully implemented
92
+ if lines
93
+ composite {
94
+ grid_layout(2, false)
95
+
96
+ @line_numbers_styled_text_proxy = styled_text(swt(swt(swt_style), :h_scroll!, :v_scroll!)) {
97
+ layout_data(:right, :fill, false, true)
98
+ text ' '*lines_width.to_i
99
+ text bind(self, :styled_text_proxy_text, read_only: true) { |text_value|
100
+ line_count = text_value.to_s.split("\n").count
101
+ line_count = 1 if line_count == 0
102
+ lines_text_size = [line_count.to_s.size, @lines_width].max
103
+ if lines_text_size > @lines_width
104
+ async_exec { swt_widget.layout }
105
+ @lines_width = lines_text_size
106
+ end
107
+ line_count.times.map {|n| (' ' * (lines_text_size - (n+1).to_s.size)) + (n+1).to_s }.join("\n") + "\n"
108
+ }
109
+ top_pixel bind(self, :styled_text_proxy_top_pixel, read_only: true)
110
+ font name: @font_name, height: OS.mac? ? 15 : 12
111
+ background color(:widget_background)
112
+ foreground :dark_blue
113
+ top_margin 5
114
+ right_margin 5
115
+ bottom_margin 5
116
+ left_margin 5
117
+ editable false
118
+ caret nil
119
+ on_focus_gained {
120
+ @styled_text_proxy&.swt_widget.setFocus
121
+ }
122
+ on_key_pressed {
123
+ @styled_text_proxy&.swt_widget.setFocus
124
+ }
125
+ on_mouse_up {
126
+ @styled_text_proxy&.swt_widget.setFocus
127
+ }
128
+ }
129
+
130
+ code_text_widget
131
+ }
132
+ else
133
+ code_text_widget
134
+ end
135
+ }
136
+
137
+ def code_text_widget
138
+ @styled_text_proxy = styled_text(swt_style) {
139
+ # custom_widget_property_owner # TODO implement to route properties here without declaring method_missing
140
+ layout_data :fill, :fill, true, true if lines
141
+ text bind(self, :styled_text_proxy_text) if lines
142
+ top_pixel bind(self, :styled_text_proxy_top_pixel) if lines
69
143
  font name: @font_name, height: 15
70
144
  foreground rgb(75, 75, 75)
71
145
  left_margin 5
@@ -73,6 +147,20 @@ module Glimmer
73
147
  right_margin 5
74
148
  bottom_margin 5
75
149
 
150
+ if lines
151
+ on_key_pressed { |event|
152
+ character = event.keyCode.chr rescue nil
153
+ case [event.stateMask, character]
154
+ when [(OS.mac? ? swt(:command) : swt(:ctrl)), 'a']
155
+ @styled_text_proxy.swt_widget.selectAll
156
+ when [(swt(:ctrl) unless OS.windows?), 'a']
157
+ jump_to_beginning_of_line
158
+ when [(swt(:ctrl) unless OS.windows?), 'e']
159
+ jump_to_end_of_line
160
+ end
161
+ }
162
+ end
163
+
76
164
  on_modify_text { |event|
77
165
  # clear unnecessary syntax highlighting cache on text updates, and do it async to avoid affecting performance
78
166
  new_text = event.data
@@ -88,18 +176,75 @@ module Glimmer
88
176
  }
89
177
 
90
178
  on_line_get_style { |line_style_event|
91
- styles = []
92
- syntax_highlighting(line_style_event.lineText).to_a.each do |token_hash|
93
- start_index = token_hash[:token_index]
94
- size = token_hash[:token_text].size
95
- token_color = SYNTAX_COLOR_MAP[token_hash[:token_type].name] || [:black]
96
- token_color = color(*token_color).swt_color
97
- styles << StyleRange.new(line_style_event.lineOffset + start_index, size, token_color, nil)
179
+ begin
180
+ styles = []
181
+ style_data = nil
182
+ syntax_highlighting(line_style_event.lineText).to_a.each do |token_hash|
183
+ start_index = token_hash[:token_index]
184
+ size = token_hash[:token_text].size
185
+ style_data = Rouge::Theme.find(theme).new.style_for(token_hash[:token_type])
186
+ foreground_color = hex_color_to_swt_color(style_data[:fg], [:black])
187
+ background_color = hex_color_to_swt_color(style_data[:bg], [:white])
188
+ font_styles = []
189
+ font_styles << :bold if style_data[:bold]
190
+ font_styles << :italic if style_data[:italic]
191
+ font_style = SWTProxy[*font_styles]
192
+ styles << StyleRange.new(line_style_event.lineOffset + start_index, size, foreground_color, background_color, font_style)
193
+ end
194
+ line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
195
+ rescue => e
196
+ Glimmer::Config.logger.error {"Error encountered with style data: #{style_data}"}
197
+ Glimmer::Config.logger.error {e.message}
198
+ Glimmer::Config.logger.error {e.full_message}
98
199
  end
99
- line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
100
200
  }
101
201
  }
102
- }
202
+ end
203
+
204
+ def syntax_highlighting(text)
205
+ return [] if text.to_s.strip.empty?
206
+ @syntax_highlighting ||= {}
207
+ unless @syntax_highlighting.keys.include?(text)
208
+ lex = lexer.lex(text).to_a
209
+ text_size = 0
210
+ @syntax_highlighting[text] = lex.map do |pair|
211
+ {token_type: pair.first, token_text: pair.last}
212
+ end.each do |hash|
213
+ hash[:token_index] = text_size
214
+ text_size += hash[:token_text].size
215
+ end
216
+ end
217
+ @syntax_highlighting[text]
218
+ end
219
+
220
+ def lexer
221
+ require 'rouge'
222
+ # TODO Try to use Rouge::Lexer.find_fancy('guess', code) in the future to guess the language or otherwise detect it from file extension
223
+ @lexer ||= Rouge::Lexer.find_fancy(language)
224
+ @lexer ||= Rouge::Lexer.find_fancy('ruby') # default to Ruby if no lexer is found
225
+ end
226
+
227
+ def hex_color_to_swt_color(color_data, default_color)
228
+ 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
229
+ 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?('#')
230
+ color_data = [color_data] unless color_data.nil? || color_data.empty? || color_data.is_a?(Array)
231
+ color_data = default_color if color_data.nil? || color_data.empty?
232
+ color(*color_data).swt_color
233
+ end
234
+
235
+ def jump_to_beginning_of_line
236
+ current_line_index = @styled_text_proxy.swt_widget.getLineAtOffset(@styled_text_proxy.swt_widget.getCaretOffset)
237
+ beginning_of_current_line_offset = @styled_text_proxy.swt_widget.getOffsetAtLine(current_line_index)
238
+ @styled_text_proxy.swt_widget.setSelection(beginning_of_current_line_offset, beginning_of_current_line_offset)
239
+ end
240
+
241
+ def jump_to_end_of_line
242
+ current_line_index = @styled_text_proxy.swt_widget.getLineAtOffset(@styled_text_proxy.swt_widget.getCaretOffset)
243
+ current_line = @styled_text_proxy.swt_widget.getLine(current_line_index)
244
+ beginning_of_current_line_offset = @styled_text_proxy.swt_widget.getOffsetAtLine(current_line_index)
245
+ new_offset = beginning_of_current_line_offset + current_line.size
246
+ @styled_text_proxy.swt_widget.setSelection(new_offset, new_offset)
247
+ end
103
248
  end
104
249
  end
105
250
  end