Name | python-import JSON |
Version |
0.1.0
JSON |
| download |
home_page | None |
Summary | Neovim plugin for python autoimports. |
upload_time | 2024-09-03 02:22:05 |
maintainer | None |
docs_url | None |
author | Kiyoon Kim |
requires_python | <3.13,>=3.9 |
license | MIT License Copyright (c) 2024 Kiyoon Kim Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
neovim
nvim
nvim-plugin
python
python-import
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 🐍 python-import.nvim
| | |
|--|--|
|[![Ruff](https://img.shields.io/badge/Ruff-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/ruff) [![StyLua](https://img.shields.io/badge/stylua-%232C2D72.svg?style=for-the-badge&logo=lua&logoColor=white)](https://github.com/JohnnyMorganz/StyLua) |[![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Style%20checking/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions)|
| [![Ruff](https://img.shields.io/badge/Ruff-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/ruff) | [![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Linting/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions) |
| [![pytest](https://img.shields.io/badge/pytest-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/pytest-dev/pytest) [![doctest](https://img.shields.io/badge/doctest-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://docs.python.org/3/library/doctest.html) | [![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Tests/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions) |
| [![uv](https://img.shields.io/badge/uv-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/uv) | [![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Check%20pip%20compile%20sync/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions) |
A simple tool that auto-inserts import statements.
https://github.com/kiyoon/python-import.nvim/assets/12980409/8a8f580f-16de-460c-af32-23fab7d2a35e
It matches with:
1. [Pre-defined lookup tables](https://github.com/kiyoon/python-import.nvim/blob/master/lua/python_import/lookup_table.lua)
2. Existing imports in the project (implemented in Python: `python-import` cli)
- It finds the imports in the project and shows a list of them to choose from.
3. pyright/basedpyright LSP completion
4. Just `import <word>` :P
This plugin doesn't detect duplicated imports. Use `ruff` to sort imports.
### Lookup table examples
- `Path` -> `from pathlib import Path`
- `np` -> `import numpy as np`
- `torch` -> `import torch`
- `logging` -> `import logging`
- `logger` ->
```python
import logging
logger = logging.getLogger(__name__)
```
Most will use the current word to find the import statement, but the treesitter node can be used to find the import statement more accurately. (e.g. `class SomeDataset(torch.utils.data.DataLoader)` -> `import torch.utils.data`)
> [!WARNING]
> This work-in-progress plugin is not yet ready for the public.
> There aren't many customisation options (e.g. how to locate the import place)
> and the behaviour and build/configuration instructions change rapidly.
> If you don't want breaking changes, please fix the version of this plugin.
## 🛠️ Installation
### Requirements
- 💻 Neovim >= 0.10
- pipx or uv (or any other way to install `python-import` cli in PATH)
- ripgrep (`brew install ripgrep` or `cargo install ripgrep`)
- fd (`brew install fd`, `cargo install fd-find` or `npm install -g fd-find`)
### Install with lazy.nvim:
```lua
{
"kiyoon/python-import.nvim",
build = "pipx install . --force",
keys = {
{
"<M-CR>",
function()
require("python_import.api").add_import_current_word_and_notify()
end,
mode = { "i", "n" },
silent = true,
desc = "Add python import",
ft = "python",
},
{
"<M-CR>",
function()
require("python_import.api").add_import_current_selection_and_notify()
end,
mode = "x",
silent = true,
desc = "Add python import",
ft = "python",
},
{
"<space>i",
function()
require("python_import.api").add_import_current_word_and_move_cursor()
end,
mode = "n",
silent = true,
desc = "Add python import and move cursor",
ft = "python",
},
{
"<space>i",
function()
require("python_import.api").add_import_current_selection_and_move_cursor()
end,
mode = "x",
silent = true,
desc = "Add python import and move cursor",
ft = "python",
},
{
"<space>tr",
function()
require("python_import.api").add_rich_traceback()
end,
silent = true,
desc = "Add rich traceback",
ft = "python",
},
},
opts = {
-- Example 1:
-- Default behaviour for `tqdm` is `from tqdm.auto import tqdm`.
-- If you want to change it to `import tqdm`, you can set `import = {"tqdm"}` and `import_from = {tqdm = nil}` here.
-- If you want to change it to `from tqdm import tqdm`, you can set `import_from = {tqdm = "tqdm"}` here.
-- Example 2:
-- Default behaviour for `logger` is `import logging`, ``, `logger = logging.getLogger(__name__)`.
-- If you want to change it to `import my_custom_logger`, ``, `logger = my_custom_logger.get_logger()`,
-- you can set `statement_after_imports = {logger = {"import my_custom_logger", "", "logger = my_custom_logger.get_logger()"}}` here.
extend_lookup_table = {
---@type string[]
import = {
-- "tqdm",
},
---@type table<string, string>
import_as = {
-- These are the default values. Here for demonstration.
-- np = "numpy",
-- pd = "pandas",
},
---@type table<string, string>
import_from = {
-- tqdm = nil,
-- tqdm = "tqdm",
},
---@type table<string, string[]>
statement_after_imports = {
-- logger = { "import my_custom_logger", "", "logger = my_custom_logger.get_logger()" },
},
},
---Return nil to indicate no match is found and continue with the default lookup
---Return a table to stop the lookup and use the returned table as the result
---Return an empty table to stop the lookup. This is useful when you want to add to wherever you need to.
---@type fun(winnr: integer, word: string, ts_node: TSNode?): string[]?
custom_function = function(winnr, word, ts_node)
-- if vim.endswith(word, "_DIR") then
-- return { "from my_module import " .. word }
-- end
end,
},
},
"rcarriga/nvim-notify", -- optional
```
### Faster building with `uv`
`pipx` is easy to configure, but `uv` is much faster. Make sure to install uv >= 0.2 and change the build script as follows:
With lazy.nvim:
```lua
{
"kiyoon/python-import.nvim",
build = "bash scripts/build_with_uv.sh ~/.virtualenvs/python-import",
-- Other configurations ...
},
```
This simply creates a virtual environment in `~/.virtualenvs/python-import` and installs the cli there with `uv pip install .`. Then it sym-links the binary to `~/.local/bin/python-import`.
### 🏋️ Health check
Run `:checkhealth python_import` and see if python-import.nvim is installed correctly.
You need to disable lazy loading or run any commands in a python file to activate the plugin first.
```
==============================================================================
python_import: require("python_import.health").check()
python-import ~
- OK Using Neovim >= 0.10.0
- OK `rg` is installed
- OK `fd` is installed
- OK `python-import` is installed
- ERROR python-import cli (0.1.0+41.g9513418.dirty) and nvim plugin version (0.1.0+42.g9f74e1d.dirty) mismatch.
```
## 💻 `python-import` CLI
The `python-import` CLI is a simple Python script that counts the number of import statements used in the project.
```
$ python-import count /path/to/project np
00004: import numpy as np
```
## TODO
- [ ] Search class/function/variable definitions from project
- [ ] Add more tests
- [ ] Command to add imports in TYPE_CHECKING
- [ ] Command to add lazy imports in methods
- [ ] Command to add imports in jupytext cell
- [ ] Search in ipynb files
- [ ] VSCode-neovim integration (use `vim.ui.select` for vscode UI support. However, this complicates stuff because it runs async)
- Currently, if you open the vscode-neovim output terminal, it is quite usable. The notification works as well. LSP doesn't work yet.
Raw data
{
"_id": null,
"home_page": null,
"name": "python-import",
"maintainer": null,
"docs_url": null,
"requires_python": "<3.13,>=3.9",
"maintainer_email": null,
"keywords": "neovim, nvim, nvim-plugin, python, python-import",
"author": "Kiyoon Kim",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/8a/7a/ad24975e173d203bb8f8ef94ff11af28d7d1fff186aeeb431be7e9e81676/python_import-0.1.0.tar.gz",
"platform": null,
"description": "# \ud83d\udc0d python-import.nvim\n\n| | |\n|--|--|\n|[![Ruff](https://img.shields.io/badge/Ruff-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/ruff) [![StyLua](https://img.shields.io/badge/stylua-%232C2D72.svg?style=for-the-badge&logo=lua&logoColor=white)](https://github.com/JohnnyMorganz/StyLua) |[![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Style%20checking/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions)|\n| [![Ruff](https://img.shields.io/badge/Ruff-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/ruff) | [![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Linting/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions) |\n| [![pytest](https://img.shields.io/badge/pytest-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/pytest-dev/pytest) [![doctest](https://img.shields.io/badge/doctest-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://docs.python.org/3/library/doctest.html) | [![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Tests/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions) |\n| [![uv](https://img.shields.io/badge/uv-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/uv) | [![Actions status](https://github.com/kiyoon/python-import.nvim/workflows/Check%20pip%20compile%20sync/badge.svg)](https://github.com/kiyoon/python-import.nvim/actions) |\n\nA simple tool that auto-inserts import statements.\n\nhttps://github.com/kiyoon/python-import.nvim/assets/12980409/8a8f580f-16de-460c-af32-23fab7d2a35e\n\nIt matches with:\n\n1. [Pre-defined lookup tables](https://github.com/kiyoon/python-import.nvim/blob/master/lua/python_import/lookup_table.lua)\n2. Existing imports in the project (implemented in Python: `python-import` cli)\n - It finds the imports in the project and shows a list of them to choose from.\n3. pyright/basedpyright LSP completion\n4. Just `import <word>` :P\n\nThis plugin doesn't detect duplicated imports. Use `ruff` to sort imports.\n\n### Lookup table examples\n\n- `Path` -> `from pathlib import Path`\n- `np` -> `import numpy as np`\n- `torch` -> `import torch`\n- `logging` -> `import logging`\n- `logger` -> \n```python\nimport logging\n\nlogger = logging.getLogger(__name__)\n```\n\nMost will use the current word to find the import statement, but the treesitter node can be used to find the import statement more accurately. (e.g. `class SomeDataset(torch.utils.data.DataLoader)` -> `import torch.utils.data`)\n\n> [!WARNING]\n> This work-in-progress plugin is not yet ready for the public.\n> There aren't many customisation options (e.g. how to locate the import place)\n> and the behaviour and build/configuration instructions change rapidly.\n> If you don't want breaking changes, please fix the version of this plugin.\n\n## \ud83d\udee0\ufe0f Installation\n\n### Requirements\n\n- \ud83d\udcbb Neovim >= 0.10\n- pipx or uv (or any other way to install `python-import` cli in PATH)\n- ripgrep (`brew install ripgrep` or `cargo install ripgrep`)\n- fd (`brew install fd`, `cargo install fd-find` or `npm install -g fd-find`)\n\n\n### Install with lazy.nvim:\n\n```lua\n {\n \"kiyoon/python-import.nvim\",\n build = \"pipx install . --force\",\n keys = {\n {\n \"<M-CR>\",\n function()\n require(\"python_import.api\").add_import_current_word_and_notify()\n end,\n mode = { \"i\", \"n\" },\n silent = true,\n desc = \"Add python import\",\n ft = \"python\",\n },\n {\n \"<M-CR>\",\n function()\n require(\"python_import.api\").add_import_current_selection_and_notify()\n end,\n mode = \"x\",\n silent = true,\n desc = \"Add python import\",\n ft = \"python\",\n },\n {\n \"<space>i\",\n function()\n require(\"python_import.api\").add_import_current_word_and_move_cursor()\n end,\n mode = \"n\",\n silent = true,\n desc = \"Add python import and move cursor\",\n ft = \"python\",\n },\n {\n \"<space>i\",\n function()\n require(\"python_import.api\").add_import_current_selection_and_move_cursor()\n end,\n mode = \"x\",\n silent = true,\n desc = \"Add python import and move cursor\",\n ft = \"python\",\n },\n {\n \"<space>tr\",\n function()\n require(\"python_import.api\").add_rich_traceback()\n end,\n silent = true,\n desc = \"Add rich traceback\",\n ft = \"python\",\n },\n },\n opts = {\n -- Example 1:\n -- Default behaviour for `tqdm` is `from tqdm.auto import tqdm`.\n -- If you want to change it to `import tqdm`, you can set `import = {\"tqdm\"}` and `import_from = {tqdm = nil}` here.\n -- If you want to change it to `from tqdm import tqdm`, you can set `import_from = {tqdm = \"tqdm\"}` here.\n\n -- Example 2:\n -- Default behaviour for `logger` is `import logging`, ``, `logger = logging.getLogger(__name__)`.\n -- If you want to change it to `import my_custom_logger`, ``, `logger = my_custom_logger.get_logger()`,\n -- you can set `statement_after_imports = {logger = {\"import my_custom_logger\", \"\", \"logger = my_custom_logger.get_logger()\"}}` here.\n extend_lookup_table = {\n ---@type string[]\n import = {\n -- \"tqdm\",\n },\n\n ---@type table<string, string>\n import_as = {\n -- These are the default values. Here for demonstration.\n -- np = \"numpy\",\n -- pd = \"pandas\",\n },\n\n ---@type table<string, string>\n import_from = {\n -- tqdm = nil,\n -- tqdm = \"tqdm\",\n },\n\n ---@type table<string, string[]>\n statement_after_imports = {\n -- logger = { \"import my_custom_logger\", \"\", \"logger = my_custom_logger.get_logger()\" },\n },\n },\n\n ---Return nil to indicate no match is found and continue with the default lookup\n ---Return a table to stop the lookup and use the returned table as the result\n ---Return an empty table to stop the lookup. This is useful when you want to add to wherever you need to.\n ---@type fun(winnr: integer, word: string, ts_node: TSNode?): string[]?\n custom_function = function(winnr, word, ts_node)\n -- if vim.endswith(word, \"_DIR\") then\n -- return { \"from my_module import \" .. word }\n -- end\n end,\n },\n },\n \"rcarriga/nvim-notify\", -- optional\n```\n\n### Faster building with `uv`\n\n`pipx` is easy to configure, but `uv` is much faster. Make sure to install uv >= 0.2 and change the build script as follows:\n\nWith lazy.nvim:\n\n```lua\n {\n \"kiyoon/python-import.nvim\",\n build = \"bash scripts/build_with_uv.sh ~/.virtualenvs/python-import\",\n -- Other configurations ...\n },\n```\n\nThis simply creates a virtual environment in `~/.virtualenvs/python-import` and installs the cli there with `uv pip install .`. Then it sym-links the binary to `~/.local/bin/python-import`.\n\n### \ud83c\udfcb\ufe0f Health check\n\nRun `:checkhealth python_import` and see if python-import.nvim is installed correctly. \nYou need to disable lazy loading or run any commands in a python file to activate the plugin first.\n\n```\n==============================================================================\npython_import: require(\"python_import.health\").check()\n\npython-import ~\n- OK Using Neovim >= 0.10.0\n- OK `rg` is installed\n- OK `fd` is installed\n- OK `python-import` is installed\n- ERROR python-import cli (0.1.0+41.g9513418.dirty) and nvim plugin version (0.1.0+42.g9f74e1d.dirty) mismatch.\n```\n\n## \ud83d\udcbb `python-import` CLI\n\nThe `python-import` CLI is a simple Python script that counts the number of import statements used in the project.\n\n```\n$ python-import count /path/to/project np\n00004: import numpy as np\n```\n\n## TODO\n- [ ] Search class/function/variable definitions from project\n- [ ] Add more tests\n- [ ] Command to add imports in TYPE_CHECKING\n- [ ] Command to add lazy imports in methods\n- [ ] Command to add imports in jupytext cell\n- [ ] Search in ipynb files\n- [ ] VSCode-neovim integration (use `vim.ui.select` for vscode UI support. However, this complicates stuff because it runs async)\n - Currently, if you open the vscode-neovim output terminal, it is quite usable. The notification works as well. LSP doesn't work yet.\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2024 Kiyoon Kim Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "Neovim plugin for python autoimports.",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/kiyoon/python-import.nvim"
},
"split_keywords": [
"neovim",
" nvim",
" nvim-plugin",
" python",
" python-import"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8a7aad24975e173d203bb8f8ef94ff11af28d7d1fff186aeeb431be7e9e81676",
"md5": "a2764294705e523434301a361b48e8bf",
"sha256": "afff5dad80c7320d412d3ca1f60a154d159a1f668d77175b5c6117d9286f6c2d"
},
"downloads": -1,
"filename": "python_import-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "a2764294705e523434301a361b48e8bf",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13,>=3.9",
"size": 14587,
"upload_time": "2024-09-03T02:22:05",
"upload_time_iso_8601": "2024-09-03T02:22:05.697832Z",
"url": "https://files.pythonhosted.org/packages/8a/7a/ad24975e173d203bb8f8ef94ff11af28d7d1fff186aeeb431be7e9e81676/python_import-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-03 02:22:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kiyoon",
"github_project": "python-import.nvim",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "python-import"
}