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.
 
 
 
 
 
 

71 lines
2.2 KiB

  1. -- ec = require("editorconfig_core")
  2. local function getProperties(fullpath)
  3. -- TODO: Avoid command injection vulnerability
  4. -- For example, following 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. -- messenger:Message("indent_style to space")
  32. SetLocalOption("tabstospaces", "on", view)
  33. elseif indent_style == "tab" then
  34. -- messenger:Message("indent_style to tab")
  35. SetLocalOption("tabstospaces", "off", view)
  36. else
  37. -- messenger:Message("unknown indent_style")
  38. end
  39. end
  40. function onViewOpen(view)
  41. -- Is this portable? (work on windows?)
  42. local pwd = os.getenv("PWD")
  43. local filename = view.Buf.Path
  44. -- prop, names = ec.parse(filepath)
  45. local fullpath = JoinPaths(pwd, filename)
  46. local properties = getProperties(fullpath)
  47. if properties["indent_style"] == nil then
  48. messenger:Message("edconf: nil")
  49. else
  50. messenger:Message("edconf: " .. properties["indent_style"])
  51. end
  52. -- SetLocalOption("tabsize", 4, view)
  53. -- SetLocalOption("tabstospaces", "on", view)
  54. setIndentation(properties, view)
  55. -- setCodingSystem(propertieps, view)
  56. end
  57. function onSave(view)
  58. messenger:Message("Saved!")
  59. end
  60. -- function getindentstyle()