xref: /vim-8.2.3635/runtime/ftplugin/ruby.vim (revision c81e5e79)
1" Vim filetype plugin
2" Language:		Ruby
3" Maintainer:		Gavin Sinclair <gsinclair at gmail.com>
4" Info:			$Id$
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 " TODO: improve optional do loops
34  let b:match_words =
35	\ '\%(' .
36	\     '\%(\%(\.\|\:\:\)\s*\|\:\)\@<!\<\%(class\|module\|begin\|def\|case\|for\|do\)\>' .
37	\   '\|' .
38	\     '\%(\%(^\|\.\.\.\=\|[{\:\,;([<>~\*/%&^|+-]\|\%(\<[_[\:lower\:]][_[\:alnum\:]]*\)\@<![!=?]\)\s*\)\@<=\%(if\|unless\|while\|until\)\>' .
39	\ '\)' .
40	\ ':' .
41	\ '\%(' .
42	\     '\%(\%(\.\|\:\:\)\s*\|\:\)\@<!\<\%(else\|elsif\|ensure\|when\)\>' .
43	\   '\|' .
44	\     '\%(\%(^\|;\)\s*\)\@<=\<rescue\>' .
45	\ '\)' .
46	\ ':' .
47	\ '\%(\%(\.\|\:\:\)\s*\|\:\)\@<!\<end\>' .
48	\ ',{:},\[:\],(:)'
49
50  let b:match_skip =
51	\ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
52	\ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Interpolation\\|" .
53	\ "NoInterpolation\\|Escape\\|Comment\\|Documentation\\)\\>'"
54
55endif
56
57setlocal formatoptions-=t formatoptions+=croql
58
59setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
60setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
61setlocal suffixesadd=.rb
62
63if exists("&ofu") && has("ruby")
64  setlocal omnifunc=rubycomplete#Complete
65endif
66
67" To activate, :set ballooneval
68if has('balloon_eval') && exists('+balloonexpr')
69  setlocal balloonexpr=RubyBalloonexpr()
70endif
71
72
73" TODO:
74"setlocal define=^\\s*def
75
76setlocal comments=:#
77setlocal commentstring=#\ %s
78
79if !exists("s:rubypath")
80  if has("ruby") && has("win32")
81    ruby VIM::command( 'let s:rubypath = "%s"' % ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,}) )
82    let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
83  elseif executable("ruby")
84    let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
85    if &shellxquote == "'"
86      let s:rubypath = system('ruby -e "' . s:code . '"')
87    else
88      let s:rubypath = system("ruby -e '" . s:code . "'")
89    endif
90    let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
91  else
92    " If we can't call ruby to get its path, just default to using the
93    " current directory and the directory of the current file.
94    let s:rubypath = ".,,"
95  endif
96endif
97
98let &l:path = s:rubypath
99
100if has("gui_win32") && !exists("b:browsefilter")
101  let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
102                     \ "All Files (*.*)\t*.*\n"
103endif
104
105let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
106      \ "| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
107      \ "| if exists('&ofu') && has('ruby') | setl ofu< | endif"
108      \ "| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
109
110let &cpo = s:cpo_save
111unlet s:cpo_save
112
113if exists("g:did_ruby_ftplugin_functions")
114  finish
115endif
116let g:did_ruby_ftplugin_functions = 1
117
118function! RubyBalloonexpr()
119  if !exists('s:ri_found')
120    let s:ri_found = executable('ri')
121  endif
122  if s:ri_found
123    let line = getline(v:beval_lnum)
124    let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
125    let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
126    let str = b.a
127    let before = strpart(line,0,v:beval_col-strlen(b))
128    let after  = strpart(line,v:beval_col+strlen(a))
129    if str =~ '^\.'
130      let str = substitute(str,'^\.','#','g')
131      if before =~ '\]\s*$'
132        let str = 'Array'.str
133      elseif before =~ '}\s*$'
134        " False positives from blocks here
135        let str = 'Hash'.str
136      elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
137        let str = 'String'.str
138      elseif before =~ '\$\d\+\.\d\+\s*$'
139        let str = 'Float'.str
140      elseif before =~ '\$\d\+\s*$'
141        let str = 'Integer'.str
142      elseif before =~ '/\s*$'
143        let str = 'Regexp'.str
144      else
145        let str = substitute(str,'^#','.','')
146      endif
147    endif
148    let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
149    let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
150    let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
151    let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
152    let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
153    let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
154    if str !~ '^\w'
155      return ''
156    endif
157    silent! let res = substitute(system("ri -f simple -T \"".str.'"'),'\n$','','')
158    if res =~ '^Nothing known about' || res =~ '^Bad argument:'
159      return ''
160    endif
161    return res
162  else
163    return ""
164  endif
165endfunction
166
167
168"
169" Instructions for enabling "matchit" support:
170"
171" 1. Look for the latest "matchit" plugin at
172"
173"         http://www.vim.org/scripts/script.php?script_id=39
174"
175"    It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
176"
177" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
178"
179" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
180"
181" 4. Ensure this file (ftplugin/ruby.vim) is installed.
182"
183" 5. Ensure you have this line in your $HOME/.vimrc:
184"         filetype plugin on
185"
186" 6. Restart Vim and create the matchit documentation:
187"
188"         :helptags ~/.vim/doc
189"
190"    Now you can do ":help matchit", and you should be able to use "%" on Ruby
191"    keywords.  Try ":echo b:match_words" to be sure.
192"
193" Thanks to Mark J. Reed for the instructions.  See ":help vimrc" for the
194" locations of plugin directories, etc., as there are several options, and it
195" differs on Windows.  Email [email protected] if you need help.
196"
197
198" vim: nowrap sw=2 sts=2 ts=8 ff=unix:
199