dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.91k stars 4.01k forks source link

Option to disable implicitly adding `using` directives #74492

Open logiclrd opened 3 months ago

logiclrd commented 3 months ago

If I type the name of an item from a namespace that is not imported, it automatically -- silently -- adds a using directive to the top of the file. Is it possible to turn this off? I don't want to get rid of the IntelliSense listing types from other namespaces, that's useful, but I personally would prefer selecting a non-imported type to simply generate a compile-time error. Many, many times now, I've found bizarre using directives at the top of files because at some point, I fat-fingered a type name. I backspaced it and corrected it, but the unnecessary using directive, added out of view, remains.

hansWurst-creator commented 3 months ago

I am having the same issue. With some packages where there are name conflicts its too easy to get the wrong using added silently and only noticing when the options of the method are not as expected.

genlu commented 3 months ago

I think this would help the problem you described https://github.com/dotnet/roslyn/issues/66898 @dibarbet do we have the infrastructure to implement the proposal above in vscode?

logiclrd commented 3 months ago

Hmm, in a round-about way that could work, but is it guaranteed to return the file to the state it was in before the directive was added?

For instance, I like to space out my usings into groups:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using DeltaQ.RTB.ActivityMonitor;
using DeltaQ.RTB.Diagnostics;
using DeltaQ.RTB.FileSystem;
using DeltaQ.RTB.Interop;
using DeltaQ.RTB.StateCache;
using DeltaQ.RTB.Storage;
using DeltaQ.RTB.SurfaceArea;
using DeltaQ.RTB.Utility;

If, in mistyping something, I accidentally refer to, say, ProvidedInstanceActivator from Autofac.Core.Activators.ProvidedInstance, then this will add a new using directive in the gap between the two:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Autofac.Core.Activators.ProvidedInstance;
using DeltaQ.RTB.ActivityMonitor;
using DeltaQ.RTB.Diagnostics;
using DeltaQ.RTB.FileSystem;
using DeltaQ.RTB.Interop;
using DeltaQ.RTB.StateCache;
using DeltaQ.RTB.Storage;
using DeltaQ.RTB.SurfaceArea;
using DeltaQ.RTB.Utility;

I correct my error and go on my merry way, and then when I save the document, the unused using directive gets removed -- it's going to leave the code like this, right?

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DeltaQ.RTB.ActivityMonitor;
using DeltaQ.RTB.Diagnostics;
using DeltaQ.RTB.FileSystem;
using DeltaQ.RTB.Interop;
using DeltaQ.RTB.StateCache;
using DeltaQ.RTB.Storage;
using DeltaQ.RTB.SurfaceArea;
using DeltaQ.RTB.Utility;

This is not the same as if it hadn't touched it at all. So, I still have to be on the look-out for it and be careful not to commit changes with random noise as a result of this function.

logiclrd commented 3 months ago

If we're talking about ideals, it would actually be pretty cool if unimported types were listed in IntelliSense, and selecting one and pressing Tab inserted the text of the type name but not the using directive, but popped open/switched to a submenu which can then either be ignored, or Tab can select an "automatically add the corresponding using directive" menu item. That way, if you're doing it on purpose, you can still get the using directive, but if it's because you mistyped something, it doesn't happen by default.

dibarbet commented 1 month ago

I think this would help the problem you described dotnet/roslyn#66898 @dibarbet do we have the infrastructure to implement the proposal above in vscode?

Yes, it could be implemented in vscode as well if we design it correctly.

I think this is a general Roslyn issue and applies to VS just as much as VSCode. Transferring this over to the roslyn side for tracking.