test_highlight.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -- Test script for syntax highlighting API
  2. print("Testing syntax highlighting API...")
  3. -- Simple function to highlight keywords in a line
  4. function highlight_keywords(buf, line_num, keywords)
  5. local line_text = buf:line(line_num)
  6. for _, keyword in ipairs(keywords) do
  7. -- Find all occurrences of this keyword
  8. local start_pos = 1
  9. while true do
  10. local pos = string.find(line_text, keyword, start_pos, true)
  11. if not pos then break end
  12. -- Create a range for this keyword
  13. local range = lumacs.Range(
  14. lumacs.Position(line_num, pos - 1),
  15. lumacs.Position(line_num, pos + #keyword - 1)
  16. )
  17. -- Create text attribute for keywords (blue, bold)
  18. local attr = lumacs.TextAttribute(
  19. lumacs.ColorType.Keyword,
  20. lumacs.Style.Bold
  21. )
  22. -- Set the style
  23. buf:set_style(range, attr)
  24. message(string.format("Highlighted '%s' at line %d, col %d-%d",
  25. keyword, line_num, pos-1, pos+#keyword-1))
  26. start_pos = pos + #keyword
  27. end
  28. end
  29. end
  30. -- Test it on the buffer
  31. local buf = editor.buffer
  32. local keywords = {"function", "local", "end", "if", "then", "for", "while"}
  33. -- Highlight keywords in all lines
  34. for line = 0, buf:line_count() - 1 do
  35. highlight_keywords(buf, line, keywords)
  36. end
  37. -- Check what styles we have on line 0
  38. local styles = buf:get_line_styles(0)
  39. message(string.format("Line 0 has %d styled ranges", #styles))
  40. message("Syntax highlighting test complete!")