Structural Search

I want to match two statements and treat the pattern as
one selection. The following code segment shows code
similar to what exists in my project:


and I want to be able to match the two lines appending the
":" and hashCode and use this as a selection so I can
replace it with some other code.

I'm getting either a no match, or multiple selections,
each corresponding to a variable I defined in the pattern:

where sb is defined as 'sb' and counts are 0 - 100, target
append is defined as 'append' and counts are 0 - 100, target
hashCode is defined as 'hashCode', counts 0-100, target.

I've tried to create a surrounding scope, defining the
surrounding class and the toString method as part of
the pattern, but I cannot get the thing to treat the
matched pattern as a single selection to be operated on.

Any ideas?

Bonny


6 comments
Comment actions Permalink

Hi,

To find code like
sb.append( ":" );
sb.append( hashCode );

please, use following pattern:

sb.append( ":" );
sb.append( $hashCode$ );

If you want to find such code only in toString() methods of some
classes, you can search for toString methods initially and then search
(with first pattern) in previous results.

Bonny Rais wrote:

I want to match two statements and treat the pattern as
one selection. The following code segment shows code
similar to what exists in my project:

  public String toString()
>  {
>     StringBuffer sb ...
>    
>     sb.append( "text1" );
>     sb.append( field1);
>     sb.append( ":" );
>     sb.append( hashCode );
>     sb.append( "some more text" );
>     ...
>   }
> ]]>


and I want to be able to match the two lines appending the
":" and hashCode and use this as a selection so I can
replace it with some other code.

I'm getting either a no match, or multiple selections,
each corresponding to a variable I defined in the pattern:

  $sb$.$append$( ":" );
>  $sb$.$append$( $hashCode$ );
> ]]>

where sb is defined as 'sb' and counts are 0 - 100, target
append is defined as 'append' and counts are 0 - 100, target
hashCode is defined as 'hashCode', counts 0-100, target.

I've tried to create a surrounding scope, defining the
surrounding class and the toString method as part of
the pattern, but I cannot get the thing to treat the
matched pattern as a single selection to be operated on.

Any ideas?

Bonny




--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Maxim, is it planned to provide an in-depth tutorial with releasing 4.5?

Tom

0
Comment actions Permalink

Hi,

Some example based tutorial is in preparation.

Thomas Singer (MoTJ) wrote:

Maxim, is it planned to provide an in-depth tutorial with releasing 4.5?

Tom



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Maxim,

The problem is not that the search does not find the right
pattern, but rather that only the variables are matched
for the purposes of replacement.
What I wanted to achieve above was to match the complete
search pattern and replace it with some other text, or
completely remove it, but when I perform the replacement,
all that gets replaced is the highligthed text in the
preview window which matches only the variable definitions

0
Comment actions Permalink

Further to my previous comment, I have succeeded in
creating a pattern that gets matched correctly by removing
all variable definition from the template.

I suppose this is valid for my current case and provides
me with a way to match multiple lines which the regular
search/replace doesn't but it does not buy me anything
else either...

And while this works for the current search, I want to
be able to define a variable other than hashCode above
that would match multiple variable usages and I don't
seem to be able to do this.

Cheers,

Bonny

0
Comment actions Permalink

Hi,

Replacement (should) replaces the match context completely.
Following should work.

Search:
$sb$.append( ":" );
$sb$.append( $hashCode$ );
Replacement (example):
$sb$.append( ': ').append( $hashCode$ );

Constraints:
sb - expression type is StringBuffer

Do you use 'This variable is target of the search' option during
replacement? Please, try without it.


Bonny Rais wrote:

Further to my previous comment, I have succeeded in
creating a pattern that gets matched correctly by removing
all variable definition from the template.

I suppose this is valid for my current case and provides
me with a way to match multiple lines which the regular
search/replace doesn't but it does not buy me anything
else either...

And while this works for the current search, I want to
be able to define a variable other than hashCode above
that would match multiple variable usages and I don't
seem to be able to do this.

Cheers,

Bonny



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Please sign in to leave a comment.