2
|
1 |
" vim: set noet nosta sw=4 ts=4 fdm=marker :
|
|
2 |
"
|
|
3 |
" Specky!
|
|
4 |
" Mahlon E. Smith <mahlon@martini.nu>
|
|
5 |
" $Id$
|
|
6 |
"
|
|
7 |
|
|
8 |
" }}}
|
|
9 |
" Hook up the functions to the user supplied key bindings. {{{
|
|
10 |
"
|
|
11 |
if exists( 'g:speckySpecSwitcherKey' )
|
|
12 |
execute 'map ' . g:speckySpecSwitcherKey . ' :call <SID>SpecSwitcher()<CR>'
|
|
13 |
endif
|
|
14 |
|
|
15 |
if exists( 'g:speckyQuoteSwitcherKey' )
|
|
16 |
execute 'map ' . g:speckyQuoteSwitcherKey . ' :call <SID>QuoteSwitcher()<CR>'
|
|
17 |
endif
|
|
18 |
|
3
|
19 |
if exists( 'g:speckyBannerKey' )
|
|
20 |
execute 'map ' . g:speckyBannerKey . ' :call <SID>MakeBanner()<CR>'
|
|
21 |
endif
|
|
22 |
|
2
|
23 |
if exists( 'g:speckyRunSpecKey' )
|
|
24 |
execute 'map ' . g:speckyRunSpecKey . ' :call <SID>RunSpec()<CR>'
|
|
25 |
endif
|
|
26 |
|
|
27 |
if exists( 'g:speckyRunRdocKey' )
|
|
28 |
execute 'map ' . g:speckyRunRdocKey . ' :call <SID>RunRdoc()<CR>'
|
|
29 |
endif
|
|
30 |
|
|
31 |
|
|
32 |
if exists( 'specky_loaded' )
|
|
33 |
finish
|
|
34 |
endif
|
|
35 |
let specky_loaded = '$Rev: 92 $'
|
|
36 |
|
|
37 |
|
|
38 |
"}}}
|
|
39 |
" Menu configuration {{{
|
|
40 |
"
|
|
41 |
let s:menuloc = '&Plugin.&specky'
|
|
42 |
execute 'menu ' . s:menuloc . '.&Jump\ to\ code/spec :call <SID>SpecSwitcher()<CR>'
|
|
43 |
execute 'menu ' . s:menuloc . '.Run\ &spec :call <SID>RunSpec()<CR>'
|
|
44 |
execute 'menu ' . s:menuloc . '.&RDoc\ lookup :call <SID>RunRdoc()<CR>'
|
|
45 |
execute 'menu ' . s:menuloc . '.Rotate\ "e\ style :call <SID>QuoteSwitcher()<CR>'
|
3
|
46 |
execute 'menu ' . s:menuloc . '.Make\ a\ &banner :call <SID>MakeBanner()<CR>'
|
2
|
47 |
|
|
48 |
|
|
49 |
" }}}
|
|
50 |
" SpecSwitcher() {{{
|
|
51 |
"
|
|
52 |
" When in ruby code or an rspec BDD file, try and search recursively through
|
|
53 |
" the filesystem (within the current working directory) to find the
|
|
54 |
" respectively matching file. (code to spec, spec to code.)
|
|
55 |
"
|
|
56 |
" This operates under the assumption that you've used chdir() to put vim into
|
|
57 |
" the top level directory of your project.
|
|
58 |
"
|
|
59 |
function! <SID>SpecSwitcher()
|
|
60 |
|
|
61 |
" If we aren't in a ruby file (specs are ruby-mode too) then we probably
|
|
62 |
" don't care too much about this function.
|
|
63 |
"
|
|
64 |
if &ft != 'ruby'
|
|
65 |
call s:err( "Not currently in ruby-mode." )
|
|
66 |
return
|
|
67 |
endif
|
|
68 |
|
|
69 |
" Ensure that we can always search recursively for files to open.
|
|
70 |
"
|
|
71 |
let l:orig_path = &path
|
|
72 |
set path=**
|
|
73 |
|
|
74 |
" Get the current buffer name, and determine if it is a spec file.
|
|
75 |
"
|
|
76 |
" /tmp/something/whatever/rubycode.rb ---> rubycode.rb
|
|
77 |
" A requisite of the specfiles is that they match to the class/code file,
|
|
78 |
" this emulates the eigenclass stuff, but doesn't require the same
|
|
79 |
" directory structures.
|
|
80 |
"
|
|
81 |
" rubycode.rb ---> rubycode_spec.rb
|
|
82 |
"
|
|
83 |
let l:filename = matchstr( bufname('%'), '[0-9A-Za-z_.-]*$' )
|
|
84 |
let l:is_spec_file = match( l:filename, '_spec.rb$' ) == -1 ? 0 : 1
|
|
85 |
|
|
86 |
if l:is_spec_file
|
|
87 |
let l:other_file = substitute( l:filename, '_spec\.rb$', '\.rb', '' )
|
|
88 |
else
|
|
89 |
let l:other_file = substitute( l:filename, '\.rb$', '_spec\.rb', '' )
|
|
90 |
endif
|
|
91 |
|
|
92 |
let l:bufnum = bufnr( l:other_file )
|
|
93 |
if l:bufnum == -1
|
|
94 |
" The file isn't currently open, so let's search for it.
|
|
95 |
execute 'find ' . l:other_file
|
|
96 |
else
|
|
97 |
" We've already got an open buffer with this file, just go to it.
|
|
98 |
execute 'buffer' . l:bufnum
|
|
99 |
endif
|
|
100 |
|
|
101 |
" Restore the original path.
|
|
102 |
"
|
|
103 |
execute 'set path=' . l:orig_path
|
|
104 |
endfunction
|
|
105 |
|
|
106 |
|
|
107 |
" }}}
|
|
108 |
" QuoteSwitcher() {{{
|
|
109 |
"
|
|
110 |
" Wrap the word under the cursor in quotes. If in ruby mode,
|
|
111 |
" cycle between quoting styles and symbols.
|
|
112 |
"
|
|
113 |
" variable -> "variable" -> 'variable' -> :variable
|
|
114 |
"
|
|
115 |
function! <SID>QuoteSwitcher()
|
|
116 |
let l:type = strpart( expand("<cWORD>"), 0, 1 )
|
|
117 |
let l:word = expand("<cword>")
|
|
118 |
|
|
119 |
if l:type == '"'
|
|
120 |
" Double quote to single
|
|
121 |
"
|
3
|
122 |
execute ":normal viWc'" . l:word . "'"
|
2
|
123 |
|
|
124 |
elseif l:type == "'"
|
|
125 |
if &ft == "ruby"
|
|
126 |
" Single quote to symbol
|
|
127 |
"
|
3
|
128 |
execute ':normal viWc:' . l:word
|
2
|
129 |
else
|
|
130 |
" Single quote to double
|
|
131 |
"
|
3
|
132 |
execute ':normal viWc"' . l:word . '"'
|
2
|
133 |
end
|
|
134 |
|
|
135 |
else
|
|
136 |
" Whatever to double quote
|
|
137 |
"
|
3
|
138 |
execute ':normal viWc"' . l:word . '"'
|
2
|
139 |
endif
|
|
140 |
|
|
141 |
" Move the cursor back into the cl:word
|
|
142 |
"
|
|
143 |
call cursor( 0, getpos('.')[2] - 1 )
|
|
144 |
endfunction
|
|
145 |
|
|
146 |
|
|
147 |
" }}}
|
3
|
148 |
" MakeBanner() {{{
|
|
149 |
"
|
|
150 |
" Create a quick banner from the current line's text.
|
|
151 |
"
|
|
152 |
function! <SID>MakeBanner()
|
|
153 |
let l:banner_text = toupper(join( split( getline('.'), '\zs' ), ' ' ))
|
|
154 |
let l:banner_text = substitute( l:banner_text, '^\s\+', '', '' )
|
|
155 |
let l:sep = repeat( '#', 72 )
|
|
156 |
let l:line = line('.')
|
|
157 |
|
|
158 |
call setline( l:line, l:sep )
|
|
159 |
call append( l:line, [ '### ' . l:banner_text, l:sep ] )
|
|
160 |
call cursor( l:line + 3, 0 )
|
|
161 |
endfunction
|
|
162 |
|
|
163 |
|
|
164 |
" }}}
|
2
|
165 |
" RunSpec() {{{
|
|
166 |
"
|
|
167 |
" Run this function while in a spec file to run the specs within vim.
|
|
168 |
"
|
|
169 |
function! <SID>RunSpec()
|
|
170 |
|
|
171 |
" If we're in the code instead of the spec, try and switch
|
|
172 |
" before running tests.
|
|
173 |
"
|
|
174 |
let l:filename = matchstr( bufname('%'), '[0-9A-Za-z_.-]*$' )
|
|
175 |
let l:is_spec_file = match( l:filename, '_spec.rb$' ) == -1 ? 0 : 1
|
|
176 |
if ( ! l:is_spec_file )
|
|
177 |
silent call <SID>SpecSwitcher()
|
|
178 |
endif
|
|
179 |
|
|
180 |
let l:spec = bufname('%')
|
|
181 |
let l:buf = 'specky:specrun'
|
|
182 |
let l:bufnum = bufnr( l:buf )
|
|
183 |
|
|
184 |
" Squash the old buffer, if it exists.
|
|
185 |
"
|
|
186 |
if buflisted( l:buf )
|
|
187 |
execute 'bd! ' . l:buf
|
|
188 |
endif
|
|
189 |
|
|
190 |
execute ( exists('g:speckyVertSplit') ? 'vert new ' : 'new ') . l:buf
|
|
191 |
setlocal buftype=nofile bufhidden=delete noswapfile filetype=specrun
|
|
192 |
set foldtext='--'.getline(v:foldstart).v:folddashes
|
|
193 |
|
|
194 |
" Set up some convenient keybindings.
|
|
195 |
"
|
|
196 |
nnoremap <silent> <buffer> q :close<CR>
|
|
197 |
nnoremap <silent> <buffer> e :call <SID>FindSpecError(1)<CR>
|
|
198 |
nnoremap <silent> <buffer> r :call <SID>FindSpecError(-1)<CR>
|
|
199 |
nnoremap <silent> <buffer> E :call <SID>FindSpecError(0)<CR>
|
|
200 |
nnoremap <silent> <buffer> <C-e> :let b:err_line=1<CR>
|
|
201 |
|
|
202 |
" Default cmd for spec
|
|
203 |
"
|
|
204 |
if !exists( 'g:speckyRunSpecCmd' )
|
|
205 |
let g:speckyRunSpecCmd = 'spec -fs'
|
|
206 |
endif
|
|
207 |
|
|
208 |
" Call spec and gather up the output
|
|
209 |
"
|
|
210 |
let l:cmd = g:speckyRunSpecCmd . ' ' . l:spec
|
|
211 |
let l:output = system( l:cmd )
|
|
212 |
call append( 0, split( l:output, "\n" ) )
|
|
213 |
call append( 0, '' )
|
|
214 |
call append( 0, 'Output of: ' . l:cmd )
|
|
215 |
normal gg
|
|
216 |
|
|
217 |
" Lockdown the buffer
|
|
218 |
"
|
|
219 |
setlocal nomodifiable
|
|
220 |
endfunction
|
|
221 |
|
|
222 |
|
|
223 |
" }}}
|
|
224 |
" RunRdoc() {{{
|
|
225 |
"
|
|
226 |
" Get documentation for the word under the cursor.
|
|
227 |
"
|
|
228 |
function! <SID>RunRdoc()
|
|
229 |
|
|
230 |
" If we aren't in a ruby file (specs are ruby-mode too) then we probably
|
|
231 |
" don't care too much about this function.
|
|
232 |
"
|
|
233 |
if ( &ft != 'ruby' && &ft != 'rdoc' )
|
|
234 |
call s:err( "Not currently in ruby-mode." )
|
|
235 |
return
|
|
236 |
endif
|
|
237 |
|
|
238 |
" Set defaults
|
|
239 |
"
|
|
240 |
if !exists( 'g:speckyRunRdocCmd' )
|
|
241 |
let g:speckyRunRdocCmd = 'ri'
|
|
242 |
endif
|
|
243 |
|
|
244 |
let l:buf = 'specky:rdoc'
|
|
245 |
let l:bufname = bufname('%')
|
|
246 |
|
|
247 |
if ( match( l:bufname, l:buf ) != -1 )
|
|
248 |
" Already in the rdoc buffer. This allows us to lookup
|
|
249 |
" something like Kernel#require.
|
|
250 |
"
|
|
251 |
let l:word = expand('<cWORD>')
|
|
252 |
else
|
|
253 |
" Not in the rdoc buffer. This allows us to lookup
|
|
254 |
" something like 'each' in some_hash.each { ... }
|
|
255 |
"
|
|
256 |
let l:word = expand('<cword>')
|
|
257 |
endif
|
|
258 |
|
|
259 |
" Squash the old buffer, if it exists.
|
|
260 |
"
|
|
261 |
if buflisted( l:buf )
|
|
262 |
execute 'bd! ' . l:buf
|
|
263 |
endif
|
|
264 |
|
|
265 |
" With multiple matches, strip the comams from the cWORD.
|
|
266 |
"
|
|
267 |
let l:word = substitute( l:word, ',', '', 'eg' )
|
|
268 |
|
|
269 |
execute ( exists('g:speckyVertSplit') ? 'vert new ' : 'new ') . l:buf
|
|
270 |
setlocal buftype=nofile bufhidden=delete noswapfile filetype=rdoc
|
|
271 |
nnoremap <silent> <buffer> q :close<CR>
|
|
272 |
|
|
273 |
" Call the documentation and gather up the output
|
|
274 |
"
|
|
275 |
let l:cmd = g:speckyRunRdocCmd . ' ' . l:word
|
|
276 |
let l:output = system( l:cmd )
|
|
277 |
call append( 0, split( l:output, "\n" ) )
|
|
278 |
execute 'normal gg'
|
|
279 |
|
|
280 |
" Lockdown the buffer
|
|
281 |
"
|
|
282 |
execute 'setlocal nomodifiable'
|
|
283 |
endfunction
|
|
284 |
|
|
285 |
|
|
286 |
" }}}
|
|
287 |
" FindSpecError( detail ) {{{
|
|
288 |
"
|
|
289 |
" Convenience searches for jumping to spec failures.
|
|
290 |
"
|
|
291 |
function! <SID>FindSpecError( detail )
|
|
292 |
|
|
293 |
let l:err_str = '(FAILED\|ERROR - \d\+)$'
|
|
294 |
|
|
295 |
if ( a:detail == 0 )
|
|
296 |
" Find the detailed failure text for the current failure line,
|
|
297 |
" and unfold it.
|
|
298 |
"
|
|
299 |
let l:orig_so = &so
|
|
300 |
set so=100
|
|
301 |
call search('^' . matchstr(getline('.'),'\d\+)$') )
|
|
302 |
if has('folding')
|
|
303 |
silent! normal za
|
|
304 |
endif
|
|
305 |
execute 'set so=' . l:orig_so
|
|
306 |
|
|
307 |
else
|
|
308 |
" Find the 'regular' failure line
|
|
309 |
"
|
|
310 |
if exists( 'b:err_line' )
|
|
311 |
call cursor( b:err_line, a:detail == -1 ? 1 : strlen(getline(b:err_line)) )
|
|
312 |
endif
|
|
313 |
call search( l:err_str, a:detail == -1 ? 'b' : '' )
|
|
314 |
let b:err_line = line('.')
|
|
315 |
nohl
|
|
316 |
|
|
317 |
endif
|
|
318 |
endfunction
|
|
319 |
|
|
320 |
|
|
321 |
" }}}
|
|
322 |
" s:err( msg ) "{{{
|
|
323 |
" Notify of problems in a consistent fashion.
|
|
324 |
"
|
|
325 |
function! s:err( msg )
|
|
326 |
echohl WarningMsg|echomsg 'specky: ' . a:msg|echohl None
|
|
327 |
endfunction " }}}
|
|
328 |
|