python.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. local python_arguments = {}
  2. -- TODO replace with path argument
  3. local flake8 = {
  4. LintCommand = "flake8 --ignore=E501 --stdin-display-name ${INPUT} -",
  5. lintStdin = true,
  6. lintFormats = {"%f:%l:%c: %m"}
  7. }
  8. local isort = {formatCommand = "isort --quiet -", formatStdin = true}
  9. local yapf = {formatCommand = "yapf --quiet", formatStdin = true}
  10. local black = {formatCommand = "black --quiet -", formatStdin = true}
  11. if O.lang.python.linter == 'flake8' then table.insert(python_arguments, flake8) end
  12. if O.lang.python.isort then table.insert(python_arguments, isort) end
  13. if O.lang.python.formatter == 'yapf' then
  14. table.insert(python_arguments, yapf)
  15. elseif O.lang.python.formatter == 'black' then
  16. table.insert(python_arguments, black)
  17. end
  18. require"lspconfig".efm.setup {
  19. -- init_options = {initializationOptions},
  20. cmd = {DATA_PATH .. "/lspinstall/efm/efm-langserver"},
  21. init_options = {documentFormatting = true, codeAction = false},
  22. filetypes = {"python"},
  23. settings = {
  24. rootMarkers = {".git/", "requirements.txt"},
  25. languages = {
  26. python = python_arguments,
  27. }
  28. }
  29. }
  30. -- npm i -g pyright
  31. require'lspconfig'.pyright.setup {
  32. cmd = {
  33. DATA_PATH .. "/lspinstall/python/node_modules/.bin/pyright-langserver",
  34. "--stdio"
  35. },
  36. on_attach = require'lsp'.common_on_attach,
  37. handlers = {
  38. ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic
  39. .on_publish_diagnostics,
  40. {
  41. virtual_text = O.lang.python.diagnostics.virtual_text,
  42. signs = O.lang.python.diagnostics.signs,
  43. underline = O.lang.python.diagnostics.underline,
  44. update_in_insert = true
  45. })
  46. },
  47. settings = {
  48. python = {
  49. analysis = {
  50. typeCheckingMode = O.lang.python.analysis.type_checking,
  51. autoSearchPaths = O.lang.python.analysis.auto_search_paths,
  52. useLibraryCodeForTypes = O.lang.python.analysis
  53. .use_library_code_types
  54. }
  55. }
  56. }
  57. }
  58. if O.lang.python.autoformat then
  59. require('lv-utils').define_augroups({
  60. _python_autoformat = {
  61. {
  62. 'BufWritePre', '*.py',
  63. 'lua vim.lsp.buf.formatting_sync(nil, 1000)'
  64. }
  65. }
  66. })
  67. end