1 -- State transitions while colorizing a single line.
2 -- Just for comments and strings.
3 -- Limitation: each fragment gets a uniform color so we can only change color
7 {prefix
='--[[', target
='block_comment'}, -- only single-line for now
8 {prefix
='--', target
='comment'},
9 {prefix
='"', target
='dstring'},
10 {prefix
="'", target
='sstring'},
11 {prefix
='[[', target
='block_string'}, -- only single line for now
14 {suffix
='"', target
='normal'},
17 {suffix
="'", target
='normal'},
20 {suffix
=']]', target
='normal'},
23 {suffix
=']]', target
='normal'},
25 -- comments are a sink
28 Comment_color
= {r
=0, g
=0, b
=1}
29 String_color
= {r
=0, g
=0.5, b
=0.5}
30 Divider_color
= {r
=0.7, g
=0.7, b
=0.7}
34 comment
=Comment_color
,
37 block_string
=String_color
,
38 block_comment
=Comment_color
,
41 Current_state
= 'normal'
43 function initialize_color()
45 Current_state
= 'normal'
48 function select_color(frag
)
49 --? print('before', '^'..frag..'$', Current_state)
50 switch_color_based_on_prefix(frag
)
51 --? print('using color', Current_state, Colors[Current_state])
52 App
.color(Colors
[Current_state
])
53 switch_color_based_on_suffix(frag
)
54 --? print('state after suffix', Current_state)
57 function switch_color_based_on_prefix(frag
)
58 if Next_state
[Current_state
] == nil then
62 for _
,edge
in pairs(Next_state
[Current_state
]) do
63 if edge
.prefix
and find(frag
, edge
.prefix
, nil, --[[plain]] true) == 1 then
64 Current_state
= edge
.target
70 function switch_color_based_on_suffix(frag
)
71 if Next_state
[Current_state
] == nil then
75 for _
,edge
in pairs(Next_state
[Current_state
]) do
76 if edge
.suffix
and rfind(frag
, edge
.suffix
, nil, --[[plain]] true) == #frag
- #edge
.suffix
+ 1 then
77 Current_state
= edge
.target
84 return s
:gsub('^%s+', ''):gsub('%s+$', '')
88 return s
:gsub('^%s+', '')
92 return s
:gsub('%s+$', '')