25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

80 lines
2.5 KiB

  1. -- ec = require("editorconfig_core")
  2. local function getProperties(fullpath)
  3. -- FIXME: Commands can be injected here!
  4. -- For example, issuing this command will create file b.txt
  5. -- $ micro "a.txt'; touch 'b.txt"
  6. local file = io.popen("editorconfig '" .. fullpath .. "'", "r")
  7. local output = file:read("*all")
  8. file:close()
  9. local properties = {}
  10. -- TODO: Which is better? output:gmatch(), string.gmatch(output, ...)
  11. for line in output:gmatch('([^\n]+)') do
  12. -- TODO: Fix regex
  13. local key, value = line:match('([^=]*)=(.*)')
  14. key = key:gsub('^%s(.-)%s*$', '%1')
  15. value = value:gsub('^%s(.-)%s*$', '%1')
  16. -- TODO: Throw error when key is empty string
  17. properties[key] = value
  18. end
  19. return properties
  20. end
  21. local function setIndentation(properties, view)
  22. local indent_size_str = properties["indent_size"]
  23. local tab_width_str = properties["tab_width"]
  24. local indent_style = properties["indent_style"]
  25. local indent_size = tonumber(indent_size_str, 10)
  26. if indent_size ~= nil then
  27. messenger:Message("set tabsize to " .. indent_size_str)
  28. SetLocalOption("tabsize", indent_size, view)
  29. end
  30. if indent_style == "space" then
  31. SetLocalOption("tabstospaces", "on", view)
  32. elseif indent_style == "tab" then
  33. SetLocalOption("tabstospaces", "off", view)
  34. else
  35. messenger:Message("unknown indent_style")
  36. end
  37. end
  38. local function setInsertFinalNewline(properties, view)
  39. local val = properties["insert_final_newline"]
  40. if val == "true" then
  41. SetLocalOption("eofnewline", true, view)
  42. elseif val == "false" then
  43. SetLocalOption("eofnewline", false, view)
  44. end
  45. end
  46. function onViewOpen(view)
  47. -- Is this portable? (work on windows?)
  48. local pwd = os.getenv("PWD")
  49. local filename = view.Buf.Path
  50. -- prop, names = ec.parse(filepath)
  51. local fullpath = JoinPaths(pwd, filename)
  52. local properties = getProperties(fullpath)
  53. if properties["indent_style"] == nil then
  54. messenger:Message("edconf: nil")
  55. else
  56. messenger:Message("edconf: " .. properties["indent_style"])
  57. end
  58. setIndentation(properties, view)
  59. -- Currently micro does not support changing coding-systems
  60. -- (Always use utf-8 with LF?)
  61. -- setCodingSystem(properties, view)
  62. -- `ruler' is not what we want!
  63. -- setMaxLineLength(properties, view)
  64. -- setTrimTrailingWhitespace(properties, view)
  65. setInsertFinalNewline(properties, view)
  66. end
  67. function onSave(view)
  68. -- messenger:Message("Saved!")
  69. end