Syntax Highlighting with StyledText via Glimmer DSL for SWT [message #1833678] |
Wed, 21 October 2020 09:07 |
Andy Maleh Messages: 75 Registered: March 2020 Location: Montreal, Quebec, Canada |
Member |
|
|
Glimmer DSL for SWT v4.17.4.0 ships with a number of changes, such as the new code_text syntax highlighting custom widget based on StyledText, the Glimmer Meta-Sample (The Sample of Samples), and the Hello, Sash Form! Sample. One noteworthy thing to mention about implementing `code_text` is that with Glimmer Ruby syntax, it was the shortest Syntax Highlighting implementation of SWT StyledText I have ever seen compared to various resources online.
https://andymaleh.blogspot.com/2020/10/glimmer-syntax-highlighting-text-widget.html
Here is the entire Glimmer view code for StyledText:
# From https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/lib/glimmer/swt/custom/code_text.rb
styled_text(swt_style) {
font name: 'Consolas', height: 15
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] || [:black]
token_color = color(*token_color).swt_color
styles << StyleRange.new(start_index, size, token_color, nil)
end
end
line_style_event.styles = styles.to_java(StyleRange) unless styles.empty?
}
}
Of course, to use it, developers can now just use the `code_text` keyword (no more need for complicated custom implementations of syntax highlighting via StyledText as the work has been automated):
code_text {
# ... properties go here, e.g. `text bind(model, :property)`
}
Result:
EclipseCon / EclipseWorld / Agile Conference Speaker
Open-Source Software Author of Glimmer DSL for SWT
[Updated on: Thu, 22 October 2020 06:05] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.24794 seconds