Global string search and replace in an image

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

Global string search and replace in an image

Andy Burnett
Hello,

Is there a method - or other mechanism - that would let me find all instances of 'foo' and replace them with 'bar' within an image? I imagine there must be, but I can't find it.

Cheers
Andy

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Chris Cunnington
On 12-09-10 7:34 PM, Andy Burnett wrote:
Hello,

Is there a method - or other mechanism - that would let me find all instances of 'foo' and replace them with 'bar' within an image? I imagine there must be, but I can't find it.

Cheers
Andy


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Try this:

OrderedCollection allInstances size

Print that. That's the number of instances of OC in the image. Now try

OrderedCollection allInstances

or

OrderedCollection allInstances first

And take a look at the living instances inside the image. That's how you find instances. Replacing them is kind of a different issue. I'd say two things about that. The first is you don't want to just replace instances in the image. You need to be more specific of the context of your instances. Are they in an OrderedCollection or an Array? If so, find that instance and change them inside.

The second thing is you wouldn't generally replace instances in the image. Just wait and they'll be eaten by the garbage collector.

So finding instances is not to hard. Use #allInstances. To replace them you need to look not so much at the whole image but the application those instances are in. Inside your app.

Oh, yea. You can use OrderedCollection allIstances and both explore and inspect. You can see inside the instances that way. (Explore and inspect are one of the main menus.)

Hope that helps,
Chris



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Bert Freudenberg

On 2012-09-11, at 01:43, Chris Cunnington <[hidden email]> wrote:

> On 12-09-10 7:34 PM, Andy Burnett wrote:
>> Hello,
>>
>> Is there a method - or other mechanism - that would let me find all instances of 'foo' and replace them with 'bar' within an image? I imagine there must be, but I can't find it.
>>
>> Cheers
>> Andy
>>
>>
>> _______________________________________________
>> Beginners mailing list
>>
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
> Try this:
>
> OrderedCollection allInstances size
>
> Print that. That's the number of instances of OC in the image. Now try
>
> OrderedCollection allInstances
>
> or
>
> OrderedCollection allInstances first
>
> And take a look at the living instances inside the image. That's how you find instances. Replacing them is kind of a different issue. I'd say two things about that. The first is you don't want to just replace instances in the image. You need to be more specific of the context of your instances. Are they in an OrderedCollection or an Array? If so, find that instance and change them inside.
>
> The second thing is you wouldn't generally replace instances in the image. Just wait and they'll be eaten by the garbage collector.
>
> So finding instances is not to hard. Use #allInstances. To replace them you need to look not so much at the whole image but the application those instances are in. Inside your app.
>
> Oh, yea. You can use OrderedCollection allIstances and both explore and inspect. You can see inside the instances that way. (Explore and inspect are one of the main menus.)
>
> Hope that helps,
> Chris

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.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Chris Cunnington
On 12-09-11 5:02 AM, Bert Freudenberg wrote:

> On 2012-09-11, at 01:43, Chris Cunnington <[hidden email]> wrote:
>
>> On 12-09-10 7:34 PM, Andy Burnett wrote:
>>> Hello,
>>>
>>> Is there a method - or other mechanism - that would let me find all instances of 'foo' and replace them with 'bar' within an image? I imagine there must be, but I can't find it.
>>>
>>> Cheers
>>> Andy
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>>
>>> [hidden email]
>>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>> Try this:
>>
>> OrderedCollection allInstances size
>>
>> Print that. That's the number of instances of OC in the image. Now try
>>
>> OrderedCollection allInstances
>>
>> or
>>
>> OrderedCollection allInstances first
>>
>> And take a look at the living instances inside the image. That's how you find instances. Replacing them is kind of a different issue. I'd say two things about that. The first is you don't want to just replace instances in the image. You need to be more specific of the context of your instances. Are they in an OrderedCollection or an Array? If so, find that instance and change them inside.
>>
>> The second thing is you wouldn't generally replace instances in the image. Just wait and they'll be eaten by the garbage collector.
>>
>> So finding instances is not to hard. Use #allInstances. To replace them you need to look not so much at the whole image but the application those instances are in. Inside your app.
>>
>> Oh, yea. You can use OrderedCollection allIstances and both explore and inspect. You can see inside the instances that way. (Explore and inspect are one of the main menus.)
>>
>> Hope that helps,
>> Chris
> 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.
>
> - Bert -
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
On reading this again this morning, it clear he was talking about source
code. My mistake. Thanks for the correction.

Chris

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Andy Burnett
In reply to this post by Andy Burnett


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 file it in again. I have tried this in the past, but the image never seems to work properly afterwards.  

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?

Cheers
Andy

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Hannes Hirzel
On 9/11/12, Andy Burnett <[hidden email]> 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.

This is surely not good as it is difficult to maintain, i.e.
translation of the interface into another language is not easy
possible.

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 file it in again. I have tried this in the past, but
> the image never seems to work properly afterwards.

No I would do the refactoring within Squeak.


> Clearly, the correct way to solve the problem is to abstract the hard coded
> text into another object - or maybe a global variable?

Yes, a simple thing you can do is to shift the strings to the class
side and collect them there in a method protocol.

So in an instance method you access the string as

     self class myHelpStringHowToAchieveThis

and on the class side you have a method protocol labeled 'string
constants'. It contains for this case the method

myHelpStringHowToAchieveThis

        ^'To achieve x you have to do this and that.'



The next step is as you write to have a global string dictionary.  For
example MyStrings.

You avoid hard coding the link to this dictionary in many methods you
have to access the dictionary indirectly.

So you access it with something like this

(Smalltalk at: #HHMyTextStrings) at: 'myHelpStringHowToAchieveThis'


The first part
(Smalltalk at: #HHMyTextStrings)
gives you the dictionary (a global variable) HHMyTextStrings without
hard coding a dependency on it during loading of the code.

then
dict at: 'myHelpStringHowToAchieveThis'
is a regular access to a string in a dictionary which keeps strings.

Note:

Instead of referencing a dictionary with HHMyTextStrings the object
might be more complex.

It could for example understand the message

HHMyTextStrings setDefaultLanguage: #French

and then serve the strings in French.

I hope this makes sense.

--Hannes




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?
>
> Cheers
> Andy
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Levente Uzonyi-2
In reply to this post by Andy Burnett
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
>
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Karl Ramberg
In reply to this post by Andy Burnett
On Tue, Sep 11, 2012 at 2:48 PM, Andy Burnett
<[hidden email]> 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 file it in again. I have tried this in the past, but
> the image never seems to work properly afterwards.

If you select the string in the method pane and press alt + e you get
a browser with all occurrences of the selected string

Karl

>
> 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?
>
> Cheers
> Andy
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Global string search and replace in an image

Andy Burnett
In reply to this post by Andy Burnett
Thanks everyone, that has given me some great ideas. I think refactoring is clearly the way to go, and - at least - I now know how to search for the terms in the source code.

Cheers
Andy

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners