-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#764 feature request: auto import #1032
#764 feature request: auto import #1032
Conversation
I think I will update to make use of |
I've tried to make use of existing Also, I have found the comment saying that As this is my first contribution I've tried to leave things as closely as they are. Some tests were failing because the last line now is handled slightly differently and now in some |
Please could you revert the changes to PythonImportSortProvider that changes its behaviour. Lets leave this PR specific to just the auto import feature. |
Oh yes, thanks for the great work. |
I've removed the changes I did for the import-sort part and added a few tests. I have tried using auto-import implementation done with |
Using symbol provider to locate where the symbol can be imported from. To improve: Position of imported line Importing modules Relative imports
sinon is very popular mock library (http://sinonjs.org/), also used in vscode tests.
Anything else I can do to improve this? |
damn, forgot about this. Will look into this tomorrow and release this soon. And thanks for the unit tests |
* 'master' of https://github.com/DonJayamanne/pythonVSCode: Fix #996 async with EXPR as VAR (#1108) fix changelog typo (#1107) #764 feature request: auto import (#1032)
Unfortunately this doesn't always work. |
Yes, but what could possibly be done if reference cannot be resolved? |
Sorry, didn't follow you here. |
Check whether the following gives you what you need: https://github.com/markbaas/python-iresolve |
I think it is best to use workspace-symbol-lookup to implement this feature. If symbol lookup as currently is implemented does not find symbols in majority of cases, then I think it's better to improve on symbol lookup, instead of using alternative symbol-resolver just for auto-import capability. |
I will have a look however, into iresolve in following few days to see if it can work better than ctags. |
Symbol lookup cannot work the way you suggest. First the code needs to tell the ide what it is using before the ide can figure it out for itself. It can't think for the user. What if your are using a method that exists on two separate modules? Which one do you want to use? |
Also the symbol lookup is provided by the python library Jedi |
It shows a quick-pick menu with all modules (this scenario is in the gif that I've attached).
Just to clarify, I think for auto-import suitable lookup is workspace-symbol-lookup So my idea was to run workspace-symbol-lookup, filter out results a bit, and display choices to user in a quick-pick menu. Instead of workspace-symbol-lookup it is sure possible to use different library, but I believe this functionality could be reused. |
I realised something just now, which I didn't know before, Workspace-Symbol-Lookup is not limited to python files. Looking for symbol might also display javascript (or other) files. So I will need to fix this in one of the ways:
However, I still think that WorkspaceSymbolProvider and auto-import should use same underlying library. |
Sorry if I am misleading with the terminology that I used. I am not sure if I correctly use these terms: "symbol-lookup", for to the jedi powered lookup of symbol in currently open file, under the cursor "workspace-symbol-lookup" symbol search across all workspace files which is available through |
I'm on 0.7.0 now, but I dont see this option when I open the Command Pallette and type autoimport... How does this work? Do I need to enable something? |
Unfortunately this feature is not available in the extension. |
Perhaps it would be much easier to use a python library for autoimports, importmagic does exactly that: |
@bluehezihhh could you provide more information, in what setup/when you run it, and if there was any stack-trace of the error? |
I'm afraid this will not work on 'sys', because symbols are looked up only in ctags file currently. So it won't work with modules It looks like https://github.com/alecthomas/importmagic that was mentioned here could do much better job than the current implementation with ctags. Woud be happy to give a go but at the moment I'm quite busy so unfortunately can't contribute much. |
Thanks a lot |
If I'm correct this feature is currently present in vscode 0.7 but not enabled/working. I also get "Running the command failed" error when I map the command to a shortcut. I believe this feature missing is one of the last things that will stop people from switching from fully-fledged IDEs like PyCharm. It just saves a ton of time compared to manually lookup and type imports. Also most required parts for this functionality is already present, like project symbols. |
Please let me know what should be improved or if you have ideas how to do things better, and I will try to implement it.
https://github.com/DonJayamanne/pythonVSCode/issues/764