Visual Studio 2010 advanced find and replace? - visual-studio-2010

Related to this question: Create a new jquery chained method, how do I do an advanced find and replace in Visual Studio (or even Notepad++ or something else) to find:
Foo($("selector"));
And replace with:
$("selector").foo();
This is because I've created a new jQuery chained method:
$("#SaveButton").click(function () {
$("#SubTotal").foo();
$("#TaxTotal").foo();
$("#Total").foo();
});
To replace this
$("#SaveButton").click(function () {
Foo($("#SubTotal"));
Foo($("#TaxTotal"));
Foo($("#Total"));
});
And now I need to find and replace all instances of the old method call with the new chained method.

Use regular expressions in the Find options of Find/Replace dialog.
Hope that helps.
NB: I run screaming madly at the mere notion of having a stab at your particular expression but it is really just a transform of a number of matches so should be doable.

Related

How to find all `action()` in code?

I made an extension method for Action and I'd like to replace all occurrences of action() with action.SafeInvoke() in the code. But the problem is that action is just an example var name, and in reality they are all different. Searching by () will obviously produce terrible results. Searching by Action and looking up every individual var's references in code and finding direct invocation among that is kind of slow.
Is it possible to somehow find all occurrences of Action invocation by () operator when the variable names are unknown?
As I understand it, you have instances of type Action like:
Action foo = ...
Action bar = ...
And then you have in the code:
foo();
bar();
You want something that will change foo() to foo.SafeInvoke() and bar() to bar.SafeInvoke().
There's nothing in Visual Studio that will do this for you automatically, and writing something to do it would be non-trivial. You'd spend a whole lot more time on that extension than you would by making the changes manually.
You can use Visual Studio's "Find usages" functionality to find every place that Action is used. That will at least help you locate where you need to make the changes.
I'd be interested to know, what your SafeInvoke does. If all it does is a null check, you might want to consider if it's really necessary.

Visual Studio 2010 IntelliSense: hints on F# operators

Is it possible to make Visual Studio to display tooltips on operators?
The following image demonstrates a tooltip hint for a function, but it does not work for operators.
Operators usually have simple type specs like 'T -> 'T -> 'T, but such hints can be useful for custom ones.
Following Daniel's suggestion, I'm posting a workaround that I've been using for myself.
The workaround is only partially helpful, and I'm still looking for any better ideas.
let (!><) a = ()
let z1 = op_BangGreaterLess 5
This code is fully valid, since an operator expression generates a function with a compiler-generated name. See this MSDN article, section "Overloaded Operator Names" for complete list of operator names.
Good news is that op_BangGreaterLess supports IntelliSense hints and it also supports "Go to Definition" (F12) command of IDE, pointing to an original operator declaration.
Bad news is that IntelliSense does not allow rapid entry of the full operator name (Ctrl+Space), so you have to type the entire name manually.
I'm afraid this is not possible (and even in Visual Studio 2012, I don't get tooltips for operators).
I suppose this could be implemented, but as you say, operators usually have simple types. When using custom operators, these should be probably simple enough so that people can use them without looking at their type (or the associated XML documentation). Otherwise, it might be better to use a named function.
That said, if you're using F# Interactive, then you can easily use that to explore the operator type:
> (!><);;
val it : ('a -> unit) = <fun:clo#2>
If I cannot use F# Interactive, I usually define a simple dummy symbol to get the IntelliSense:
let dummy () = (!><)
Note that I added unit argument to define a function and avoid value restriction error.

how to match function code block with regex

What I like to do is remove all functions which has specific word for example, if the word is 'apple':
void eatapple()
{
// blah
// blah
}
I'd like to delete all code from 'void' to '}'.
What I tried is:
^void.*apple(.|\n)*}
But it took very long time I think something is wrong here.
I'm using Visual Studio. Thank you.
To clarify jeong's eventual solution, which I think is pretty clever: it works, but it depends on the code being formatted in a very particular way. But that's OK, because most IDE's can enforce a particular formatting standard anyway. If I may give a related example - the problem that brought me here - I was looking to find expressions of the form (in Java)
if (DEBUG) {
// possibly arbitrary statements or blocks
}
which, yes, isn't technically regular, but I ran the Eclispe code formatter on the files to make sure they all necessarily looked like this (our company's usual preferred code style):
if (DEBUG) {
statement();
while (whatever) {
blahblahblah(etc);
}
// ...
}
and then looking for this (this is Java regex syntax, of course)
^(\s*)if \(DEBUG.*(?:\n\1 .*)*\n\1\}
did the trick.
Finally did it.
^void.*(a|A)pple\(\)\n\{\n((\t.*\n)|(^$\n))*^\}
Function blocks aren't regular, so using a regular expression in this situation is a bad idea.
If you really have a huge number of functions that you need to delete (more than you can delete by hand (suggesting there's something wrong with your codebase — but I digress)) then you should write a quick brace-counting parser instead of trying to use regular expressions in this situation.
It should be pretty easy, especially if you can assume the braces are already balanced. Read in tokens, find one that matches "apple", then keep going until you reach the brace that matches with the one immediately after the "apple" token. Delete everything between.
In theory, regular language is not able to express a sentence described by context free grammar. If it is a one time job, why don't you just do it manually.
Switch to VI. Then you can select the opening brace and press d% to delete the section.

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.

Visual Studio refactoring: Remove method

Is there any Visual Studio Add-In that can do the remove method refactoring?
Suppose you have the following method:
Result DoSomething(parameters)
{
return ComputeResult(parameters);
}
Or the variant where Result is void.
The purpose of the refactoring is to replace all the calls to DoSomething with calls to ComputeResult or the expression that uses the parameters if ComputeResult is not a method call.
If I understand the question, then Resharper calls this 'inline method' - Ctrl - R + I
I would do it the simpliest way:
rename ComputeResult method to ComputeResultX
rename DoSomething method to ComputeResult
remove DoSomething method (which is now ComputeResult)
rename ComputeResultX method back to ComputeResult
Maybe VS will show some conflict because of the last rename, but ignore it.
By "rename" I mean: overwrite the name of the method and after it use the dropdown (Shift+Alt+F10) and select "rename". It will replace all occurences with the new name.
There are a few products available to add extra refactoring options to Visual Studio 2005 & 2008, a few of the better ones are Refactor! Pro and Resharper.
As far as remove method, there is a description in the canonical Refactoring book about how to do this incrementally.
Personally, I follow a pattern something along these lines (assume that compiling and running unit tests occurs between each step):
Create the new method
Remove the body of the old method, change it to call the new method
Search for all references to the old method (right click the method name and select "Find all Reference"), change them to calls to the new method
Mark the old method as [Obsolete] (calls to it will now show up as warnings during the build)
Delete the old method
When it comes to refactoring like that, try out ReSharper.
Just right click on the method name, click "Find usages", and refactor until it cannot find any references.
And as dlamblin mentioned, the newest version of ReSharper has the possibility to inline a method. That should do just what you need.
ReSharper is definitely the VS 2008 plug in to have for refactoring. However it does not do this form of refactoring in one step; you will have to Refactor->rename DoSomething to ComputeResult and ignore the conflict with the real ComputeResult. Then delete the definition which was DoSomething. It's almost one step.
However maybe it can do it one step. If I read that correctly.
You can also right click the method name and click "Find all References" in Visual Studio.
I personally would just do a CTRL + SHIFT + H to Find & Replace

Resources