python.lua 2.5 KB

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