Neovim plugin to copy the reference path of a Python symbol.
some.module.func_1)from some.module import func_1)Requires Neovim >=0.8.0.
With folke/lazy.nvim:
-- Stable version
{
'AnsonH/copy-python-path.nvim',
version = '*',
cmd = { "CopyPythonPath" },
}
With wbthomason/packer.nvim:
-- Stable version
use {"AnsonH/copy-python-path.nvim", tag = "*" }
Open a Python file and place the cursor on the following supported symbols:
def func_1(), async def func_2())class MyClass:)import numpy as np, from some.module import func_1)Then, run the command :CopyPythonPath <format> to copy to clipboard:
:CopyPythonPath dotted - Copies the dotted path (e.g. some.module.func_1):CopyPythonPath import - Copies the import path (e.g. from some.module import func_1)Let's say we have a file called app.py:
""" app.py """
import numpy as np
from user.models import User
# (1) ๐
def func_1():
pass
# (2) ๐
async def func_2():
pass
# (3) ๐
class MyClass:
# (4) ๐
class Meta:
pass
# (5) ๐
def method_1(self):
# (6) ๐
User()
# (7) ๐
return np.array([])
# (8) ๐
MODULE_VAR = 'foo'
| Cursor Location | :CopyPythonPath dotted |
:CopyPythonPath import |
|---|---|---|
| (1) Function definition | app.func_1 |
from app import func_1 |
| (2) Async function definition | app.func_2 |
from app import func_2 |
| (3) Class definition | app.MyClass |
from app import MyClass |
| (4) Inner class | app.MyClass.Meta |
from app import MyClassยน |
| (5) Class method | app.MyClass.method_1 |
from app import MyClassยน |
| (6) Imported symbol | user.models.User |
from user.models import Userยฒ |
| (7) Imported symbol with alias | numpy |
import numpy |
| (8) Module-level variable | app.MODULE_VAR |
from app import MODULE_VAR |
| Elsewhere in the file | app |
from app import |
Notes:
This plugin does NOT set up any keymappings by default. You can define custom keymappings in your Neovim config, for example:
vim.api.nvim_set_keymap('n', '<Leader>yd', ':CopyPythonPath dotted<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<Leader>yi', ':CopyPythonPath import<CR>', { noremap = true, silent = true })
:CopyPythonPath <format> [<register>]
Copies the reference path of the Python symbol under the cursor.
| Argument | Description | Accepted Values | Default Value |
|---|---|---|---|
format |
The path format to copy | dotted, import |
N.A. (required) |
register |
(optional) The register to copy to | Any valid register name | + (clipboard) |
The plugin API is available via:
local copy_python_path = require('copy-python-path')
get_path_under_cursorGets the Python path of the symbol underneath the cursor.
--- Gets the Python path of the symbol underneath the cursor.
---@param format string The Python path format. Accepted values are:
--- - `"dotted"`: Dotted path (e.g. `user.models.User`)
--- - `"import"`: Import statement (e.g. `from user.models import User`)
---@return string path
copy_python_path.get_path_under_cursor(format)
Example: Copy the shell command for running a Django test:
-- e.g. `./manage.py test some.module.func_1`
vim.api.nvim_create_user_command("CopyDjangoTestCommand", function(opts)
local copy_python_path = require("copy-python-path")
local path = copy_python_path.get_path_under_cursor("dotted")
local command = "./manage.py test " .. path
vim.fn.setreg("+", command)
end, {})
Special thanks to neovim-plugin-boilerplate for the plugin boilerplate code.