python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

Code action to add/delete imports

Open youben11 opened this issue 5 years ago • 10 comments

This is based on the previous work of @gatesn

  • Actions to import suggested modules based on the index of symbols of all available python modules (might be slow to build but cached for future use)
  • Linting hint about unresolved symbols only as we aren't sure if those symbols aren't just variables.

Last features

  • Actions to remove unreferenced imports
  • Linting warning about unreferenced imports, variables and functions

related #86

youben11 avatar Oct 28 '19 21:10 youben11

@gatesn is there any way to make the call to pyls_code_actions non blocking?

youben11 avatar Oct 29 '19 20:10 youben11

@youben11 I've checked out your version and tried it with a Webdriver/Selenium sample. Works fantastic to e.g autoimport from selenium import webdriver. What doesn't work though is importing local references from another file. Is that the supposed way how importmagic works?

zewa666 avatar Nov 05 '19 09:11 zewa666

Hi @zewa666, thank you for testing this functionality and providing feedback.

Yeah the index of symbols is built from the python modules found under sys.path, but I will certainly add the local workspace so you can have suggestions to import from project files.

youben11 avatar Nov 09 '19 16:11 youben11

The importmagic doesn't return the location of unreferenced/unresolved symbols so we needed to look them up ourselves, the search logic was a simple naive .find() call which might return false positives (e.g 'os' is found in 'pos'). Next I will be looking on how to simply tokenize the source code and search among the tokens so we make sure the matching is correct.

youben11 avatar Nov 14 '19 20:11 youben11

@youben11 perhaps you could use Jedi's names() call instead of walking the AST yourself. It may help, haven't verified: https://jedi.readthedocs.io/en/latest/docs/api.html#jedi.names

gatesn avatar Nov 15 '19 10:11 gatesn

@gatesn the disadvantage with jedi.names is that it does only return definitions, symbols such as function calls aren't returned. The implementation using tokenize is pretty straightforward, however, I have to deal with the two different version of python here as the function takes different parameters for py2 and py3.

Update

Actually there is a backward compatible function to use that works for both version.

youben11 avatar Nov 15 '19 13:11 youben11

Any motion on this PR? Would love to have this functionality in the language server!

gjabell avatar Jul 07 '20 12:07 gjabell

Hi there! Thanks for working on the import function :+1:

After reading the comments it is not clear to me what is left to do. Since I really want this feature I would be happy to contribute - if I would know hat to do :D

weeman1337 avatar May 02 '21 08:05 weeman1337

This project is not maintained anymore. However, the community created a fork of it at

https://github.com/python-lsp/python-lsp-server/

So please submit this PR there if you want to see it finally merged.

ccordoba12 avatar May 02 '21 14:05 ccordoba12