Skip to content

Commit

Permalink
Extracted code_text syntax-highlighted text widget & made sash_form w…
Browse files Browse the repository at this point in the history
…eights accept splatted array
  • Loading branch information
AndyObtiva committed Oct 21, 2020
1 parent 2ee06c7 commit f9b8d1b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 59 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Here is a list of tasks to do (moved to [CHANGELOG.md](CHANGELOG.md) once done).
- Build a mini Glimmer app to launch samples (sample of samples meta-sample)
- Have meta-sample load samples from gems
- Syntax Color Highlighting in metasample
- Make sash_form weights accept splat array elements (not wrapped in [])


## Soon

Expand Down Expand Up @@ -201,7 +203,6 @@ items <=> binding {
- Add `widget` keyword to build proxies for swt widgets without directly using Glimmer::SWT::WidgetProxy
- Look into modularizing the menu and prefrences into separate components for a scaffolded app/custom-shell
- Consider adding sash_form children style for having a fixed size when resizing, or provide a flexible alternative via sash widget
- Make sash_form weights accept splat array elements (not wrapped in [])
- Make sash_form weights not execute till the closing of the sash_form (to allow putting it above content instead of below as required by Java)
- Speed up glimmer command with CRuby compatibility via jruby-jars gem
- Build a TUI for browsing/running internal gem samples
Expand Down
1 change: 1 addition & 0 deletions lib/glimmer-dsl-swt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
require 'logging'
require 'nested_inherited_jruby_include_package'
require 'super_module'
require 'rouge'

# Internal requires
require 'ext/glimmer/config'
Expand Down
1 change: 1 addition & 0 deletions lib/glimmer/dsl/swt/custom_widget_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require 'glimmer/dsl/top_level_expression'
require 'glimmer/ui/custom_widget'
require 'glimmer/ui/custom_shell'
require 'glimmer/swt/custom/code_text'

module Glimmer
module DSL
Expand Down
82 changes: 82 additions & 0 deletions lib/glimmer/swt/custom/code_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'glimmer/ui/custom_widget'

module Glimmer
module SWT
module Custom
class CodeText
include Glimmer::UI::CustomWidget

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

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

def text
swt_widget&.text
end

def syntax_highlighting
return [] if text.to_s.strip.empty?
code = text
lexer = Rouge::Lexer.find_fancy('ruby', code)
lex = lexer.lex(code).to_a
code_size = 0
lex_hashes = lex.map do |pair|
{token_type: pair.first, token_text: pair.last}
end.each do |hash|
hash[:token_index] = code_size
code_size += hash[:token_text].size
end
end

before_body {
@swt_style = swt_style == 0 ? [:border, :multi, :v_scroll, :h_scroll] : swt_style
}

body {
styled_text(swt_style) {
font name: 'Lucida Console', height: 16
foreground rgb(75, 75, 75)
left_margin 5
top_margin 5
right_margin 5
bottom_margin 5

on_line_get_style { |line_style_event|
styles = []
line_style_event.lineOffset
syntax_highlighting.each do |token_hash|
if token_hash[:token_index] >= line_style_event.lineOffset && token_hash[:token_index] < (line_style_event.lineOffset + line_style_event.lineText.size)
start_index = token_hash[:token_index]
size = token_hash[:token_text].size
token_color = (SYNTAX_COLOR_MAP[token_hash[:token_type].name] || color(:black)).swt_color
styles << StyleRange.new(start_index, size, token_color, nil)
end
end
line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
}
}
}
end
end
end
end
9 changes: 8 additions & 1 deletion lib/glimmer/swt/widget_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WidgetProxy
"radio" => [:radio],
"scrolled_composite" => [:border, :h_scroll, :v_scroll],
"spinner" => [:border],
"styled_text" => [:border],
"styled_text" => [:border, :multi, :v_scroll, :h_scroll],
"table" => [:virtual, :border, :full_selection],
"text" => [:border],
"toggle" => [:toggle],
Expand Down Expand Up @@ -622,6 +622,13 @@ def widget_custom_attribute_mapping
getter: {name: 'getSelectionCount'},
setter: {name: 'setSelection', invoker: lambda { |widget, args| @swt_widget.setSelection(@swt_widget.getCaretPosition, @swt_widget.getCaretPosition + args.first) if args.first }},
},
'weights' => {
getter: {name: 'getWeights'},
setter: {name: 'setWeights', invoker: lambda do |widget, args|
args = args.first if args.first.is_a?(Array) && args.size == 1
@swt_widget.setWeights(args)
end},
},
}
end

Expand Down
60 changes: 3 additions & 57 deletions samples/meta_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,7 @@ def name
def content
@content ||= File.read(file)
end

def syntax_highlighting
code = content
lexer = Rouge::Lexer.find_fancy('ruby', code)
lex = lexer.lex(code).to_a
code_size = 0
lex_hashes = lex.map do |pair|
{token_type: pair.first, token_text: pair.last}
end.each do |hash|
hash[:token_index] = code_size
code_size += hash[:token_text].size
end.reject do |hash|
hash[:token_type] == Rouge::Token::Tokens::Text
end
end


def launch
load file
end
Expand Down Expand Up @@ -156,52 +141,13 @@ def launch
}
}

# TODO extract the following to a code_text widget that has syntax highlighting and language detection
styled_text(:multi, :border, :v_scroll, :h_scroll) { |proxy|
code_text {
text bind(SampleDirectory, 'selected_sample.content')
font name: 'Lucida Console', height: 16
foreground rgb(75, 75, 75)
editable false
caret nil
left_margin 5
top_margin 5
right_margin 5
bottom_margin 5

lex_color_map = {
Builtin: rgb(215,58,73),
Class: rgb(3,47,98),
Constant: rgb(0,92,197),
Double: rgb(0,92,197),
Escape: color(:red),
Function: color(:blue),
Instance: rgb(227,98,9),
Integer: color(:blue),
Keyword: color(:blue),
Name: rgb(111,66,193), #purple
Operator: color(:red),
Punctuation: color(:blue),
Single: rgb(106,115,125), # Also, Comments
Symbol: color(:dark_green),
Pseudo: color(:dark_red),
Interpol: color(:blue),
}
on_line_get_style { |line_style_event|
styles = []
line_style_event.lineOffset
SampleDirectory.selected_sample.syntax_highlighting.each do |token_hash|
if token_hash[:token_index] >= line_style_event.lineOffset && token_hash[:token_index] < (line_style_event.lineOffset + line_style_event.lineText.size)
start_index = token_hash[:token_index]
size = token_hash[:token_text].size
token_color = (lex_color_map[token_hash[:token_type].name] || color(:black)).swt_color
styles << StyleRange.new(start_index, size, token_color, nil)
end
end
line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
}
}

weights [1, 2]
weights 1, 2
}
}.open
end
Expand Down

0 comments on commit f9b8d1b

Please sign in to comment.