Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically add NuGet dependencies and using statements in Visual Studio Code?

I am using Visual Studio Code to develop an ASP.NET 5 application on Mac. In order to use new classes (framework or third-party) in my .cs file, I need to

  1. Manually add a NuGet dependency in project.json and then
  2. Manually add a using statement to my .cs file.

It seems that there should be a better way to import new functionality that doesn't involve searching for the right NuGet and the correct namespace. Any suggestions?

like image 936
Nikolai Samteladze Avatar asked Dec 25 '15 22:12

Nikolai Samteladze


People also ask

How do I add a NuGet package to VS code?

To find and install a NuGet package with Visual Studio, follow these steps: Load a project in Solution Explorer, and then select Project > Manage NuGet Packages. The NuGet Package Manager window opens. Select the Browse tab to display packages by popularity from the currently selected source (see Package sources).


2 Answers

Well, once I got my IntelliSense issues figured out (Visual Studio Code on Mac), I don't have to type using statements anymore. OmniSharp-based IntelliSense is smart enough to suggest (Cmd + . on Mac) adding them for me:

enter image description here

I still have to add a NuGet dependency manually, but I think this is the default behavior in the full Visual Studio too and you need ReSharper to get smarter than that.

enter image description here

like image 85
Nikolai Samteladze Avatar answered Oct 23 '22 07:10

Nikolai Samteladze


Unfortunately, as far as I know of, the short answer is no.

Visual Studio Code is meant to be a light weight editor, so does not have support for the kind of feature you are describing out of the box. The full Visual Studio on Windows does have support for that. When you type the name of a class/type you want to use, eg. JsonConvert, it will detect that the missing type is available on NuGet and offer you the ability to download the correct package and add the using statement. (In the screenshot I already have the NuGet package installed, so it only needs to add the using statement)

Roslyn Codefix

This feature is available in the Community edition of Visual Studio, which you can download for free from the visualstudio.com website. This does require you to run Windows, so I'm not sure if you consider this an option.

Now on the wishful thinking side: VS Code does support extensions these days and it has the power of the Roslyn engine, so theoretically someone could write an extension that will offer this functionality in the future. You could also try getting it added to the core editor, by opening an issue on GitHub: https://github.com/Microsoft/vscode/issues I'm afraid neither of these will really help you in the short term though.

like image 24
Christiaan Rakowski Avatar answered Oct 23 '22 07:10

Christiaan Rakowski