Repository navigation
Expand file tree
/
Copy pathpython.lua
More file actions
66 lines (52 loc) · 1.54 KB
/
Copy pathpython.lua
File metadata and controls
66 lines (52 loc) · 1.54 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
local M = {}
local function escape_magic(pattern)
return pattern:gsub("%W", "%%%1")
end
function M.get_class_module(callback)
vim.lsp.buf.definition({
on_list = function(definitions)
local type_def = definitions["items"][1]
local absolute_path = type_def["filename"]
local code = type_def["text"]
local cwd = vim.fn.getcwd()
local relative_path = absolute_path:match(escape_magic(cwd) .. "/(.*)%.py")
local class_name = code:match("^%s*class%s+([%w_]+)")
local def_name = code:match("^%s*def%s+([%w_]+)")
local symbol_name = class_name or def_name
local symbol_path
if symbol_name == nil then
return vim.notify("Couldn't resolve symbol in " .. code .. ".", "ERROR")
end
if relative_path == nil then
symbol_path = symbol_name
else
relative_path = relative_path:match(".*site%-packages/(.*)") or relative_path
local module_path = relative_path:gsub("/", ".")
symbol_path = module_path .. "." .. symbol_name
end
callback(symbol_path)
end,
})
end
function M.get_class_at_cursor(callback, mark)
if mark == nil then
mark = true
end
local node = vim.treesitter.get_node()
while node and node:type() ~= "class_definition" do
node = node:parent()
end
if not node then
vim.notify("Couldn't find class at cursor.", "ERROR")
return nil
end
local row, col, _ = node:start()
vim.cmd(string.format([[exe "normal m'%sG%s\<Bar>w"]], row + 1, col + 1))
M.get_class_module(function(symbol_path)
callback(symbol_path)
if mark then
vim.cmd('exe "normal ``"')
end
end)
end
return M