Problems with creating my own syntax highlighter

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?

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