- [Text area] Add syntax highlighting for single line comment.

This commit is contained in:
Tanasart 2023-11-07 18:07:17 +07:00
parent ee7d0901dc
commit 043bc5eba9
4 changed files with 49 additions and 33 deletions

Binary file not shown.

View file

@ -15,33 +15,38 @@ var constant = ["MATRIX_VIEW", "MATRIX_PROJECTION", "MATRIX_WORLD", "MATRIX_WORL
for( var i = 0, n = array_length(constant); i < n; i++ )
global.glsl_constant[? constant[i]] = 1;
global.HLSL_BREAK_TOKEN = [" ", "(", ")", "[", "]", "{", "}", ".", ",", ";", "+", "-", "*", "/", "^", "="];
global.HLSL_BREAK_TOKEN = [" ", "(", ")", "[", "]", "{", "}", ".", ",", ";", "+", "-", "*", "/", "^", "=", "//"];
function hlsl_token_splice(str) {
var st = [];
var ss = str;
var sp;
var cc;
var sp, cc, del;
do {
sp = 999999;
del = "";
for( var i = 0, n = array_length(global.HLSL_BREAK_TOKEN); i < n; i++ ) {
var _pos = string_pos(global.HLSL_BREAK_TOKEN[i], ss);
if(_pos != 0) sp = min(sp, _pos);
var _del = global.HLSL_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;
}
}
if(sp == 999999) { //no delim left
if(del == "") { //no delim left
array_push(st, ss);
break;
}
var _ss = string_copy(ss, 1, sp - 1);
array_push(st, _ss);
array_push(st, del);
cc = string_char_at(ss, sp);
array_push(st, cc);
ss = string_copy(ss, sp + 1, string_length(ss) - sp);
var dl = string_length(del);
ss = string_copy(ss, sp + dl, string_length(ss) - sp - dl + 1);
} until(sp == 0);
return st;
@ -51,14 +56,19 @@ function draw_code_hlsl(_x, _y, str) {
var tx = _x;
var ty = _y;
var words = hlsl_token_splice(str);
var comment = false;
for( var j = 0; j < array_length(words); j++ ) {
var word = words[j];
var wordNoS = string_trim(word);
if(wordNoS == "//") comment = true;
draw_set_color(COLORS._main_text);
if(word == "(" || word == ")" || word == "[" || word == "]" || word == "{" || word == "}")
if(comment)
draw_set_color(COLORS.lua_highlight_comment);
else if(word == "(" || word == ")" || word == "[" || word == "]" || word == "{" || word == "}")
draw_set_color(COLORS.lua_highlight_bracklet);
else if(ds_map_exists(global.glsl_reserved, word))
draw_set_color(COLORS.lua_highlight_keyword);

View file

@ -8,33 +8,38 @@ var reserved = ["and", "break", "do", "else", "elseif", "end", "false",
for( var i = 0, n = array_length(reserved); i < n; i++ )
global.lua_reserved[? reserved[i]] = 1;
global.CODE_BREAK_TOKEN = [" ", "(", ")", "[", "]", "{", "}", ",", ";", "+", "-", "*", "/", "^", "="];
global.CODE_BREAK_TOKEN = [" ", "(", ")", "[", "]", "{", "}", ",", ";", "+", "-", "*", "/", "^", "=", "--"];
function lua_token_splice(str) {
var st = [];
var ss = str;
var sp;
var cc;
var sp, cc, del;
do {
sp = 999999;
del = "";
for( var i = 0, n = array_length(global.CODE_BREAK_TOKEN); i < n; i++ ) {
var _pos = string_pos(global.CODE_BREAK_TOKEN[i], ss);
if(_pos != 0) sp = min(sp, _pos);
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;
}
}
if(sp == 999999) { //no delim left
if(del == "") { //no delim left
array_push(st, ss);
break;
}
var _ss = string_copy(ss, 1, sp - 1);
array_push(st, _ss);
array_push(st, del);
cc = string_char_at(ss, sp);
array_push(st, cc);
ss = string_copy(ss, sp + 1, string_length(ss) - sp);
var dl = string_length(del);
ss = string_copy(ss, sp + dl, string_length(ss) - sp - dl + 1);
} until(sp == 0);
return st;
@ -47,6 +52,7 @@ function draw_code_lua(_x, _y, str) {
var isStr = true;
var strSpl = string_splice(str, "\"");
var amo = array_length(strSpl);
var comment = false;
var word;
for( var i = 0; i < amo; i++ ) {
@ -71,8 +77,12 @@ function draw_code_lua(_x, _y, str) {
word = words[j];
var wordNoS = string_trim(word);
if(wordNoS == "//") comment = true;
draw_set_color(COLORS._main_text);
if(word == "(" || word == ")" || word == "[" || word == "]" || word == "{" || word == "}")
if(comment)
draw_set_color(COLORS.lua_highlight_comment);
else if(word == "(" || word == ")" || word == "[" || word == "]" || word == "{" || word == "}")
draw_set_color(COLORS.lua_highlight_bracklet);
else if(ds_map_exists(global.lua_reserved, word))
draw_set_color(COLORS.lua_highlight_keyword);

View file

@ -580,16 +580,12 @@ function textArea(_input, _onModify) : textInput(_input, _onModify) constructor
draw_text_path(ch_x, ch_y, _str);
break;
case TEXT_AREA_FORMAT.codeLUA :
if(syntax_highlight)
draw_code_lua(ch_x, ch_y, _str);
else
draw_text_add(ch_x, ch_y, _str);
if(syntax_highlight) draw_code_lua(ch_x, ch_y, _str);
else draw_text_add(ch_x, ch_y, _str);
break;
case TEXT_AREA_FORMAT.codeHLSL :
if(syntax_highlight)
draw_code_hlsl(ch_x, ch_y, _str);
else
draw_text_add(ch_x, ch_y, _str);
if(syntax_highlight) draw_code_hlsl(ch_x, ch_y, _str);
else draw_text_add(ch_x, ch_y, _str);
break;
}