Howdy! I have been using LiteXL for a while now as a daily driver and recently I wanted to add syntax highlighting to it in the way that Roblox Studio does it. Lite is written in Lua so you can extend the editor with the Lua language.
What I am trying to do is have LiteXL syntax highlight code to the right of . characters. Similar to how Roblox does this with properties for example, or table indexes.
Does anyone know of a string pattern that could achieve this? I have tried multiple the closest I got was. The problem with this is it highlights the period and also if you have a function call at the end it will also highlight that. Which over writes the default function syntax.
"%.%w+"
I know this pattern matching stuff can get really complex so I just figured id ask for some help. Thank you and have a wonderful day!
Example of what I am trying to filter.
List.Hello.World.run()
I want the worlds Hello and World to be highlighted.
That doesn’t fit with what you said before. run is also to the right of a ‘.’, so why not highlight run?
Anyway, I don’t think this is possible using a single string.match call, because such a pattern cannot be represented using Lua string patterns. Specifically, string patterns cannot capture a repetition of a sequence. E.g. this stackoverflow thread, or a simpler example is just capturing repetitions of “ab”.
IMO your best bet is to use a proper RegEx library.
local function tokenise(str,token)
local tokens = {};
local target = string.byte(token);
local first = 1;
for idx = 1, #str do
local byte = str:byte(idx);
if byte == target then
tokens[table.getn(tokens)+1] = string.sub(str,first,idx-1);
first = idx+1;
end
end
-- make sure we ALWAYS add the last token!
tokens[table.getn(tokens)+1] = string.sub(str,first,#str);
return tokens;
end
local store = {"List"};
local words = {"Hello","World"};
local func = {"run()"};
local tokens = tokenise("List.Hello.World.run()",'.');
for _, token in (tokens) do
if table.find(store,token) then
print(token);
elseif table.find(words,token) then
warn(token);
elseif table.find(func,token) then
print(token);
end
end