-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpylance.lua
More file actions
91 lines (82 loc) · 2.33 KB
/
pylance.lua
File metadata and controls
91 lines (82 loc) · 2.33 KB
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
local util = require("lspconfig.util")
local root_files = {
"pyproject.toml",
"setup.py",
"setup.cfg",
"requirements.txt",
"Pipfile",
}
-- Removed deprecated functions from https://github.com/lithammer/nvim-pylance/blob/master/lua/pylance.lua
local function exepath(expr)
local ep = vim.fn.exepath(expr)
return ep ~= "" and ep or nil
end
local function get_python_path(workspace)
local path = util.path
-- 1. Use activated virtualenv.
if vim.env.VIRTUAL_ENV then
return path.join(vim.env.VIRTUAL_ENV, "bin", "python")
end
-- 2. Find and use virtualenv in workspace directory.
for _, pattern in ipairs({ "*", ".*" }) do
local match = vim.fn.glob(path.join(workspace, pattern, "pyvenv.cfg"))
if vim.fn.empty(match) ~= 1 then
return path.join(path.dirname(match), "bin", "python")
end
end
-- 3. Find and use virtualenv managed by Poetry.
if exepath("poetry") and path.is_file(path.join(workspace, "poetry.lock")) then
local output = vim.fn.trim(vim.fn.system("poetry env info -p"))
if path.is_dir(output) then
return path.join(output, "bin", "python")
end
end
-- 4. Find and use virtualenv managed by Pipenv.
if exepath("pipenv") and path.is_file(path.join(workspace, "Pipfile")) then
local output = vim.fn.trim(vim.fn.system("cd " .. workspace .. "; pipenv --py"))
if path.is_dir(output) then
return output
end
end
-- 5. Fallback to system Python.
return exepath("python3") or exepath("python") or "python"
end
return {
default_config = {
before_init = function(_, config)
if not config.settings.python then
config.settings.python = {}
end
if not config.settings.python.pythonPath then
config.settings.python.pythonPath = get_python_path(config.root_dir)
end
end,
cmd = {
"node",
vim.fn.expand("~/.vscode/extensions/ms-python.vscode-pylance-*/dist/server_nvim.js", false, true)[1],
"--stdio",
},
filetypes = { "python" },
single_file_support = true,
root_dir = util.root_pattern(unpack(root_files)),
settings = {
python = {
analysis = {
inlayHints = {
variableTypes = true,
functionReturnTypes = true,
callArgumentNames = true,
pytestParameters = true,
},
},
},
},
},
commands = {},
docs = {
description = [[
https://github.com/microsoft/pylance-release
`pylance`, Fast, feature-rich language support for Python
]],
},
}