Hi everybody
I'm a long time TextMate user which I love more than any other editor. Before writing that e-mail I've looked on the web, downloaded the full archives of this list and grep it with some keywords and nothing came out.
As a developer I often want to extract a part of a line of code into a variable, and put it's assignment in the previous line. Example :
something a_method(with, arguments), something_else
would become
my_variable = a_method(with, arguments) something my_variable, something_else
I can't figure how to : * extract the selected text and put it in a new line on top of the current, prefixed by a variable assignment * have the variable name selected and ready to overwrite, mirrored where it has been extracted from * preserve the indentation (on both lines) * preserve what was before and after the extracted part
I've seen people do this in Vim and Emacs, but never in TextMate. I've asked for help and James Edward Gray II gave me some advice on Twitter and here https://gist.github.com/3515210
It seems that the best way would be to replace the whole document.
I don't really see what I should use : a macro, a snippet, a command, or a combination of them.
I guess that I could do this with a macro + snippet, but I've not found (yet) the list of available commands (moveToEndOfParagraph, insertText, …) and their documentation.
I'd be happy to have a pre-cooked solution if it exists, but any advice would be helpful
Thanks
-- Jérémy Lecour http://jeremy.wordpress.com - http://twitter.com/jlecour
On Aug 30, 2012, at 10:43 AM, Jérémy Lecour wrote:
As a developer I often want to extract a part of a line of code into a variable, and put it's assignment in the previous line. Example :
something a_method(with, arguments), something_else
would become
my_variable = a_method(with, arguments) something my_variable, something_else
hmm, since you select something anyway, one could think of selecting 'something' as well.
If 'something' consists of one word only then this _very fast written_ snippet should work:
select: "something a_method(with, arguments)"
and invoke that snippet
${1:variable} = `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*\w+\s*//'` `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*(\w+)\s*.*/\1/'` ${1/\s*//}
Cheers, --Hans
On Aug 30, 2012, at 11:11 AM, Hans-Jörg Bibiko bibiko@eva.mpg.de wrote:
select: "something a_method(with, arguments)"
and invoke that snippet
${1:variable} = `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*\w+\s*//'` `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*(\w+)\s*.*/\1/'` ${1/\s*//}
Neat approach but no need to call out to shell / perl:
${1:variable} = ${TM_SELECTED_TEXT/\A\s*\w+\s*//} ${TM_SELECTED_TEXT/\A\s*(\w+).*/$1/} ${1}
And here’s a tip regarding selection: in 2.0 using a “unit selection”, i.e. the stuff under Edit → Select, using shift + movement will always extend selection, so e.g. with something like:
something a_method(with, arguments), something_else
Place caret inside parenthesis, use ⇧⌘B (select to enclosing pairs) and then one can use ⌥⇧← to extend the selection to the method name (with 1.x unit selections would produce a regular “anchored” selection, so using left/right movement would shrink/expand respectively).
Thank you Alan and Hans for your answer.
On Aug 30, 2012, at 11:11 AM, Hans-Jörg Bibiko bibiko@eva.mpg.de wrote:
select: "something a_method(with, arguments)"
and invoke that snippet
${1:variable} = `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*\w+\s*//'` `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*(\w+)\s*.*/\1/'` ${1/\s*//}
Neat approach but no need to call out to shell / perl:
${1:variable} = ${TM_SELECTED_TEXT/\A\s*\w+\s*//} ${TM_SELECTED_TEXT/\A\s*(\w+).*/$1/} ${1}
It is working with the exact (very simple) example that I've written previously. But it is not working at all with a slightly more complicated real example like that :
@articles = @article.category_id.present? ? Article.where('category_id = ?', @article.category_id) : []
I'd like to obtain this :
variable = Article.where('category_id = ?', @article.category_id) @articles = @article.category_id.present? ? variable : []
I guess your solution doesn't work in this example because of the whitespaces, …
In Vim, it seems that it can be done with a prompt of the desired variable name, then replacing the selected text with it, and finally inserting the assignment on a new line above.
Does this seems feasible in TextMate?
And here’s a tip regarding selection: in 2.0 using a “unit selection”, i.e. the stuff under Edit → Select, using shift + movement will always extend selection, so e.g. with something like:
something a_method(with, arguments), something_else
Place caret inside parenthesis, use ⇧⌘B (select to enclosing pairs) and then one can use ⌥⇧← to extend the selection to the method name (with 1.x unit selections would produce a regular “anchored” selection, so using left/right movement would shrink/expand respectively).
I should have stated that I'm using TextMate Version 1.5.11 (1634)
I'm not a big fan of alpha software for my day to day use ;-)
Thank you so much for your help, it's really appreciated.
Jérémy Lecour Conception et développement d'applications web http://jeremy.wordpress.com - http://twitter.com/jlecour
On Aug 30, 2012, at 5:52 PM, Jérémy Lecour jeremy.lecour@gmail.com wrote:
[…] Does this seems feasible in TextMate?
A computer can do almost everything as long as you can come up with the proper algorithm. I am rather lost as what exactly the algorithm you ask for should do, besides the “sometimes I have some code, and I would like it to move around a little and introduce a new variable” ;)
If you have seen the feature in vim, it might be worth simply finding the code for it, and see exactly what it does in which situations.
Le 30 août 2012 à 19:20, Allan Odgaard mailinglist@textmate.org a écrit :
On Aug 30, 2012, at 5:52 PM, Jérémy Lecour jeremy.lecour@gmail.com wrote:
[…] Does this seems feasible in TextMate?
A computer can do almost everything as long as you can come up with the proper algorithm. I am rather lost as what exactly the algorithm you ask for should do, besides the “sometimes I have some code, and I would like it to move around a little and introduce a new variable” ;)
If you have seen the feature in vim, it might be worth simply finding the code for it, and see exactly what it does in which situations.
I'm sorry Allan if I didn't make myself clear or seemed to ask for the moon (for free). That was not my intention.
What I was trying to say is that my first example was voluntarily extremely simple, and the proposed solution didn't work for a more complicated but common situation.
I've been in the software business for many years and I know that we can do anything with the correct amount of effort. By asking if it was feasible with TextMate it was more of a question about the tools provided by TextMate. Tools that I've had trouble grasping, probably because I've not spent enough time learning them.
As I've (poorly) explained, within Vim the desired feature is done like this (I'll try to get the "real" code) : * select the portion to extract * prompt for a variable name * replace the selected text by the variable name * insert a new line just above the current one with the extracted portion prefix by the variable name and an equal
Maybe these steps can be reproduced within TextMate. Using a snippet and a placeholder was not a "requirement" for me, but an idea of a possible solution.
Again, I'm sorry if I sound like a spoiled kid, crying for a free toy. That's not me. I'm really grateful of your work and your help.
Cheers
Jérémy Lecour Conception et développement d'applications web 06 22 43 88 94 - http://jeremy.wordpress.com - http://twitter.com/jlecour
Here is a bundle "Command" that I use to do this. It is written for scala so the variable declaration is preceded with the 'var' keyword. Just remove it if you are working with a language such as Ruby that does not require this.
https://gist.github.com/3543696
On Thu, Aug 30, 2012 at 4:59 PM, Jérémy Lecour jeremy.lecour@gmail.comwrote:
Le 30 août 2012 à 19:20, Allan Odgaard mailinglist@textmate.org a écrit :
On Aug 30, 2012, at 5:52 PM, Jérémy Lecour jeremy.lecour@gmail.com
wrote:
[…] Does this seems feasible in TextMate?
A computer can do almost everything as long as you can come up with the
proper algorithm. I am rather lost as what exactly the algorithm you ask for should do, besides the “sometimes I have some code, and I would like it to move around a little and introduce a new variable” ;)
If you have seen the feature in vim, it might be worth simply finding
the code for it, and see exactly what it does in which situations.
I'm sorry Allan if I didn't make myself clear or seemed to ask for the moon (for free). That was not my intention.
What I was trying to say is that my first example was voluntarily extremely simple, and the proposed solution didn't work for a more complicated but common situation.
I've been in the software business for many years and I know that we can do anything with the correct amount of effort. By asking if it was feasible with TextMate it was more of a question about the tools provided by TextMate. Tools that I've had trouble grasping, probably because I've not spent enough time learning them.
As I've (poorly) explained, within Vim the desired feature is done like this (I'll try to get the "real" code) :
- select the portion to extract
- prompt for a variable name
- replace the selected text by the variable name
- insert a new line just above the current one with the extracted portion
prefix by the variable name and an equal
Maybe these steps can be reproduced within TextMate. Using a snippet and a placeholder was not a "requirement" for me, but an idea of a possible solution.
Again, I'm sorry if I sound like a spoiled kid, crying for a free toy. That's not me. I'm really grateful of your work and your help.
Cheers
Jérémy Lecour Conception et développement d'applications web 06 22 43 88 94 - http://jeremy.wordpress.com - http://twitter.com/jlecour
textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate
Le 31 août 2012 à 00:48, Curt Sellmer sellmerfud@gmail.com a écrit :
Here is a bundle "Command" that I use to do this. It is written for scala so the variable declaration is preceded with the 'var' keyword. Just remove it if you are working with a language such as Ruby that does not require this.
Curt my man, you rock !
I've noticed that the TM_SELECTION variable might be a TM2 thing, not available in TM1, so I've forked your command to adapt it to TM1 : https://gist.github.com/3550146
Thank you Allan, Hans, Curt and James (via Twitter) for your help on this.
Jérémy Lecour Conception et développement d'applications web http://jeremy.wordpress.com - http://twitter.com/jlecour