python-ls.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. -- TODO: replace with path argument
  23. local flake8 = {
  24. LintCommand = "flake8 --ignore=E501 --stdin-display-name ${INPUT} -",
  25. lintStdin = true,
  26. lintFormats = { "%f:%l:%c: %m" },
  27. }
  28. local isort = { formatCommand = "isort --quiet -", formatStdin = true }
  29. if O.lang.python.linter == "flake8" then
  30. table.insert(python_arguments, flake8)
  31. end
  32. if O.lang.python.isort then
  33. table.insert(python_arguments, isort)
  34. end
  35. if not require("lv-utils").check_lsp_client_active "efm" then
  36. require("lspconfig").efm.setup {
  37. -- init_options = {initializationOptions},
  38. cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
  39. init_options = { documentFormatting = true, codeAction = false },
  40. root_dir = require("lspconfig").util.root_pattern(".git/", "requirements.txt"),
  41. filetypes = { "python" },
  42. settings = {
  43. rootMarkers = { ".git/", "requirements.txt" },
  44. languages = {
  45. python = python_arguments,
  46. },
  47. },
  48. }
  49. end
  50. end
  51. M.lsp = function()
  52. if require("lv-utils").check_lsp_client_active "pyright" then
  53. return
  54. end
  55. -- npm i -g pyright
  56. require("lspconfig").pyright.setup {
  57. cmd = {
  58. DATA_PATH .. "/lspinstall/python/node_modules/.bin/pyright-langserver",
  59. "--stdio",
  60. },
  61. on_attach = require("lsp").common_on_attach,
  62. handlers = {
  63. ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  64. virtual_text = O.lang.python.diagnostics.virtual_text,
  65. signs = O.lang.python.diagnostics.signs,
  66. underline = O.lang.python.diagnostics.underline,
  67. update_in_insert = true,
  68. }),
  69. },
  70. settings = {
  71. python = {
  72. analysis = {
  73. typeCheckingMode = O.lang.python.analysis.type_checking,
  74. autoSearchPaths = O.lang.python.analysis.auto_search_paths,
  75. useLibraryCodeForTypes = O.lang.python.analysis.use_library_code_types,
  76. },
  77. },
  78. },
  79. }
  80. end
  81. M.dap = function()
  82. if O.plugin.dap.active then
  83. local dap_install = require "dap-install"
  84. dap_install.config("python_dbg", {})
  85. end
  86. end
  87. return M