errorpane.lua 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. local m = {} -- single floating pane showing error message and stack trace
  2. m.__index = m
  3. local buffer = require'buffer'
  4. local panes = require'pane'
  5. local editors = require'editors'
  6. local highlighting =
  7. { -- taken from base16-woodland
  8. background = 0x231e18, --editor background
  9. cursorline = 0x433b2f, --cursor background
  10. caret = 0xc6bcb1, --cursor
  11. whitespace = 0x111111, --spaces, newlines, tabs, and carriage returns
  12. comment = 0x9d8b70, --either multi-line or single-line comments
  13. string_start = 0x9d8b70, --starts and ends of a string. There will be no non-string tokens between these two.
  14. string_end = 0x9d8b70,
  15. string = 0xb7ba53, --part of a string that isn't an escape
  16. escape = 0x6eb958, --a string escape, like \n, only found inside strings
  17. keyword = 0xb690e2, --keywords. Like "while", "end", "do", etc
  18. value = 0xca7f32, --special values. Only true, false, and nil
  19. ident = 0xd35c5c, --identifier. Variables, function names, etc
  20. number = 0xca7f32, --numbers, including both base 10 (and scientific notation) and hexadecimal
  21. symbol = 0xc6bcb1, --symbols, like brackets, parenthesis, ., .., etc
  22. vararg = 0xca7f32, --...
  23. operator = 0xcabcb1, --operators, like +, -, %, =, ==, >=, <=, ~=, etc
  24. label_start = 0x9d8b70, --the starts and ends of labels. Always equal to '::'. Between them there can only be whitespace and label tokens.
  25. label_end = 0x9d8b70,
  26. label = 0xc6bcb1, --basically an ident between a label_start and label_end.
  27. unidentified = 0xd35c5c, --anything that isn't one of the above tokens. Consider them errors. Invalid escapes are also unidentified.
  28. selection = 0x353937,
  29. }
  30. function m.init(width, height, content)
  31. height = height * 1.5
  32. m.pane = panes.new(width, height)
  33. m.cols = math.floor(width * m.pane.canvasSize / m.pane.fontWidth)
  34. m.rows = math.floor(height * m.pane.canvasSize / m.pane.fontHeight)
  35. m.buffer = buffer.new(m.cols, m.rows,
  36. function(text, col, row, tokenType) -- draw single token
  37. local color = highlighting[tokenType] or 0xFFFFFF
  38. lovr.graphics.setColor(color)
  39. m.pane:drawText(text, col, row)
  40. end,
  41. function (col, row, width, tokenType) --draw rectangle
  42. local color = highlighting[tokenType] or 0xFFFFFF
  43. lovr.graphics.setColor(color)
  44. m.pane:drawTextRectangle(col, row, width)
  45. end)
  46. m.buffer:setName('-Error pane-')
  47. if content then m.setContent(content) end
  48. end
  49. function m.draw()
  50. m.pane:draw()
  51. end
  52. function m.setContent(content)
  53. m.buffer:setText(content)
  54. m.refresh()
  55. end
  56. function m.refresh()
  57. m.pane:drawCanvas(function()
  58. lovr.graphics.clear(highlighting.background)
  59. m.buffer:drawCode()
  60. end)
  61. end
  62. function m.jumpToSource()
  63. local line = m.buffer.lines[m.buffer.cursor.y]
  64. filename, lineNumber = line:match('([^ %c\\%.]+%.lua):(%d+):')
  65. if filename and lineNumber and #filename > 0 and editors.active then
  66. editors.active:openFile(filename)
  67. editors.active.buffer:jumpToLine(tonumber(lineNumber))
  68. editors.active:refresh()
  69. end
  70. end
  71. -- key handling
  72. function m.keypressed(k)
  73. if k == 'ctrl+up' then
  74. m.buffer:moveUp()
  75. m.refresh()
  76. elseif k == 'ctrl+down' then
  77. m.buffer:moveDown()
  78. m.refresh()
  79. elseif k == 'ctrl+enter' or k == 'ctrl+return' then
  80. m.jumpToSource()
  81. end
  82. end
  83. return m