My live syntax highlighter does not work

image

I am not sure why my syntax highlighter does not highlight the first int keyword.

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 pairs({"int"}) do
		syntax = syntax:gsub("(%W)(".. v.. ")(%W)", function(l, str, r)
			return l..format(str, "#859900")..r
		end)
	end
	editor.Text = syntax
end)

I am using RichText feature

4 Likes

could you elaborate on your problem?

1 Like

Well, it does not highlight all int words. As you can see, it only highlights the second int word.

this only goes through a single element

perhaps use for v in string.gmatch(editor.Text, "int") ?

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 [TextScaled Support Added]
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.