glimmer-dsl-swt 4.18.3.1 → 4.18.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -1
  3. data/README.md +739 -324
  4. data/VERSION +1 -1
  5. data/glimmer-dsl-swt.gemspec +9 -4
  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 +171 -53
  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/shell_proxy.rb +13 -1
  21. data/lib/glimmer/swt/table_proxy.rb +43 -15
  22. data/lib/glimmer/swt/widget_proxy.rb +11 -2
  23. data/lib/glimmer/ui/custom_shell.rb +1 -0
  24. data/lib/glimmer/ui/custom_widget.rb +10 -3
  25. data/samples/elaborate/meta_sample.rb +1 -1
  26. data/samples/elaborate/meta_sample/meta_sample_logo.png +0 -0
  27. data/samples/elaborate/tetris.rb +90 -17
  28. data/samples/elaborate/tetris/model/game.rb +89 -8
  29. data/samples/elaborate/tetris/{view/game_over_dialog.rb → model/past_game.rb} +12 -41
  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 +131 -0
  33. data/samples/elaborate/tetris/view/playfield.rb +1 -1
  34. data/samples/elaborate/tetris/view/score_lane.rb +6 -6
  35. data/samples/elaborate/tetris/view/tetris_menu_bar.rb +70 -3
  36. data/samples/hello/hello_canvas.rb +10 -9
  37. data/samples/hello/hello_canvas_animation.rb +5 -5
  38. data/samples/hello/hello_code_text.rb +92 -0
  39. data/samples/hello/hello_table.rb +6 -4
  40. data/samples/hello/hello_table/baseball_park.png +0 -0
  41. metadata +8 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.18.3.1
1
+ 4.18.4.0
@@ -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.1 ruby lib
5
+ # stub: glimmer-dsl-swt 4.18.4.0 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.1"
9
+ s.version = "4.18.4.0"
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-27"
14
+ s.date = "2021-02-02"
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,12 +136,14 @@ 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",
143
+ "samples/elaborate/tetris/model/past_game.rb",
141
144
  "samples/elaborate/tetris/model/tetromino.rb",
142
145
  "samples/elaborate/tetris/view/block.rb",
143
- "samples/elaborate/tetris/view/game_over_dialog.rb",
146
+ "samples/elaborate/tetris/view/high_score_dialog.rb",
144
147
  "samples/elaborate/tetris/view/playfield.rb",
145
148
  "samples/elaborate/tetris/view/score_lane.rb",
146
149
  "samples/elaborate/tetris/view/tetris_menu_bar.rb",
@@ -155,6 +158,7 @@ Gem::Specification.new do |s|
155
158
  "samples/hello/hello_canvas_transform.rb",
156
159
  "samples/hello/hello_checkbox.rb",
157
160
  "samples/hello/hello_checkbox_group.rb",
161
+ "samples/hello/hello_code_text.rb",
158
162
  "samples/hello/hello_combo.rb",
159
163
  "samples/hello/hello_computed.rb",
160
164
  "samples/hello/hello_computed/contact.rb",
@@ -180,6 +184,7 @@ Gem::Specification.new do |s|
180
184
  "samples/hello/hello_styled_text.rb",
181
185
  "samples/hello/hello_tab.rb",
182
186
  "samples/hello/hello_table.rb",
187
+ "samples/hello/hello_table/baseball_park.png",
183
188
  "samples/hello/hello_world.rb",
184
189
  "vendor/swt/linux/swt.jar",
185
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,112 @@ 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
36
38
 
37
- def text
38
- swt_widget&.text
39
+ def text=(value)
40
+ @styled_text_proxy&.swt_widget&.text = value
39
41
  end
40
42
 
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
43
+ def text(*args, dsl: false, &block)
44
+ # If dsl: true is passed, operate using Glimmer DSL (which is the default that happens through super method_missing)
45
+ if dsl
46
+ super(*args, &block)
47
+ else
48
+ @styled_text_proxy&.swt_widget&.text
53
49
  end
54
- @syntax_highlighting[text]
55
- end
56
-
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')
60
50
  end
61
51
 
62
52
  before_body {
53
+ require 'rouge'
54
+ require 'ext/rouge/themes/glimmer'
63
55
  @swt_style = swt_style == 0 ? [:border, :multi, :v_scroll, :h_scroll] : swt_style
64
56
  @font_name = display.get_font_list(nil, true).map(&:name).include?('Consolas') ? 'Consolas' : 'Courier'
57
+ if lines == true
58
+ @lines_width = 4
59
+ elsif lines.is_a?(Hash)
60
+ @lines_width = lines[:width]
61
+ end
65
62
  }
66
63
 
67
64
  body {
68
- styled_text(swt_style) {
65
+ # TODO enable this once fully implemented
66
+ if lines
67
+ composite {
68
+ grid_layout(2, false)
69
+
70
+ @line_numbers_text = styled_text(:multi, :border) {
71
+ layout_data(:right, :fill, false, true)
72
+ text ' '*lines_width.to_i
73
+ text(bind(self, :styled_text_proxy_text, read_only: true) { |text_value|
74
+ line_count = text_value.to_s.split("\n").count
75
+ line_count = 1 if line_count == 0
76
+ lines_text_size = [line_count.to_s.size, @lines_width].max
77
+ if lines_text_size > @lines_width
78
+ async_exec { swt_widget.layout }
79
+ @lines_width = lines_text_size
80
+ end
81
+ line_count.times.map {|n| (' ' * (lines_text_size - (n+1).to_s.size)) + (n+1).to_s }.join("\n") + "\n"
82
+ }, dsl: true)
83
+ top_pixel bind(self, :styled_text_proxy_top_pixel, read_only: true)
84
+ font name: @font_name, height: OS.mac? ? 15 : 12
85
+ background color(:widget_background)
86
+ foreground :dark_blue
87
+ top_margin 5
88
+ right_margin 5
89
+ bottom_margin 5
90
+ left_margin 5
91
+ editable false
92
+ caret nil
93
+ on_focus_gained {
94
+ @styled_text_proxy&.swt_widget.setFocus
95
+ }
96
+ on_key_pressed {
97
+ @styled_text_proxy&.swt_widget.setFocus
98
+ }
99
+ on_mouse_up {
100
+ @styled_text_proxy&.swt_widget.setFocus
101
+ }
102
+ }
103
+
104
+ code_text_widget
105
+ }
106
+ else
107
+ code_text_widget
108
+ end
109
+ }
110
+
111
+ def code_text_widget
112
+ @styled_text_proxy = styled_text(swt_style) {
113
+ layout_data :fill, :fill, true, true if lines
114
+ text(bind(self, :styled_text_proxy_text), dsl: true) if lines
115
+ top_pixel bind(self, :styled_text_proxy_top_pixel) if lines
69
116
  font name: @font_name, height: 15
70
117
  foreground rgb(75, 75, 75)
71
118
  left_margin 5
@@ -73,6 +120,20 @@ module Glimmer
73
120
  right_margin 5
74
121
  bottom_margin 5
75
122
 
123
+ if lines
124
+ on_key_pressed { |event|
125
+ character = event.keyCode.chr rescue nil
126
+ case [event.stateMask, character]
127
+ when [(OS.mac? ? swt(:command) : swt(:ctrl)), 'a']
128
+ @styled_text_proxy.swt_widget.selectAll
129
+ when [(swt(:ctrl) unless OS.windows?), 'a']
130
+ jump_to_beginning_of_line
131
+ when [(swt(:ctrl) unless OS.windows?), 'e']
132
+ jump_to_end_of_line
133
+ end
134
+ }
135
+ end
136
+
76
137
  on_modify_text { |event|
77
138
  # clear unnecessary syntax highlighting cache on text updates, and do it async to avoid affecting performance
78
139
  new_text = event.data
@@ -88,18 +149,75 @@ module Glimmer
88
149
  }
89
150
 
90
151
  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)
152
+ begin
153
+ styles = []
154
+ style_data = nil
155
+ syntax_highlighting(line_style_event.lineText).to_a.each do |token_hash|
156
+ start_index = token_hash[:token_index]
157
+ size = token_hash[:token_text].size
158
+ style_data = Rouge::Theme.find(theme).new.style_for(token_hash[:token_type])
159
+ foreground_color = hex_color_to_swt_color(style_data[:fg], [:black])
160
+ background_color = hex_color_to_swt_color(style_data[:bg], [:white])
161
+ font_styles = []
162
+ font_styles << :bold if style_data[:bold]
163
+ font_styles << :italic if style_data[:italic]
164
+ font_style = SWTProxy[*font_styles]
165
+ styles << StyleRange.new(line_style_event.lineOffset + start_index, size, foreground_color, background_color, font_style)
166
+ end
167
+ line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
168
+ rescue => e
169
+ Glimmer::Config.logger.error {"Error encountered with style data: #{style_data}"}
170
+ Glimmer::Config.logger.error {e.message}
171
+ Glimmer::Config.logger.error {e.full_message}
98
172
  end
99
- line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
100
173
  }
101
174
  }
102
- }
175
+ end
176
+
177
+ def syntax_highlighting(text)
178
+ return [] if text.to_s.strip.empty?
179
+ @syntax_highlighting ||= {}
180
+ unless @syntax_highlighting.keys.include?(text)
181
+ lex = lexer.lex(text).to_a
182
+ text_size = 0
183
+ @syntax_highlighting[text] = lex.map do |pair|
184
+ {token_type: pair.first, token_text: pair.last}
185
+ end.each do |hash|
186
+ hash[:token_index] = text_size
187
+ text_size += hash[:token_text].size
188
+ end
189
+ end
190
+ @syntax_highlighting[text]
191
+ end
192
+
193
+ def lexer
194
+ require 'rouge'
195
+ # TODO Try to use Rouge::Lexer.find_fancy('guess', code) in the future to guess the language or otherwise detect it from file extension
196
+ @lexer ||= Rouge::Lexer.find_fancy(language)
197
+ @lexer ||= Rouge::Lexer.find_fancy('ruby') # default to Ruby if no lexer is found
198
+ end
199
+
200
+ def hex_color_to_swt_color(color_data, default_color)
201
+ 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
202
+ 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?('#')
203
+ color_data = [color_data] unless color_data.nil? || color_data.empty? || color_data.is_a?(Array)
204
+ color_data = default_color if color_data.nil? || color_data.empty?
205
+ color(*color_data).swt_color
206
+ end
207
+
208
+ def jump_to_beginning_of_line
209
+ current_line_index = @styled_text_proxy.swt_widget.getLineAtOffset(@styled_text_proxy.swt_widget.getCaretOffset)
210
+ beginning_of_current_line_offset = @styled_text_proxy.swt_widget.getOffsetAtLine(current_line_index)
211
+ @styled_text_proxy.swt_widget.setSelection(beginning_of_current_line_offset, beginning_of_current_line_offset)
212
+ end
213
+
214
+ def jump_to_end_of_line
215
+ current_line_index = @styled_text_proxy.swt_widget.getLineAtOffset(@styled_text_proxy.swt_widget.getCaretOffset)
216
+ current_line = @styled_text_proxy.swt_widget.getLine(current_line_index)
217
+ beginning_of_current_line_offset = @styled_text_proxy.swt_widget.getOffsetAtLine(current_line_index)
218
+ new_offset = beginning_of_current_line_offset + current_line.size
219
+ @styled_text_proxy.swt_widget.setSelection(new_offset, new_offset)
220
+ end
103
221
  end
104
222
  end
105
223
  end