17

I want to change method signature from

public static async Task Load()

to

public static async Task LoadAsync()

How to define a custom patterns in ReSharper?

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
lone
  • 193
  • 5

1 Answers1

35

If I understood you correctly, you want to define a custom pattern to change async Task Method() signature to async Task MethodAsync(). If so, this is possible by using Custom Patterns!

For this, go to ReSharper's Options, then Code Inspection → Custom Patterns:

  • Click Add Pattern

  • In the new dialog, make sure Replace is selected

  • Type the Search and Replace pattern exactly as they appear in the image below. Depending on your ReSharper versions, the placeholder parameters should appear automatically. If not, press the Add Placeholder button.

  • Double click the method placehoder, and add the following RegEx: \b\w+(?<!Async)\b - this tells only to match method names NOT ending in Async already.

  • In the Pattern Severity combobox select Show as Hint or Show as Suggestion, depending on your preference.

  • Click Add, then Save (or Save To → Team Shared, to have this pattern stored in the team-shared settings, available to all your teammates).

ReSharper will now flag all public async Task methods that are not already end with Async:

And you can now press Alt-Enter to quick-fix it!

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
  • Very, very grateful to you for your answer, I'm thinking about again if it is private static Task public Task How to define it? – lone Mar 20 '14 at 02:26
  • You can create another pattern, almost identical, but with using the `private` keyword. – Igal Tabachnik Mar 20 '14 at 08:09
  • Strange, it should match any parameters, regardless. The `$parameters$` placeholder should consider 0 or more parameters, so it works on both `Foo()` and `Foo(string arg)`, for example. Is your placeholder defined correctly? – Igal Tabachnik Mar 20 '14 at 08:11
  • 1
    @IgalTabachnik Important note: The `async` in the search pattern is redundant, Resharper doesn't match it. This pattern will also highlight `public Task Load(){ }` (which isn't necessarily bad in this case) – i3arnon Jul 14 '14 at 21:07
  • this is awesome, however, would you happen to know how i can find ANY methods in the entire solution that share at least 2 of the same parameters? – Alex Gordon May 23 '16 at 17:23