Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ use { -- pytrize {{{
-- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'},
-- uncomment if you want to lazy load but not use the commands
-- module = 'pytrize',
config = 'require("pytrize").setup()',
config = function()
require("pytrize").setup({
-- metrics = true, -- uncomment to log timing info for jump and rename
})
end,
} -- }}}
```

Expand All @@ -68,7 +72,9 @@ For example using [`lazy.nvim`](https://github.com/folke/lazy.nvim):
version = '*',
dependencies = { 'nvim-lua/plenary' },
ft = 'python', -- Load only for python files
opts = {},
opts = {
-- metrics = true, -- uncomment to log timing info for jump and rename
},
-- uncomment if you want to lazy load
-- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'},
} -- }}}
Expand All @@ -84,6 +90,7 @@ Requires [`plenary.nvim`](https://github.com/nvim-lua/plenary.nvim).
{
no_commands = false,
highlight = 'LineNr',
metrics = false,
preferred_input = 'telescope',
}
```
Expand All @@ -92,6 +99,7 @@ where:

- `no_commands` can be set to `true` and the commands `Pytrize` etc won't be declared.
- `highlight` defines the highlighting used for the virtual text.
- `metrics` when set to `true`, logs timing information via `vim.notify` after each jump-to-fixture and rename operation. Useful for understanding performance in large projects. The jump reports total time and index-build time; the rename reports total, grep, scoping (fixture resolution), and apply time.
- `preferred_input` which method to query input to prefer (if it's installed), see the [Input](#input)-section below.

## Details
Expand Down
15 changes: 15 additions & 0 deletions lua/pytrize/jump/fixture.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ 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
Expand All @@ -18,7 +21,10 @@ M.to_declaration = function()
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))
Expand All @@ -27,6 +33,15 @@ M.to_declaration = function()

open_file(location.file)
vim.api.nvim_win_set_cursor(0, {location.linenr, 0})

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
22 changes: 20 additions & 2 deletions lua/pytrize/rename.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ local warn = require('pytrize.warn').warn
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

local function get_fixture_name()
return vim.fn.expand('<cword>')
end
Expand Down Expand Up @@ -222,12 +225,16 @@ local function rename(old_name, new_name)
return
end

local t0 = hrtime()

local py_files = find_python_files(root_dir, old_name)
if #py_files == 0 then
warn(string.format('No Python files contain "%s"', old_name))
return
end

local t_grep = hrtime()

-- First pass: determine which files to process. Only rename in files where
-- the fixture resolves to the current file (not shadowed by a closer definition).
local files_to_process = {}
Expand All @@ -243,6 +250,8 @@ local function rename(old_name, new_name)
end
end

local t_scope = hrtime()

local total_replacements = 0
local files_changed = 0

Expand Down Expand Up @@ -277,13 +286,22 @@ local function rename(old_name, new_name)
end
end

local t_end = hrtime()

if total_replacements == 0 then
warn(string.format('No fixture references found for "%s"', old_name))
else
vim.notify(string.format(
local msg = string.format(
'Pytrize: Renamed "%s" -> "%s" in %d file(s) (%d occurrence(s))',
old_name, new_name, files_changed, total_replacements
), vim.log.levels.INFO)
)
if require('pytrize.settings').settings.metrics then
msg = msg .. string.format(
'\n total=%s grep=%s scoping=%s apply=%s',
ms(t_end - t0), ms(t_grep - t0), ms(t_scope - t_grep), ms(t_end - t_scope)
)
end
vim.notify(msg, vim.log.levels.INFO)
end
end

Expand Down
1 change: 1 addition & 0 deletions lua/pytrize/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local warn = require('pytrize.warn').warn
M.settings = {
no_commands = false,
highlight = 'LineNr',
metrics = false,
-- preferred_input = 'telescope',
}

Expand Down