How to remove all comments in .cs files in VS2010? - visual-studio-2010

Is there anyway or any addin for VS2010 that can remove all the comments in the .cs file including "/// Summary" and "//"?
I don't want to do it manully foreach .cs files.

If you want it, you get it. Firstly read this http://msdn.microsoft.com/en-us/library/2k3te2cs.aspx
You exactly need expression that will match to the end of the line.
And replace it with ' ' or etc.
And then you follow this manual http://geekswithblogs.net/MarkPearl/archive/2011/04/12/vs2010-multiline-find-amp-replace.aspx
and replace all.

I'd second to Feanor. You can record a macro, like search for comment '//' and then delete that line (Ctrl+L). Edit the macro to put that in a loop. Then run this macro for each file.
If you can spend some more time in writing macro, you can loop through the Projects and ProjectItems. So that will remove comment lines from entire solution. You can learn more about EnvDTE for that.

Related

How to replace/remove tag in multiple csproj files

Is it possible to remove tag from multiple csproj files (e.g. NuGetPackageImportStamp) by using search and replace functionality?
Actually Notepad++ has 'Find in files' functionality to replace in files in the selected location. It is possible to insert filter, use regex...
Sorry for bothering. I didn’t even think to look there.

Visual Studio Replace in Files Searches Skipped Matches After Each Replace

In VS Find and Replace > Replace in Files seems to repeatedly search skipped matches before finding new matches.
Perform a Find and Replace in multiple files: File_1, File_2 and File_3
Skip one of the matches on File_1 and proceed to the next file with Find Next
Make some replacements on the next file, File_2, using Replace
After the final replacement on File_2, the Find Next match goes back to previously searched files, File_1 in this case, before going on to matches in File_3
I end up skipping through more and more previously searched files and skipped matches, just to find new matches. The Skip File option doesn't seem to help.
Is there a way to make Find/Replace search all un-searched files before looping back to the previously searched files?
I'm using VS 2012 but I seem to remember the same behavior in 2010.
Update: this is a bug. Please vote for a fix on this Visual Studio UserVoice suggestion.
I never noticed that. You can do a Find in Files, Find All. That way you have a list of all instances and can work your way down the list. Double-click the first one, press Ctrl-H, and replace or skip all matches in that file. Go back to your list and click the first match in the next file.
It will be harder to lose your place this way, but it is still tedious if you have a lot of files/matches to go through.
VS 2013 has the same behavior, in case you were curious. Sorry I don't have a better answer.

Find completely commented files which are commented by /* */

Similar to this question. I would like to find all commented files. But in my case /* */ is a possibility.
Apparently when you write changes to a database project, dropped objects are only commented out instead of deleting the file. I would like to remove all of these commented out files from the project.
Is is possible to find all files which start with /* and end with */?
Using Visual studio
Your regex is:
/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/
Use the following options in "find in files" (Ctrl-Shift-F)
This reference has even more options ostermiller.com/findcomments.html
I'm sure you could mod this to find the /// comment as well
Studio allows you to search with regular expressions so you could write a regular expression to find /* at the beginning and */ at the end of any file. I'm no regex wizard so I'm not sure of the exact syntax. Maybe something like /*.*/ would work?
Any regex gurus know the proper expression here?

How can I search in Visual Studio and get it to ignore what is commented out?

I am refactoring a C++ codebase in Visual Studio 2005. I'm about half way through this process now and I've commented out a lot of old code and replaced or moved it. Now I'm searching to see that I have to change next but the search function keeps bringing me the old commented out stuff I no longer care about. I don't really want to delete that old code yet, just in case.
Is there any way I can search all files in the solution and get results ignoring what is commented out? I don't see a way in visual studio itself, is the perhaps a plug-in that would do it?
As the other provided solutions didn't work for me, I finally discovered the following solution:
^~(:b*//).*your_search_term
Short explanation:
^ from beginning of line
~( NOT the following
:b* any number of white spaces, followed by
// the comment start
) end of NOT
.* any character may appear before
your_search_term your search term :-)
Obviouly this will only work for // and ///-style comments.
You must click "Use Regular Expressions " Button (dot and asterisk) on your find window to apply regex search
In newer versions of visual studio .net regex is used which has a slightly different syntax:
^(?![ \t]*//).*your_search_term
My take:
yes you can use regular expressions, those tend to be too slow and thinking about them distracts from focusing on real stuff - your software.
I prefer non-obtrusive semi-inteligent methods:
Poor man's method:
Find references if you happen to use intelisense on
Or even better:
Visual assist and it's colored "Find all References" and "Go To" mapped to handy shortcuts. This speeds up navigation tremendously.
If you comment your old code with // you can use regular expressions while searching for something in your codebase. Something like this for example: ^[^/][^/].*your_function_name.*.
Previous answer gave a false-positive on cases where otherwise matching lines were placed on lines containing other source:
++i; // your_search_term gets found, don't want it found
So replaced the :b* with .* and added the <> so only entire words are found, and then went after some of the older C-style comments where there's a /* on the line:
^~(.*//)~(.*/\*).*<your_search_term>
In my case I was hunting for all instances of new, not amenable to refactor assistance, and boatloads of false-positives. I also haven't figured out how to avoid matches in quoted strings.
Just to add on, as I was doing a "find all" for division operator used in the code, used the below to exclude comments as well as </ and /> from aspx files:
^~(.*//)~(.*/\*)~(.*\<\/)~(.*/\>).*/
In Visual Basic within Visual Studio 2015, I was able to search for text outside of comments by adapting glassiko's comment from the most upvoted answer
^(?![ \t]*[']).*mysearchterm
And in C# you would use glassiko's comment exactly as it was
^(?![ \t]*//).*mysearchterm
Better use \s I think. ^(?![\s]*//).*your_search_term
delete the commented out code, it is in source control right? there is no need to keep it in the file as well.

How can I keep from retyping the same line of code over and over?

I want to understand if code snippets are what I am looking for here.
I wind up writing the same line of code over and over during a refactoring.
Is there anyway I can create a shortcut that will spit out a line of code that I need?
Another easier option is to drag the code blocks that you re-use frequently onto the general tab of your toolbox area. You could even organize them with their own tab name and all.
alt text http://blogs.telerik.com/Libraries/MetaBlog/WindowsLiveWriter-VisualStudioTooltipsunpluggedDragandDro_EF10-generalTabDragged.sflb
Are you repeating the same line of code over and over on many different days?
Or are you encountering a situation where you have the same line to write many times as a part of a single task, but today's line of code will be different to tomorrows?
If you have the same line/block of code that you use often, a snippet is a good way to capture that in a reusable form (better, IMHO, than copy/paste because you can parameterise them).
However, if you're just looking for a quick way to repeat the same line that's come up now, check out Visual Studio's ability to record keystrokes.
Try this:
Put your cursor on a blank line inside a C# method.
Select Tools|Macros|Record Temporary Macro (often this is Control-Shift-R)
Type "example();" and press return
Select Tools|Macros|Stop Recording
You've just created a temporary macro that you can play back at any time - usually the keystroke for this is Control-Shift-P.
The key to this technique is that the macro records everything you do - with some practise, you can record edits to a line of code and repeat those edits on other lines.
I've used this in the past to create repetative code blocks - like assigning sets of properties from one object to another.
Depending on the code snippet, it would almost always be arguable that this line of code belongs in a util method, rather than copypasta.. But otherwise, yeah - a snippet is probably the best place.
Code Snippets sound like the right approach, although you could investigate Macros inside Visual Studio, which can be very powerful.
One advantage of a code snippet over adding it to the toolbox is that you can define the parts of the code that you want to change. I wrote a code snippet that generated something like the following code:
public class *className*Collection : List<*className*>
Where I only typed className once and it was automatically filled into the other parts.

Resources