1
0
mirror of https://github.com/anthonyaxenov/lite-xl-env-syntax.git synced 2024-11-24 22:24:56 +00:00

Improved string values, some refactorings

This commit is contained in:
Anthony Axenov 2022-06-17 08:21:57 +08:00
parent 828b42cee6
commit 36ccc3e434
Signed by: anthony
GPG Key ID: EA9EC32FF7CCD4EC
3 changed files with 194 additions and 180 deletions

View File

@ -8,7 +8,7 @@ Unfortunately, in fact `env`-file syntax is more complicated than it seems, so c
My goal is to do so somehow in future but I think plugin is already good enought to use in most of basic cases.
Currently available highlighting:
* line and inline comments;
* comments;
* keys;
* `export`;
* values:
@ -20,14 +20,8 @@ Currently available highlighting:
* string interpolation `${myvar}`;
TODO and known issues:
* unquoted string values are not highlighted as string;
* quotes inside unquoted strings are not escaped (highlighted substring inside not highlighted string);
* in unquoted string values, word + `=` are showed as inlined key;
* inside any quoted strings interpolations and literals are not highlighted;
* numeric values containing more than 1 decimal separator are not highlighted as string;
* literals used as keys must be highlighted as keys, not literals;
* quoted strings used as keys must be highlighted as keys, not strings;
* escaped literals must not be used keys;
* escaped literals must not be used as keys;
* ...
You can use `test.env` file to check how plugin works.

View File

@ -5,39 +5,67 @@
local syntax = require "core.syntax"
local key_pattern = '[a-zA-Z_]+[a-zA-Z0-9_]*'
local unicode_sequence = "'?\\u%x%x%x%x'?"
local escaped_literals = "\\[nrtfb\\\"']"
local null_pattern = '%s*[Nn][Uu][Ll][Ll]%s*'
local true_pattern = '%s*[Tt][Rr][Uu][Ee]%s*'
local false_pattern = '%s*[Ff][Aa][Ll][Ss][Ee]%s*'
local synt_key = {
symbols = {},
patterns = {
{ type = "keyword2", pattern = key_pattern },
},
}
local synt_dqs = {
symbols = {},
patterns = {
{ pattern = { "%${", "}" }, type = "keyword", syntax = synt_key },
{ pattern = "0[bB][%d]+", type = "number" },
{ pattern = "0[xX][%da-fA-F]+", type = "number" },
{ pattern = "[-+]?%.?%d+", type = "number" },
{ pattern = escaped_literals, type = "literal" }, -- escaped chars
{ pattern = unicode_sequence, type = "literal" }, -- unicode sequence
{ pattern = '[%w%p%s]', type = "string" },
},
}
syntax.add {
name = "language_env",
files = { "%.env$" },
comment = '#',
symbols = {},
patterns = {
{type = "comment" , pattern = "#.*$"},
{type = "function", pattern = "export"},
{type = "literal", pattern = "[Nn][Uu][Ll][Ll]%s*"}, -- null
{type = "literal", pattern = "[Tt][Rr][Uu][Ee]%s*"}, -- true
{type = "literal", pattern = "[Ff][Aa][Ll][Ss][Ee]%s*"}, -- false
{type = "literal", pattern = "\\[nrtfb\\\"']"}, -- escaped chars
{type = "literal", pattern = "'\\u%x%x%x%x'"}, -- unicode sequence
-- quoted strings
{type = "string", pattern = {'"', '"', '\\'}},
{type = "string", pattern = {"'", "'", '\\'}},
{type = "string", pattern = {'"""', '"""', '\\'}},
{type = "string", pattern = {"'''", "'''", '\\'}},
{ pattern = "#.*$", type = "comment" },
{ pattern = "export", type = "function" },
{ pattern = null_pattern, type = "literal" },
{ pattern = true_pattern, type = "literal" },
{ pattern = false_pattern, type = "literal" },
{ pattern = escaped_literals, type = "literal" },
{ pattern = unicode_sequence, type = "literal" },
-- interpolation
{ pattern = { "%${", "}" }, type = "keyword", syntax = synt_key },
-- numbers
{type = "number", pattern = "0[bB][%d]+"},
{type = "number", pattern = "0[xX][%da-fA-F]+"},
{type = "number", pattern = "[-+]?%.?%d+"},
{ -- keys
pattern = "[a-zA-Z_]+[a-zA-Z%d_]+()%s*=%s*",
type = { "keyword2", "operator" },
},
{ -- interpolated var
pattern = "%${()[a-zA-Z_]+[a-zA-Z%d_]+()}",
type = {"keyword", "keyword2", "keyword"},
},
{ pattern = "0[bB][%d]+", type = "number" },
{ pattern = "0[xX][%da-fA-F]+", type = "number" },
{ pattern = "[-+]?%.?%d+", type = "number" },
-- keys
{ pattern = '[\'"].*[\'"]%s*=.*', type = "normal" },
-- {
-- pattern = '[\'"]?'..escaped_literals..'[\'"]?%s*=.*',
-- type = "normal"
-- },
-- {
-- pattern = '[\'"]?'..null_pattern..'[\'"]?%s*=.*',
-- type = "normal"
-- },
{ pattern = key_pattern..'%s*()=%s*', type = { "keyword2", "operator" }},
-- quoted strings
{ pattern = {'"', '"', '\\'}, type = "string", syntax = synt_dqs},
{ pattern = {"'", "'", '\\'}, type = "string" },
{ pattern = {'"""', '"""', '\\'}, type = "string" },
{ pattern = {"'''", "'''", '\\'}, type = "string" },
},
}

282
test.env
View File

@ -1,6 +1,6 @@
# https://lite-xl.com/?/tutorials/syntax/highlighting
# https://www.lua.org/manual/5.3/manual.html#6.4.1
# https://hexdocs.pm/dotenvy/dotenv-file-format.html
# https://www.lua.org/manual/5.4/manual.html#6.4.1
# https://hexdocs.pm/dotenvy/dotenv-file-format.html"
#########################################################
lorem ipsum dolor sit amet
@ -62,146 +62,7 @@ FaLSE=FaLSE
FAlSE =FAlSE
FALSe= FALSe
fALSE = fALSE
#########################################################
string_no_quotes=string value
string_no_quotes =string value
string_no_quotes= string value
string_no_quotes = string value
string_in_quotes1='string value'
string_in_quotes1 ='string value'
string_in_quotes1= 'string value'
string_in_quotes1 = 'string value'
string_in_quotes2="string value"
string_in_quotes2 ="string value"
string_in_quotes2= "string value"
string_in_quotes2 = "string value"
#########################################################
quotes1_inside_string_no_quotes=string'quotes'value
quotes1_inside_string_no_quotes =string 'quotes'value
quotes1_inside_string_no_quotes= string'quotes' value
quotes1_inside_string_no_quotes = string 'quotes' value
esc_quotes1_inside_string_no_quotes=string\'quotes\'value
esc_quotes1_inside_string_no_quotes =string \'quotes\'value
esc_quotes1_inside_string_no_quotes= string\'quotes\' value
esc_quotes1_inside_string_no_quotes = string \'quotes\' value
quotes2_inside_string_no_quotes=string"quotes"value
quotes2_inside_string_no_quotes =string "quotes"value
quotes2_inside_string_no_quotes= string"quotes" value
quotes2_inside_string_no_quotes = string "quotes" value
esc_quotes2_inside_string_no_quotes=string\"quotes\"value
esc_quotes2_inside_string_no_quotes =string \"quotes\"value
esc_quotes2_inside_string_no_quotes= string\"quotes\" value
esc_quotes2_inside_string_no_quotes = string \"quotes\" value
#########################################################
string_no_quotes_and_literals=true started this string
string_no_quotes_and_literals =false started th=is string
string_no_quotes_and_literals= null started this string
string_no_quotes_and_literals = \n started this string
string_no_quotes_and_literals=now true inside string
string_no_quotes_and_literals =now false inside string
string_no_quotes_and_literals= now null inside string
string_no_quotes_and_literals = now \n inside string
string_no_quotes_and_literals=string and null
string_no_quotes_and_literals =string and true
string_no_quotes_and_literals= string and false
string_no_quotes_and_literals = string and \n
#########################################################
string_in_quotes1_and_literals='true started this string'
string_in_quotes1_and_literals ='false started this string'
string_in_quotes1_and_literals= 'null started this string'
string_in_quotes1_and_literals = '\n started this string'
string_in_quotes1_and_literals='now true inside string'
string_in_quotes1_and_literals ='now false inside string'
string_in_quotes1_and_literals= 'now null inside string'
string_in_quotes1_and_literals = 'now \n inside string'
string_in_quotes1_and_literals='string and null'
string_in_quotes1_and_literals ='string and true'
string_in_quotes1_and_literals= 'string and false'
string_in_quotes1_and_literals = 'string and \n'
#########################################################
string_in_quotes2_and_literals="true started this string"
string_in_quotes2_and_literals ="false started this string"
string_in_quotes2_and_literals= "null started this string"
string_in_quotes2_and_literals = "\n started this string"
string_in_quotes2_and_literals="now true inside string"
string_in_quotes2_and_literals ="now false inside string"
string_in_quotes2_and_literals= "now null inside string"
string_in_quotes2_and_literals = "now \n inside string"
string_in_quotes2_and_literals="string and null"
string_in_quotes2_and_literals ="string and true"
string_in_quotes2_and_literals= "string and false"
string_in_quotes2_and_literals = "string and \n"
#########################################################
STRING="just string"
STRING ="just string"
STRING= "just string"
STRING = "just string"
#########################################################
STRING=string #comment
STRING =string #comment
STRING= string # comment
STRING = string # comment
#########################################################
STRING='#string'#comment
STRING ='#string' #comment
STRING= '#string'# comment
STRING = '#string' # comment
#########################################################
STRING="#string"#comment
STRING ="#string" #comment
STRING= "#string"# comment
STRING = "#string" # comment
#########################################################
escaped_specials=\n\r\t\f\b\\\'\"
escaped_specials =\n\r\t\f\b\\\'\"
escaped_specials= \n\r\t\f\b\\\'\"
escaped_specials = \n\r\t\f\b\\\'\"
escaped_specials=\r\t\f\b\n\\\'\"
escaped_specials =\r\t\f\b\n\\\'\"
escaped_specials= \r\t\f\b\n\\\'\"
escaped_specials = \r\t\f\b\n\\\'\"
\n\r\t\f\b\\\'\" = baaad
#########################################################
q_escaped_specials='\n\r\t\f\b\\\'\"'
q_escaped_specials ='\n\r\t\f\b\\\'\"'
q_escaped_specials= '\n\r\t\f\b\\\'\"'
q_escaped_specials = '\n\r\t\f\b\\\'\"'
#########################################################
q_escaped_specials="\n\r\t\f\b\\\'\""
q_escaped_specials ="\n\r\t\f\b\\\'\""
q_escaped_specials= "\n\r\t\f\b\\\'\""
q_escaped_specials = "\n\r\t\f\b\\\'\""
"string"=baaad
#########################################################
string_with_keys=key=key=key=key=
string_with_keys =key=sfd =key=k asdf ey=
string_with_keys= key=key=key=key=
string_with_keys = key=key=key=key=
#########################################################
interpolated=${MYVAR} #comment
interpolated= ${MYVAR}
interpolated=${MYVAR}@example.com
interpolated= ${MYVAR}@example.com
interpolated="${MYVAR}@example.com"
interpolated="This, that, ${MYVAR} and something else"
interpolated="This, that and ${MYVAR}"
interpolated_multiline="""
${MYVAR} lorem ipsum
dolor ${MYVAR} sit
amet ${MYVAR}
"""
#########################################################
not_interpolated='${MYVAR}, this and that'
not_interpolated='This, that, ${MYVAR} and something else'
not_interpolated='This, that and ${MYVAR}'
not_interpolated= 'This, that, ${MYVAR} and something else'
not_interpolated= 'This, that and ${MYVAR}' #comment
not_interpolated_multiline='''
${MYVAR} lorem ipsum
dolor ${MYVAR} sit
amet ${MYVAR}
'''
#########################################################
######################################################### OK
number=1025
number =-1025
number= +1025
@ -229,6 +90,137 @@ number_with_2_delimiters=+1025.1234.
number_with_2_delimiters= .123.
number_with_2_delimiters= -.1234.
number_with_2_delimiters=+.1234.
#########################################################
######################################################### OK
string_no_quotes=string value
string_no_quotes =string value
string_no_quotes= string value
string_no_quotes = string value
string_in_quotes1='string value'
string_in_quotes1 ='string value'
string_in_quotes1= 'string value'
string_in_quotes1 = 'string value'
string_in_quotes2="string value"
string_in_quotes2 ="string value"
string_in_quotes2= "string value"
string_in_quotes2 = "string value"
######################################################### IDK
quotes1_inside_string_no_quotes=string'quotes'value
quotes1_inside_string_no_quotes =string 'quotes'value
quotes1_inside_string_no_quotes= string'quotes' value
quotes1_inside_string_no_quotes = string 'quotes' value
esc_quotes1_inside_string_no_quotes=string\'quotes\'value
esc_quotes1_inside_string_no_quotes =string \'quotes\'value
esc_quotes1_inside_string_no_quotes= string\'quotes\' value
esc_quotes1_inside_string_no_quotes = string \'quotes\' value
quotes2_inside_string_no_quotes=string"quotes"value
quotes2_inside_string_no_quotes =string "quotes"value
quotes2_inside_string_no_quotes= string"quotes" value
quotes2_inside_string_no_quotes = string "quotes" value
esc_quotes2_inside_string_no_quotes=string\"quotes\"value
esc_quotes2_inside_string_no_quotes =string \"quotes\"value
esc_quotes2_inside_string_no_quotes= string\"quotes\" value
esc_quotes2_inside_string_no_quotes = string \"quotes\" value
######################################################### OK
string_no_quotes_and_literals=true started this string
string_no_quotes_and_literals =false started this string
string_no_quotes_and_literals= null started this string
string_no_quotes_and_literals = \n started this string
string_no_quotes_and_literals=now true inside string
string_no_quotes_and_literals =now false inside string
string_no_quotes_and_literals= now null inside string
string_no_quotes_and_literals = now \n inside string
string_no_quotes_and_literals=string and null
string_no_quotes_and_literals =string and true
string_no_quotes_and_literals= string and false
string_no_quotes_and_literals = string and \n
######################################################### OK
string_in_quotes1_and_literals='true started this string'
string_in_quotes1_and_literals ='false started this string'
string_in_quotes1_and_literals= 'null started this string'
string_in_quotes1_and_literals = '\n started this string'
string_in_quotes1_and_literals='now true inside string'
string_in_quotes1_and_literals ='now false inside string'
string_in_quotes1_and_literals= 'now null inside string'
string_in_quotes1_and_literals = 'now \n inside string'
string_in_quotes1_and_literals='string and null'
string_in_quotes1_and_literals ='string and true'
string_in_quotes1_and_literals= 'string and false'
string_in_quotes1_and_literals = 'string and \n'
######################################################### OK
string_in_quotes2_and_literals="true started this string"
string_in_quotes2_and_literals ="false started this string"
string_in_quotes2_and_literals= "null started this string"
string_in_quotes2_and_literals = "\n started this string"
string_in_quotes2_and_literals="now true inside string"
string_in_quotes2_and_literals ="now false inside string"
string_in_quotes2_and_literals= "now null inside string"
string_in_quotes2_and_literals = "now \n inside string"
string_in_quotes2_and_literals="string and null"
string_in_quotes2_and_literals ="string and true"
string_in_quotes2_and_literals= "string and false"
string_in_quotes2_and_literals = "string and \n"
######################################################### OK
unquoted=string #comment
unquoted =string #comment
unquoted= string # comment
unquoted = string # comment
######################################################### OK
single_quoted='#string'#comment
single_quoted ='#string' #comment
single_quoted= '#string'# comment
single_quoted = '#string' # comment
######################################################### OK
dounble_quoted="#string"#comment
dounble_quoted ="#string" #comment
dounble_quoted= "#string"# comment
dounble_quoted = "#string" # comment
######################################################### OK
escaped_specials=\n\r\t\f\b\\\'\"
escaped_specials =\n\r\t\f\b\\\'\"
escaped_specials= \n\r\t\f\b\\\'\"
escaped_specials = \n\r\t\f\b\\\'\"
######################################################### TODO
\n\r\t\f\b\\\'\" = baaad
######################################################### OK
sq_escaped_specials='\n\r\t\f\b\\\'\"'
sq_escaped_specials ='\n\r\t\f\b\\\'\"'
sq_escaped_specials= '\n\r\t\f\b\\\'\"'
sq_escaped_specials = '\n\r\t\f\b\\\'\"'
######################################################### OK
dq_escaped_specials="\n\r\t\f\b\\\'\""
dq_escaped_specials ="\n\r\t\f\b\\\'\""
dq_escaped_specials= "\n\r\t\f\b\\\'\""
dq_escaped_specials = "\n\r\t\f\b\\\'\""
"quoted_string_as_key"=baaad
'quoted_string_as_key'=baaad
"quoted_string_as_key"=baaad
'quoted_string_as_key'=baaad
######################################################### TODO
string_with_keys=baaad=baaad=baaad=baaad=
string_with_keys =baaad=baaad =baaad=k asdf baaad=
string_with_keys= baaad=baaad=baaad=baaad=
string_with_keys = baaad=baaad=baaad=baaad=
######################################################### OK
interpolated=${MYVAR} #comment
interpolated= ${MYVAR}
interpolated=${MYVAR}@example.com
interpolated= ${MYVAR}@example.com
interpolated="${MYVAR}@example.com"
interpolated="This, that, ${MYVAR}, \n, 654.00, '\u0000' something else"
interpolated="This, \u0000, that, \t and ${MYVAR} "
interpolated_multiline="""
${MYVAR} lorem ipsum
dolor ${MYVAR} sit\t #notcomment
amet ${MYVAR}
"""
######################################################### OK
not_interpolated='${MYVAR}, this and that'
not_interpolated='This, that, ${MYVAR} and something else'
not_interpolated='This, that and ${MYVAR}'
not_interpolated= 'This, that, ${MYVAR} and something else'
not_interpolated= 'This, that and ${MYVAR}' #comment
not_interpolated_multiline='''
${MYVAR} lorem ipsum
dolor ${MYVAR} sit\t #notcomment
amet ${MYVAR}
'''