RBRegexExtensions
In an
earlier post,
I showed how I converted a pragma-based type specification from using
a literal binding reference of the form #{String}
to a symbol
#String
. Unfortunately, by the time I figured out that I could do
this, I had some 1700 references to the two type-specification pragmas
in my image. I wasn’t fond of the idea of fixing all of those by
hand.
I started out by trying to make the Rewrite Tool do what I want, but was unsuccessful. It could be because I couldn’t figure out the syntax, but I also believe that the version I was using didn’t support pragmas. Fortunately, I remembered another tool, RBRegexExtensions, that allows you to do regular expression search and replace operations in the refactoring browser.
I first browsed for all references to #fitReturns:
, selected all of
the methods, and used the tool to replace <fitReturns\:
(.*)#{(\w+)}(.*)>
with <fitReturns: $1#$2$3>
to pick up all of the
return-type specifications. This expression matches the common case
of a simple type such as #{String}
, but also handles cases where the
binding references are nested inside of arrays, such as
#(#{String})
. I re-ran this expression until it no longer found any
matches. I then repeated the process for #fitTakes:
to pick up the
parameter type specifications.
By now all my text-editor-using readers are laughing because regex search and replace is stock-in-trade in that environment, and yet here I am blogging about using it in Smalltalk.
When all you can do is manipulate text, you need regexes often. However, in Smalltalk most of the tools — especially the automated refactorings — work at the level of parse trees and not text. Because of this, many Smalltalk programmers don’t think about doing a “simple” brute-force regex search and replace, even when it’s the right tool for the job.
RBRegexExtensions is included with Visualworks Smalltalk as a contributed parcel. Thanks to John Brant for this handy tool, and to anyone else involved in its development.