• 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax Highlightning in tuiTextBox
Syntax Highlightning (or any other formatting of the text) in NTUI is called "styling".
This is how it works for now (Function names will change slightly in final NTUI), since don't want to expose newtypes.

Function.l myStyleCallback{*textBox.tuiTextBox,lpos.l,*tline.tline}

*tline\sclength = 0 ; reset the style information
cpos.l = 0 ; start at character position 0

While cpos<*tline\clength ; go through the whole line
c.b = Peek.b(*tline\text+cpos) : cpos+1 ; grab a character

Select c
Case @"a" ; if we see an "a"...

; make it bold and red

; and return to normal

; nothing happens for other characters

End Select


*tline\flags|#TUITBLF_STYLED ; mark the line as styled
Function Return #TUISCB_DONE ; tell NTUI we are done

End Function
; this creates a global variable "*myStyleCallback" that points to our functions
; and tells AmiBlitz3 not to remove the function, since it is never called directly

ntui_SetAttr{*textBox,#TUITBA_STYLECB,*myStyleCallback} ; attach the callback function to the textbox
ntui_SetAttr{*textBox,#TUITBA_STYLECB,Null} ; remove the callback function from the textbox[/ab3]

There are a few things to mention:
1. The example makes all "a"'s in the text bold and red.

2. You don't have to remove the callback when you free the textBox. This is only necessary if you want to make sure it will never called again, e.g. if your style callback allocated resources that you want to free before NTUI.

3. Between the label "myStyleCallback:" and the function declaration MUST NOT be any code.

4. It is allowed to write to the text buffer (Poke), but the length MUST be preserved. E.g. Casing can be altered. Note that you are actually changing the text buffer. If you just want to just format for display, you need "Style Commands" for this.

5. There are many "Style Commands", not only bold and colors. See TUITBSC_... for more. New commands can be easily added if necessary.

6. To style the text, you don't need a StyleCallback. You can also go through the whole text and style it. However, the style callback has so many advantages that you really want to use it. E.g., you don't need to parse the whole text, only the visible lines (=much faster). It also keeps track while editing the text line.

7. If you parse a line and the result may affect the next or previous line (e.g. multi-line comment), you need to return #TUISCB_NEXT or #TUISCB_PREV instead of #TUISCB_DONE. You can store the parser state as flags in *tline\flags. There are currently two flags, #TUITBLF_INCOMMENT and #TUITBLF_INTAG.

8. If the highlightning is more complex that just tracking a character, I highly recommend using a stack-based parser rather than a state machine.
99% of all programming languages can be parsed by a stack based parser with lookahead of 1 or 2 characters. I can give an example if there is interest.

Nachrichten in diesem Thema

Gehe zu:

Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste