« MBS Xojo Developer Co… | Home | MBS Xojo Plugins, ver… »

Custom styles in Scintilla Control

While we have 100+ lexers to colorize your text in ScintillaControlMBS control, you may sometimes need a custom solution. If none of our existing lexers suits you, you can do it all yourself in Xojo.

You would implement StyleNeeded event to provide coloring for a range of text. You get the position where the action happened, e.g. user typed a few letters. Then you query EndStyled property to know when existing style information ended. Since you are asked usually in chunks, you may start where you finished recently. From there we usually go back to the start of the line, so we query the line with LineFromPosition and the start of the line with PositionFromLine. Then we colorize from that position to the current one.

Scintilla with custom lexer

Here is a sample code to assign style 2 to digits, while keeping everything else untouched:

EventHandler Sub StyleNeeded(Position as Integer) #pragma BackgroundTasks false Dim startPos As Integer = Me.EndStyled Dim lineNumber As Integer = Me.LineFromPosition(startPos) Dim LineStartPos As Integer = Me.PositionFromLine(lineNumber) System.DebugLog "Style at "+Position.ToString+" from "+LineStartPos.ToString+" to "+position.ToString Dim t As String = Me.Text Dim pos As Integer = LineStartPos While pos < position Dim ch As Integer = t.Middle(pos, 1).Asc If ch >= 48 And ch <= 57 Then // digit Me.SetStyling(pos, 1, Me.Style(2)) end if pos = pos + 1 Wend End EventHandler

Your StyleNeeded event needs be quick to perform well. It is called after each key press to color the current line. And it may need to color the visible range, if the user scrolls. To improve performance, we use #pragma BackgroundTasks to speed up a bit.

We also have a Xojo lexer built-in the plugin:

Scintilla with Xojo lexer

Please try this with the next pre-release version of our plugin (24.0pr7 or later) due to a little bug fix coming in there.

The biggest plugin in space...
07 01 24 - 10:17