bugfix: Unicode
[view.love.git] / colorize.lua
blob6a0a7e571a1483e9de266cd5da87b4bea0f4bc9d
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
4 -- at word boundaries.
5 Next_state = {
6 normal={
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
14 dstring={
15 {suffix='"', target='normal'},
17 sstring={
18 {suffix="'", target='normal'},
20 block_string={
21 {suffix=']]', target='normal'},
23 block_comment={
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}
33 Colors = {
34 normal=Text_color,
35 comment=Comment_color,
36 sstring=String_color,
37 dstring=String_color,
38 block_string=String_color,
39 block_comment=Comment_color,
42 Current_state = 'normal'
44 function initialize_color()
45 --? print('new line')
46 Current_state = 'normal'
47 end
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)
56 end
58 function switch_color_based_on_prefix(frag)
59 if Next_state[Current_state] == nil then
60 return
61 end
62 frag = rtrim(frag)
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
66 break
67 end
68 end
69 end
71 function switch_color_based_on_suffix(frag)
72 if Next_state[Current_state] == nil then
73 return
74 end
75 frag = rtrim(frag)
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
79 break
80 end
81 end
82 end