{"id":13484704,"url":"https://github.com/relastle/vim-nayvy","last_synced_at":"2025-04-09T20:05:39.390Z","repository":{"id":55396679,"uuid":"238428909","full_name":"relastle/vim-nayvy","owner":"relastle","description":" :new_moon: Enriching python coding in Vim :snake: ","archived":false,"fork":false,"pushed_at":"2025-04-08T04:21:27.000Z","size":322,"stargazers_count":68,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T20:05:34.894Z","etag":null,"topics":["ale","coc","coc-sources","docstring","docstrings","import","neovim","productivity","python","snippets","ultisnips","vim"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/relastle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-05T10:56:34.000Z","updated_at":"2025-01-24T11:55:25.000Z","dependencies_parsed_at":"2024-01-13T19:18:16.041Z","dependency_job_id":"41d49bc8-4e9c-4966-96d7-c9afc18cd02f","html_url":"https://github.com/relastle/vim-nayvy","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relastle%2Fvim-nayvy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relastle%2Fvim-nayvy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relastle%2Fvim-nayvy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relastle%2Fvim-nayvy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/relastle","download_url":"https://codeload.github.com/relastle/vim-nayvy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ale","coc","coc-sources","docstring","docstrings","import","neovim","productivity","python","snippets","ultisnips","vim"],"created_at":"2024-07-31T17:01:30.759Z","updated_at":"2025-04-09T20:05:39.369Z","avatar_url":"https://github.com/relastle.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003evim-nayvy\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003eEnriching python coding.\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://pypi.org/project/nayvy/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/v/nayvy?color=%23032794\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/nayvy/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/pyversions/nayvy?color=032794\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/nayvy/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/l/nayvy?color=032794\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/relastle/vim-nayvy/releases\"\u003e\u003cimg src=\"https://img.shields.io/github/v/tag/relastle/vim-nayvy?color=032794\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/relastle/vim-nayvy/actions\"\u003e\u003cimg src=\"https://github.com/relastle/vim-nayvy/workflows/pythontests/badge.svg\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## 1. Installation\n\nUsing [vim-plug](https://github.com/junegunn/vim-plug)\n\n```vim\nPlug 'relastle/vim-nayvy'\n```\n\n## 2. Usage\n\n### 2.1 Commands\n\n#### NayvyImports\n\n![nayvy_imports](https://user-images.githubusercontent.com/6816040/76696704-9576a480-66d1-11ea-9561-b08914e263f4.gif)\n\n#### NayvyImportFZF\n\n![nayvy_import_fzf](https://user-images.githubusercontent.com/6816040/76696705-9ad3ef00-66d1-11ea-9d7c-cf62b7f597c0.gif)\n\n#### NayvyTestGenerate \u0026 NayvyTestGenerateFZF\n\n![nayvy_test_generate](https://user-images.githubusercontent.com/6816040/76715742-f608ee80-6770-11ea-9f8e-d156292c48d6.gif)\n\n### 2.2 Use with other plugin.\n\n#### 2.2.1 [ALE](https://github.com/dense-analysis/ale)\n\n[ALE](https://github.com/dense-analysis/ale), Asynchronous Lint Engine, is one of the SOTA plugins of Vim.\nEven though LSP is becoming more and more popular, the plugin provides brilliant features,\nincluding great abstraction against code fixer.\n\nThus, vim-nayvy provides code fixer funciton `nayvy#ale_fixer`.\n\nVim script Below is a example of using `nayvy#ale_fixer` with\n[autopep8](https://github.com/hhatto/autopep8) and [isort](https://github.com/timothycrosley/isort).\n\n```vim\nlet g:ale_fixers = {\n      \\ 'python': ['nayvy#ale_fixer', 'autopep8', 'isort'],\n      \\ }\n\n\" or if you already defined `g:ale_fixer`, write\nlet g:ale_fixers['python'] = ['nayvy#ale_fixer', 'autopep8', 'isort']\n```\n\nAnd here is demonstrations.\n\n![ale_fixer](https://user-images.githubusercontent.com/6816040/76823820-ac93ce80-6858-11ea-9a76-9c3b81b1b4e0.gif)\n\n#### 2.2.2 [ultisnips](https://github.com/SirVer/ultisnips)\n\n**Auto import when snippet expansion**\n\n`vim-nayvy` provides `auto_import` function used with UltiSnips' snippet.\nUltiSnips provides `post_expand` trigger for each single snippet,\nwhich executes prerefined command when the snippet is expanded.\n\nThe snippet fragment below import `auto_import` function from `nayvy`,\nand defines pretty print post fix completion that add import statement\n`from pprint import pprint as pp` if not exists.\n\n```\nglobal !p\nfrom nayvy_vim_if.ultisnips import (\n\tauto_import,\n)\nendglobal\n\npost_expand \"auto_import(snip, 'from pprint import pprint as pp', 0)\"\nsnippet '((\\S|\\.)+)\\.pp' \"pprint postfix completion\" r\n`!p\nvar_name = match.group(1)\nsnip.rv = \"pp(\" + var_name + \")\"\n`\nendsnippet\n```\n(The sample snippet is [here](./UltiSnips/python.snippets) and tested with a [vader script](./tests/test_ultisnips.vader))\n\nNote that three arguments of `auto_import` are\n- Snip object of UltiSnips. you should always pass `snip`\n- Import statement string\n- The import level\n  - 0: Standard library imports.\n  - 1: Related third party imports.\n  - 2: Local application/library specific imports.\n\n(cf. https://www.python.org/dev/peps/pep-0008/#imports)\n\nAnd here is demonstrations.\n\n![ultisnips_auto_import_demo](https://user-images.githubusercontent.com/6816040/76824986-00ec7d80-685c-11ea-8945-d7386b3f620f.gif)\n\n**Pydocstring expansionn**\n\n```\nglobal !p\nfrom nayvy_vim_if.ultisnips import (\n\tgenerate_pydocstring,\n)\nendglobal\n\npost_jump \"generate_pydocstring(snip)\"\nsnippet \"\"\" \"Pydocstring\" w\nendsnippet\n```\n\nThe snippet above enables the pydocstring expansion when the triple-quotes are triggered in a function.\n\nHere is a demonstration.\n\n![nayvy_ultisnips_pydocstring](https://user-images.githubusercontent.com/6816040/103473328-a7e5da00-4dda-11eb-91f8-9fb166cf1a9e.gif)\n\n\n\n#### 2.2.3 [coc.nvim](https://github.com/neoclide/coc.nvim)\n\nThis plugins also provide auto-completion of importable items, follewd by auto-importing of the completed items.\nThis is thanks to coc's ability to easily creating new coc sources\n[Create custom source · neoclide/coc.nvim Wiki](https://github.com/neoclide/coc.nvim/wiki/Create-custom-source).\n\nYou can see how auto-importing is conducted in the GIF below.\n\n![nayvy_coc](https://user-images.githubusercontent.com/6816040/89722514-38371300-da25-11ea-8af3-7b93643d8a46.gif)\n\nWhen you auto-complete Class/Function inside the current python project, the signature and docstring will also be shown like this.\n\n![nayvy_coc_info](https://user-images.githubusercontent.com/6816040/90265780-080ccb80-de8e-11ea-9922-99394018eed5.png)\n\nTo use this function, please bundle `neoclide/coc.nvim` as well as `relastle/vim-nayvy`.\n\n```vim\nPlug 'neoclide/coc.nvim', {'branch': 'release'}\nPlug 'relastle/vim-nayvy'\n```\n\nThe automatic import in completion is conducted by the item-selected event.\nBy default, the event is fired by \u003ckbd\u003eCtrl\u003c/kbd\u003e + \u003ckbd\u003ey\u003c/kbd\u003e (when the item is focused in pmenu).\n\nIf you use the following settings described [here](https://github.com/neoclide/coc.nvim#example-vim-configuration),\n\n```vim\n\" Use \u003ccr\u003e to confirm completion, `\u003cC-g\u003eu` means break undo chain at current\n\" position. Coc only does snippet and additional edit on confirm.\n\" \u003ccr\u003e could be remapped by other vim plugin, try `:verbose imap \u003cCR\u003e`.\nif exists('*complete_info')\n  inoremap \u003cexpr\u003e \u003ccr\u003e complete_info()[\"selected\"] != \"-1\" ? \"\\\u003cC-y\u003e\" : \"\\\u003cC-g\u003eu\\\u003cCR\u003e\"\nelse\n  inoremap \u003cexpr\u003e \u003ccr\u003e pumvisible() ? \"\\\u003cC-y\u003e\" : \"\\\u003cC-g\u003eu\\\u003cCR\u003e\"\nendif\n```\n\nthe completion event can be trigger by just \u003ckbd\u003eEnter\u003c/kbd\u003e (, which I personally recommend 👍).\n\n\n## 3. Configurations\n\n### 3.1 Configuration with vim `g:` variable or environment variable.\n\nAll configurable settings can be configured through vim script variables and environment variables.\n\nThis is because you may set some project-specific settings different from your global vim settings.\nThus, the priority of load settings is as follows.\n\n```\nEnvironment variable -\u003e Vim script variable -\u003e Default variable\n```\n\n| Vim Script variable name         | Environment variable name       | Description                                                                                   |\n| ---                              | ---                             | ---                                                                                           |\n| `g:nayvy_import_path_format`     | `$NAYVY_IMPORT_PATH_FORMAT`     | Define the import statement format when importing the class/function inside the same package. |\n| `g:nayvy_linter_for_fix`         | `$NAYVY_LINTER_FOR_FIX`         | Define the linter to use when autofixing the missing imports or unused imports.               |\n| `g:nayvy_pyproject_root_markers` | `$NAYVY_PYPROJECT_ROOT_MARKERS` | Define marker (filenames) indicating the python project root directory.                       |\n| `g:nayvy_import_config_path`     | `$NAYVY_IMPORT_CONFIG_PATH`     | Define the file path containing your own import statement lines.                              |\n| `g:nayvy_coc_enabled`            | `$NAYVY_COC_ENABLED`            | Define whether coc is enabled (1) or not (0).                                                 |\n| `g:nayvy_coc_completion_icon`    | `$NAYVY_COC_COMPLETION_ICON`    | Define icon rendered in the completion item fron nayvy coc sources.                           |\n| `g:nayvy_coc_menu_max_width`     | `$NAYVY_COC_MENU_MAX_WIDTH`     | Define max length of menu represented in completion menu by the coc source.                   |\n| `g:nayvy_cmp_enabled`            | `$NAYVY_CMP_ENABLED`            | Define whether cmp is enabled (1) or not (0).                                                 |\n\n#### g:nayvy_import_path_format ($NAYVY_IMPORT_PATH_FORMAT)\n\n- `all_absolute` (Prefer all project classes/functions are imported with ***absolute*** path.)\n- `all_relative` (Prefer all project classes/functions are imported with ***relative*** path.)\n- `under_relative` (Prefer sub-package classes/functions are imported with ***relative*** path and ther other with ***absolute*** path)\n\n\u003e default: `all_absolute`\n\n#### g:nayvy_linter_for_fix ($NAYVY_LINTER_FOR_FIX)\n\n- `ruff`\n- `pyflakes`\n- `flake8`\n\n\u003e default: `ruff`\n\n#### g:nayvy_pyproject_root_markers ($NAYVY_PYPROJECT_ROOT_MARKERS)\n\nExample of vim\n\n```vim\nlet g:nayvy_pyproject_root_markers = [\n  \\ 'pyproject.toml',\n  \\ 'setup.py',\n  \\ 'setup.cfg',\n  \\ 'requirements.txt',\n\\ ]\n```\n\nExample of environment variable\n\n```bash\nexport NAYVY_PYPROJECT_ROOT_MARKERS='pyproject.toml,setup.py'  # comma-separated format\n```\n\n\u003e default: `['pyproject.toml', 'setup.py', 'setup.cfg', 'requirements.txt']`\n\n#### g:nayvy_import_config_path ($NAYVY_IMPORT_CONFIG_PATH)\n\nsee the section below (3.2 Import configration).\n\n\u003e default: ``\n\n#### g:nayvy_coc_enabled ($NAYVY_COC_ENABLED)\n\n- 1: enabled\n- 0: disabled\n\n\u003e default: `1`\n\n#### g:nayvy_coc_completion_icon ($NAYVY_COC_COMPLETION_ICON)\n\n![nayvy_coc_completion_icon](https://user-images.githubusercontent.com/6816040/90264644-5ae58380-de8c-11ea-917b-c1cc24daa1d0.png)\n\nPlease set any string as you like 😄.\n\n\u003e default: ` nayvy`\n\n#### g:nayvy_coc_menu_max_width ($NAYVY_COC_MENU_MAX_WIDTH)\n\nIf the completion menu gets too wide, it may bother you.\nSo you can specify the max length of whole import statement.\nIf the statement length gets longer than the value, the `from` part of import statement(, which typically tends to be longer) will be trimmed.\n\n![nayvy_coc_completion_menu](https://user-images.githubusercontent.com/6816040/90264634-57ea9300-de8c-11ea-90da-768ada57d660.png)\n\n\u003e default: `-1` (no limit)\n\n### 3.2 Importing configuration\n\nNayvy detects import statement should be used by looking into\n`$XDG_CONFIG_PATH/nayvy/import_config.nayvy`.\n(if $XDG_CONFIG_PATH is not set, `~/.config/nayvy/import_config.nayvy`)\n\nIf you set `g:nayvy_import_config_path` or `$NAYVY_IMPORT_CONFIG_PATH`, the file will be used.\n\nYou can use environment variable in the path like,\n\n```vim\nlet g:nayvy_import_config_path = '$HOME/nayvy.py'\n```\n\nIn the file, you can write any python import statements like this.\n\n```python\nfrom typing import List, Optional\nfrom pprint import pprint as pp\nimport sys\nimport os\n\nimport numpy as np\n\nfrom .hoge import HogeHogeHoge as hoge\n```\n\nLine breaks seperating python import blocks are important,\ncause nayvy determines the line where a statement inserted by it.\n\nMy own setting is [here](https://gist.github.com/relastle/130f942699f8270c9fec587acbf80f30).\nFeel free to copy and paste and use it.\n\n\n## 4. Feature roadmap\n\n- [x] Auto imports (add and remove) based on pre-defined rules.\n- [x] Importing multiple modules using [fzf](https://github.com/junegunn/fzf).\n- [x] Touching or jumping to test script.\n- [x] Auto generating or jump to test function.\n- [x] Generating test functions using [fzf](https://github.com/junegunn/fzf).\n- [x] Providing some domain objects useful in creating [ultisnips](https://github.com/SirVer/ultisnips) snippets.\n- [x] Providing coc custom source which can insert import statement automatically.\n- [x] Utility function of converting function arguments to docstring (for UltiSnips).\n- [ ] Make auto generated Test template configurable. (Now unittest, standard library, is supported)\n\n## 5. Philosophy\n\nMost python code parsing algorithms of `vim-nayvy` is not strict, and it contains some heuristics\n( In other words, it is not based on AST, or hierarchical module structure).\nHowever, it is intentional. I personally think the heuristics works well for most real-world usecases,\nand has benefit in terms of performance.\nThe strategy is also robust against partially broken codes.\nSome code actions (such as implementing test function and jumping) should be executable\nwhen python code in current buffer is incomplete (cannot parsed via AST, or unimportable by `importlib`).\n\nNow, it is the era of [LSP](https://microsoft.github.io/language-server-protocol).\nI do not think prividing aid for code completion via non-LSP plugin is demanded,\nas I personally think code completion is one of the most powerful and stable features privided by LSP.\nThus, the main aim of `vim-nayvy` is providing a little bit utility functions :smile:\n\n\n## 6. Note\n\n#### :construction:\n\n- Please note that any destructive change (backward incompatible) can be done without any announcement.\n- This plugin is in a very early stage of development.  Feel free to report problems or submit feature requests in [Issues](https://github.com/relastle/vim-nayvy/issues), or make [PRs](https://github.com/relastle/vim-nayvy/pulls).\n\n## 7. [LICENSE](./LICENSE)\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frelastle%2Fvim-nayvy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frelastle%2Fvim-nayvy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frelastle%2Fvim-nayvy/lists"}