Pattern to convert type instantiation to using

Hi,

I have been playing with the structural search and replace feature and tried to create a pattern for replacing var i = new Type(); to a using statement. This is what my pattern looks like:

$type$ $var$ = new $type$($args$);
$stmt$

This is replaced with:

using($type$ $var$ = new $type$($args$))
{
  $stmt$
}


However, I am never getting the fix to show up on code like this:

StringBuilder builder = new StringBuilder();
builder.AppendLine("");
builder.AppendLine("");

Any suggestions on what I am doing wrong?

Thanks,
b3n

3 comments
Comment actions Permalink

Hello Ben,

In order to match this piece of code you should turn on 'Limit minimal number
of statements' option and set its value to '2' for the 'stmt' placeholder.

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

using($type$ $var$ = new $type$($args$))
{
$stmt$
}



0
Comment actions Permalink

Hi Andrey,

I changed the pattern the way you suggested but I still can't get the suggestion to pop up when in the text editor.

This is the piece of code I tried it on:

StringBuilder builder = new StringBuilder();

builder.AppendLine("<div>");
builder.AppendLine("<h3>");


I wasn't sure if the empty line confuses resharper so I tried without but the same result. The only suggestions I get
is to replace the StringBuilder with the var keyword.

Thanks,
ben

0
Comment actions Permalink

Hello Ben,

Could you please export your pattern as an xml file (through Pattern Catalog
window) and attach it here? Thank you!

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hi Andrey,

I changed the pattern the way you suggested but I still can't get the
suggestion to pop up when in the text editor.

This is the piece of code I tried it on:

StringBuilder builder = new StringBuilder();

builder.AppendLine("<div>");
builder.AppendLine("<h3>");
I wasn't sure if the empty line confuses resharper so I tried without
but the same result. The only suggestions I get

is to replace the StringBuilder with the var keyword.

Thanks,
ben
---
Original message URL:
http://devnet.jetbrains.net/message/5270335#5270335



0

Please sign in to leave a comment.