TextEdit how to highlight match

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By osne

Hi all, I am in the process of making a console.
I have a scene set up with a LineEdit to get input and two TextEdit readonly
one to output logs and one to output the valid command based on the input string.
Let’s say the list of commands is this “specator, +specate, spec_move_acc, player, +player, player_move_acc” and the input string is this “sp”
I want it to highlight only the macth for each command
In other words the desired result is this using grep to display the desired result
(I used grep a linux command to display the desired result)

This would seem a pretty trivial thing but I can’t find any information on how to achieve it.
P.S. Sorry if this a duplicate question but I couldn’t find my previous one on my questions tab so I think it didn’t sumbit.

:bust_in_silhouette: Reply From: jgodfrey

Maybe someone will prove me wrong, but I’m not sure you can do exactly what you’re after in a TextEdit control. Basic syntax highlighting can be done in two different ways.

  • On specified keywords, via add_keyword_color()
  • On a string of characters found between specified start and stop delimiters via add_color_region()

The add_keyword_color() is close to what you want, but it requires that the defined keywords be complete words (so, surrounded by whitespace).

So, in your example, you could easily define sp as a keyword, but in this string…

special sp cusp hospital

only the lone sp would be highlighted. The others would not since they aren’t surrounded by whitespace characters.

That said, you probably could get the effect you want via a RichTextLabel and bbcode, but it might require significant changes to your current design…