summaryrefslogtreecommitdiff
path: root/lua/lsp/python-ls.lua
blob: 1631577b111a1ab7e99963a8e95f8dacc7a9d246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
local M = {}

M.format = function()
  O.formatters.filetype["python"] = {
    function()
      return {
        exe = O.lang.python.formatter.exe,
        args = O.lang.python.formatter.args,
        stdin = not (O.lang.python.formatter.stdin ~= nil),
      }
    end,
  }

  require("formatter.config").set_defaults {
    logging = false,
    filetype = O.formatters.filetype,
  }
end

M.lint = function()
  if require("lv-utils").check_lsp_client_active "efm" then
    return
  end
  local python_arguments = {}

  -- TODO: replace with path argument
  local flake8 = {
    LintCommand = "flake8 --ignore=E501 --stdin-display-name ${INPUT} -",
    lintStdin = true,
    lintFormats = { "%f:%l:%c: %m" },
  }

  local isort = { formatCommand = "isort --quiet -", formatStdin = true }

  if O.lang.python.linter == "flake8" then
    table.insert(python_arguments, flake8)
  end

  if O.lang.python.isort then
    table.insert(python_arguments, isort)
  end

  if not require("lv-utils").check_lsp_client_active "efm" then
    require("lspconfig").efm.setup {
      -- init_options = {initializationOptions},
      cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
      init_options = { documentFormatting = true, codeAction = false },
      root_dir = require("lspconfig").util.root_pattern(".git/", "requirements.txt"),
      filetypes = { "python" },
      settings = {
        rootMarkers = { ".git/", "requirements.txt" },
        languages = {
          python = python_arguments,
        },
      },
    }
  end
end

M.lsp = function()
  if require("lv-utils").check_lsp_client_active "pyright" then
    return
  end
  -- npm i -g pyright
  require("lspconfig").pyright.setup {
    cmd = {
      DATA_PATH .. "/lspinstall/python/node_modules/.bin/pyright-langserver",
      "--stdio",
    },
    on_attach = require("lsp").common_on_attach,
    handlers = {
      ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
        virtual_text = O.lang.python.diagnostics.virtual_text,
        signs = O.lang.python.diagnostics.signs,
        underline = O.lang.python.diagnostics.underline,
        update_in_insert = true,
      }),
    },
    settings = {
      python = {
        analysis = {
          typeCheckingMode = O.lang.python.analysis.type_checking,
          autoSearchPaths = O.lang.python.analysis.auto_search_paths,
          useLibraryCodeForTypes = O.lang.python.analysis.use_library_code_types,
        },
      },
    },
  }
end

M.dap = function()
  if O.plugin.dap.active then
    local dap_install = require "dap-install"
    dap_install.config("python_dbg", {})
  end
end

return M