# Problems with creating my own syntax highlighter

**URL:** <https://www.ruby-forum.com/t/problems-with-creating-my-own-syntax-highlighter/118187>\
**Category:** wxRuby\
**Created:** [October 24, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/problems-with-creating-my-own-syntax-highlighter/118187 "2007-10-24T17:00:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Bielert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/41988e/32.png) [@Christian\_Bielert](https://www.ruby-forum.com/u/Christian_Bielert)\
**Post date:** [October 24, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/problems-with-creating-my-own-syntax-highlighter/118187/1 "2007-10-24T17:00:18Z")

</div>

Well, I wanted to make my own syntax highlighter using  
Wx::StyledTextCtrl.  
The easiest way to do it seemed to parse the text contents in a  
content-changed-event. But it doesn’t seem to work…

I tried to edit the scintilla sample and added this event(@sci is the  
StyledTextCtrl):

```
@sci.evt_stc_change(@sci.get_id) do
    @sci.clear_document_style
    @sci.start_styling(1,31)
    @sci.style_set_font_attr(10, 10, "Courier", true, true, true)
    @sci.set_styling(2,10)
    puts 1
end

```

This prints out “1” when you edit the text(twice actually, which i do  
not  
understand), but doesn’t change the text layout. But if I put it in an  
event  
triggered by a button, it will work(onEOL is triggered by clicking the  
“show  
end of line” button in the scintilla sample):

def onEOL  
@sci.clear\_document\_style  
@sci.start\_styling(1,31)  
@sci.style\_set\_font\_attr(10, 10, “Courier”, true, true, true)  
@sci.set\_styling(2,10)  
puts 1  
end

As soon as I click the button, the second and third character get  
styled. I  
don’t get why it doesn’t work in the text content changed event. What  
happens there, is it intended not to work? What is the best way to get  
it to  
work?

---

<div class="post-metadata">

**Author:** ![Christian\_Bielert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/41988e/32.png) [@Christian\_Bielert](https://www.ruby-forum.com/u/Christian_Bielert)\
**Post date:** [October 24, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/problems-with-creating-my-own-syntax-highlighter/118187/2 "2007-10-24T21:51:35Z")

</div>

Christian Bielert wrote:

> Well, I wanted to make my own syntax highlighter using  
> Wx::StyledTextCtrl. The easiest way to do it seemed to parse the text  
> contents in a content-changed-event. But it doesn’t seem to work…  
> I would have expected the code you posted to work too, but the following  
> seems to work for me (Linux/GTK), hooking into evt\_stc\_updateui

```
@sci.style_set_font_attr(10, 10, "Courier", true, true, true)
@sci.evt_stc_updateui(@sci) do | e |
  @sci.clear_document_style
  @sci.start_styling(2,31)
  @sci.set_styling(5,10)
  @sci.set_save_point
end

```

style\_set\_xxx only needs to be called once per style, per control. It  
seems to be a reasonably expensive call so putting it outside the loop  
got rid of any lag for me.

There are a few odd things going on with StyledTextEvent - from the  
Scintilla docs evt\_stc\_change should only get called when the text is  
modifed, but it seems to be getting called repeatedly. And  
StyledTextEvent should inherit from Wx::Event, but it currently inherits  
from Wx::Control (!). I’ll look into those and hopefully fix something  
up for the next release.

Thanks for the report.

cheers  
alex
