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.
Minkoo S. (Guest)
on 2006-05-26 22: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 S.
Philip H. (Guest)
on 2006-05-26 23: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
Mauricio F. (Guest)
on 2006-05-26 23:36
(Received via mailing list)
On Sat, May 27, 2006 at 03:56:25AM +0900, Minkoo S. 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.
James B. (Guest)
on 2006-05-26 23:55
(Received via mailing list)
Minkoo S. 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 B.

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

  - Edsger W. Dijkstra
Stephan M. (Guest)
on 2006-05-27 15:39
(Received via mailing list)
* Minkoo S. <removed_email_address@domain.invalid> [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.
Logan C. (Guest)
on 2006-05-28 02:32
(Received via mailing list)
On May 26, 2006, at 3:53 PM, James B. 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.
James B. (Guest)
on 2006-05-28 04:07
(Received via mailing list)
Logan C. 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
Jeff R. (Guest)
on 2006-05-28 13:19
(Received via mailing list)
Mauricio F. 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
Daniel S. (Guest)
on 2006-05-28 14: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
Minkoo S. (Guest)
on 2006-05-28 22: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 S.
Minkoo S. (Guest)
on 2006-05-28 22: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 S.
Alex Y. (Guest)
on 2006-05-28 23:15
(Received via mailing list)
Minkoo S. 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?
Jeff R. (Guest)
on 2006-05-29 04:32
(Received via mailing list)
Minkoo S. 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.