Guest User

Untitled

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. function! s:AlternateFile()
  2. let fn = substitute(expand('%'), "^".getcwd()."/", "", "")
  3. let head = fnamemodify(fn, ':h')
  4. let tail = fnamemodify(fn, ':t')
  5.  
  6. if match(head, '^lib') >= 0
  7. return substitute(head, '^lib', 'test', '').'/test_'.tail
  8. elseif match(head, '^test') >= 0
  9. return substitute(head, '^test', 'lib', '').'/'.substitute(tail, '^test_', '', '')
  10. endif
  11. return ''
  12. endfunction
  13.  
  14. function! s:Alternate(cmd)
  15. let file = s:AlternateFile()
  16. "if file != '' && filereadable(file)
  17. if a:cmd == 'T'
  18. let cmd = 'tabe'
  19. elseif a:cmd == 'S'
  20. let cmd = 'sp'
  21. else
  22. let cmd = 'e'
  23. endif
  24. exe ':'.cmd.' '.file
  25. "else
  26. "echomsg 'No alternate file is defined: '.file
  27. "endif
  28. endfunction
  29.  
  30. function! s:Init(path)
  31. try
  32. command A :call s:Alternate('')
  33. command AE :call s:Alternate('E')
  34. command AS :call s:Alternate('S')
  35. command AV :call s:Alternate('V')
  36. command AT :call s:Alternate('T')
  37. catch /^Vim\%((\a\+)\)\=:E174/
  38. endtry
  39. endfunction
  40.  
  41. function! s:Detect()
  42. let dir = getcwd()
  43. if filereadable(dir . '/config/environment.rb')
  44. " don't load if this is a rails project
  45. return 0
  46. endif
  47.  
  48. if filereadable(dir . '/Rakefile') && isdirectory(dir . '/lib') && filereadable(dir . '/test/helper.rb')
  49. return s:Init(dir)
  50. endif
  51.  
  52. return 0
  53. endfunction
  54.  
  55. augroup nonRailsRubyDetect
  56. autocmd VimEnter * call s:Detect()
  57. augroup END
Add Comment
Please, Sign In to add comment