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 -- these don't mostly work well until we can change color within words
10 -- {prefix='"', target='dstring'},
11 -- {prefix="'", target='sstring'},
12 {prefix
='[[', target
='block_string'}, -- only single line for now
15 {suffix
='"', target
='normal'},
18 {suffix
="'", target
='normal'},
21 {suffix
=']]', target
='normal'},
24 {suffix
=']]', target
='normal'},
26 -- comments are a sink
29 Comment_color
= {r
=0, g
=0, b
=1}
30 String_color
= {r
=0, g
=0.5, b
=0.5}
31 Divider_color
= {r
=0.7, g
=0.7, b
=0.7}
35 comment
=Comment_color
,
38 block_string
=String_color
,
39 block_comment
=Comment_color
,
42 Current_state
= 'normal'
44 function initialize_color()
46 Current_state
= 'normal'
49 function select_color(frag
)
50 --? print('before', '^'..frag..'$', Current_state)
51 switch_color_based_on_prefix(frag
)
52 --? print('using color', Current_state, Colors[Current_state])
53 App
.color(Colors
[Current_state
])
54 switch_color_based_on_suffix(frag
)
55 --? print('state after suffix', Current_state)
58 function switch_color_based_on_prefix(frag
)
59 if Next_state
[Current_state
] == nil then
63 for _
,edge
in pairs(Next_state
[Current_state
]) do
64 if edge
.prefix
and starts_with(frag
, edge
.prefix
) then
65 Current_state
= edge
.target
71 function switch_color_based_on_suffix(frag
)
72 if Next_state
[Current_state
] == nil then
76 for _
,edge
in pairs(Next_state
[Current_state
]) do
77 if edge
.suffix
and ends_with(frag
, edge
.suffix
) then
78 Current_state
= edge
.target