Search and replace source code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Search and replace source code

laura
Hi all,

How do you replace all 'expression1' in source code by 'expression2'?
I can find them with Finder but don't see any easy way to replace them.
My problem is i want to change parameter names in many methods.

Best,
Laura
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace source code

Aliaksei Syrel
Hi Laura,

you can use this method:

replaceAll: aString with: anotherString
"replaces aString in source code in the whole image in all methods with anotherString"
((self systemNavigation allMethodsWithSourceString: aString matchCase: false) collect: #compiledMethod)
do: [ :each | each methodClass compile: (each sourceCode copyReplaceAll: aString with: anotherString) classified: each category ]

It will replace all  'expression1' by 'expression2' globally in the whole image. Will take some time

Cheers,
Alex

On Thu, Mar 19, 2015 at 5:51 PM, Laura Risani <[hidden email]> wrote:
Hi all,

How do you replace all 'expression1' in source code by 'expression2'?
I can find them with Finder but don't see any easy way to replace them.
My problem is i want to change parameter names in many methods.

Best,
Laura

Reply | Threaded
Open this post in threaded view
|

Re: Search and replace source code

Thierry Goubier
In reply to this post by laura
Hi Laura,

you can use the parse tree rewriter of the Refactoring Browser set. Like
that, you can have a degree of control about what you do replace and on
what.

For example, I'd try something like:

| rewrite |
rewrite := RBParseTreeRewriter new
        replace: 'expression1'
        with: 'expression2'.
myClass methods
        do: [ :e |
                (rewrite executeTree: (RBParser parseMethod: e sourceCode))
                        ifTrue: [ myClass compile: rewrite tree newSource ] ]

I'm not entirely sure it will do what I expect, but I'm trying to learn
to use it more than I usually do, because it's very powerfull.

Another example where the rewriter execute some smalltalk code during
the search replace:

https://gist.github.com/ThierryGoubier/54ba2fcf3cd2e7db9b1e

Thierry

Le 19/03/2015 17:51, Laura Risani a écrit :
> Hi all,
>
> How do you replace all 'expression1' in source code by 'expression2'?
> I can find them with Finder but don't see any easy way to replace them.
> My problem is i want to change parameter names in many methods.
>
> Best,
> Laura


Reply | Threaded
Open this post in threaded view
|

Re: Search and replace source code

Sean P. DeNigris
Administrator
Thierry Goubier wrote
you can use the parse tree rewriter of the Refactoring Browser set
Didn't someone demo a cool rewrite tool GUI recently? I don't remember where exactly...
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace source code

Thierry Goubier
Le 19/03/2015 19:05, Sean P. DeNigris a écrit :
> Thierry Goubier wrote
>> you can use the parse tree rewriter of the Refactoring Browser set
>
> Didn't someone demo a cool rewrite tool GUI recently? I don't remember where
> exactly...

Yes! Mark Rizun rewrite tool (http://myfuncoding.blogspot.fr/). Recommended!

But you can't have fun and use smalltalk scripts inside your search and
rewrite terms with it ;)

Thierry

Reply | Threaded
Open this post in threaded view
|

Re: Search and replace source code

laura
Thank you all, your answers allowed me to solve my problem. Also very interesting tools!

On Thu, Mar 19, 2015 at 3:19 PM, Thierry Goubier <[hidden email]> wrote:
Le 19/03/2015 19:05, Sean P. DeNigris a écrit :
Thierry Goubier wrote
you can use the parse tree rewriter of the Refactoring Browser set

Didn't someone demo a cool rewrite tool GUI recently? I don't remember where
exactly...

Yes! Mark Rizun rewrite tool (http://myfuncoding.blogspot.fr/). Recommended!

But you can't have fun and use smalltalk scripts inside your search and rewrite terms with it ;)

Thierry