Visual Studio 2008 macro to switch between header and source files? - visual-studio

Does anyone know how to make a macro or something to switch between foo.hpp and foo.cpp? I would really appreciate having a macro like this. I guess it would help if it actually opened the corresponding file, but kept the old one in a tab.
Thanks

Perhaps one (or a combination) of the following will help you:
Switch between a Header and a CPP File in .NET (a macro at codeproject)
Switch between header and impl files in VS.NET (describes how to create the macro and bind it to a key)
Switch between Source and Header C++ Files (A recent macro from codeguru)
Macro to Switch Between Header and CPP File (An old macro from codeguru)
Several other solutions found on Google...

Why not use right click > Go to declaration | Go to definition?

In Visual Studio 2013 it was added as Ctrl + K, Ctrl + O

Some of the options ran slow for me in large projects. This one doesn't look in other folders but works faster and should cover most needs:
switch between .cpp & .h files

You could try using 'Switch' - this is an addin that switches between source and header files, but also lets you switch between other types of related files - XAML and codebehind, designer and code etc etc.
Switch

Related

Automatic Clean-Up of Code in Visual Studio 2010

I am wondering, if it is possible, to have some kind of automatic code-clean-up in Visual Studio 2010.
Especially interesting would be:
Auto-Indent
Remove tailing empty lines
Remove unused usings
at special cases, e. g. on save of the file.
I haven't found anything in the options, but maybe I was just kinda blinded... ...afaik in eclipse it was at least possible to intend on save. There should be something like that in Visual Studio?
UPDATE: Thanks for the fast replies, the thing is that I am ware of the keyboard-shortcuts (but nevertheless thanks for mentioning), but I am working through tons of foreign code by now and it would be awesom not having to do it manually for every file :)
Auto-indent can be done with Edit/Advanced/Format Document, or equivalent keyboard shortcut (Ctrl-K, Ctrl-D for me, YMMV).
Tailing empty lines I'm not aware of an automatic solution for, though there may be an extension available (or you could write your own).
Removing unused usings is a built in VS refactoring, and also available in tools like Resharper or DevExpress. (Right click code, then Organize Usings/Remove and sort.)
I'm not aware of a standard way to have these happen automatically for every document save, but you could probably write a macro or VS extension that would make this happen.
Some of the above already exist directly in Visual Studio:
Ctrl - K - D will reformat the document (Edit -> Advanced -> Format Document).
There is a refactoring that deals with using statements (right click in the code, there is an option for "Organise Using").
As for empty lines, I don't know of anything built in.
But tools like Resharper do have code cleanup capabilities that deal with all of the above and more (Resharper menu -> Tools -> Cleanup Code).
None of the above will execute on save by default, but you should be able to write a macro that runs on the save event.
You could try CodeMaid. Its an open source solution for what you are describing.
To Auto-indent you can ue ctrl+k+d pretty much like ctrl+shift+f in eclipse. But for unused usings I dont know any.

Cleanup spacing - C++ / Visual Studio 2010

Is there a built-in feature or available add-on for Visual Studio 2010 that will clean up spacing in C++ code so that annoying blocks like this:
RandomVar=RandomList.RandomMethod();
will become
RandomVar = RandomList.RandomMethod();
(Same goes for spacing in loops, etc.)
...or do I need to do this myself with find/replace and regex?
I think You are looking for Edit->Advanced->Format document. This will re-format your current document according to settings in Tools->Options->Text Editor->c/c++->Formatting.
It's keyboard shortcut Ctrl+E+D in my case.
Although originally for C code formatting lint has been modified to cover C++ and ported to many platforms.
Some lint variants only report inconsistent layout, others can fix it for you. some are free others paid for versions.
This would be an outside the IDE fix as this is a sperate tool (at least traditionally it is!)
This wikipeadia article lists a few possible sources for lint tools
(this was a footer in my orginal question, but as that posed two solutions I extracted it here so you can accept a specifc answer should you find one apropriate)
This is not a proper "Solution" but a possible work-around without the need for finding external tools
In the "Tools" menu pick "Options..."
In this dialog navigate to
"Text Editor -> C/C++ -> Formatting"
Set the layout options as you
would like your code to look.
Click OK.
Now in a unit with "bad"
formatting from the menu select "Edit->Advanced->Format document" and the IDE wil reformat the
document to match your settings.
For C#, VB etc the "Formating" option has several sub nodes that provide a fine grain of control for the sort of spacing options you ask for. The list for C/C++ is flat and very limited by comparison. I suspect there is not enough flexability for the layout you want.
So you probably cannot get the formatting you want directly. How about...
Set the spacing rules you want for C++ in the C# settings
Temporarily adding a C# project to your solution
adding a class to that
emptying it.
paste the C++ in (causing a reformat)
copy back to the original file
rinse and repeat
remove the temporary C# project
I have to say this is not pretty, but given the syntactical similarities between C++ and C# its probably a close match.
The auto-formatting settings for C++ in Eclipse are more extensive than in Visual Studio, so I ended up using that instead.

VS2010 - HLSL Intellisense?

Are there any free components out there that will give highlighting and/or intellisense for HLSL?
I've seen mentions of something called "InteliSense.Net," but the author's site is down and I can't find a download anywhere.
Not a direct answer to your question, but this is what I do to solve the same problem:
I set Visual Studio to open *.fx files in Notepad++. (Right click the file, Open With, Add Notepad++, Set Default). Of course, you can use any editor you like.
I use a HLSL syntax highlighting file for Notepad++ that I found on Google. I think it's this one.
And finally, I keep this MSDN page open in my browser: Intrinsic Functions (DirectX HLSL). Because HLSL is so simple - with no way to mess around defining classes and such - and the programs are usually quite small and self-contained, I find that this is sufficient.
Try NShader that is a VS2012/2010/2008 extension for HLSL/CG/GLSL syntax highlighting.
With a little effort you can configure manually MSVC2010 IDE in a way it will highlight HLSL files and will do partial auto-completion/intelisense:
Part 1)
In 'Tools' menu click 'Options'
In the 'Options' dialog box expand to 'Text Editor' node and select 'File Extension'
Now on the right side you can add extensions and configure editor for them
Add extensions: hlsl and fx with 'Editor' field set to 'Microsoft Visual C++'
Check 'Map extensions files to' and set it to 'Microsoft Visual C++'
Part 2)
Create 'usertype.dat' text file beside VCExpress.exe or devenv.exe (typically located "c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"). Populate 'usertype.dat' file with hlsl keywords you want highlighted i.e:
float2
float3
float4
float4x4
matrix
cbuffer
tbuffer
Part 3) Restart MSVC
Part 4) In VC Open 'Tools'\'Options' dialog box again and navigate to 'Environment'\'Fonts and Colors' and configure it to your liking. Color of user keywords added via 'usertype.dat' is controlled through 'C/C++ User Keywords' display item (Personally I matched it with 'Keyword' settings).
Part 4a) In VC 'Tools'\'Options'\'Text Editor'\'C/C++'\'Advanced' on the right side you might want to consider setting field 'Disable Squiggles' to true (since hlsl is not a valid C++ code VC would normally underline C++ compile errors in your shader code)
ShaderSense project seems to solve syntax highlighting and some kind of intellisense for HLSL files
HLSL Tools for Visual Studio brings IntelliSense to HLSL in Visual Studio. It includes statement completion, signature help, live errors, quick info, and more.
Disclaimer - I'm the author of HLSL Tools.

In Visual Studio 2010, how do I quickly switch from a .h file to the corresponding .cpp file? [duplicate]

This is a feature I have grown accustomed to in Eclipse (Ctrl+Tab). Is there an equivalent in Visual C++?
In Visual Studio 2013 and later there is a default keyboard shortcut for this: Ctrl+K, Ctrl+O
(You will need to hold down Ctrl and type ko and then release Ctrl)
In earlier versions, see:
Visual Studio Macro to switch between CPP and H files
or
Open Corresponding File in Visual Assist
In Visual Studio 2013 a default keyboard shortcut for this is Ctrl+K, Ctrl+O
You could add this macro to your VS config (via Tools -> Macros -> Macro Explorer) then assign a hotkey to it (via Tools -> Options -> Environment -> Keyboard).
I only just wrote it (been meaning to try this for ages!) but it seems to work so far, in both VS2008 and VS2010.
Since it's a macro you can edit it to include whatever rules you want (e.g. looking in other folders, or special naming rules if you have a single header shared by multiple cpp files or similar).
Here's the macro (I'm sure it could be better written; I'm unfamiliar with the VS objects and only realised macros were using .Net about half-way through writing the thing :)):
Sub FileSwitch()
Try
Dim CurrentPath As String = DTE.ActiveDocument.FullName
Dim OtherPath As String
If (IO.Path.HasExtension(CurrentPath)) Then
Dim CurrentExtension As String = IO.Path.GetExtension(CurrentPath)
Select Case CurrentExtension
Case ".h", ".hpp", ".hxx"
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".cpp")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".c")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".cxx")
End If
End If
Case ".cpp", ".c", ".cxx"
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".h")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".hpp")
If (Not IO.File.Exists(OtherPath)) Then
OtherPath = IO.Path.ChangeExtension(CurrentPath, ".hxx")
End If
End If
Case Else
End Select
If (OtherPath <> Nothing) Then
DTE.ItemOperations.OpenFile(OtherPath)
End If
End If
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
Here's a (very wide :)) screenshot showing what the macro editor and hotkey/options dialogs should look like, to help those not familiar with them:
Try PhatStudio. It's free and comes with an easy installer.
ALT + S = Switch between header/source file
ALT + O = Open a file (supports instant search via typing, like the start menu in Windows Vista/7).
Try Visual Assist, which sports this very feature (amongst others):
http://www.wholetomato.com/
The code browsing functionality -- of which the header/cpp swap is one part -- are really good.
(I also really rated its intellisense and refactoring features, but not everybody I've spoken to has agreed with me.)
EDIT: just remembered, the Nifty Solution Plugin also does this -- plus another handly Visual Assist-like thing, though nothing else -- and they're free:
http://code.google.com/p/niftyplugins/
(The guy's perforce plugin is great, too. Much better than the default VSSCC rubbish.)
For Visual Studio 2013, as mentioned by others it's the command named:
EditorContextMenus.CodeWindow.ToggleHeaderCodeFile
and it has as default combination of keys: Ctrl+K,Ctrl+O, but it can be changed if you introduce the new combination of keys that you like in the
Press shortcut keys:
under
Tools -> Options -> Environment -> Keyboard.
So you can choose a custom keys combination as my favorite for .h to .cpp switch is Ctrl+Tab.
In Visual Studio 2008 and 2010, you can right click in your .cpp file and choose Go To Header File ... that will take you in one direction. For the other direction, if you right click something you're declaring in the header, and choose Go To Definition, that will take you in the other direction. You might have to go through an ambiguity resolution dialog if you choose the constructor, because the function name matches the class name, but if you choose anything else, you'll go straight where you want. I know this is a two-click approach, rather than one keystroke, but it does do what you want.
If you position your mouse over a function declaration in the header and press F12, the cpp file will be opened at the definition of the cpp file... I use this feature extensively!
I don't see this answer here, but at least in Visual Studio 2012 (Express included!), you can just assign your own keyboard command to go to the header file (NOTE: Only goes one way -- you can't go back to the source file unfortunately...)
Go to Tools/Options/Environment/Keyboard.
Find the following command: EditorContextMenus.CodeWindow.GoToHeaderFile
Assign whatever key combination you want (Alt-S works)
Profit
Not sure which versions of VS this works in, but it doesn't require any add-ins and seems to do the trick in at least one direction.
Try using Switch - it's an addin that lets you flick between source and header, code and designer, XAML and codebehind etc etc:
http://www.dwmkerr.com/switch/ or directly from Products and Extensions for Visual Studio
I'm a fan of Visual Assist for doing this. Its not cheap but it provides a lot more functionality than switching between header and source. I also use its open file in project and class browsing features a lot. Of course the macro is free...
There's also a macro listed on the Whole Tomato support forum which has a few more file mappings.
Visual assist also does not support Visual studio express editions. So you are stuck with the macro if you are using that IDE>
In Visual Studio 2008 it's Alt + O.
In their (in)finite wisdom, MS decided to remove macros in MSVS 2012, so the macro above won't work.
For MSVS 2012, I found this:
http://www.dwmkerr.com/switch/
It's highly configurable + if you want to help improving it, you can do so on GitHub.
In Visual Studio 2013 it's ALT + O

Visual Studio 2010 Beta 2: Can I print in color?

I have to turn in a hard copy of some code with an assignment. Is there any way in Visual Studio 2010 to print C# source code with syntax highlighting?
PS: The assignment is solving a math problem, so the choice of language isn't important and the teacher doesn't need to compile and run the program. She just wants to see our approach and results.
There is an extension now :) Visual Studio 2010 Color Printing Extension
Works well! :)
The best way I've found to accomplish this is to copy from Visual Studio and paste into something like MS Word or OpenOffice Writer.
This gives you full source code, with syntax highlighting. You can then print from Word (including adding your intro documentation before the code, etc).
Just to let everyone know, unfortunately printing in color was cut from Visual Studio 2010 because of resource constraints. Since we've rewritten the editor from scratch in WPF, we didn't have time to reimplement everything so we had to sacrifice this feature. We will try to implement this in the next version of Visual Studio. For now, copy to clipboard and paste into other app such as Microsoft Word is the recommended solution for printing code with color.
If you go to Tools -> Options -> Environment -> Fonts and Colors you can change settings to print with syntax highlighting (change 'Show settings for' dropdown to 'Printer'). But you will need to change all the individual settings to match your IDE (I don't know of a way to make it automatic)
Edit: you can use that "Use..." button next to the dropdown to copy settings from the Text Editor
Simplest of all copy code to clipboard and paste into MS-Word is the way I do and it works...
Have a look at VS.NETcodePrint 2010 availabe from www.starprinttools.com. You will be able to print and export the color coded output to PDF.
Joginder Nahil
Due the fact MSVS does not support it anymore I think the best way is really to copy the code and paste it into WinWord.
The advantages are listed below. You can
set the font/size exactly how you want it.
set the format of line numbers.
have your own header/footer.
remove #region from printing.
add a watermark to the output.
For me - I print once in 2 months a source code - it is a very comfortable way which I never could achieve with any 3rd party extension.

Resources