forked from AckslD/nvim-pytrize.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixture.lua
More file actions
47 lines (38 loc) · 1.28 KB
/
fixture.lua
File metadata and controls
47 lines (38 loc) · 1.28 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
local M = {}
local warn = require('pytrize.warn').warn
local open_file = require('pytrize.jump.util').open_file
local paths = require('pytrize.paths')
local ts_utils = require('pytrize.ts')
local function hrtime() return (vim.uv or vim.loop).hrtime() end
local function ms(t) return string.format('%.1fms', t / 1e6) end
M.to_declaration = function()
local fixture = vim.fn.expand('<cword>')
if fixture == '' then
warn('no word under cursor')
return
end
local filepath = vim.api.nvim_buf_get_name(0)
local root_dir = paths.split_at_root(filepath)
if root_dir == nil then
return
end
local t0 = hrtime()
local fixtures = ts_utils.build_fixture_index(filepath, root_dir)
local t_index = hrtime()
local location = fixtures[fixture]
if location == nil then
warn(string.format('fixture "%s" not found', fixture))
return
end
open_file(location.file)
vim.api.nvim_win_set_cursor(0, {location.linenr, location.col})
if require('pytrize.settings').settings.metrics then
local total = hrtime() - t0
local index = t_index - t0
vim.notify(string.format(
'Pytrize jump: total=%s index=%s',
ms(total), ms(index)
), vim.log.levels.INFO)
end
end
return M