blob: 054c35e5fa0fc3acebba246898c9c53e4b4161ee [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00002" Language: Ruby
Bram Moolenaarec7944a2013-06-12 21:29:15 +02003" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
4" URL: https://github.com/vim-ruby/vim-ruby
Bram Moolenaard09091d2019-01-17 16:07:22 +01005" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
6" Last Change: 2019 Jan 06
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008if (exists("b:did_ftplugin"))
Bram Moolenaar60a795a2005-09-16 21:55:43 +00009 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000010endif
11let b:did_ftplugin = 1
12
Bram Moolenaar60a795a2005-09-16 21:55:43 +000013let s:cpo_save = &cpo
14set cpo&vim
15
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000016if has("gui_running") && !has("gui_win32")
Bram Moolenaarec7944a2013-06-12 21:29:15 +020017 setlocal keywordprg=ri\ -T\ -f\ bs
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000018else
19 setlocal keywordprg=ri
20endif
21
Bram Moolenaar60a795a2005-09-16 21:55:43 +000022" Matchit support
23if exists("loaded_matchit") && !exists("b:match_words")
24 let b:match_ignorecase = 0
25
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000026 let b:match_words =
Bram Moolenaar79166c42007-05-10 18:29:51 +000027 \ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000028 \ ':' .
Bram Moolenaar79166c42007-05-10 18:29:51 +000029 \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000030 \ ':' .
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020031 \ '\%(^\|[^.\:@$]\)\@<=\<end\:\@!\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000032 \ ',{:},\[:\],(:)'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000033
34 let b:match_skip =
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000035 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
Bram Moolenaar79166c42007-05-10 18:29:51 +000036 \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020037 \ "Regexp\\|RegexpDelimiter\\|" .
Bram Moolenaar79166c42007-05-10 18:29:51 +000038 \ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
39 \ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
40 \ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
41 \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000042endif
43
44setlocal formatoptions-=t formatoptions+=croql
45
Bram Moolenaarec7944a2013-06-12 21:29:15 +020046setlocal include=^\\s*\\<\\(load\\>\\\|require\\>\\\|autoload\\s*:\\=[\"']\\=\\h\\w*[\"']\\=,\\)
Bram Moolenaar60a795a2005-09-16 21:55:43 +000047setlocal suffixesadd=.rb
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000048
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000049if exists("&ofu") && has("ruby")
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000050 setlocal omnifunc=rubycomplete#Complete
51endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000052
53" TODO:
54"setlocal define=^\\s*def
55
56setlocal comments=:#
57setlocal commentstring=#\ %s
58
Bram Moolenaarec7944a2013-06-12 21:29:15 +020059if !exists('g:ruby_version_paths')
60 let g:ruby_version_paths = {}
61endif
62
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020063function! s:query_path(root) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +020064 let code = "print $:.join %q{,}"
Bram Moolenaard09091d2019-01-17 16:07:22 +010065 if &shell =~# 'sh' && empty(&shellxquote)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020066 let prefix = 'env PATH='.shellescape($PATH).' '
Bram Moolenaar60a795a2005-09-16 21:55:43 +000067 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +020068 let prefix = ''
69 endif
70 if &shellxquote == "'"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020071 let path_check = prefix.'ruby --disable-gems -e "' . code . '"'
Bram Moolenaarec7944a2013-06-12 21:29:15 +020072 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020073 let path_check = prefix."ruby --disable-gems -e '" . code . "'"
Bram Moolenaarec7944a2013-06-12 21:29:15 +020074 endif
75
76 let cd = haslocaldir() ? 'lcd' : 'cd'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020077 let cwd = fnameescape(getcwd())
Bram Moolenaarec7944a2013-06-12 21:29:15 +020078 try
79 exe cd fnameescape(a:root)
80 let path = split(system(path_check),',')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020081 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020082 return path
83 finally
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020084 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020085 endtry
86endfunction
87
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020088function! s:build_path(path) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +020089 let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
90 if &g:path !~# '\v^\.%(,/%(usr|emx)/include)=,,$'
91 let path = substitute(&g:path,',,$',',','') . ',' . path
92 endif
93 return path
94endfunction
95
96if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
97 let s:version_file = findfile('.ruby-version', '.;')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020098 if !empty(s:version_file) && filereadable(s:version_file)
Bram Moolenaarec7944a2013-06-12 21:29:15 +020099 let b:ruby_version = get(readfile(s:version_file, '', 1), '')
100 if !has_key(g:ruby_version_paths, b:ruby_version)
101 let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
102 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000103 endif
104endif
105
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200106if exists("g:ruby_path")
107 let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
108elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
109 let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
110 let s:ruby_path = s:build_path(s:ruby_paths)
111else
112 if !exists('g:ruby_default_path')
113 if has("ruby") && has("win32")
114 ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
115 elseif executable('ruby')
116 let g:ruby_default_path = s:query_path($HOME)
117 else
118 let g:ruby_default_path = map(split($RUBYLIB,':'), 'v:val ==# "." ? "" : v:val')
119 endif
120 endif
121 let s:ruby_paths = g:ruby_default_path
122 let s:ruby_path = s:build_path(s:ruby_paths)
123endif
124
125if stridx(&l:path, s:ruby_path) == -1
126 let &l:path = s:ruby_path
127endif
128if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
129 let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
130endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000131
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200132if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000133 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000134 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000135endif
136
Bram Moolenaard09091d2019-01-17 16:07:22 +0100137let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
Bram Moolenaar79166c42007-05-10 18:29:51 +0000138 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
139 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
Bram Moolenaard09091d2019-01-17 16:07:22 +0100140
141if get(g:, 'ruby_recommended_style', 1)
142 setlocal shiftwidth=2 softtabstop=2 expandtab
143 let b:undo_ftplugin .= ' | setl sw< sts< et<'
144endif
145
146" To activate, :set ballooneval
147if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
148 setlocal balloonexpr=RubyBalloonexpr()
149 let b:undo_ftplugin .= "| setl bexpr="
150endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000151
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200152function! s:map(mode, flags, map) abort
153 let from = matchstr(a:map, '\S\+')
154 if empty(mapcheck(from, a:mode))
Bram Moolenaard09091d2019-01-17 16:07:22 +0100155 exe a:mode.'map' '<buffer>' a:map
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200156 let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
157 endif
158endfunction
159
Bram Moolenaard09091d2019-01-17 16:07:22 +0100160cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200161cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100162let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200163
Bram Moolenaar79166c42007-05-10 18:29:51 +0000164if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200165 nmap <buffer><script> <SID>: :<C-U>
166 nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
167
Bram Moolenaard09091d2019-01-17 16:07:22 +0100168 nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
169 nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
170 nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
171 nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
172 xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
173 xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
174 xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
175 xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000176
Bram Moolenaard09091d2019-01-17 16:07:22 +0100177 nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
178 nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
179 nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
180 nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
181 xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
182 xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
183 xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
184 xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000185
186 let b:undo_ftplugin = b:undo_ftplugin
187 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
188 \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
Bram Moolenaar1d689522010-05-28 20:54:39 +0200189
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200190 if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200191 onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
192 onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
193 xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
194 xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
195 let b:undo_ftplugin = b:undo_ftplugin
196 \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
197 \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
198 endif
199
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200200 if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200201 onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
202 onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
203 xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
204 xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
205 let b:undo_ftplugin = b:undo_ftplugin
206 \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
207 \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
208 endif
209
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200210 call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200211
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200212 cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
Bram Moolenaard09091d2019-01-17 16:07:22 +0100213 call s:map('n', '<silent>', '<C-]> <SID>:exe v:count1."tag <Plug><ctag>"<SID>tagzv<CR>')
214 call s:map('n', '<silent>', 'g<C-]> <SID>:exe "tjump <Plug><ctag>"<SID>tagzv<CR>')
215 call s:map('n', '<silent>', 'g] <SID>:exe "tselect <Plug><ctag>"<SID>tagzv<CR>')
216 call s:map('n', '<silent>', '<C-W>] <SID>:exe v:count1."stag <Plug><ctag>"<SID>tagzv<CR>')
217 call s:map('n', '<silent>', '<C-W><C-]> <SID>:exe v:count1."stag <Plug><ctag>"<SID>tagzv<CR>')
218 call s:map('n', '<silent>', '<C-W>g<C-]> <SID>:exe "stjump <Plug><ctag>"<SID>tagzv<CR>')
219 call s:map('n', '<silent>', '<C-W>g] <SID>:exe "stselect <Plug><ctag>"<SID>tagzv<CR>')
220 call s:map('n', '<silent>', '<C-W>} <SID>:exe v:count1."ptag <Plug><ctag>"<CR>')
221 call s:map('n', '<silent>', '<C-W>g} <SID>:exe "ptjump <Plug><ctag>"<CR>')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200222
223 call s:map('n', '<silent>', 'gf <SID>c:find <Plug><cfile><CR>')
224 call s:map('n', '<silent>', '<C-W>f <SID>c:sfind <Plug><cfile><CR>')
225 call s:map('n', '<silent>', '<C-W><C-F> <SID>c:sfind <Plug><cfile><CR>')
226 call s:map('n', '<silent>', '<C-W>gf <SID>c:tabfind <Plug><cfile><CR>')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000227endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000228
229let &cpo = s:cpo_save
230unlet s:cpo_save
231
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000232if exists("g:did_ruby_ftplugin_functions")
233 finish
234endif
235let g:did_ruby_ftplugin_functions = 1
236
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200237function! RubyBalloonexpr() abort
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000238 if !exists('s:ri_found')
239 let s:ri_found = executable('ri')
240 endif
241 if s:ri_found
242 let line = getline(v:beval_lnum)
243 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
244 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
245 let str = b.a
246 let before = strpart(line,0,v:beval_col-strlen(b))
247 let after = strpart(line,v:beval_col+strlen(a))
248 if str =~ '^\.'
249 let str = substitute(str,'^\.','#','g')
250 if before =~ '\]\s*$'
251 let str = 'Array'.str
252 elseif before =~ '}\s*$'
253 " False positives from blocks here
254 let str = 'Hash'.str
255 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
256 let str = 'String'.str
257 elseif before =~ '\$\d\+\.\d\+\s*$'
258 let str = 'Float'.str
259 elseif before =~ '\$\d\+\s*$'
260 let str = 'Integer'.str
261 elseif before =~ '/\s*$'
262 let str = 'Regexp'.str
263 else
264 let str = substitute(str,'^#','.','')
265 endif
266 endif
267 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
268 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
269 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
270 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
271 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
272 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
273 if str !~ '^\w'
274 return ''
275 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200276 silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000277 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000278 return ''
279 endif
280 return res
281 else
282 return ""
283 endif
284endfunction
285
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200286function! s:searchsyn(pattern, syn, flags, mode) abort
287 let cnt = v:count1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200288 norm! m'
289 if a:mode ==# 'v'
290 norm! gv
291 endif
292 let i = 0
Bram Moolenaard09091d2019-01-17 16:07:22 +0100293 call map(a:syn, 'hlID(v:val)')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200294 while i < cnt
295 let i = i + 1
296 let line = line('.')
297 let col = col('.')
298 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaard09091d2019-01-17 16:07:22 +0100299 while pos != 0 && index(a:syn, s:synid()) < 0
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200300 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaar79166c42007-05-10 18:29:51 +0000301 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200302 if pos == 0
303 call cursor(line,col)
304 return
305 endif
306 endwhile
Bram Moolenaar79166c42007-05-10 18:29:51 +0000307endfunction
308
Bram Moolenaard09091d2019-01-17 16:07:22 +0100309function! s:synid() abort
310 return synID(line('.'),col('.'),0)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200311endfunction
312
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200313function! s:wrap_i(back,forward) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200314 execute 'norm k'.a:forward
315 let line = line('.')
316 execute 'norm '.a:back
317 if line('.') == line - 1
318 return s:wrap_a(a:back,a:forward)
319 endif
320 execute 'norm jV'.a:forward.'k'
321endfunction
322
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200323function! s:wrap_a(back,forward) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200324 execute 'norm '.a:forward
325 if line('.') < line('$') && getline(line('.')+1) ==# ''
326 let after = 1
327 endif
328 execute 'norm '.a:back
329 while getline(line('.')-1) =~# '^\s*#' && line('.')
330 -
331 endwhile
332 if exists('after')
333 execute 'norm V'.a:forward.'j'
334 elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
335 execute 'norm kV'.a:forward
336 else
337 execute 'norm V'.a:forward
338 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000339endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000340
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200341function! RubyCursorIdentifier() abort
Bram Moolenaar1d689522010-05-28 20:54:39 +0200342 let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
343 let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
344 let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200345 let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
Bram Moolenaar1d689522010-05-28 20:54:39 +0200346 let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
347 let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
348 let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
349 let [lnum, col] = searchpos(pattern,'bcn',line('.'))
350 let raw = matchstr(getline('.')[col-1 : ],pattern)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200351 let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
Bram Moolenaar1d689522010-05-28 20:54:39 +0200352 return stripped == '' ? expand("<cword>") : stripped
353endfunction
354
Bram Moolenaard09091d2019-01-17 16:07:22 +0100355function! RubyCursorTag() abort
356 return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
357endfunction
358
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200359function! RubyCursorFile() abort
360 let isfname = &isfname
361 try
362 set isfname+=:
363 let cfile = expand('<cfile>')
364 finally
365 let isfname = &isfname
366 endtry
367 let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
368 let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
369 let ext = getline('.') =~# '^\s*\%(require\%(_relative\)\=\|autoload\)\>' && cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaard09091d2019-01-17 16:07:22 +0100370 if s:synid() ==# hlID('rubyConstant')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200371 let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100372 let cfile = substitute(cfile,'^::','','')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200373 let cfile = substitute(cfile,'::','/','g')
374 let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
375 let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
376 return tolower(cfile) . '.rb'
377 elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
378 let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1') . ext
379 elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
380 let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
381 let cfile = expand('%:p:h') . target . ext
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200382 elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200383 let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1') . ext
384 elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
385 let cfile = expand('%:p:h') . strpart(cfile, 2)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200386 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200387 return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200388 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200389 let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
390 let cfile = substitute(cfile, cwdpat, '.', '')
391 if fnameescape(cfile) !=# cfile
392 return '+ '.fnameescape(cfile)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200393 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200394 return cfile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200395 endif
396endfunction
397
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000398"
399" Instructions for enabling "matchit" support:
400"
401" 1. Look for the latest "matchit" plugin at
402"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000403" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000404"
405" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
406"
407" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
408"
409" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
410"
411" 4. Ensure this file (ftplugin/ruby.vim) is installed.
412"
413" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000414" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000415"
416" 6. Restart Vim and create the matchit documentation:
417"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000418" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000419"
420" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000421" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000422"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000423" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000424" locations of plugin directories, etc., as there are several options, and it
425" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
426"
427
Bram Moolenaar446cb832008-06-24 21:56:24 +0000428" vim: nowrap sw=2 sts=2 ts=8: