Forum: Ruby Automatic code generation in vim.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47b36de21d7ecbc824c81d24802a6290?d=identicon&s=25 Minkoo Seo (pool007)
on 2006-05-26 20:58
(Received via mailing list)
Hi list.

This is an editor question for Ruby. I've seen the demo of RoR
where textmate is widely used. Among the others, the most
interesting feature was automatic code generation like

forin

=>

for <cursor here> in <collections>

end

I've searched for similar functionality in vim community, but I couldn't
find. There was scriptEmu(
http://www.vim.org/scripts/script.php?script_id=1318),
but it does not work with my vim7.0 installation.

Does anybody have a nice solution for automatic code generation
for Ruby in VIM?

Sincerely,
Minkoo Seo
48124635945b45221ba12a26371f9e3e?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-05-26 21:01
(Received via mailing list)
>
> This is an editor question for Ruby. I've seen the demo of RoR
> where textmate is widely used. Among the others, the most
> interesting feature was automatic code generation like
>
> forin
>
> =>
>
> for <cursor here> in <collections>

Not sure if pre-existing stuff exists for this (but I think so) but you
can use vim mappings for this.

http://www.vim.org/htmldoc/map.html

-philip
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2006-05-26 21:36
(Received via mailing list)
On Sat, May 27, 2006 at 03:56:25AM +0900, Minkoo Seo wrote:
> Hi list.
>
> This is an editor question for Ruby. I've seen the demo of RoR
> where textmate is widely used. Among the others, the most
> interesting feature was automatic code generation like
[...]
> I've searched for similar functionality in vim community, but I couldn't
> find. There was scriptEmu(
> http://www.vim.org/scripts/script.php?script_id=1318),
> but it does not work with my vim7.0 installation.
>
> Does anybody have a nice solution for automatic code generation
> for Ruby in VIM?

http://blog.rosejn.net/articles/2005/12/09/snippets-0-01
http://blog.rosejn.net/articles/2006/02/28/snippetmagic-0-02

I read it's closer to TextMate than snippetsEmu.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-05-26 21:55
(Received via mailing list)
Minkoo Seo wrote:

>
> Does anybody have a nice solution for automatic code generation
> for Ruby in VIM?


Get the vim stuff for Ruby, and look in ruby-macros.vim

I used that to learn how to write my own simple autotext stuff.


For example:

# If I enter ifFILE, then vim should call the ifFILE function
iab <buffer> ifFILE <C-R>=<SID>ifFILE()

# which is
function! s:ifFILE()
     return "if __FILE__ == $0 \<Esc>o \<Esc>oend"
endfunction



--
James Britt

"The use of anthropomorphic terminology when dealing with
computing systems is a symptom of professional immaturity."

  - Edsger W. Dijkstra
Fcc5cdf0f0f3e1a3a39c11ed4bf8d5e5?d=identicon&s=25 Stephan Mueller (Guest)
on 2006-05-27 13:39
(Received via mailing list)
* Minkoo Seo <minkoo.seo@gmail.com> [060526 21:10]:
> Does anybody have a nice solution for automatic code generation
> for Ruby in VIM?

you may have a look at the file plugin/imaps.vim from the latex-suite
project. Taken from the header of that file:

>>>
Motivation:

this script provides a way to generate insert mode mappings which do not
suffer from some of the problem of mappings and abbreviations while
allowing cursor placement after the expansion. It can alternatively be
thought of as a template expander.
<<<

The script supports place holder and more. I did not try yet, but I am
shure this script might proove quite usefull for ruby editing as well.

HTH,

Steph.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-28 00:32
(Received via mailing list)
On May 26, 2006, at 3:53 PM, James Britt wrote:

>
>
>
Incidentally, I tried this put and changed it to
function! s:ifFILE()
     return "if __FILE__ == $0 \<Esc>o \<Esc>oend\<Esc>ki"
endfunction

which sticks the cursor where you would expect it to be.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-05-28 02:07
(Received via mailing list)
Logan Capaldo wrote:

> Incidentally, I tried this put and changed it to
> function! s:ifFILE()
>     return "if __FILE__ == $0 \<Esc>o \<Esc>oend\<Esc>ki"
> endfunction
>
> which sticks the cursor where you would expect it to be.

Oh, nice.  Thanks for the pointer.



James
567898c496278341be69087507d5ed24?d=identicon&s=25 Jeff Rose (Guest)
on 2006-05-28 11:19
(Received via mailing list)
Mauricio Fernandez wrote:
>> Does anybody have a nice solution for automatic code generation
>> for Ruby in VIM?
>
> http://blog.rosejn.net/articles/2005/12/09/snippets-0-01
> http://blog.rosejn.net/articles/2006/02/28/snippetmagic-0-02
>
> I read it's closer to TextMate than snippetsEmu.

I haven't been able to work on it for a while, but snippet magic is for
this exact purpose, in vim.  Actually, the TextMate snippet definitions
are all open source and available in a public repository so I converted
them to YAML and now use them as the basis for snippet magic's default
definitions.  Give it a try and see what you think.  Lots of people have
sent mail or posted that they are using it, but it does still have some
issues that need to be fixed.  (A better binding for ruby-vim would help
a lot here...)  I'll try to get back to it soon, but if anyone wants to
help out that would be great.

-Jeff
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-05-28 12:42
(Received via mailing list)
On a side note, has anyone got code generation (e.g. adding "end")
working with Vim in the console on Linux? It only work with GVim for me
-- the console version doesn't recognize the shift key or something...
Anyone?


Daniel
47b36de21d7ecbc824c81d24802a6290?d=identicon&s=25 Minkoo Seo (pool007)
on 2006-05-28 20:34
(Received via mailing list)
I've tried snippet magic, but I can't figure out how to use. As an
example,
when I type 'forin<tab>' it expands like

for element in collection
    element.
end

and my cursor is at 't' of 'element*t*'. And the status of vim is
command-mode.
(The mode after <ESC> key is pressed)

Now, what I have to do? Because my cursor is at 't' of 'element', I
can't
change
the name of element using 'cw'. I just stuck.

Sincerely,
Minkoo Seo
47b36de21d7ecbc824c81d24802a6290?d=identicon&s=25 Minkoo Seo (pool007)
on 2006-05-28 20:34
(Received via mailing list)
AFAIK, it's impossible to map 'shift' key under console mode of *NIX.
So, try to map it with another key binding like <c+_>.

Sincerely,
Minkoo Seo
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-05-28 21:15
(Received via mailing list)
Minkoo Seo wrote:
>
> Now, what I have to do? Because my cursor is at 't' of 'element', I can't
> change
> the name of element using 'cw'. I just stuck.
caw should do what you want, I think?  Surely it's collection that you
want to change the name of, though?  I mean, in a for loop, the inner
loop variable name is less likely to be important than the collection
name?
567898c496278341be69087507d5ed24?d=identicon&s=25 Jeff Rose (Guest)
on 2006-05-29 02:32
(Received via mailing list)
Minkoo Seo wrote:
>
> Now, what I have to do? Because my cursor is at 't' of 'element', I can't
> change
> the name of element using 'cw'. I just stuck.

Hmmm.  This has been one of the most frustrating things about this
thing.  For some it works just fine, and others it falls on its face.  I
don't know if it has to do with vim versions, preset mappings or what.
One problem is that since the ruby bindings are so minimal many commands
on the buffer have to be done by passing vim-script through ruby.  I
think this makes it susceptible to mappings and a mess of other junk, as
opposed to a true binding that would have ruby methods directly
operating on the buffer.  Anyway...

What should happen in this case is that 'element' should be highlighted
in select mode.  (Highlight something in visual mode and type <ctl>-g)
When you type it will change the word to whatever variable name you
want, and when you hit tab it will automatically update the mirrors of
that element, which in this case means the variable in the body.  The
idea is that you tab through the snippet, editing the important bits and
tab jumping to where you want to be next.

Once the term is over at the end of this month I'll get more serious
about this again.

-Jeff
This topic is locked and can not be replied to.