Hi,
I was looking earlier on at a video some guy did of a VIM macro he'd written that could turn:
object.method { |x| op( x ) }
into
object.method do |x| op( x ) end
and back again. At the time I didn't think much of it and now of course I find myself wanting to do it.
I thought for a few minutes about how you might achieve this in TxMt and then realised I didn't have the first clue. I tried recording a macro but of course it would only work for the simple case, as soon as I did:
object.meth2 { |x,y| op(x,y) }
it broke.
Can this (and/or the inverse) be done with a simple recordable macro?
Regards,
Matt -- Matt Mower :: http://matt.blogs.it/
On 25/11/2005, at 21:08, Matt Mower wrote:
I was looking earlier on at a video some guy did of a VIM macro he'd written that could turn: [...] into [...]
I thought for a few minutes about how you might achieve this in TxMt and then realised I didn't have the first clue. I tried recording a macro but of course it would only work for the simple case, as soon as I did: [...] it broke.
Can this (and/or the inverse) be done with a simple recordable macro?
Yes -- the trick is to use a regexp replacement.
It's a complex expression though, if you search for: ^([\s&&[^\n]]*) (.*?)\s*(?:{\s*(|[^|]+|)\s*(.*?)\s*}|do\s*(|[^|]+|)\s*(\S.*$) \s*end)
It'll find both types of blocks, and put the stuff in captures. Then do replace all in selection with this format string: $1$2(?3: do $3\n $1 $4\n$1end: { $5 $6 })
The (?3:…) part does a conditional replacement, based on whether capture 3 matches or not (which depends on the type of block-style we matched.
I recorded the macro myself and attached below. Save the file to e.g. Ruby.tmbundle/Macros -- it's bound to ctrl-shift-B by default (with scope source.ruby). I can add it to the default bundle, if this is of general use.
definitely of general use, Allan.
that regular expression is going to give me nightmares.
cheers, David
On 27/11/2005, at 11:10 AM, Allan Odgaard wrote:
On 25/11/2005, at 21:08, Matt Mower wrote:
I was looking earlier on at a video some guy did of a VIM macro he'd written that could turn: [...] into [...]
I thought for a few minutes about how you might achieve this in TxMt and then realised I didn't have the first clue. I tried recording a macro but of course it would only work for the simple case, as soon as I did: [...] it broke.
Can this (and/or the inverse) be done with a simple recordable macro?
Yes -- the trick is to use a regexp replacement.
It's a complex expression though, if you search for: ^([\s&&[^\n]]*) (.*?)\s*(?:{\s*(|[^|]+|)\s*(.*?)\s*}|do\s*(|[^|]+|)\s*(\S.*$) \s*end)
It'll find both types of blocks, and put the stuff in captures. Then do replace all in selection with this format string: $1$2(?3: do $3\n$1 $4\n$1end: { $5 $6 })
The (?3:…) part does a conditional replacement, based on whether capture 3 matches or not (which depends on the type of block-style we matched.
I recorded the macro myself and attached below. Save the file to e.g. Ruby.tmbundle/Macros -- it's bound to ctrl-shift-B by default (with scope source.ruby). I can add it to the default bundle, if this is of general use.
<Toggle Single : Multi Line Block.plist>
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
just to complicate matters, I much prefer the curlybrace style expansion. It's also more robust:
items.collect { |i| i.children }.reject{|c| c.children.empty? }
with your regex macro becomes
items.collect do |i| i.children end.reject{|c| c.children.empty? }
which is illegal Ruby; with a curly-style expansion your macro holds up admirably.
I know it's easy enough to change manually, but if your'e considering putting it in the distribution I'd advise to err on the side of curlies
( hope I'm not starting a ruby style religious war ... )
cheers, David
On 27/11/2005, at 11:10 AM, Allan Odgaard wrote:
On 25/11/2005, at 21:08, Matt Mower wrote:
I was looking earlier on at a video some guy did of a VIM macro he'd written that could turn: [...] into [...]
I thought for a few minutes about how you might achieve this in TxMt and then realised I didn't have the first clue. I tried recording a macro but of course it would only work for the simple case, as soon as I did: [...] it broke.
Can this (and/or the inverse) be done with a simple recordable macro?
Yes -- the trick is to use a regexp replacement.
It's a complex expression though, if you search for: ^([\s&&[^\n]]*) (.*?)\s*(?:{\s*(|[^|]+|)\s*(.*?)\s*}|do\s*(|[^|]+|)\s*(\S.*$) \s*end)
It'll find both types of blocks, and put the stuff in captures. Then do replace all in selection with this format string: $1$2(?3: do $3\n$1 $4\n$1end: { $5 $6 })
The (?3:…) part does a conditional replacement, based on whether capture 3 matches or not (which depends on the type of block-style we matched.
I recorded the macro myself and attached below. Save the file to e.g. Ruby.tmbundle/Macros -- it's bound to ctrl-shift-B by default (with scope source.ruby). I can add it to the default bundle, if this is of general use.
<Toggle Single : Multi Line Block.plist>
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
On 28/11/05, David Lee david@davelee.com.au wrote:
just to complicate matters, I much prefer the curlybrace style expansion. It's also more robust:
items.collect { |i| i.children }.reject{|c| c.children.empty? }
with your regex macro becomes
items.collect do |i| i.children end.reject{|c| c.children.empty? }
which is illegal Ruby; with a curly-style expansion your macro holds up admirably.
Ummm there's nothing illegal about that expression.
Regards,
Matt -- Matt Mower :: http://matt.blogs.it/
mm, appears you're right. i stand corrected
cheers, David
On 29/11/2005, at 1:38 AM, Matt Mower wrote:
On 28/11/05, David Lee david@davelee.com.au wrote:
just to complicate matters, I much prefer the curlybrace style expansion. It's also more robust:
items.collect { |i| i.children }.reject{|c| c.children.empty? }
with your regex macro becomes
items.collect do |i| i.children end.reject{|c| c.children.empty? }
which is illegal Ruby; with a curly-style expansion your macro holds up admirably.
Ummm there's nothing illegal about that expression.
Regards,
Matt
Matt Mower :: http://matt.blogs.it/
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
actually, a separate command to switch blocks between do .. end and { .. } syntax would be pretty sweet if you feel inclined. ...
at the rate textmate's coming a long I gotta work out how to get work to spring for an imac ..
cheers, David
On 27/11/2005, at 11:10 AM, Allan Odgaard wrote:
On 25/11/2005, at 21:08, Matt Mower wrote:
I was looking earlier on at a video some guy did of a VIM macro he'd written that could turn: [...] into [...]
I thought for a few minutes about how you might achieve this in TxMt and then realised I didn't have the first clue. I tried recording a macro but of course it would only work for the simple case, as soon as I did: [...] it broke.
Can this (and/or the inverse) be done with a simple recordable macro?
Yes -- the trick is to use a regexp replacement.
It's a complex expression though, if you search for: ^([\s&&[^\n]]*) (.*?)\s*(?:{\s*(|[^|]+|)\s*(.*?)\s*}|do\s*(|[^|]+|)\s*(\S.*$) \s*end)
It'll find both types of blocks, and put the stuff in captures. Then do replace all in selection with this format string: $1$2(?3: do $3\n$1 $4\n$1end: { $5 $6 })
The (?3:…) part does a conditional replacement, based on whether capture 3 matches or not (which depends on the type of block-style we matched.
I recorded the macro myself and attached below. Save the file to e.g. Ruby.tmbundle/Macros -- it's bound to ctrl-shift-B by default (with scope source.ruby). I can add it to the default bundle, if this is of general use.
<Toggle Single : Multi Line Block.plist>
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate