Skip to content

Instantly share code, notes, and snippets.

@deanohyeah
Last active June 10, 2017 05:31
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 deanohyeah/f8df50740a083525419d021f9901ed6a to your computer and use it in GitHub Desktop.
Save deanohyeah/f8df50740a083525419d021f9901ed6a to your computer and use it in GitHub Desktop.
Cucumber js step finder
let b:cucumber_root = '~/src/current/ui/test/features/step_definitions'
if !exists("b:cucumber_steps_glob")
let b:cucumber_steps_glob = b:cucumber_root.'/**/*'
endif
function! s:getallsteps()
let step_pattern = '\/\^.*$\/'
let steps = []
for file in split(glob(b:cucumber_steps_glob),"\n")
let lines = readfile(file)
let num = 0
for line in lines
let num += 1
if line =~ step_pattern
let type = matchstr(line,'Given\|When\|Then')
let steps += [[file, num, type, matchstr(line,step_pattern)]]
endif
endfor
endfor
return steps
endfunction
function! s:stepmatch(pattern,step)
if a:pattern =~ '^/.*/$'
let pattern = a:pattern[1:-2]
else
return 0
endif
try
" convert to vim pattern
let vimpattern = substitute(substitute(pattern,'\\\@<!(?:','%(','g'),'\\\@<!\*?','{-}','g')
" vim doesn't support +?
let vimpattern = substitute(vimpattern, '+?', '+', 'g')
" \v is for very magic for more normal regex suppot
if a:step =~# '\v'.vimpattern
return 1
endif
catch
echo vimpattern
echo 'Pattern not supported'
endtry
endfunction
let g:cucumberStepPatterns = s:getallsteps()
function! FindStep()
let files = []
let line = getline('.')
" strip whitespace and type
let lineTypeStripped = matchend(line, 'Given \|When \|Then ')
let line = strpart(line, lineTypeStripped)
for stepPattern in g:cucumberStepPatterns
let hasMatch = s:stepmatch(stepPattern[3],line)
if hasMatch
let files = add(files, stepPattern)
endif
endfor
if len(files) == 1
execute "tabe +" . files[0][1] . " " . files[0][0]
else
for file in files
" only open the zombie one for now
" make extensible in the future
if file[0] =~ 'zombie'
execute "tabe +" . file[1] . " " . file[0]
endif
echo file
endfor
endif
endfunction
nmap ds :call FindStep()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment