Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

78 linhas
2.5 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. -- Currently micro does not support changing coding-systems
  56. -- (Always use utf-8 with LF?)
  57. -- setCodingSystem(properties, view)
  58. -- `ruler' is not what we want!
  59. -- setMaxLineLength(properties, view)
  60. -- setTrimTrailingWhitespace(properties, view)
  61. -- We have eofnewline! Use this!
  62. -- setInsertFinalNewline(properties, view)
  63. end
  64. function onSave(view)
  65. messenger:Message("Saved!")
  66. end
  67. -- function getindentstyle()