GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” GitHub - AnsonH/copy-python-path.nvim: Neovim plugin to copy the reference path of a Python symbol.

BranchesTags
Open more actions menu

Latest commit

 

History

28 Commits
28 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
doc
doc
 
 
lua/copy-python-path
lua/copy-python-path
 
 
plugin
plugin
 
 
scripts
scripts
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
.luacheckrc
.luacheckrc
 
 
.luarc.json
.luarc.json
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 

Repository files navigation

copy-python-path.nvim

Neovim plugin to copy the reference path of a Python symbol.

Features

  • Supports copying different path formats (see examples):
    • Dotted path (e.g. some.module.func_1)
    • Import path (e.g. from some.module import func_1)
  • Supports various Python symbol definitions (see Getting started)
  • Simple Python project root detection
  • Allow copy to user-specified register
  • No LSP setup required

Installation

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 = "*" }

Getting started

Open a Python file and place the cursor on the following supported symbols:

  • Function definitions (e.g. def func_1(), async def func_2())
  • Class definitions (e.g. class MyClass:)
  • Class methods and inner classes
  • Module-level variable definitions
  • Imported symbols (e.g. 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)

Path format examples

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:

  1. Inner classes and class methods cannot be directly imported, so it only imports the outer class.
  2. When the symbol is imported, it copies the original path of where it was imported from.

Custom keymappings

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 })

Command

: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)

API

The plugin API is available via:

local copy_python_path = require('copy-python-path')

get_path_under_cursor

Gets 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, {})

Similar Work

  • kawamataryo/copy-python-path - VS Code plugin that inspired this project
  • ranelpadon/python-copy-reference.vim - Vim Script plugin with similar functionality

Special thanks to neovim-plugin-boilerplate for the plugin boilerplate code.

About

Neovim plugin to copy the reference path of a Python symbol.

Topics

neovimneovim-pluginpython

Resources

Readme
MIT license
Activity

Stars

5 stars

Watchers

0 watching

Forks

1 fork
Report repository

Releases

Contributors

Languages

You are visitor 01402520 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.