Using the Pattern Library

The documentation for the Pattern Library seems rather skimpy... is there a
good site or place to go for details on how to use it?

Here's something I'm trying to do.  I wanted a find/replace like this:

Find:
= "";

Replace:
= string.Empty;

I don't care what's on the other side of the equal sign, I just want to
flash a warning if someone is using a literal empty string instead of
string.Empty.

This didn't work as I expected.  It found something, but didn't find any of
the examples I was looking right at.

So I looked around and found some examples, and ended up adding a "place
holder" (which was really confusing and not well documented) with a name of
'var' and a meaning of 'identifier', and changed the search/replace patterns
to this:

$var$ = "";
$var$ = string.Empty;

It still didn't find the examples I was looking at in the code, so I changed
it to this:

string $var$ = "";
string $var$ = string.Empty;

And NOW it finds the examples I'm looking at, but won't find the instances
it previously found.

How do I create a "pattern" that just finds an assignment to a string
variable (whether in a declaration or not) of a literal empty string?  I
cannot figure this out, and it seems like it should be so simple.

Help?



4 comments
Comment actions Permalink

Hello Paul,

Do you need assignment or actually any instance of "" expression out there?
What about passing "" as argument? Using as initializer in object initializer
or collection initializer syntax? Array of string creation expression? Using
as initializer for fields?

You probably just want to find "" and replace with string.Empty, without
any specific details.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PB> The documentation for the Pattern Library seems rather skimpy... is
PB> there a good site or place to go for details on how to use it?
PB>
PB> Here's something I'm trying to do.  I wanted a find/replace like
PB> this:
PB>
PB> Find:
PB> = "";
PB> Replace:
PB> = string.Empty;
PB> I don't care what's on the other side of the equal sign, I just want
PB> to flash a warning if someone is using a literal empty string
PB> instead of string.Empty.
PB>
PB> This didn't work as I expected.  It found something, but didn't find
PB> any of the examples I was looking right at.
PB>
PB> So I looked around and found some examples, and ended up adding a
PB> "place holder" (which was really confusing and not well documented)
PB> with a name of 'var' and a meaning of 'identifier', and changed the
PB> search/replace patterns to this:
PB>
PB> $var$ = "";
PB> $var$ = string.Empty;
PB> It still didn't find the examples I was looking at in the code, so I
PB> changed it to this:
PB>
PB> string $var$ = "";
PB> string $var$ = string.Empty;
PB> And NOW it finds the examples I'm looking at, but won't find the
PB> instances it previously found.
PB>
PB> How do I create a "pattern" that just finds an assignment to a
PB> string variable (whether in a declaration or not) of a literal empty
PB> string?  I cannot figure this out, and it seems like it should be so
PB> simple.
PB>
PB> Help?
PB>


0
Comment actions Permalink

That's one possibility , but I'm finding that such a pattern finds and flags
way more than what I really want to flag as a warning.  For example, I don't
want to find things inside of Attribute definitions... I only want to find
things in the body C# code (inside classes, inside methods, inside property
implementations, etc).

I guess I'm really asking "Is there a way to do what I want here, in the
abstract?"  The specific example may not make a whole lot of sense, but I
was curious why just using

= "";

Didn't find things that were right there in front of my face, and I'm
curious how you find things like

string foo = "";

AND

foo = "";

using the same pattern.  In my attempts, I could only make it find one or
the other.  And the first one (= "";) gave me a "could not parse" error.

So getting back to my example... is there a way to say:  Find "" and replace
it with string.Empty except if the "" is outside of a method scope?

An example of what I don't want to find:

    ///  wrote in message
news:151d24b61b5a418ccd7e4f5a66339@news.intellij.net...

Hello Paul,

>

Do you need assignment or actually any instance of "" expression out
there? What about passing "" as argument? Using as initializer in object
initializer or collection initializer syntax? Array of string creation
expression? Using as initializer for fields?
You probably just want to find "" and replace with string.Empty, without
any specific details.
Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>


0
Comment actions Permalink

Hello Paul,

There is no way to specify context in which match is accepted as of now.
Sounds like a great idea for improvement, thanks!

In general the rule is simple - you create valid C# construct which constitutes
complete thing (in terms of AST, parsing etc). You can match statement, expression,
member declaration, etc. But not half of it. Then you replace parts of the
construct with placeholders and apply some rules what they can represent.

Since = ""; is not anything, you cannot match it. For now, you will have
to have separate patterns for each case. As for documentation, we are working
on it.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PB> That's one possibility , but I'm finding that such a pattern finds
PB> and flags way more than what I really want to flag as a warning.
PB> For example, I don't want to find things inside of Attribute
PB> definitions... I only want to find things in the body C# code
PB> (inside classes, inside methods, inside property implementations,
PB> etc).
PB>
PB> I guess I'm really asking "Is there a way to do what I want here, in
PB> the abstract?"  The specific example may not make a whole lot of
PB> sense, but I was curious why just using
PB>
PB> = "";
PB>
PB> Didn't find things that were right there in front of my face, and
PB> I'm curious how you find things like
PB>
PB> string foo = "";
PB>
PB> AND
PB>
PB> foo = "";
PB>
PB> using the same pattern.  In my attempts, I could only make it find
PB> one or the other.  And the first one (= "";) gave me a "could not
PB> parse" error.
PB>
PB> So getting back to my example... is there a way to say:  Find "" and
PB> replace it with string.Empty except if the "" is outside of a method
PB> scope?
PB>
PB> An example of what I don't want to find:
PB>
PB> ///  "Develop with pleasure!"


0
Comment actions Permalink

Thank you, that's all very helpful.  And yes, being able to specify a
context would be very helpful as well!

"Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
news:151d24b61b5a678ccd7eb0971298e@news.intellij.net...

Hello Paul,

>

There is no way to specify context in which match is accepted as of now.
Sounds like a great idea for improvement, thanks!

>

In general the rule is simple - you create valid C# construct which
constitutes complete thing (in terms of AST, parsing etc). You can match
statement, expression, member declaration, etc. But not half of it. Then
you replace parts of the construct with placeholders and apply some rules
what they can represent.
Since = ""; is not anything, you cannot match it. For now, you will have
to have separate patterns for each case. As for documentation, we are
working on it.
Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>

PB> That's one possibility , but I'm finding that such a pattern finds
PB> and flags way more than what I really want to flag as a warning.
PB> For example, I don't want to find things inside of Attribute
PB> definitions... I only want to find things in the body C# code
PB> (inside classes, inside methods, inside property implementations,
PB> etc).
PB> PB> I guess I'm really asking "Is there a way to do what I want here,
in
PB> the abstract?"  The specific example may not make a whole lot of
PB> sense, but I was curious why just using
PB> PB> = ""; PB> PB> Didn't find things that were right there in front of
my face, and
PB> I'm curious how you find things like
PB> PB> string foo = "";
PB> PB> AND
PB> PB> foo = "";
PB> PB> using the same pattern.  In my attempts, I could only make it find
PB> one or the other.  And the first one (= "";) gave me a "could not
PB> parse" error.
PB> PB> So getting back to my example... is there a way to say:  Find ""
and
PB> replace it with string.Empty except if the "" is outside of a method
PB> scope?
PB> PB> An example of what I don't want to find:
PB> PB> /// <summary>
PB> /// The get counter result.
PB> /// </summary>
PB> [GeneratedCode("", "")]  <== do NOT want to flag this
PB> public partial class GetCounterResult
PB> {
PB> private foo()
PB> {
PB> string bar = "";  <== DO want to flag this
PB> bar += "" + "baz";  <== also want to flag this
PB> }
PB> }
PB> And is there further, more detailed documentation on how to
PB> construct the find and replace patterns in general?
PB> PB> Thanks!
PB> PB> - Paul
PB> PB> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
PB> news:151d24b61b5a418ccd7e4f5a66339@news.intellij.net...
PB>

>>> Hello Paul,
>>>
>>> Do you need assignment or actually any instance of "" expression out
>>> there? What about passing "" as argument? Using as initializer in
>>> object
>>> initializer or collection initializer syntax? Array of string
>>> creation
>>> expression? Using as initializer for fields?
>>> You probably just want to find "" and replace with string.Empty,
>>> without
>>> any specific details.
>>> Sincerely,
>>> Ilya Ryzhenkov
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>

0

Please sign in to leave a comment.