init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. local Job = require("plenary.job")
  2. local Path = require("plenary.path")
  3. local namespace_name = "maj-peg"
  4. local status = {
  5. setup = false,
  6. enabled = true,
  7. has_mypy = false,
  8. has_mypy_baseline = false
  9. }
  10. local setup = function(mypy_flags, mypybaseline_flags)
  11. local namespace = vim.api.nvim_create_namespace(namespace_name)
  12. local group = vim.api.nvim_create_augroup(namespace_name, { clear = true })
  13. status.has_mypy = vim.fn.system({ "which", "mypy" }) ~= ""
  14. status.has_mypy_baseline = vim.fn.system({ "which", "mypy-baseline" }) ~= ""
  15. if mypy_flags == nil then
  16. mypy_flags = { "--follow-imports", "silent" }
  17. end
  18. if mypybaseline_flags == nil then
  19. mypybaseline_flags = { "filter" }
  20. end
  21. vim.api.nvim_create_autocmd({ "FileType" },
  22. {
  23. pattern = "python",
  24. group = group,
  25. callback = function(outer_args)
  26. vim.api.nvim_create_autocmd({ "BufWritePost", "FileType" }, {
  27. buffer = outer_args.buf,
  28. group = group,
  29. callback = function(args)
  30. vim.diagnostic.reset(namespace, args.buf)
  31. if status.enabled == false then return end
  32. local command = "mypy " ..
  33. table.concat(mypy_flags, " ") .. " " ..
  34. vim.api.nvim_buf_get_name(args.buf)
  35. if status.has_mypy_baseline then
  36. command = command ..
  37. " | mypy-baseline " ..
  38. table.concat(mypybaseline_flags, " ")
  39. end
  40. local path = Path:new(vim.api.nvim_buf_get_name(args.buf))
  41. :make_relative()
  42. local diagnostics = {}
  43. Job:new({
  44. command = "zsh",
  45. args = { "-c", command },
  46. on_stdout = function(_, line)
  47. if line == nil or line:sub(1, path:len()) ~= path then
  48. return
  49. end
  50. line = line:sub(path:len() + 1)
  51. local lnum = line:match(':%d+:')
  52. line = line:gsub(':%d: ', '', 1)
  53. if lnum == nil then return end
  54. lnum = lnum:gsub(':', '')
  55. lnum = tonumber(lnum) - 1
  56. local severity = vim.diagnostic.severity.INFO
  57. local severity_text = line:match('%a+: ')
  58. if severity_text == nil or severity_text == "hint: " then
  59. severity = vim.diagnostic.severity.HINT
  60. elseif severity_text == "error: " then
  61. severity = vim.diagnostic.severity.ERROR
  62. elseif severity_text == "warn: " then
  63. severity = vim.diagnostic.severity.WARN
  64. elseif severity_text == "info: " then
  65. severity = vim.diagnostic.severity.INFO
  66. end
  67. line = line:gsub('.+: ', '', 1)
  68. table.insert(
  69. diagnostics,
  70. {
  71. lnum = tonumber(lnum),
  72. col = 0,
  73. source = namespace_name,
  74. severity = severity,
  75. message = line
  76. })
  77. end,
  78. on_exit = function()
  79. vim.schedule(function()
  80. vim.diagnostic.set(namespace,
  81. args.buf, diagnostics)
  82. end)
  83. end,
  84. }):start()
  85. end
  86. })
  87. end
  88. })
  89. status.setup = true
  90. end
  91. return {
  92. setup = setup,
  93. toggle = function()
  94. status.enabled = not status.enabled;
  95. end,
  96. status = function()
  97. return status
  98. end
  99. }