python.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. local M = {}
  2. M.config = function()
  3. O.lang.python = {
  4. -- @usage can be flake8 or yapf
  5. linter = "",
  6. isort = false,
  7. diagnostics = {
  8. virtual_text = { spacing = 0, prefix = "" },
  9. signs = true,
  10. underline = true,
  11. },
  12. analysis = {
  13. type_checking = "basic",
  14. auto_search_paths = true,
  15. use_library_code_types = true,
  16. },
  17. formatter = {
  18. exe = "yapf",
  19. args = {},
  20. },
  21. }
  22. end
  23. M.format = function()
  24. O.formatters.filetype["python"] = {
  25. function()
  26. return {
  27. exe = O.lang.python.formatter.exe,
  28. args = O.lang.python.formatter.args,
  29. stdin = not (O.lang.python.formatter.stdin ~= nil),
  30. }
  31. end,
  32. }
  33. require("formatter.config").set_defaults {
  34. logging = false,
  35. filetype = O.formatters.filetype,
  36. }
  37. end
  38. M.lint = function()
  39. if require("lv-utils").check_lsp_client_active "efm" then
  40. return
  41. end
  42. local python_arguments = {}
  43. local flake8 = {
  44. LintCommand = "flake8 --ignore=E501 --stdin-display-name ${INPUT} -",
  45. lintStdin = true,
  46. lintFormats = { "%f:%l:%c: %m" },
  47. }
  48. local isort = { formatCommand = "isort --quiet -", formatStdin = true }
  49. if O.lang.python.linter == "flake8" then
  50. table.insert(python_arguments, flake8)
  51. end
  52. if O.lang.python.isort then
  53. table.insert(python_arguments, isort)
  54. end
  55. if not require("lv-utils").check_lsp_client_active "efm" then
  56. require("lspconfig").efm.setup {
  57. cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
  58. init_options = { documentFormatting = true, codeAction = false },
  59. root_dir = require("lspconfig").util.root_pattern(".git/", "requirements.txt"),
  60. filetypes = { "python" },
  61. settings = {
  62. rootMarkers = { ".git/", "requirements.txt" },
  63. languages = {
  64. python = python_arguments,
  65. },
  66. },
  67. }
  68. end
  69. end
  70. M.lsp = function()
  71. if require("lv-utils").check_lsp_client_active "pyright" then
  72. return
  73. end
  74. -- npm i -g pyright
  75. require("lspconfig").pyright.setup {
  76. cmd = {
  77. DATA_PATH .. "/lspinstall/python/node_modules/.bin/pyright-langserver",
  78. "--stdio",
  79. },
  80. on_attach = require("lsp").common_on_attach,
  81. handlers = {
  82. ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  83. virtual_text = O.lang.python.diagnostics.virtual_text,
  84. signs = O.lang.python.diagnostics.signs,
  85. underline = O.lang.python.diagnostics.underline,
  86. update_in_insert = true,
  87. }),
  88. },
  89. settings = {
  90. python = {
  91. analysis = {
  92. typeCheckingMode = O.lang.python.analysis.type_checking,
  93. autoSearchPaths = O.lang.python.analysis.auto_search_paths,
  94. useLibraryCodeForTypes = O.lang.python.analysis.use_library_code_types,
  95. },
  96. },
  97. },
  98. }
  99. end
  100. M.dap = function()
  101. if O.plugin.dap.active then
  102. local dap_install = require "dap-install"
  103. dap_install.config("python_dbg", {})
  104. end
  105. end
  106. return M