My live syntax highlighter does not work

Wait, how I suppose to use it?

is this in ROBLOX, or some other editor?

My roblox game that highlights code image

try this

local editor = script.Parent
local function format(s, c)
	return "<font color=\"".. c.. "\">".. s.. "</font>"
end
editor.FocusLost:Connect(function()
	local syntax = editor.Text
	for _, v in string.gmatch(string.lower(editor.Text),string.lower("int")) do
		syntax = syntax:gsub("(%W)(".. v.. ")(%W)", function(l, str, r)
			return l..format(str, "#859900")..r
		end)
	end
	editor.Text = syntax
end)
1 Like

Did you turn the property instead the text on?
image

I am still trying to figure out smth. How many times does the for loop run? Only Once, or Multiple times

Yeah I did turn the property on.
image

Oh, I found what the issue was; source: RichText [Live]
Checked one of their examples;

<font color= "rgb(240, 40, 10)">red text</font>

You forgot to use

"rgb("..c..")"

Oh and just to be sure change c to a string with:

"rgb("..tostring(c)..")"

so the “format” function would be:

local function format(s, c)
	return "<font color=\"".. "rgb("..tostring(c)..")".. "\">".. s.. "</font>"
end

Wait a second, you gotta be quotes around a few parts. (Nvm you already did this.)

I don’t think so. I used this person’s post to make this.

Can you show me what you send as the color and text paremeters?
Oh and it might be because you forgot to add Quotes to the s;

return "<font color=\"".. c.. "\">\"".. s.. "\"</font>"

Btw, can you send me what’s the Text is after you used this function once.

nothing, happens.
however if I use it multiple times it will work on all other ones…

Your issue is your gsub pattern.

Replace (%W)(keyword)(%W) with (%W?)(keyword)(%W?)

1 Like

it works. one more question, how can I make it format it after changing the text, because when I tried to, then it would just get into infinite loop.

You should have a seperate TextLabel for the formatted code, instead of editing the TextBox’s text. They can overlay eachother to look as if they’re a single one, they just have seperate text.

Yeah okay so I can see a lot wrong with it; if you don’t put any text in it:

<font color= "#859900"></font>

it won’t do anything and literally use that as the text:
image
when I used it with text: i literally put this inside the text property:

<font color= "#859900">text </font>

then it became:
image

I will try to do that. Give me a minute.

image
It appears that real editor shows additional }. I am not sure why.
oh wait
its because there are missing \n

Make sure all the settings are the exact same on both the textbox and textlabel, otherwise things like that may happen. Double check your font, font size, spacing, etc.

nvm. I fixed the issue by using another textbox instead of textlabel. However more than 3 line breaks have the same issue.
image

That’s due to RichText, which seems to strip extra newlines. I’m really not sure if that behavior is intentional, but I don’t think it should be doing that so you might want to file a bug report.

1 Like