lua_mode.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- Lua Mode for Lumacs
  2. -- Extracted from init.lua
  3. define_major_mode("lua-mode", {
  4. file_patterns = {"%.lua$"},
  5. comment_syntax = "--",
  6. highlight = function()
  7. local buf = editor.buffer
  8. buf:clear_styles()
  9. -- Keywords to highlight
  10. local keywords = {
  11. "function", "local", "end", "if", "then", "else", "elseif",
  12. "for", "while", "do", "return", "break", "and", "or", "not",
  13. "true", "false", "nil", "in", "repeat", "until"
  14. }
  15. -- Highlight each line
  16. for line_num = 0, buf:line_count() - 1 do
  17. local line_text = buf:line(line_num)
  18. -- Highlight keywords
  19. for _, keyword in ipairs(keywords) do
  20. local start_pos = 1
  21. while true do
  22. local pattern = "%f[%w]" .. keyword .. "%f[%W]"
  23. local pos = string.find(line_text, pattern, start_pos)
  24. if not pos then break end
  25. local range = lumacs.Range(
  26. lumacs.Position(line_num, pos - 1),
  27. lumacs.Position(line_num, pos + #keyword - 1)
  28. )
  29. buf:set_style(range, lumacs.TextAttribute(lumacs.ColorType.Keyword, 0))
  30. start_pos = pos + #keyword
  31. end
  32. end
  33. -- Highlight strings
  34. local start_pos = 1
  35. while true do
  36. local quote_start = string.find(line_text, '"', start_pos, true)
  37. if not quote_start then break end
  38. local quote_end = string.find(line_text, '"', quote_start + 1, true)
  39. if not quote_end then break end
  40. local range = lumacs.Range(
  41. lumacs.Position(line_num, quote_start - 1),
  42. lumacs.Position(line_num, quote_end)
  43. )
  44. buf:set_style(range, lumacs.TextAttribute(lumacs.ColorType.String, 0))
  45. start_pos = quote_end + 1
  46. end
  47. -- Highlight comments
  48. local comment_pos = string.find(line_text, "--", 1, true)
  49. if comment_pos then
  50. local range = lumacs.Range(
  51. lumacs.Position(line_num, comment_pos - 1),
  52. lumacs.Position(line_num, #line_text)
  53. )
  54. buf:set_style(range, lumacs.TextAttribute(lumacs.ColorType.Comment, 0))
  55. end
  56. end
  57. end,
  58. setup = function()
  59. editor:message("[lua-mode] Lua mode activated")
  60. end,
  61. cleanup = function()
  62. editor:message("[lua-mode] Lua mode deactivated")
  63. end,
  64. keybindings = {
  65. -- Lua-specific keybindings can go here
  66. }
  67. })