Pixel-Composer/scripts/luaHighlight/luaHighlight.gml

100 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-09-15 20:12:02 +02:00
global.lua_arguments = [];
2023-01-04 02:30:04 +01:00
global.lua_reserved = ds_map_create();
var reserved = ["and", "break", "do", "else", "elseif", "end", "false",
"for", "function", "if", "in", "local", "nil", "not",
"or", "repeat", "return", "then", "true", "until", "while"];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(reserved); i < n; i++ )
2023-01-04 02:30:04 +01:00
global.lua_reserved[? reserved[i]] = 1;
global.CODE_BREAK_TOKEN = [" ", "(", ")", "[", "]", "{", "}", ",", ";", "+", "-", "*", "/", "^", "=", "--"];
2023-09-08 14:51:05 +02:00
2023-09-17 13:23:24 +02:00
function lua_token_splice(str) {
2023-01-04 02:30:04 +01:00
var st = [];
var ss = str;
var sp, cc, del;
2023-01-04 02:30:04 +01:00
do {
sp = 999999;
del = "";
2023-09-08 14:51:05 +02:00
for( var i = 0, n = array_length(global.CODE_BREAK_TOKEN); i < n; i++ ) {
var _del = global.CODE_BREAK_TOKEN[i];
var _pos = string_pos(_del, ss);
if(_pos != 0 && _pos < sp || (_pos == sp && string_length(del) < string_length(_del))) {
sp = _pos;
del = _del;
}
2023-01-04 02:30:04 +01:00
}
if(del == "") { //no delim left
2023-01-04 02:30:04 +01:00
array_push(st, ss);
break;
}
var _ss = string_copy(ss, 1, sp - 1);
array_push(st, _ss);
array_push(st, del);
2023-01-04 02:30:04 +01:00
var dl = string_length(del);
ss = string_copy(ss, sp + dl, string_length(ss) - sp - dl + 1);
2023-01-04 02:30:04 +01:00
} until(sp == 0);
return st;
}
2023-09-07 20:59:14 +02:00
function draw_code_lua(_x, _y, str) {
var tx = _x;
2023-01-04 02:30:04 +01:00
var ty = _y;
2023-03-11 01:40:17 +01:00
var isStr = true;
var strSpl = string_splice(str, "\"");
var amo = array_length(strSpl);
var comment = false;
2023-01-04 02:30:04 +01:00
var word;
for( var i = 0; i < amo; i++ ) {
2023-03-11 01:40:17 +01:00
var _w = strSpl[i];
2023-03-13 10:45:56 +01:00
_w = string_replace_all(_w, "\n", "");
2023-01-09 03:14:20 +01:00
2023-01-04 02:30:04 +01:00
isStr = !isStr;
if(isStr) {
2023-03-11 01:40:17 +01:00
word = "\"" + string(_w);
2023-01-04 02:30:04 +01:00
if(i < amo - 1) word += "\"";
2023-03-11 01:40:17 +01:00
draw_set_color(COLORS.lua_highlight_string);
2023-08-13 13:10:20 +02:00
draw_text_add(tx, ty, word);
2023-01-04 02:30:04 +01:00
tx += string_width(word);
continue;
}
2023-09-17 13:23:24 +02:00
var words = lua_token_splice(_w);
2023-01-04 02:30:04 +01:00
for( var j = 0; j < array_length(words); j++ ) {
word = words[j];
2023-09-07 20:59:14 +02:00
var wordNoS = string_trim(word);
2023-01-04 02:30:04 +01:00
if(wordNoS == "//") comment = true;
2023-01-04 02:30:04 +01:00
draw_set_color(COLORS._main_text);
if(comment)
draw_set_color(COLORS.lua_highlight_comment);
else if(word == "(" || word == ")" || word == "[" || word == "]" || word == "{" || word == "}")
2023-01-04 02:30:04 +01:00
draw_set_color(COLORS.lua_highlight_bracklet);
else if(ds_map_exists(global.lua_reserved, word))
draw_set_color(COLORS.lua_highlight_keyword);
else if(wordNoS == string_decimal(wordNoS))
draw_set_color(COLORS.lua_highlight_number);
else if(j < array_length(words) - 1) {
if(words[j + 1] == "(")
draw_set_color(COLORS.lua_highlight_function);
}
2023-08-13 13:10:20 +02:00
draw_text_add(tx, ty, word);
2023-01-04 02:30:04 +01:00
tx += string_width(word);
}
}
}