xref: /vim-8.2.3635/runtime/ftplugin/ruby.vim (revision 5c73622a)
1" Vim filetype plugin
2" Language:		Ruby
3" Maintainer:		Gavin Sinclair <gsinclair at gmail.com>
4" Info:			$Id: ruby.vim,v 1.40 2008/06/29 04:18:43 tpope Exp $
5" URL:			http://vim-ruby.rubyforge.org
6" Anon CVS:		See above site
7" Release Coordinator:  Doug Kearns <[email protected]>
8" ----------------------------------------------------------------------------
9"
10" Original matchit support thanks to Ned Konz.  See his ftplugin/ruby.vim at
11"   http://bike-nomad.com/vim/ruby.vim.
12" ----------------------------------------------------------------------------
13
14" Only do this when not done yet for this buffer
15if (exists("b:did_ftplugin"))
16  finish
17endif
18let b:did_ftplugin = 1
19
20let s:cpo_save = &cpo
21set cpo&vim
22
23if has("gui_running") && !has("gui_win32")
24  setlocal keywordprg=ri\ -T
25else
26  setlocal keywordprg=ri
27endif
28
29" Matchit support
30if exists("loaded_matchit") && !exists("b:match_words")
31  let b:match_ignorecase = 0
32
33  let b:match_words =
34	\ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
35	\ ':' .
36	\ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
37	\ ':' .
38	\ '\<end\>' .
39	\ ',{:},\[:\],(:)'
40
41  let b:match_skip =
42	\ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
43	\ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
44	\ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
45	\ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
46	\ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
47	\ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
48endif
49
50setlocal formatoptions-=t formatoptions+=croql
51
52setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
53setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
54setlocal suffixesadd=.rb
55
56if exists("&ofu") && has("ruby")
57  setlocal omnifunc=rubycomplete#Complete
58endif
59
60" To activate, :set ballooneval
61if has('balloon_eval') && exists('+balloonexpr')
62  setlocal balloonexpr=RubyBalloonexpr()
63endif
64
65
66" TODO:
67"setlocal define=^\\s*def
68
69setlocal comments=:#
70setlocal commentstring=#\ %s
71
72if !exists("s:rubypath")
73  if has("ruby") && has("win32")
74    ruby VIM::command( 'let s:rubypath = "%s"' % ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,}) )
75    let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
76  elseif executable("ruby")
77    let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
78    if &shellxquote == "'"
79      let s:rubypath = system('ruby -e "' . s:code . '"')
80    else
81      let s:rubypath = system("ruby -e '" . s:code . "'")
82    endif
83    let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
84  else
85    " If we can't call ruby to get its path, just default to using the
86    " current directory and the directory of the current file.
87    let s:rubypath = ".,,"
88  endif
89endif
90
91let &l:path = s:rubypath
92
93if has("gui_win32") && !exists("b:browsefilter")
94  let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
95                     \ "All Files (*.*)\t*.*\n"
96endif
97
98let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
99      \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
100      \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
101      \."| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
102
103if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
104
105  noremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','b')<CR>
106  noremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','')<CR>
107  noremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','b')<CR>
108  noremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','')<CR>
109
110  noremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b')<CR>
111  noremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','')<CR>
112  noremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b')<CR>
113  noremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','')<CR>
114
115  let b:undo_ftplugin = b:undo_ftplugin
116        \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
117        \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
118endif
119
120let &cpo = s:cpo_save
121unlet s:cpo_save
122
123if exists("g:did_ruby_ftplugin_functions")
124  finish
125endif
126let g:did_ruby_ftplugin_functions = 1
127
128function! RubyBalloonexpr()
129  if !exists('s:ri_found')
130    let s:ri_found = executable('ri')
131  endif
132  if s:ri_found
133    let line = getline(v:beval_lnum)
134    let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
135    let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
136    let str = b.a
137    let before = strpart(line,0,v:beval_col-strlen(b))
138    let after  = strpart(line,v:beval_col+strlen(a))
139    if str =~ '^\.'
140      let str = substitute(str,'^\.','#','g')
141      if before =~ '\]\s*$'
142        let str = 'Array'.str
143      elseif before =~ '}\s*$'
144        " False positives from blocks here
145        let str = 'Hash'.str
146      elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
147        let str = 'String'.str
148      elseif before =~ '\$\d\+\.\d\+\s*$'
149        let str = 'Float'.str
150      elseif before =~ '\$\d\+\s*$'
151        let str = 'Integer'.str
152      elseif before =~ '/\s*$'
153        let str = 'Regexp'.str
154      else
155        let str = substitute(str,'^#','.','')
156      endif
157    endif
158    let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
159    let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
160    let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
161    let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
162    let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
163    let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
164    if str !~ '^\w'
165      return ''
166    endif
167    silent! let res = substitute(system("ri -f simple -T \"".str.'"'),'\n$','','')
168    if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
169      return ''
170    endif
171    return res
172  else
173    return ""
174  endif
175endfunction
176
177function! s:searchsyn(pattern,syn,flags)
178    norm! m'
179    let i = 0
180    let cnt = v:count ? v:count : 1
181    while i < cnt
182        let i = i + 1
183        let line = line('.')
184        let col  = col('.')
185        let pos = search(a:pattern,'W'.a:flags)
186        while pos != 0 && s:synname() !~# a:syn
187            let pos = search(a:pattern,'W'.a:flags)
188        endwhile
189        if pos == 0
190            call cursor(line,col)
191            return
192        endif
193    endwhile
194endfunction
195
196function! s:synname()
197    return synIDattr(synID(line('.'),col('.'),0),'name')
198endfunction
199
200"
201" Instructions for enabling "matchit" support:
202"
203" 1. Look for the latest "matchit" plugin at
204"
205"         http://www.vim.org/scripts/script.php?script_id=39
206"
207"    It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
208"
209" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
210"
211" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
212"
213" 4. Ensure this file (ftplugin/ruby.vim) is installed.
214"
215" 5. Ensure you have this line in your $HOME/.vimrc:
216"         filetype plugin on
217"
218" 6. Restart Vim and create the matchit documentation:
219"
220"         :helptags ~/.vim/doc
221"
222"    Now you can do ":help matchit", and you should be able to use "%" on Ruby
223"    keywords.  Try ":echo b:match_words" to be sure.
224"
225" Thanks to Mark J. Reed for the instructions.  See ":help vimrc" for the
226" locations of plugin directories, etc., as there are several options, and it
227" differs on Windows.  Email [email protected] if you need help.
228"
229
230" vim: nowrap sw=2 sts=2 ts=8:
231