about summary refs log tree commit diff stats
path: root/colorize.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-09-03 14:13:22 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-09-03 14:13:22 -0700
commite1c5a42f311fdafd88506726bbe480f3fcc2d1a3 (patch)
tree6628729cc55947d0bd5d306704e88b57680c3514 /colorize.lua
parent9c72ff1bb4fc1ba08acfb0324079da6fe49f3a4a (diff)
downloadview.love-e1c5a42f311fdafd88506726bbe480f3fcc2d1a3.tar.gz
editing source code from within the app
integrated from pong.love via text.love:
  https://merveilles.town/@akkartik/108933336531898243
Diffstat (limited to 'colorize.lua')
-rw-r--r--colorize.lua83
1 files changed, 83 insertions, 0 deletions
diff --git a/colorize.lua b/colorize.lua
new file mode 100644
index 0000000..c0d2117
--- /dev/null
+++ b/colorize.lua
@@ -0,0 +1,83 @@
+-- State transitions while colorizing a single line.
+-- Just for comments and strings.
+-- Limitation: each fragment gets a uniform color so we can only change color
+-- at word boundaries.
+Next_state = {
+  normal={
+    {prefix='--', target='comment'},
+    {prefix='"', target='dstring'},
+    {prefix="'", target='sstring'},
+  },
+  dstring={
+    {suffix='"', target='normal'},
+  },
+  sstring={
+    {suffix="'", target='normal'},
+  },
+  -- comments are a sink
+}
+
+Comments_color = {r=0, g=0, b=1}
+String_color = {r=0, g=0.5, b=0.5}
+Divider_color = {r=0.7, g=0.7, b=0.7}
+
+Colors = {
+  normal=Text_color,
+  comment=Comments_color,
+  sstring=String_color,
+  dstring=String_color
+}
+
+Current_state = 'normal'
+
+function initialize_color()
+--?   print('new line')
+  Current_state = 'normal'
+end
+
+function select_color(frag)
+--?   print('before', '^'..frag..'$', Current_state)
+  switch_color_based_on_prefix(frag)
+--?   print('using color', Current_state, Colors[Current_state])
+  App.color(Colors[Current_state])
+  switch_color_based_on_suffix(frag)
+--?   print('state after suffix', Current_state)
+end
+
+function switch_color_based_on_prefix(frag)
+  if Next_state[Current_state] == nil then
+    return
+  end
+  frag = rtrim(frag)
+  for _,edge in pairs(Next_state[Current_state]) do
+    if edge.prefix and find(frag, edge.prefix, nil, --[[plain]] true) == 1 then
+      Current_state = edge.target
+      break
+    end
+  end
+end
+
+function switch_color_based_on_suffix(frag)
+  if Next_state[Current_state] == nil then
+    return
+  end
+  frag = rtrim(frag)
+  for _,edge in pairs(Next_state[Current_state]) do
+    if edge.suffix and rfind(frag, edge.suffix, nil, --[[plain]] true) == #frag then
+      Current_state = edge.target
+      break
+    end
+  end
+end
+
+function trim(s)
+  return s:gsub('^%s+', ''):gsub('%s+$', '')
+end
+
+function ltrim(s)
+  return s:gsub('^%s+', '')
+end
+
+function rtrim(s)
+  return s:gsub('%s+$', '')
+end