Hello, i was needing a specific use for a syntax highlighter, that allowed me to create a small footprint syntax highlighter, without using a ton of third party scripts. I thought someone else might find it useful, so i am going to post it here. All it does it let you syntax highlight lua in a textbox, by formating a textlabel with richtext format.
Screenshot of the layout
This is what it looks like before you play(so dont freak out if it doesnt work until you click play)
Here is the source:
local frame = script.Parent
local tbtext = frame:WaitForChild("TBText")
local tbsyntax = frame:WaitForChild("TLSyntax")
local syntaxPatterns = {}
local localvars = {}
local registeredVars = {
"script",
"game",
"Game",
"Vector3",
"CFrame",
"_G",
"global",
"shared",
"Color3",
"UDim2",
}
local registeredFunctions = {
"wait"
}
-- Function to register syntax patterns
function register(pattern, replacement)
syntaxPatterns[#syntaxPatterns + 1] = {pattern, replacement}
end
-- Function to register local variables
function registerLocalVariable(var)
localvars[#localvars + 1] = var
end
-- Register syntax patterns
register("(local%s+)([a-zA-Z0-9]+)", '<font color="rgb(255,124,126)">%1</font><font color="rgb(255,255,255)">%2</font>')
register("(function%s+)([a-zA-Z0-9]+)", '<font color="rgb(255,124,126)">%1</font><font color="rgb(255,255,127)"><u>%2</u></font>')
register("(function%s*)", '<font color="rgb(255,124,126)">%1</font>')
register("(end%s*)", '<font color="rgb(255,124,126)">%1</font>')
-- Register string and number patterns
--register("(\".-\")", '<font color="rgb(255,255,127)">%1</font>')
--register("(%b+-?%d+%.?%d*)", '<font color="rgb(255,200,200)">%1</font>')
register("(%-%-[^\n]*)", '<font color="rgb(0,255,0)">%1</font>') -- Single-line comment
register("(%-%-%[%[.-%]%])", '<font color="rgb(0,255,0)">%1</font>') -- Multi-line comment
-- Function to escape angle brackets
local function ReplaceAngleBrackets(source)
source = string.gsub(source, "<", "<")
source = string.gsub(source, ">", ">")
return source
end
-- Function to get local variables from a scriptp
local function GetLocalVariables(scr)
local variables = {}
for line in scr:gmatch("[^\n]+") do
local var = line:match("local%s+([%a_][%w_]*)")
if var then
table.insert(variables, var)
end
end
return variables
end
-- Function to highlight syntax
local function HighlightSyntax(source)
source = ReplaceAngleBrackets(source)
localvars = GetLocalVariables(source)
-- Apply syntax highlighting for known patterns
for _, _pat in pairs(syntaxPatterns) do
local pattern, repl = _pat[1], _pat[2]
source = string.gsub(source, pattern, repl)
end
-- Highlight registered variables
for _, _var in pairs(registeredVars) do
local pattern = "(%s*)(%b".._var..")(%s*)"
local repl = "%1<font color=\"rgb(85,255,127)\">%2</font>%3"
source = string.gsub(source, pattern, repl)
end
-- Highlight registered functions
for _, _var in pairs(registeredFunctions) do
local pattern = "(%s*)(%b".._var..")(%s*)"
local repl = "%1<font color=\"rgb(0,85,255)\">%2</font>%3"
source = string.gsub(source, pattern, repl)
end
return source
end
-- Function to update the syntax-highlighted text
local function Update()
tbsyntax.Text = HighlightSyntax(tbtext.Text)
end
-- Connect the Update function to the PropertyChanged signal of tbtext
tbtext:GetPropertyChangedSignal("Text"):Connect(Update)
-- Initial update to highlight syntax
Update()
-- Example test variable
local test = Color3.new(1, 0.486275, 0.494118)