I have a code like this:
myType.DoSomething(myParams);
That should immediately be followed by this statement
anotherType.DoeSomethingDifferent(otherParams);
Is there a way on ReShaprer I can identify all those sources where the first statement is NOT followed by the second one? I tried to use the "Search With Pattern" command with the following:
$aVar$.DoSomething($args$)
However this only lists all usages of exactly that method on myType. I need those usages that arte not followed by the second statement. Is this possible?
EDIT: Of course I could simply add the two lines into one single method. However there MIGHT be scenarios in which writing the first line without the second one is usefull. I simply want to grab those.
Related
Im trying to make a bot that will recognize different commands. For example, one command would be " add 'x text' " , as a TODO list. I have set up the recognition of the command by splitting the input to an array, and then getting array[0], or else the first word which will always be the command. Now, I would like to somehow save the other items in the sentence that was split except from the keyword (AKA [0] ) , meaning [1] [2] [3] etc. What is one way of getting all the items besides the [0] one ? I've tried to skip it by doing: myarray.Skip(x) , but I failed at making it work. Thanks for all your suggestions!
As #L-Three has suggested, you can use string.Join(" ", myarray.Skip(1).ToArray()) to get the rest of the string without the first word.
Your example with myarray.Skip(x) didn't work because Skip(x) is a Linq function and thus it is a deferred execution operation. It means that you just described what you need to do, but the action isn't executed yet. A call to '.ToArray()', '.ToList()' for example (as L-Three has used in his solution) will execute the action.
It is important to note that there are other ways to achieve what you want (like using Regex - as it is already suggested in the comments) which might be safer than the method that you are using now.
I need to find all functions in my VS solution with a certain attribute and insert a line of code at the end and at the beginning of each one. For identifying the functions, I've got as far as
\[attribute\]\r?\n(.*)void(.*)\r?\n.*\{\r?\n([^\{\}]*)\}
But that only works on functions that don't contain any other blocks of code delimited by braces. If I set the last capturing group to [\s\S] (all characters), it simply selects all text from the start of the first function to the end of the last one. Is there a way to get around this and select just one whole function?
I am afraid balancing constructs themselves are not enough since you may have unbalanced number of them in the method body. You can still try this regex that will handle most of the caveats:
\[attribute\](?<signature>[^{]*)(?<body>(?:\{[^}]*\}|//.*\r?\n|"[^"]*"|[\S\s])*?\{(?:\{[^}]*\}|//.*\r?\n|"[^"]*"|[\S\s])*?)\}
See demo on RegexStorm
The regex will ignore all { and } in the string literals and //-like comments, and will consume {...} blocks. The only thing it does not support is /*...*/ multiline comments. Please let me know if you also need to account for them.
The bad news is that you can't do that by the Search-And-Replace feature because it doesn't support balancing groups. You can write a separate program in C# that does it for you.
The construct to get the matching closing brace is:
(?=\{)(?:(?<open>\{)|(?<-open>\})|[^\{\}])+?(?(open)(?!))
This matches a block of {...}. But as #DmitryBychenko mentioned it doesn't respect comments or strings.
I have multiple lines of code in my solution that I'd like to remove.
They are, in this particular scenario:
System.Diagnostics.Trace.WriteLine([STRING]);
I could've done a 'Replace All' on System.Diagnostics.Trace.WriteLine but, obviously, I want to remove the entire thing (including the string parameter passed into the method) - I want the whole line of code to be deleted wherever a call to this method is made.
Is there a way in which I can do this (any way to provide a wildcard included in the replace criteria maybe?). Also, could I do this with comments/todo's too?
I appreciate this may seem bad practice to have to get to this point but would, for now, appreciate answers that are only on topic as opposed to answers that try and propose ways of preventing this in the future (which I've already acknowledged).
You can use Regular Expression in Visual Studio Find and Replace dialog, use:
Find: ^[ \t]*(System.Diagnostics.Trace.WriteLine\(.*\)\;).*\n
Replace:
That's will delete the entire line.
You'll need to use the Regular Expression option in the Replace dialog, and something like this should do:
System.Diagnostics.Trace.WriteLine\("\w*"\);
Cheers
EDIT: Obviously, if you are passing variables to WriteLine, just remove the quotes:
System.Diagnostics.Trace.WriteLine\(\w*\);
If you mean to remove all instances of System.Diagnostics.Trace.WriteLine then you could do the following:
Go to Replace in Files dialog box
Enter System.Diagnostics.Trace.WriteLine.* in the find what box
Leave the Replace with box empty
Choose Entire Solution for Look in
Expand Find Options and tick Use and ensure Regular Expressions is shown.
Previously I was using object context for my application. So I was using AddToTable() methods like
context.AddToTable(entity);
but now I am using DbContext and I have to use the Add method like
context.Table.Add(entity);
Now I'm having the older method called more than 300 times. So I was wondering is there any solution to change the methods solution wide.
If you want to use you Regex search and replace for this, something along these lines might do the job.
Given that you don't have any other methods named AddToX( you could use the following regex:
\.AddTo([^(]+)\(
This would match .AddToX( then you replace with .\1.Add( where \1 is a back-reference, so it might be $1 depending on your flavor of regex.
That would transform .AddToX( into .X.Add(.
How you would process your code with the regex I can't answer so that would only be half the puzzle, many IDE's have project wide search and replace I guess.
Regardless, make backups before you do anything of this nature.
I have a lot of T-SQL queries I need to parse and replace certain values.
A query may look like this
SELECT dbo.udfTest(123,'Bob') as Foo
or alternatively
SELECT dbo.udfTest(123) as Foo
My task is to replace the number value with another given value but as I contemplate just rolling something up using the string class and doing substrings etc I start to run into lots of edge cases like this
SELECT dbo.udfTest ( 123 ) as Foo
or
SELECT [dbo].[udfTest]( 123 ) as Foo
or
SELECT [dbo].[udfTest]( 123 ) as Foo1, dbo.udfTest(123) as Foo2
Throw in any combination of whitespace, casing, brackets, nested parenthesis and you can imagine the number of variations I would have to cover...nasty.
Which brings me to wondering if there is a better way? RegEx may be a play but I figured I would toss it out to get some opinions.
You might be able to use the database features of Visual Studio. See API Reference for Database Features of Visual Studio, especially the Microsoft.Data.Schema.ScriptDom Namespace.
Also, a quick search for "parse mdx query" turned up several interesting hits. I'm pretty sure I once found a tool that could parse MDX queries, then use the parse tree to create a formatted version.
Perhaps the article Getting to the Crown Jewels will help. If nothing else, it may give you a hint about who to ask for help.
Matching multiple possible patterns says RegEx right off the bat. The RegEx will be ugly but it should get you where you want to go pretty easily.
If the end goal is to replace some set of numbers with a new set of numbers (basically a translation), wouldn't it be easier update your UDF to use a lookup table to map the old values to the new ones instead of trying to intercept every method call before it takes place? (Unless your examples have been greatly simplified in that they only show the same function being called each time, but in reality there's many that would have to be updated)