Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created September 19, 2017 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujihisa/7a429f43033945e19444f38964b71e75 to your computer and use it in GitHub Desktop.
Save ujihisa/7a429f43033945e19444f38964b71e75 to your computer and use it in GitHub Desktop.

How to develop CRuby easily with Vim

Tatsuhiro Ujihisa github.com/ujihisa

@ujm

>(._. )<
  • Develop CRuby with Vim
  • Hard to structure things you should know for 5min talk

-> FAQ style 11 questions and answers

Q0. I'm stuck

Play some games

Q1. Indentation?

Q. Indentation? (1/8)

"CRuby indentations look weird!" - A Rubyist Vimmer

static VALUE
rb_str_casecmp(VALUE str1, VALUE str2)
{
    VALUE s = rb_check_string_type(str2);
    if (NIL_P(s)) {
>-return Qnil;
    }
    return str_casecmp(str1, s);
}

Q. Indentation? (2/8)

Indent 4 spaces for C with tabs for eight-space indentation (emacs default)

according to ruby/doc/contributing.rdoc

For CRuby buffers, do this:

Q. Indentation? (3/8)

in your ~/.vimrc

" Same as default
set cindent
set tabstop=8
set noexpandtab
" Different to the default
set shiftwidth=4 " it was 8
set softtabstop=4 " it was 0

^ works for first file

set shiftwidth=4 softtabstop=4 is equivalent

Q. Indentation? (4/8)

"works for first file" ??

Common mistake 1:

:set is a buffer-local option. Don't :set directly at the top level.

Q. Indentation? (5/8)

in your ~/.vimrc

autocmd FileType c set shiftwidth=4
autocmd FileType c set softtabstop=4

^ works, but not reloadable

Q. Indentation? (6/8)

"works, but not reloadable" ??

Common mistake 2

autocmd FileType c set shiftwidth=4
autocmd FileType c set softtabstop=4
  • :autocmd adds an event every
  • When you reload this file it registers an additional event again

Q. Indentation? (7/8)

augroup ujihisa-cruby
  autocmd!
  autocmd FileType c set shiftwidth=4
  autocmd FileType c set softtabstop=4
augroup END

Q. Indentation? (8/8)

Or

  • Just install github.com/mrkn/vim-cruby
    • new filetype: cruby (instead of c)
    • This also has ruby idiom (e.g. VALUE) for highlight and completion

Q2. jump from ruby method to c?

Q. jump from ruby method to c? (1/)

e.g.

'a'.upcase

'a'.method(:upcase).
  source_location #=> nil

Q. jump from ruby method to c? (2/)

Q. jump from ruby method to c? (3/)

CRuby defines C function with rb_define_method

// string.c
rb_define_method(
  rb_cSymbol, "upcase", sym_upcase, -1);

Q. jump from ruby method to c? (4/)

install this vim plugin

https://github.com/ujihisa/cruby-defs.vim

(to be released during RubyKaigi)

* nontrivial but possible.
* manual index
* rdoc or ri (mystery)
  • Q. ruby completion
    • (not a cruby dev question)
    • use language server and fix rcodetools

cont'd to pdf slides

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment