Skip to content
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

Merged
merged 3 commits into from
Jul 22, 2017
Merged

#764 feature request: auto import #1032

merged 3 commits into from
Jul 22, 2017

Conversation

skyjur
Copy link
Contributor

@skyjur skyjur commented Jun 19, 2017

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

autoimport

@skyjur
Copy link
Contributor Author

skyjur commented Jun 20, 2017

I think I will update to make use of isort -a (https://github.com/timothycrosley/isort#adding-an-import-to-multiple-files)

@skyjur
Copy link
Contributor Author

skyjur commented Jun 22, 2017

I've tried to make use of existing importSortProvider.ts, and refactored it to support addImport().

Also, I have found the comment saying that --diff doesn't work together with passing code as stdin on isort, but in my testing it worked just fine. So I've updated to use stdin. I think it makes some things simpler as well as it should be more efficient than creating a temporary file.

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
cases it doesn't produce a TextEdit for the last line where it used to. I've refactored out very long lines while working on fixing them.

@DonJayamanne
Copy link
Owner

Please could you revert the changes to PythonImportSortProvider that changes its behaviour. Lets leave this PR specific to just the auto import feature.
Please create a separate PR that makes changes to PythonImportSortProvider to improve it with the changes to the arguments, etc.
its easier to focus on one change at a time in each PR, easier for testing too.

@DonJayamanne
Copy link
Owner

Oh yes, thanks for the great work.

@skyjur
Copy link
Contributor Author

skyjur commented Jul 4, 2017

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 isort -a for a while, and I found that, at least for me it was very inconvenient to have isort running everytime on auto-import. Because sometimes isort would just mess up my imports and I had no way out of it. Having auto-import added on first line, and manually moving it to correct position felt more acceptable to me.

skyjur and others added 3 commits July 6, 2017 11:26
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.
@skyjur
Copy link
Contributor Author

skyjur commented Jul 20, 2017

Anything else I can do to improve this?

@DonJayamanne
Copy link
Owner

damn, forgot about this. Will look into this tomorrow and release this soon.
Unfortunately I released a new version a moment ago. Sorry, thanks again for your great effort. I've been wanting to work on an auto import. Please hang in there, i'll release this soon.

And thanks for the unit tests

@DonJayamanne DonJayamanne merged commit 2d36152 into DonJayamanne:master Jul 22, 2017
DonJayamanne added a commit that referenced this pull request Jul 24, 2017
* '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)
@DonJayamanne
Copy link
Owner

Unfortunately this doesn't always work.
I've tested this. It works only when the editor is able to resolve references, and I don't believe that'll work majority of the time.
I should have informed you about this earlier, but I will be reverting the change.

@skyjur
Copy link
Contributor Author

skyjur commented Jul 24, 2017

It works only when the editor is able to resolve references

Yes, but what could possibly be done if reference cannot be resolved?

@DonJayamanne
Copy link
Owner

DonJayamanne commented Jul 24, 2017

Yes, but would could be done if reference cannot be resolved?

Sorry, didn't follow you here.

@DonJayamanne
Copy link
Owner

Check whether the following gives you what you need: https://github.com/markbaas/python-iresolve

@skyjur
Copy link
Contributor Author

skyjur commented Jul 24, 2017

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.

@skyjur
Copy link
Contributor Author

skyjur commented Jul 24, 2017

I will have a look however, into iresolve in following few days to see if it can work better than ctags.

@DonJayamanne
Copy link
Owner

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?

@DonJayamanne
Copy link
Owner

Also the symbol lookup is provided by the python library Jedi

@skyjur
Copy link
Contributor Author

skyjur commented Jul 24, 2017

What if your are using a method that exists on two separate modules? Which one do you want to use?

It shows a quick-pick menu with all modules (this scenario is in the gif that I've attached).

Also the symbol lookup is provided by the python library Jedi

Just to clarify, I think for auto-import suitable lookup is workspace-symbol-lookup
Jedi is used for active editor symbol-lookup. However this is limited to only symbols that already had been imported and can be resolved to single definition in a file.
Workspace symbol lookup (Ctrl+p followed by #) is using ctags from what I've seen (https://github.com/DonJayamanne/pythonVSCode/blob/master/src/client/workspaceSymbols/provider.ts/)

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.

@skyjur
Copy link
Contributor Author

skyjur commented Jul 24, 2017

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:

  1. add filtering by file type
  2. call WorkspaceSymbolProvider directly
  3. use iresolve (If i find that it works) instead of WorkspaceSymbolProvider

However, I still think that WorkspaceSymbolProvider and auto-import should use same underlying library.

@skyjur
Copy link
Contributor Author

skyjur commented Jul 24, 2017

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 Ctrl+P followed by #

@dhensen
Copy link

dhensen commented Aug 10, 2017

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?

@DonJayamanne
Copy link
Owner

Unfortunately this feature is not available in the extension.

@ghost
Copy link

ghost commented Aug 24, 2017

Perhaps it would be much easier to use a python library for autoimports, importmagic does exactly that:
https://github.com/alecthomas/importmagic

@blueboxH
Copy link

blueboxH commented Sep 5, 2017

How do I use this feature? When I try to use it , this error occurs.
Running the contributed command:'python.autoImportAtCursor' failed.
image

What should I do? Thank you very much

@skyjur
Copy link
Contributor Author

skyjur commented Sep 5, 2017

@bluehezihhh could you provide more information, in what setup/when you run it, and if there was any stack-trace of the error?

@blueboxH
Copy link

blueboxH commented Sep 5, 2017

I do it on windows10,and this is my path.
image

I can run "python" and "ctags" command in cmd. And I set as this
image
But when I select impotr like this,I can not find it
image
finally I press "alt+enter" when Cursor at "sys" like this
image
but I can't find any information about the warning. what should I do? Thank you very much!

@skyjur
Copy link
Contributor Author

skyjur commented Sep 5, 2017

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 import x and only works with from x import y.

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.

@blueboxH
Copy link

blueboxH commented Sep 5, 2017

Thanks a lot

@ferdynice
Copy link

ferdynice commented Sep 23, 2017

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.

Goom11 pushed a commit to mostafaeweda/pythonVSCode that referenced this pull request Aug 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants