Can VisualStudio adjust newline and formatting settings each time a file is opened? - visual-studio-2010

I'm working with a small team, and we're fairly evenly split between the following styles:
public void Method() {
// code
}
and:
public void Method()
{
// code
}
I know we can each setup Visual Studio (2010 Professional) to place New Lines where we want them, but that's only at the time the code is authored.
Is it possible to have the code re-formatted when it's opened (or by running an External Tool?) to the current user's preference, or do we really need to have a long debate about the best style (or abandon any hope of consistent code)?
(note: I am NOT asking which style is best, please don't try answering that question. Just can we have VS eliminate the debate by automatically re-formatting existing code into the style preferred at the moment?)
Thanks for any help!

You can create an envrimental events macro to do just that. This link tells you how to create one of these macros.
Private Sub DocumentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
DTE.ExecuteCommand("Edit.FormatDocument")
End Sub

Related

How can I add "comments" that don't become part of the code base?

Often when I am learning a new technology, I like to add verbose comments everywhere, so that if I come back and am confused about something, I can refer back to the comments. However, sometimes comments and certain frameworks (ahem React) don't mix well.
I'm looking for, perhaps an extension of some sort, that will allow me to place comments in Visual Studio that will never get placed in the actual source code. Something a la commenting in Excel.
I think that is a kind of complicated to find, an easy alternative that you can try are the #regions on C# to hide/ show your comments:
#region comments
/**
* Some comments here
*/
#endregion
public void myMethod(){
//TODO
}

Does C# 6 make it possible to imply type inference (not explicitly use the var keyword)?

This is an odd question, I know.
Earlier this week, I was browsing hacker news and glossed past an article about the power of C# 6. I meant to 'pocket' the link at the time but I didn't and now I can't find the article again.
Long story short, there was an example of code which looked roughly like this...
using static Variables;
public class Blah
{
public Blah()
{
s = "something"
Console.WriteLine(s);
}
}
... as if to say, somehow it was possible using some magical combination of C# 6 features to remove the 'var' element of that equation and have valid code.
I'm struggling to see how that's possible without some extra Roslyn magic and even then, I'd struggle.
If you know where the article is please let me know. If not, is this even remotely possible? I'm fairly confident it would be an odd thing to do,..I'm just intrigued.
Thanks in advance.

Sorting the Visual Studio Function List drop down to code order instead of alphabetical order

Colleague has asked the question and we were unable to find it - on the off chance thought it might be worth asking.
In a file you may have:
void BFunction()
void AFunction()
void CFunction()
now in the function list drop down, it'll have sorted those in alphabetical order:
AFunction
BFunction
CFunction
I prefer it this way actually. However my colleague is wondering if you can make that drop down sorted to the order the functions appear in the code, so that in the drop down it'd also be:
BFunction
AFunction
CFunction
Any suggestions?
I know thats this is not what you asking for, but you could try the VS10x CodeMap Visual Studio 2010 extension, it works the way you want.
But if you are very interested on changing the functionality of drop down list, i'm afraid that you would have to develop your own navigation bar.
Hope helps!

How do I get Visual Studio to generate code using System types (Int32) instead of built-in aliases (int)

Can I get Visual Studio to transform the built-in aliases into the System types? For example, if I define the following interface
public interface IExample
{
Int32 DoWork(String input);
}
and use VS to automatically generate the interface, I get the built-in types.
public class Demo : IExample
{
public int DoWork(string input) { }
}
I want those to automatically change to the System types
public class Demo : IExample
{
public Int32 DoWork(String input) { }
}
I'm not looking for a full installable solution, just a starting point. Can I write a script in VS that's hooked to text completion or on-save? Should I write an add-on that has a context menu item for projects - 'Convert aliases to System types'?
Note: I prefer the System types because they are formatted by VS like other types. Built-in aliases are formatted like keywords. Also, it's a coding style guideline at my current job.
Update: It's clear from MS that existing VS code-generation will always produce the built-in aliases.
The built-in aliases are formatted like keywords because the are keywords. The compiler performs the translation from language data type keywords to underlying CLR type for you.
There is nothing in Visual Studio that will automatically do this translation for you so you will need to write your own utility that will do this. There are several different ways to do this - you can write a macro or a VS plugin - but (IMHO) none of them will be trivial to write and ensure correctness.
You state that this is both personal preference and coding style requirements. I think the formatting concerns seem misplaced. It is generally easier/cleaner/preferred to use the language aliases instead of the CLR types. Other than the syntax coloring in the text editor there is absolutely no difference between the two and really no reasons to use the CLR names for your types. What are the reasons (if any) given in the coding style requirements for forcing the use of the CLR types instead of the language aliases?
The two types mean exactly the same thing, there is no danger that one will mean something different.
The only difference is how they look, if thats important to you, you can write a macro that will replace any built-in-aliases with System types.

Macro expansion in Visual Studio macro or add in

I have a VS project with an IntermediateDirectory like this: "....\temp\$(SolutionName)\$(ProjectName)".
I can read this value using a macro or add in, however, I would need the actual directory to manipulate files there. Right now, I manually replace the "$(SolutionName)" and "$(ProjectName)" with the respective values, which works fine but might become complicated when different macros or even user macros from property sheets are used.
So my question is:
Does the Visual Studio API have a built in function to expand macros like these? Or is there some other elegant solution?
There is an elegant solution! But I only know the one that applies to C++ projects.
Assuming you're in a C# add-in:
// Get the main project from the first startup project
VCProject vcMainProject = (VCProject)(_applicationObject.Solution.SolutionBuild.StartupProjects as IVCCollection).Item(1);
Project mainProj = (Project)_vcMainProject .Object;
// Get the configuration we'll be using
IVCCollection cfgs = (IVCCollection)_vcMainProject .Configurations;
VCConfiguration vcCfg = (VCConfiguration) cfgs.Item(mainProj.ConfigurationManager.ActiveConfiguration.ConfigurationName + "|" + mainProj.ConfigurationManager.ActiveConfiguration.PlatformName);
string finalString = vcCfg.Evaluate("....\temp\$(SolutionName)\$(ProjectName)");
You can also check out this page:
http://msdn.microsoft.com/en-us/library/czt44k0x%28VS.71%29.aspx
If you're not using this for C++, there should be a similar interface for the Project, Configuration, and Solution classes provided for other languages (C# and VB).
As far as i know, there is no API available that will expand those macro values. Although it shouldn't be too hard to write a quick and dirty implementation that deals with only the values that you care about.
For instance, in this case you only care about 2 values (SolutionName and ProjectName). If these are the values you are primarily interested in use a simple search and replace with the best values.
Yes this is a sub-optimal solution. But it may help to unblock your progress.

Resources