Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Syntax Highlighting with StyledText via Glimmer DSL for SWT(Shortest StyledText Syntax Highlighting Implementation with Glimmer Ruby DSL)
Syntax Highlighting with StyledText via Glimmer DSL for SWT [message #1833678] Wed, 21 October 2020 09:07 Go to next message
Andy Maleh is currently offline Andy MalehFriend
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:

https://raw.githubusercontent.com/AndyObtiva/glimmer-dsl-swt/master/images/glimmer-meta-sample.png


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

Re: Syntax Highlighting with StyledText via Glimmer DSL for SWT [message #1833796 is a reply to message #1833678] Fri, 23 October 2020 20:49 Go to previous message
Andy Maleh is currently offline Andy MalehFriend
Messages: 75
Registered: March 2020
Location: Montreal, Quebec, Canada
Member
Gladiator (Glimmer Editor) has been upgraded to take advantage of the new `code_text` custom widget in Glimmer DSL for SWT.

https://raw.githubusercontent.com/AndyObtiva/glimmer-cs-gladiator/master/images/glimmer-gladiator.png


EclipseCon / EclipseWorld / Agile Conference Speaker
Open-Source Software Author of Glimmer DSL for SWT
Previous Topic:How to change SWT Table Item selection background color
Next Topic:ImageData loader with 1bpp images
Goto Forum:
  


Current Time: Sat Apr 20 16:06:25 GMT 2024

Powered by FUDForum. Page generated in 0.04211 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top