neoclide / coc-python

Python extension for coc.nvim, fork of vscode-python
574 stars 51 forks source link

Automatic imports #90

Open harryjubb opened 5 years ago

harryjubb commented 5 years ago

Hi,

Fantastic tool!

Is there any scope to support automatic imports, for example, if I write:

pprint("Hello world")

That then automatically (or via a :CocCommand) adds:

from pprint import pprint

At the top of the file; or if there are multiple possibilities, offers the user a choice in a list.

I haven't used it, but it looks like it would be functionality similar to vscode-importmagic. It looks like it is proposed for the Microsoft Python Language Server also.

dagadbm commented 4 years ago

any news?

chemzqm commented 4 years ago

It have to be done by the language server, when MPLS support it, you should be able to use it with coc.nvim.

eugenhu commented 4 years ago

This seems to be working now, not sure when exactly it was introduced but see

https://github.com/microsoft/python-language-server/issues/19

ACLeiChen commented 4 years ago

It still doesn't work for me in coc. In vscode, I can trigger this by hover my mouse to the symbol and clicking Quick fix.... But in coc, even if I :call CocActionAsync('doQuickfix'), I got [coc.nvim] No quickfix action available

chemzqm commented 4 years ago

It's feature of MPLS, you won't have this when jedi enabled.

ACLeiChen commented 4 years ago

I have "python.jediEnabled": false in my coc-settings.json, I also tried calling :CocCommand python.upgradePythonLanguageServer, but the result is the same.

chemzqm commented 4 years ago

Hm, the client code may need some change as well, please give me an example which works with VSCode.

eugenhu commented 4 years ago

Haven't tried 'quick fix', but the auto import option is there for me when I do 'code actions' on the unimported module.

last-partizan commented 4 years ago

It's feature of MPLS, you won't have this when jedi enabled.

https://devblogs.microsoft.com/python/announcing-pylance-fast-feature-rich-language-support-for-python-in-visual-studio-code/

Looks like it's feature of pylance, new language server for python. And mpls will be deprecated.

Our long-term plan is to transition our Microsoft Python Language Server users over to Pylance and eventually deprecate and remove the old language server as a supported option.

relastle commented 4 years ago

I have been developing a plugin that would satisfy the feature.

https://github.com/relastle/vim-nayvy (Especially coc.nvim custom source part is what you really want https://github.com/relastle/vim-nayvy#223-cocnvim)

The plugin uses some heuristics, but it seems works so well for me to import functions or classes inside python's project (or you can configure the statements to import as you like, pls see https://github.com/relastle/vim-nayvy#3-configurations).

dagadbm commented 4 years ago

I believe pyright is doing this already

On Tue, Aug 11, 2020 at 4:58 PM Hiroki Konishi notifications@github.com wrote:

I have been developing a plugin that would satisfy the feature.

https://github.com/relastle/vim-nayvy (Especially coc.nvim custom source part is what you really want https://github.com/relastle/vim-nayvy#223-cocnvim)

The plugin uses some heuristics, but it seems works so well for me to import functions or classes inside python's project (or you can configure the import statements to import as you like see https://github.com/relastle/vim-nayvy#3-configurations).

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/neoclide/coc-python/issues/90#issuecomment-672042994, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD7WGR4EA22FK3HYQGV4WHTSAFTBHANCNFSM4IVH4FZA .

last-partizan commented 4 years ago

I believe pyright is doing this already

Yesterday i installed coc-pyright, and it's pretty good expirience out of the box, including completions, renaming and autoimports. But for now it's lacking support for django dynamic methods, like get_FOO_choices for fields with choices, and the doesn't plan to add plugin support until project is more mature.

xulongwu4 commented 4 years ago

@last-partizan Can you elaborate a bit about how do to autoimports with coc-pyright? Thanks.

last-partizan commented 3 years ago

@xulongwu4 they just works, you write datac, it shows menu with autoimport variants.

image

xulongwu4 commented 3 years ago

Thanks @last-partizan. autoimport doesn't seem to work if I just open a file and type datac. I found this gif which seems to explain well how auto-import works.