Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use regex to find strings in Resharper's Custom Patterns?

Our code base has a lot of StringBuilder.AppendFormats that have strings that end with newline characters. I've made an extension method called AppendLineFormat and would like to use Resharper's Custom Patterns feature to identify and fix those old calls to use the new extension method (and to suggest this new extension method to others in the future).

StringBuilder sb = new StringBuilder();
int i = 1;
sb.AppendFormat("i is {0}\r\n", i);

into

sb.AppendLineFormat("i is {0}", i);

Is there a way to use regex (with replacement) to identify strings that match? I don't want to turn every AppendFormat into an AppendLineFormat - only the ones with strings that end in \r\n. Here's what I've got so far.

Search Pattern:

$sb$.AppendFormat($newLineString$,$args$)

Replace Pattern:

$sb$.AppendLineFormat($newLineString$,$args$)

Where

  • $sb$ is an expression of type System.Text.StringBuilder
  • $newLineString$ is an identifier matching "(.*)\\r\\n" (totally wrong, I know)
  • $args$ is any number of arguments
like image 554
marchica Avatar asked Oct 31 '22 15:10

marchica


1 Answers

Unfortunately as for now it's not supported.

There is a bunch of issues, you can vote for them:

  • https://youtrack.jetbrains.com/issue/RSRP-201057
  • https://youtrack.jetbrains.com/issue/RSRP-263248

As a workaround you can use Visual Studio Find and Replace feature.

Search pattern:

\.AppendFormat\(\"(.+?)(?>\\r\\n\")

Replace pattern:

.AppendLineFormat("$1"

enter image description here

like image 119
Anton Sizikov Avatar answered Nov 12 '22 23:11

Anton Sizikov