How to find/replace complicated statements in Visual Studio? - visual-studio-2010

I'm trying to do something that I believe is easy but I couldn't figure it out. I'm simply trying to find a section of code and replace it. I have a method in several files
[TestMethod, ExpectedException(typeof (InvalidOperationException))]
public void RetrieveWithInvalidKey()
And I'm trying to replace it with:
[TestMethod]
public void RetrieveWithInvalidKey()
I can't just find [TestMethod, ExpectedException(typeof (InvalidOperationException))] because there are many others that I do not want to change. I only want to change the ones with the public void RetrieveWithInvalidKey() afterwards. I tried to find [TestMethod, ExpectedException(typeof (InvalidOperationException))]\npublic void RetrieveWithInvalidKey() using regular expressions but it didn't find it.
How do I find this specific block of code for replacing? I also have ReSharper if that can do it but it didn't recognize a method pattern.

When using regex in find box, you must escape the '[', ']', '(', and ')' characters with a backslash.
Thus you should use:
\[TestMethod, ExpectedException\(typeof \(InvalidOperationException\)\)\]\n:b*public void RetrieveWithInvalidKey\(\)
for your search.
Type in the following in the replace box:
[TestMethod]
public void RetrieveWithInvalidKey()
That should get you what you want.

Related

vector of boost::function not working with _placeholder

I need your help. I tried a lot to find a solution for the problem but failed so far.
Idea:
Create a vector of "command tuples" like the following:
typedef boost::tuple<std::string, boost::function<void()>> command_tuple;
std::vector<command_tuple> commands {
boost::make_tuple("command1", boost::bind(&myclass::command1, this))
};
If the string "command1" is used the void command1() function is called. The ID index value is based on find_if search for the string ("command1" is ID=0) on the vector.
This is working fine!
boost::get<1>(commands[ID])();
Problem:
I found no way so far to use a vector of boost::function (pointers) defined with any kind type of function parameters:
typedef boost::tuple<std::string, boost::function<void(const char *)>> command_tuple;
std::vector<command_tuple> commands {
boost::make_tuple("command1", boost::bind(&myclass::command1, this, std::placeholders::_1))
};
This will fail with a cannot convert std::_Placeholder<1> to type const char * compilation error.
This is therefore not possible:
boost::get<1>(commands[ID])("dynamic string argument");
Pinning down of the problem:
hmmm ... so I found out so far that even a simple vector of boost::function does not seem to work (in the way I am trying to use it):
std::vector<boost::function<void(const char *)>> commands {
boost::bind(&myclass:command1, this, std::placeholders::_1)
};
Fails with the same compilation error.
It is possible to replace the std::placeholders::_1 with "static string constants" to avoid any compilation errors. But my idea was to use a "dynamic string argument".
Can someone please give me a quick hint of what I am doing wrong or maybe show some simple alternatives. I want to have quite a big list of function pointers that I can call based on their "string name".
I simply want to avoid a big "switch select case" style code block to
select the function to be executed based on a dynamic "input string".
Thanks for your time!
like T.C. replied in the comments. You should never mix std::bind, std::placeholders and boost::bind.

Select and paste a code as a snippet

Say I have a section of code e.g:
[StringLength(15, ErrorMessage = "Length must be less than or equal to 15")]
public string UserDefinedName0 { get; set; }
[StringLength(63, ErrorMessage = "Length must be less than or equal to 63")]
public string UserDefinedValue0 { get; set; }
Is there any plugin or visual studio feature or third party windows tool that allows me to copy and paste it such that I get a parameter for say "0" when I paste.
I think this is what you are looking for:
Visual Studio Tip: Setting Up Code Snippets with Variable Parameters
I don't know of a plug-in that would do that, but can think of two solutions that might be helpful:
Write a short macro yourself. It's easier than you might think.
Use RegEx replace. Not sure if this will suite your purpose fully, but I have used it so many times to do some clever code duplication.

Does Sublime Text 2 have the ability to region code similar to Visual Studio?

In Visual Studio you can minimize huge chunks of code using regions; they essentially just surround the code and minimize it in the window.
Does Sublime have a feature similar to this?
By default, you can select some code the go to Edit > Code Folding > Fold. There are tons of plugins that leverage the code-folding api for more options.
There's a request on the official site to "ask for features" here.
But apparently:
FYI, Jon has stated that this is not possible in the current
implementation of the editor control. Looks like we're waiting till V3
guys.
Jon being the programmer behind Sublime Text 2.
There might be a way to fake it by creating a plugin that looks for markers and removes the code region in between the markers, but it probably wouldn't look good. With the current API, it's probably your best bet!
By the way, there is some code folding in Sublime Text, if you hover your mouse next to the line number, you will see some arrows appearing when you can fold / unfold.
I ended up using custom comment tags, indented one level less than the code I want to fold. It doesn't look best, though it serves its purpose.
class Foobar {
// ...some code
// <fold
function foo() {
}
function bar() {
}
// </fold
// more code...
}
This (at the moment) folds to:
class Foobar {
// ...some code
// <fold[...]
// </fold
// more code...
}
Having a native ST2 support for this would be nice.
This looks what you are looking for. You can define tags for #region and #endregion for each language, or a generic tag for all of them.
If you are obsessed with intendation, this solution may make you uncomfortable but here it is, once upon a time while I had been writing a semi-complex jQuery plugin I've had constants, variables, private and public functions sections and foldings like so;
;(function($, undefined, window) {...
/* Consts */
var FOO = "BAR";
/* Variables */
var m_Foo = "bar";
/* Functions */
/* Public Functions */
function foo() {...}
function bar() {...}
/* Private Functions */
function _foo() {...}
function _bar() {...}
})(jQuery, window);
As you can see it is all about intendation. Sections can be folded; Consts, Variables, Functions. And also inside Functions section, Public Functions and Private Functions are both can be folded.
You can also use one line comment (//) to name your fold. So the idea underneath that is simple; ST2 thinks that the more intended lines belongs to first less-intended comment above them, like C/C++ compilers how handle brackets as own unique code blocks.
To fold the code select the code and press
ctrl + shift + [
To unfold the code put the cursor there and press
ctrl + shift + ]
I think that like myself, the OP has come to appreciate a little-known feature in VS called regions that many equate to code-folding, but is FAR more powerful and above, Dio Phung provided the answer that I wanted, and I suspect the OP wanted, but he didn't share as an answer so here it is.
The difference between "code-folding" as it's provided in Sublime Text is that it's based on code/compiler syntax while "regions" and what this plugin does, allow you infinitely more freedom, though it's a freedom that's more or less dependant on the code you're working with to begin with (deeply nested, or properly modularized).
If you are on Sublime Text 3, here is a plugin that can do it :
github.com/jamalsenouci/sublimetext-syntaxfold – Dio Phung
In languages which support 3 types of comments (e.g. PHP) I use the hashtag comment for regions, as shown in the other answers. It's also good for keeping track of what's being done
# default options
$a = 3;
$b = 'bob';
$old_code = 1;
# bugfix #130
$result = magic_function($data);
fix_stuff($result);
$old_code = $result;
Otherwise use triple slash ///, or //# etc.
In sublime text, it works like this, it shades the lines you want to collapse and presses (Control + Shift +?)
I have the most recent version of sublimetext.

Visual Studio 2010 IDE Find Options Issue

I have an overloaded method which have these signatures:
void Method(string a,string b);
void Method(string a,string b,string c);
void Method(string a,string b,string c,string d,string e);
What I want to do in Visual Studio IDE is to find all lines in the current project where Method is called with only less than 5 parameters (e.g. the first and second signatures in the example).
Is it possible to do this (or any plugin resharper etc. is able to that)
Try this regex in the find window:
(Method\(([:a:b]+,[:a:b]*)^1\))|(Method\(([:a:b]+,[:a:b]*)^2\))|(Method\(([:a:b]+,[:a:b]*)^3\))
It's an OR basically of:
(Method\(([:a:b]+,[:a:b]*)^N\))
where N looks for N of alpha or whitespace followed by a comma
You can search with regexp, so you should be able to do something like "/void[]Method\(([^\)^,]+,){1,3}\)/". I don't know if it is the exact syntax, but you have the idea.

VS 2008 Code Snippet Indentation

In VS 2008, there are XML code snippets that seem to be pretty awesome :) However, it seems that the snippets we created do not indent properly. For example if we have the following code:
{
...
{
...
{
InsertSnippet here.
We get something like:
{
...
{
...
{
FirstLineofSnippet
SecondLineOfSnippet
ThirdLineOfSnippet
Is there any way to make it so that all lines keep the same indentation?
This usually indicates that the code you are inserting is semantically incorrect - at the time of insert, not when you filled in the snippet values.
To prevent this you can use default values in your snippet to make the snipped syntactivaly correct. E.g. to declare an argument list for a function:
<Literal Editable="true">
<ID>methodArguments</ID>
<ToolTip>methodArguments</ToolTip>
<Default>params object[] args</Default>
<Function>
</Function>
</Literal>
The <Default> Element provides the declaration. So using this in a snippet declaring a funciton will lead to this:
public void FunctionName(params object[] args) { }
And it then lets you replace params object[] args with whatever argument declarations you need.
paste the first line without indentation, then select the block and tab until you get the whole block to the indentation you want. AFAIK what you see is the standard behavior and it happens because the location you paste from wasn't indented as far as you wanted.
My unit test snippet appears to be indenting correctly. It may be due to the fact that the code is wrapped in a CDATA block. VisualStudio also tends to auto-format the block as soon as it is added to the code. Does your snippet have as many open braces as closing ones? If the two match up, and the snippet is inserted in a valid/compilable location, it should auto-format.
It seems that Visual Studio does an auto-format on the inserted lines. My snippet wasn't correctly formatted (i.e. indented) either, neither by snippet insertion, nor by manual format (Ctrl-K-F). It contained "delegate{...}", maybe the formatter cannot handle this.
Try swithing from Insert spaces to Keep tabs in Tools > Options > TextEditor > C# > Tabs, it worked for me.

Resources