You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

93 lines
2.9 KiB

  1. -- ec = require("editorconfig_core")
  2. local function setIndentation(properties, view)
  3. local indent_size_str = properties["indent_size"]
  4. local tab_width_str = properties["tab_width"]
  5. local indent_style = properties["indent_style"]
  6. -- TODO: Fix logic to decide indent_size
  7. local indent_size = tonumber(indent_size_str, 10)
  8. local tab_width = tonumber(tab_width_str, 10)
  9. if indent_size_str == "tab" then
  10. indent_size = tab_width
  11. elseif tab_width == nil then
  12. tab_width = indent_size
  13. end
  14. if indent_size ~= nil then
  15. messenger:Message("set tabsize to " .. indent_size_str)
  16. SetLocalOption("tabsize", indent_size, view)
  17. end
  18. if indent_style == "space" then
  19. SetLocalOption("tabstospaces", "on", view)
  20. if indent_size ~= nil then
  21. messenger:Message("set tabsize to " .. tostring(indent_size))
  22. SetLocalOption("tabsize", indent_size, view)
  23. end
  24. elseif indent_style == "tab" then
  25. SetLocalOption("tabstospaces", "off", view)
  26. if tab_width ~= nil then
  27. messenger:Message("set tabsize to " .. tostring(tab_width))
  28. SetLocalOption("tabsize", tab_width, view)
  29. end
  30. else
  31. messenger:Message("unknown indent_style")
  32. end
  33. end
  34. local function setInsertFinalNewline(properties, view)
  35. local val = properties["insert_final_newline"]
  36. if val == "true" then
  37. SetLocalOption("eofnewline", true, view)
  38. elseif val == "false" then
  39. SetLocalOption("eofnewline", false, view)
  40. end
  41. end
  42. local function applyProperties(properties, view)
  43. setIndentation(properties, view)
  44. -- Currently micro does not support changing coding-systems
  45. -- (Always use utf-8 with LF?)
  46. -- setCodingSystem(properties, view)
  47. -- `ruler' is not what we want!
  48. -- setMaxLineLength(properties, view)
  49. -- setTrimTrailingWhitespace(properties, view)
  50. setInsertFinalNewline(properties, view)
  51. -- messenger:Message("ed output: " .. output)
  52. end
  53. function onEditorConfigExit(output)
  54. -- messenger:Message(output)
  55. -- FIXME: messege when editorconfig exit with error
  56. local properties = {}
  57. -- TODO: Which is better? output:gmatch(), string.gmatch(output, ...)
  58. for line in output:gmatch('([^\n]+)') do
  59. -- TODO: Fix regex
  60. -- TODO: Throw error for invalid output
  61. local key, value = line:match('([^=]*)=(.*)')
  62. key = key:gsub('^%s(.-)%s*$', '%1')
  63. value = value:gsub('^%s(.-)%s*$', '%1')
  64. properties[key] = value
  65. end
  66. local view = CurView()
  67. applyProperties(properties, view)
  68. end
  69. local function getApplyProperties(view)
  70. local fullpath = view.Buf.AbsPath
  71. messenger:Message("editorconfig " .. fullpath)
  72. -- JobSpawn("editorconfig", {fullpath}, "", "", "init.onEditorConfigExit")
  73. JobStart("editorconfig " .. fullpath, "", "", "init.onEditorConfigExit")
  74. end
  75. function onViewOpen(view)
  76. getApplyProperties(view)
  77. -- messenger:Message(view.Buf.AbsPath)
  78. end
  79. function onSave(view)
  80. -- messenger:Message("Saved!")
  81. end