Give option to search only in uncommented code
When searching for a string in a project's source code, I often find myself wasting time examining lines that have been commented out. it would be useful to offer an option to return only lines that are not commented out, so as not to confuse the results.
Paul Topping commented
Such a feature could also be extended to code included/excluded by the preprocessor given the current intellisense knowledge.
A nice implementation might also tag search hits as "in comment" and allow one to group hits in search results based on that property.
'Use Find References' and 'delete commented code' are (good) solutions to *different* problems. This suggestion is about searching for arbitrary text when you know that the results you want are definitely not in comments or doc-comments. For example, find all "= null" in your code so you can check them. Or find all mentions of "para" (e.g. "firstPara", "lastPara", "Paragraph") without hitting all the <param> entries in your doc comments. Or a bug causes an error message to be reported, "out of memory", so you want to find the code that emitted that text to locate the buggy code - but Find in Files returns 350 matches even though there are only 2 lines of *code* in your codebase that include that text.
Where is this "Find References" option? Or do you mean right-click on a variable and Find All References? Because whilst that is useful, there are still many times when you need to search for something that isn't an object, for example, a string value or a line of code.
Ben Voigt [Visual C++ MVP] commented
Instead of a textual search, use "Find References". That will exclude commented code, remarks in comments, appearances inside quoted strings, and similarly named but unrelated items.
I totally agree with this, but it's not just commented out code. Valid comments can quite easily contain the same content as a bit of code you are searching for.
There really needs to be an option to search in comments, defaulted to off. How often do people actually need to search comments?!
Michael Paterson commented
I agree that if code is commented out, delete it. That's what source control is for.
Tim Burris commented
@Boogaloo: It's obvious in the code view. Colorization does not apply to find results, so especially in the case of multi-line comments, it's not all all obvious whether the line is commented out.
Mark Hurd commented
I think this is a good idea, but it must separately allow/deny single line comments, multi-line comments, XML comments, and, importantly, #IFed out code.
Sometimes I want to search only comments, sometimes only code, sometimes both.
But I probably search my code 20-40 times every day. I don't want to fiddle with a bunch of radio buttons on every search, especially now that the options are always hidden by default!
I'd actually like to have two or three hotkeys for different "find modes", with different defaults in the search dialog for each hotkey.
Justin Chase commented
I would recommend deleting commented out code. Use source control to preserve history of old code.
It should be pretty obvious that the code you are looking at is commented out. If not, try changing your "Comment" color to something less like your actual code color.
really many users are having this annoying problem of search results including comments too.. -.-"
Oftentimes when searching and/or replacing in code, the text I'm searching for exists in both code and comments. An option to exclude comments from search results would save a lot of time.