python-language-server
python-language-server copied to clipboard
Code action to add/delete imports
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
@gatesn is there any way to make the call to pyls_code_actions non blocking?
@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?
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.
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 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 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.
Any motion on this PR? Would love to have this functionality in the language server!
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
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.