Insert Import statements needed for Python code completion in Neovim

Target audience of the article

--People who want to realize the IDE-like writing taste of Python with Neovim --People using coc.nvim

Introduction

In the integrated development environment (IDE) such as IntelliJ, it is as follows I think it is a common story that the Import statement required when making a completion decision is also inserted at the same time.

java_completion.gif

The purpose of this article is to make this relatively light when writing Python with vim.

What can be achieved

It's my work, but https://github.com/relastle/vim-nayvy With this plugin You can do the following:

nayvy_coc.gif

Introduction method

It can be installed below.

Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'relastle/vim-nayvy'

I'm just using the custom source feature of coc.nvim There is no need to install a separate coc plugin.

Since the import statement is inserted when the completion is decided If nothing is set, you need to explicitly press the (Ctrl-y) key to determine.

(See README for details)

I think it works with vim like with-python3.

Supplement

I think Python's import statement is a bit more specific than some other languages in the following ways:

--It is very common to import with another name (import xxxx as yy) --You can import a package (subpackage) and specify an internal function in its namespace, or you can directly specify the function to import (import xxxx | from xxxx import zzz).

More specifically

import os.path

os.path.dirname('hoge/fuga')
from os.path import dirname

dirname('hoge/fuga')

I think it's basically up to the writer to decide which one to write.

It's an extreme theory

import numpy as np

It is usually desirable to write, but for personal development (or if there is agreement within the team)

import numpy

It will be okay to develop as.

Due to these characteristics, the problem of automatically inserting import statements at the time of completion in Python is solved. I think it is difficult to solve it on the assumption that it will be used by everyone.

This plug-in solves the problem by having the user prepare a large number of import statements in advance.

By writing a large amount of import statements that I usually use in $ HOME / .config / nayvy / import_config.nayvy ($ XDG_CONFIG_PATH / nayvy / import_config.nayvy), I use it as a custom source. (If you don't put the file, only the standard library package / module will be completed.)

My personal story is that most of the time I do type inspection with mypy and develop it.

from typing import Any, Callable, ClassVar, Generic, Optional, Tuple, Type, TypeVar, Union, AbstractSet, ByteString, Container, ContextManager, Hashable, ItemsView, Iterable, Iterator, KeysView, Mapping, MappingView, MutableMapping, MutableSequence, MutableSet, Sequence, Sized, ValuesView, Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection, AsyncGenerator, AsyncContextManager, Reversible, SupportsAbs, SupportsBytes, SupportsComplex, SupportsFloat, SupportsInt, SupportsRound, ChainMap, Counter, Deque, Dict, DefaultDict, List, OrderedDict, Set, FrozenSet, NamedTuple, Generator, AnyStr, cast, get_type_hints, NewType, no_type_check, no_type_check_decorator, NoReturn, overload, Text, TYPE_CHECKING, Protocol

There is a line like this

import numpy as np
import pandas as pd
import tensorflow as tf
import seaborn as sns
import requests
import aiohttp
from logzero import logger
import yaml
import click
from click_help_colors import HelpColorsGroup, HelpColorsCommand
import pytest
import yaml

I write import statements for 3rd-party plug-ins that I use frequently like this.

Recommended Posts

Insert Import statements needed for Python code completion in Neovim
VS Code settings for developing in Python with completion
Summary of various for statements in Python
Settings for Python coding in Visual Studio Code
Specific sample code for working with SQLite3 in Python
Expose settings.json for efficient Python coding in VS Code
Python3> slice copy / slice notation> used in for statements, etc.
Python code for k-means method in super simple case
Dynamically import scripts in Python
Search for strings in Python
Python code memo for yourself
[Python] Insert ":" in MAC address
Techniques for sorting in Python
Useful tricks related to list and for statements in Python
Generate QR code in Python
[Python] Sample code for Python grammar
Character code learned in Python
About "for _ in range ():" in python
Create a Python environment for professionals in VS Code on Windows
Check for memory leaks in Python
Check for external commands in python
[Python] Generate QR code in memory
Working with LibreOffice in Python: import
Automatically format Python code in Vim
Write selenium test code in python
[TouchDesigner] Tips for for statements using python
Use Python in pyenv with NeoVim
Run unittests in Python (for beginners)
Code tests around time in Python
[Introduction] Insert line breaks in Python 3
Python> import this> Show Easter egg> Tried in online code execution environment
Code reading of Safe, a library for checking password strength in Python
Fourier series verification code written in Python
INSERT into MySQL with Python [For beginners]
difference between statements (statements) and expressions (expressions) in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Tips for dealing with binaries in Python
Type annotations for Python2 in stub files!
Precautions when using for statements in pandas
(For myself) Put Flask in VS Code
Template for writing batch scripts in python
[Python] Organizing how to use for statements
Process multiple lists with for in Python
MongoDB for the first time in Python
python> coding guide> PEP 0008 --Style Guide for Python Code
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
A tool for easily entering Python code
Code required for API conversion in sqlalchemy
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Get the EDINET code list in Python
Module import and exception handling in python
Notes on using code formatter in Python
R code compatible sheet for Python users
Import Error in Python3: No module named'xxxxx'
I searched for the skills needed to become a web engineer in Python