@@ -23,16 +23,28 @@ let g:did_vim_angular_ftplugin_functions = 1
2323
2424function SwitchToAlternateFile ()
2525 let l: currentpath = expand (' %' )
26- let l: newpath = " "
26+ let l: newpaths = []
2727
2828 if l: currentpath = ~ " test/unit"
29- let l: newpath = substitute (l: currentpath , " test/unit" , " app/src" , " " )
29+ let l: newpaths = [
30+ \ substitute (l: currentpath , " test/unit" , " app/js" , " " ),
31+ \ substitute (l: currentpath , " test/unit" , " app/src" , " " ),
32+ \ substitute (substitute (l: currentpath , " test/unit" , " app/js" , " " ), " Spec.js" , " .js" , " " ),
33+ \ substitute (substitute (l: currentpath , " test/unit" , " app/src" , " " ), " Spec.js" , " .js" , " " )
34+ \ ]
3035 elseif l: currentpath = ~ " app/src"
31- let l: newpath = substitute (l: currentpath , " app/src" , " test/unit" , " " )
36+ let l: newpaths = [substitute (l: currentpath , " app/src" , " test/unit" , " " ), substitute (substitute (l: currentpath , " app/src" , " test/unit" , " " ), " .js" , " Spec.js" , " " )]
37+ elseif l: currentpath = ~ " app/js"
38+ let l: newpaths = [substitute (l: currentpath , " app/js" , " test/unit" , " " ), substitute (substitute (l: currentpath , " app/js" , " test/unit" , " " ), " .js" , " Spec.js" , " " )]
3239 endif
3340
34- if l: newpath != " " && filereadable (l: newpath )
35- execute ' edit ' . l: newpath
41+ if l: newpaths != []
42+ for path in l: newpaths
43+ if filereadable (path )
44+ execute ' edit ' . path
45+ return
46+ endif
47+ endfor
3648 endif
3749endfunction
3850
@@ -42,55 +54,56 @@ let g:FindIgnore = ['coverage/', 'test/', '.git']
4254
4355" Find file in current directory and edit it.
4456function ! Find (... )
45- let path = " ."
46- let query= a: 1
47-
48- if a: 0 == 2
49- let cmd= a: 2
50- else
51- let cmd= " open"
52- endif
57+ let path = " ."
58+ let query= a: 1
59+ echo " query: " . query
60+
61+ if a: 0 == 2
62+ let cmd= a: 2
63+ else
64+ let cmd= " open"
65+ endif
5366
5467
55- if ! exists (" g:FindIgnore" )
56- let ignore = " "
57- else
58- let ignore = " | egrep -v '" .join (g: FindIgnore , " |" )." '"
59- endif
68+ if ! exists (" g:FindIgnore" )
69+ let ignore = " "
70+ else
71+ let ignore = " | egrep -v '" .join (g: FindIgnore , " |" )." '"
72+ endif
6073
61- let l: command= " find " .path ." -type f -iname '*" .query." *'" .ignore
62- let l: list= system (l: command )
63- let l: num= strlen (substitute (l: list , " [^\n ]" , " " , " g" ))
74+ let l: command= " find " .path ." -type f -iname '*" .query." *'" .ignore
75+ let l: list= system (l: command )
76+ let l: num= strlen (substitute (l: list , " [^\n ]" , " " , " g" ))
6477
65- if l: num < 1
66- echo " '" .query." ' not found"
67- return
68- endif
78+ if l: num < 1
79+ echo " '" .query." ' not found"
80+ return
81+ endif
6982
70- if l: num == 1
71- exe cmd . " " . substitute (l: list , " \n " , " " , " g" )
83+ if l: num == 1
84+ exe cmd . " " . substitute (l: list , " \n " , " " , " g" )
85+ else
86+ let tmpfile = tempname ()
87+ exe " redir! > " . tmpfile
88+ silent echon l: list
89+ redir END
90+ let old_efm = &efm
91+ set efm = % f
92+
93+ if exists (" :cgetfile" )
94+ execute " silent! cgetfile " . tmpfile
7295 else
73- let tmpfile = tempname ()
74- exe " redir! > " . tmpfile
75- silent echon l: list
76- redir END
77- let old_efm = &efm
78- set efm = % f
79-
80- if exists (" :cgetfile" )
81- execute " silent! cgetfile " . tmpfile
82- else
83- execute " silent! cfile " . tmpfile
84- endif
96+ execute " silent! cfile " . tmpfile
97+ endif
8598
86- let &efm = old_efm
99+ let &efm = old_efm
87100
88- " Open the quickfix window below the current window
89- botright copen
101+ " Open the quickfix window below the current window
102+ botright copen
90103
91- call delete (tmpfile)
92- endif
93- " echom l:command
104+ call delete (tmpfile)
105+ endif
106+ " echom l:command
94107endfunction
95108
96109command ! -nargs =* Find :call Find (<f-args> )
@@ -106,30 +119,86 @@ function! s:dashcase(word)
106119endfunction
107120
108121
109- function ! s: GF (cmd, file ) abort
110- endfunction
111-
112122function ! FindFileBasedOnAngularServiceUnderCursor (cmd)
113- let l: thingundercursor = expand (' <cfile>' )
114- if l: thingundercursor = ~ " /"
115- execute " e<cfile>"
116- return
123+ let l: fileundercursor = expand (' <cfile>' )
124+
125+ " Maybe the person actually has the cursor over a file path.
126+ " do more standard gf stuff in that case
127+ if l: fileundercursor = ~ " /"
128+
129+ if filereadable (l: fileundercursor )
130+ execute " e " . l: fileundercursor
131+ return
132+ endif
133+
134+ " app is the angular 'public root' conventionally.
135+ " this will help us find things like the template here:
136+ " $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
137+ if filereadable (" app/" . l: fileundercursor )
138+ execute " e " . " app/" . l: fileundercursor
139+ return
140+ endif
141+
117142 endif
118- let l: dashcased = s: dashcase (l: thingundercursor )
143+
144+ let l: wordundercursor = expand (' <cword>' )
145+ let l: dashcased = s: dashcase (l: wordundercursor )
119146 " echo l:dashcased
120147 let l: filethatmayexist = l: dashcased . " .js"
121148 " let l:filethatmayexist = printf("%.js", tolower(l:dashcased))
122149 " echo l:filethatmayexist
123150 execute " Find " . l: filethatmayexist . " " . a: cmd
124151endfunction
125152
126- augroup vim_angular_go_to_file
153+ " helper function. goes to end of line first ($) so it doesn't go the previous
154+ " function if your cursor is sitting right on top of the pattern
155+ function ! s: SearchUpForPattern (pattern)
156+ execute ' silent normal! ' . ' $?' . a: pattern . " \r "
157+ endfunction
158+
159+ function ! AngularRunSpec ()
160+
161+ " save cursor position so we can go back
162+ let b: angular_pos = getpos (' .' )
163+
164+ cal s: SearchUpForPattern (' it(' )
165+
166+ let l: wordundercursor = expand (' <cword>' )
167+
168+ if l: wordundercursor == " it"
169+ " if there was a spec (anywhere in the file) highlighted with "iit" before, revert it to "it"
170+ let l: positionofspectorun = getpos (' .' )
171+
172+ " this can move the cursor, hence setting the cursor back
173+ % s /iit/ it/ge
174+
175+ " move cursor back to the spec we want to run
176+ call setpos (' .' , l: positionofspectorun )
177+
178+ " change the current spec to "it"
179+ execute ' silent normal! cwiit'
180+ else
181+ call setpos (' .' , b: angular_pos )
182+ cal s: SearchUpForPattern (' iit(' )
183+ let l: wordundercursor = expand (' <cword>' )
184+ execute ' normal! cwit'
185+ endif
186+
187+ update " write the file if modified
188+
189+ " Reset cursor to previous position.
190+ call setpos (' .' , b: angular_pos )
191+ endfunction
192+
193+ command ! -nargs =* AngularRunSpec :call AngularRunSpec ()
194+
195+ augroup vim_angular_mappings
127196 autocmd !
128- autocmd FileType javascript nnoremap <silent> <buffer> gf :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("open")<CR>
129- autocmd FileType javascript nnoremap <silent> <buffer> <C-W> f :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("split")<CR>
130- autocmd FileType javascript nnoremap <silent> <buffer> <C-W><C-F> :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("split")<CR>
131- autocmd FileType javascript nnoremap <silent> <buffer> <C-W> gf :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("tabedit")<CR>
197+ autocmd FileType javascript nnoremap <silent> <buffer> gf :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("open")<CR>
198+ autocmd FileType javascript nnoremap <silent> <buffer> <C-W> f :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("split")<CR>
199+ autocmd FileType javascript nnoremap <silent> <buffer> <C-W><C-F> :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("split")<CR>
200+ autocmd FileType javascript nnoremap <silent> <buffer> <C-W> gf :<C-U> exe FindFileBasedOnAngularServiceUnderCursor("tabedit")<CR>
201+ autocmd FileType javascript nnoremap <silent> <buffer> <Leader> rs :AngularRunSpec<CR>
132202augroup END
133203
134204" vim:set sw = 2 :
135-
0 commit comments