mdtree.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. VERSION = "1.0.0"
  2. local micro = import("micro")
  3. local buffer = import("micro/buffer")
  4. local config = import("micro/config")
  5. local action = import("micro/action")
  6. function init()
  7. config.MakeCommand("mdtree", mdtree, config.OptionComplete)
  8. config.AddRuntimeFile("mdtree", config.RTPlugin, "mdtree.lua")
  9. config.AddRuntimeFile("mdtree", config.RTPlugin, "bar.lua")
  10. config.AddRuntimeFile("mdtree", config.RTSyntax, "syntax.yaml")
  11. end
  12. headings = {}
  13. function mdtree(buffer_pane)
  14. local buf = buffer_pane.Buf
  15. -- Make sure that you don't create new sidebar when one exists
  16. if SideBar:IsOpen()
  17. then
  18. if not buffer_pane == SideBar.buffer_pane
  19. then
  20. SideBar:SetActive()
  21. end
  22. else
  23. -- Prevent opening bar for non-markdown files
  24. -- NOTE: must be here after check for bar existence
  25. -- because otherwise bar will be checked if it is md file
  26. if buf:FileType() ~= "markdown"
  27. then
  28. micro.InfoBar():Error("File isn't markdown")
  29. return
  30. end
  31. SideBar:Create(buffer_pane)
  32. SideBar:SetActive()
  33. end
  34. end
  35. -------------------------
  36. -- SideBar callbacks
  37. -------------------------
  38. function SideBar.callbacks:Update()
  39. SideBar:Clear()
  40. if SideBar.owner ~= nil then
  41. parse_headings(SideBar.owner.Buf)
  42. end
  43. ident_headings()
  44. print_headings()
  45. end
  46. function SideBar.callbacks:Jump(y)
  47. SideBar.owner.Cursor:GotoLoc({
  48. X = 0,
  49. Y = headings[y + 1].position
  50. })
  51. SideBar.owner:Center()
  52. -- TODO: Make main file active
  53. -- SideBar.buffer_pane:SetActive(false)
  54. -- SideBar.owner:SetActive(true)
  55. local tab = micro.CurTab()
  56. tab:SetActive(tab:GetPane(SideBar.owner:ID()))
  57. end
  58. -------------------------
  59. -- Parsing functions
  60. -------------------------
  61. function is_heading(line)
  62. local head, tail = string.find(line, "#+ .*")
  63. if head ~= nil and tail ~= nil
  64. then
  65. return true
  66. else
  67. return false
  68. end
  69. end
  70. function parse_headings(buf)
  71. headings = {}
  72. for i = 0, buf:LinesNum()
  73. do
  74. local line = buf:Line(i)
  75. if is_heading(line)
  76. then
  77. local heading = {text=line, position=i }
  78. table.insert(headings, heading)
  79. end
  80. end
  81. end
  82. function ident_headings()
  83. for i, heading in ipairs(headings) do
  84. local head, tail = string.find(heading.text, "^#+")
  85. local header_level = tail - head
  86. heading.text = string.rep(" ", header_level) .. heading.text
  87. end
  88. end
  89. function print_headings()
  90. for i, heading in ipairs(headings) do
  91. --Don't add \n in the end of the tree
  92. if i == #headings
  93. then
  94. SideBar:Append(heading.text)
  95. else
  96. SideBar:Append(heading.text .. "\n")
  97. end
  98. end
  99. end