[Newbies] Re: Global string search and replace in an image

Levente Uzonyi leves at elte.hu
Tue Sep 11 14:32:49 UTC 2012


On Tue, 11 Sep 2012, Andy Burnett wrote:

> 
>
>       I thought Andy was talking about source code.
>
>       Personally, if I have to rename a method, I search for all senders and fix them with copy&paste. Same for class renames, inst var renames,
>       etc. There just are not that many occurrences, so fixing each one individually is quick, plus I get to verify that the change is indeed what I
>       wanted.
>
>       There is a tool to automate a lot of this called the Refactoring Browser, which many developers like, but I don't even have it in my image.
> 
> 
> Thanks very much to both of you.
> 
> Bert, you are quite right, I was talking about changing strings in source code - sorry, I should have made that clear.
> 
> The problem is that we I am dealing with an image where a number of strings have been hard coded into various methods. What I was looking for was a global
> - source code - search and replace.  Does that exist?  Or, is it possible to FileOut the entire source tree, do a search and replace externally, and then

Searching is implemented in SystemNavigation. If you don't want to do 
manual replace or roll your own code, then you can use the refactoring 
tools in OmniBrowser (note that it has some known bugs in Squeak 4.x).

If you want to search methods which have a String literal that has 'foo' 
as a substring, then you can use the following expression:

SystemNavigation default browseMethodsWithString: 'foo' matchCase: true

If you want to search for methods, which have a String literal 'foo', then 
try this:

SystemNavigation default browseAllSelect: [ :method |
 	method hasLiteralSuchThat: [ :each |
 		each isString
 			and: [ each isSymbol not
 			and: [ each = 'foo' ] ] ] ]

> file it in again. I have tried this in the past, but the image never seems to work properly afterwards.  

If you filed out "system" code and did the search and replace in that too, 
then things could have gone wrong.

> 
> Clearly, the correct way to solve the problem is to abstract the hard coded text into another object - or maybe a global variable? Perhaps I will just
> have to bite the bullet.  This would make it far easier for internationalisation etc.  Actually, are there any packages to help with internationalisation?

If you don't want to hardcode Strings, then you can use a class and create 
class side methods for each of string. Or you can create a SharedPool, but 
the class sounds like a better solution for your problem.

In the soon to be released Squeak 4.4 GetText support is coming.


Levente

> 
> Cheers
> Andy
> 
>


More information about the Beginners mailing list