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.
 
 
 
 
 
 

68 lines
2.0 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, 10)
  26. if indent_size ~= nil then
  27. SetLocalOption("tabsize", indent_size, view)
  28. end
  29. if indent_style == "space" then
  30. messenger:Message("indent_style to space")
  31. SetLocalOption("indentchar", " ", view)
  32. elseif indent_style == "tab" then
  33. messenger:Message("indent_style to tab")
  34. SetLocalOption("indentchar", "\t", view)
  35. else
  36. messenger:Message("unknown indent_style")
  37. end
  38. end
  39. function onViewOpen(view)
  40. -- Is this portable? (work on windows?)
  41. local pwd = os.getenv("PWD")
  42. local filename = view.Buf.Path
  43. -- prop, names = ec.parse(filepath)
  44. local fullpath = JoinPaths(pwd, filename)
  45. local properties = getProperties(fullpath)
  46. if properties["indent_style"] == nil then
  47. messenger:Message("edconf: nil")
  48. else
  49. messenger:Message("edconf: " .. properties["indent_style"])
  50. end
  51. setIndentation(properties, view)
  52. -- setCodingSystem(propertieps, view)
  53. end
  54. function onSave(view)
  55. messenger:Message("Saved!")
  56. end
  57. -- function getindentstyle()