Roslyn: Offer code fix to add usings for pasted code snippets

Created on 2 Apr 2016  Â·  32Comments  Â·  Source: dotnet/roslyn

Filed on behalf of a customer from interviews

Area-IDE Feature Request Resolution-Fixed

Most helpful comment

I don't auto-add these on paste. I think we should offer a code fix after pasting so that my file isn't dramatically changed simply by pasting code from one to another unless I, as the developer, choose to. :smile:

All 32 comments

I don't auto-add these on paste. I think we should offer a code fix after pasting so that my file isn't dramatically changed simply by pasting code from one to another unless I, as the developer, choose to. :smile:

Posting on behalf of customer from interviews May-2017.

The request is to have the ability to resolve all imports with one action.
Similar to Eclipse (and maybe ReSharper), a user can paste code without typing in the usings.
The editor will prompt (or the user can call an explicit action) to have all the unresolved references imported (prompting the user in turn for any ambiguous references / multiple choices).

Note: Request includes the ability to activate this refactoring with a single key action.
We could assign a default shortcut key to it. Or just have a command that users can assign their own shortcut to.

Thought: Could this action be triggered when users call "Remove and Sort Usings"?
Perhaps that's a bad idea.

Scenario:
It’s possible that I have just added a Util file to my project – copied from another project.
When I first open the file, many of the references are invalid or missing.
I apply the command to fix all usings.
If there are no ambiguities, it adds all required usings to the top of the file.
If there are ambiguities (either multiple options, or no valid option), it steps the user through them one-by-one.
If the user has a selection when they activate the command, it will only resolve missing references for the selection.

Suggestion:
Scope this to Current Selection (if one exists), else Current Document.
Applying fixes to other documents could be considered out-of-scope for this issue.

The shortcut everyone uses to organize and add missing imports in Java is ctrl-shift-O (organize imports shortcut). The request here is for the same feature for C#.

Specifically Ctrl+shift+O? I think that's a global VS "open project" shortcut

The point is not the specific keyboard shortcut, but the functionality it provides.

Of course. I just wanted to clarify what you meant.

In IntelliJ, the shortcut is Ctrl+Alt+O to organize imports.
They use Alt+Enter to import missing usings (one by one). But unlike other refactorings, this one pops up pro-actively as a suggestion and moves to the next unresolved import immediately after until they are all resolved.

feature will be available via PR in 16.0.Preview1

What was implemented? (The discussion mentioned various possibilities.)

@justcla A code refactoring that is available following a paste operation where there are missing imports.

Nice.

Now if only we could go one step further and have the refactoring appear for any unused imports.
Should I create a new issue for that?

If it is a behavior that you want, please do. =)

Now if only we could go one step further and have the refactoring appear for any unused imports.
Should I create a new issue for that?

Could you clarify what you mean by that?

Ideally, any time there is a missing import, a suggestion should be given to import the missing namespace.
We can do this after paste. But we could also do this on file open. And when text is typed.

IntelliJ do this by popping a tooltip near the closest instance of missing import. Pressing Alt-Enter activates the refactoring, which gives a choice of namespaces to import.

As soon as one missing import is resolved, focus moves to the next instance, allowing you to easily resolve all the missing imports in the file.

Ideally, any time there is a missing import, a suggestion should be given to import the missing namespace.

Sorry... how is this different from teh existing 'add import' feature? Thanks!

IntelliJ do this by popping a tooltip near the closest instance of missing import. Pressing Alt-Enter activates the refactoring, which gives a choice of namespaces to import.

Yup. We've had that since 2005 :)

As soon as one missing import is resolved, focus moves to the next instance, allowing you to easily resolve all the missing imports in the file.

I'm not a fan of that idea at all... if i add an import on one line, i often want to then just keep working on that line. Jumping to the next issue seems like that would really screw things up. If you do want that, i would prefer you just navigate using the error list. Or we have a keystroke to navigate through fixable errors.

Ideally, any time there is a missing import, a suggestion should be given to import the missing namespace.

Sorry... how is this different from the existing 'add import' feature? Thanks!

The key difference is in how it proactively pops up. No need to navigate to the error, then invoke an action.

IntelliJ do this by popping a tooltip near the closest instance of missing import. Pressing Alt-Enter activates the refactoring, which gives a choice of namespaces to import.

Yup. We've had that since 2005 :)

No, we don't have this. In our version, the user must painfully navigate to the error first (as you described later), then separately invoke the lightbulb, then start looking for import choices. Instead, without moving your cursor, you could see a pop-up that says "Press Alt+Enter to import System.X.Y". You press Alt+Enter, errors go away and you keep coding without taking your caret away from where you were.

As soon as one missing import is resolved, focus moves to the next instance, allowing you to easily resolve all the missing imports in the file.

I'm not a fan of that idea at all... if i add an import on one line, i often want to then just keep working on that line. Jumping to the next issue seems like that would really screw things up. If you do want that, i would prefer you just navigate using the error list. Or we have a keystroke to navigate through fixable errors.

Maybe the view can stay in place. But when you think about it, do you really want to do anything at all while your file has missing imports? The model of "address all missing imports as soon as possible" seems fairly sensible. The file you're working with will be much more usable once the compiler understands all the classes. And to fix all without having to manually navigate to each one, is a huge timesaver. It's the sort of thing you appreciate once you've used it.
And then the act of having to navigate to every single missing import in the file is massively tiresome!

The key difference is in how it proactively pops up. No need to navigate to the error, then invoke an action.

I'm not getting why this is a desirable interaction experience. Why am i getting prompted to do things unrelated to the code i'm working on?

No, we don't have this. In our version, the user must painfully navigate to the error first

Why is this painful? If i'm not even at the code in question... how do i know what the right thing is to do? :)

Instead, without moving your cursor, you could see a pop-up that says "Press Alt+Enter to import System.X.Y"

That seems super random. Why am i being asked this? Is System.X.Y useful in some way? Do i need it? Why is it appropriate to add it?

What if it says "add this reference, or this nuget package"? It feels really spooky to me that it's just asking me these questions and i have no context to go "yup, that's definitely what we want for this file". With an explicit action, or local context, i can see why it is offering me these things.

You press Alt+Enter, errors go away and you keep coding without taking your caret away from where you were.

I mean, if editor wants to add a feature where alt-enter just opens the lightbulb up if one exists somewhere in the file, i guess that can happen. But it seems super weird to me. Will it also have things like "generate method foo" and all the other fixes that could be applied depending on errors in unrelated locations in the file?

And then the act of having to navigate to every single missing import in the file is massively tiresome!

I don't have any idea waht that means. I just hit f8 to cycle through my issues. If you want an editor shortcut to cycle through fixable problems in the file, i would not be opposed to it. However, i definitely want to actually see (or have some understanding) of what code is triggering the fix suggestion. Otherwise, it just feels like some random suggestion that doesn't make sense to me as to why it is happening.

Maybe the view can stay in place. But when you think about it, do you really want to do anything at all while your file has missing imports?

Missing imports arise for me when i'm writing new code (in which case, i'm in the right location), or i'm doing something like taking a chunk of other code and pasting it in (in which case this feature applies). It's unclear to me the scenario where somehow i now have a bunch of unimported namespaces, but i'm now someplace else totally random in the file...

I understand it seems strange. I suppose you've never used it before. Some things just need to be seen and tried to understand their usefulness.

This would be a pop-up specific for imports only - not for any other kind of lightbulb action.
And the reason you'd want it is because the imports are missing and files are pretty useless with missing imports.

Of course, there are many slow and painful ways to address fixes and imports. (Stepping through an error list is one.) But it's nice to be looking for ways to improve the process. Similar to a one-click code cleanup. You could argue that a user can simply navigate to each issue and address it. But isn't it nice when the tooling helps you out a bit? ;-)

The first time I saw this, I thought "Hey that's odd... why are you prompting me to fix this missing import?" But I just pressed Alt+Enter, the import was added, the error went away, and I kept typing.

You get left thinking... "Thanks!" Delightful!

I'm available on gitter. i'd like to chat about this more.

And the reason you'd want it is because the imports are missing and files are pretty useless with missing imports.

Files are useless without missing references as well :) and without missing nuget packages. And without other missing files in the same project containing the types they need. Etc. etc. I mean, code that doesn't compile is pretty useless :)

The first time I saw this, I thought "Hey that's odd... why are you prompting me to fix this missing import?" But I just pressed Alt+Enter, the import was added, the error went away, and I kept typing.

Sure. Which is the experience you get today when... you know... you're typing :D

The first time I saw this, I thought "Hey that's odd... why are you prompting me to fix this missing import?" But I just pressed Alt+Enter, the import was added, the error went away, and I kept typing.

Sure. Which is the experience you get today when... you know... you're typing :D

Today, you must first navigate to the line that contains the missing import.
Then you must press Alt+Enter just to open the lightbulb.
Then you must press Enter to select the import (if it's the first choice), or else press arrow keys to get the import, then press Enter.

This feature would do all that with one keypress, and without the need to navigate to the offending line.

Today, you must first navigate to the line that contains the missing import.

Right. but you said: But I just pressed Alt+Enter, the import was added, the error went away, and I kept typing.

Presumably you had the missing import because you typed something that needed that import. So you had contxt about what it was going to add. I'm trying to figure out the scenario of:

i was typing in some totally different location, and the editor then told me: hey, for you should add an import, so that these other non-visible locations will be fixed.

Today, you must first navigate to the line that contains the missing import.

Sure.... that seems good. Now i know what code it's trying to fix :)

Then you must press Alt+Enter just to open the lightbulb.

Yup. Because maybe adding the import is appropriate. Or maybe adding a package is appropriate. Or maybe a reference. Or maybe i need to fix the spelling of something :) etc. etc.

Then you must press Enter to select the import (if it's the first choice), or else press arrow keys to get the import, then press Enter.

Right... hence why this is good. If you have to make a decision, then being at the code in question is really useful.

Ok. Talked offline. What sounds viable is a shortcut, or experience, where we may be a little more agressive about prompting ot add imports. But really for only code that is in view. This helps provide necessary context so that people know what the suggestion is in reference to.

We also discussed that this may need to be an opt-in feature. i.e. "automatically prompt to add missing imports" so that people don't get too annoyed if this starts appearing.

Overall, i like these ideas. though i definitely think the need for them is mitigated somewhat by the presence of the new "introduce imports on paste" feature. But it's still definitely worth thinking about in the future!

Agreed. Nicely put, Cyrus. Thanks.

Though, I'd prefer an opt-out experience (i.e. on by default) - same as it would be for Paste.

Was this page helpful?
0 / 5 - 0 ratings