forked from burnettk/vim-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular.vim
More file actions
344 lines (285 loc) · 11.6 KB
/
Copy pathangular.vim
File metadata and controls
344 lines (285 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
" angular.vim
" Maintainer: Kevin Burnett
" Last Change: 2014 April 6
" https://github.com/scrooloose/syntastic/issues/612#issuecomment-19456342
"
" define your own proprietary attributes before this plugin loads, in your
" .vimrc, like so:
" let g:syntastic_html_tidy_ignore_errors = [' proprietary attribute "myhotcompany-']
" let g:syntastic_html_tidy_blocklevel_tags = ['myCustomTag']
"
" or copy the mechanism used here to ensure you get both your settings and
" the ones defined by the plugin.
if !exists('g:syntastic_html_tidy_ignore_errors')
let g:syntastic_html_tidy_ignore_errors = []
endif
let g:syntastic_html_tidy_ignore_errors += [
\ '> proprietary attribute "',
\ 'trimming empty <'
\ ]
if !exists('g:syntastic_html_tidy_blocklevel_tags')
let g:syntastic_html_tidy_blocklevel_tags = []
endif
let g:syntastic_html_tidy_blocklevel_tags += [
\ 'ng-include',
\ 'ng-form'
\ ]
if !exists('g:angular_find_ignore')
let g:angular_find_ignore = []
endif
let g:angular_find_ignore += [
\ 'coverage/',
\ 'build/',
\ 'dist/',
\ 'test/',
\ '.git/'
\ ]
" Helper
" Find file in or below current directory and edit it.
function! s:Find(...) abort
let path="."
let query=a:1
if a:0 == 2
let cmd=a:2
else
let cmd="open"
endif
if !exists("g:angular_find_ignore")
let ignore = ""
else
let ignore = " | egrep -v '".join(g:angular_find_ignore, "|")."'"
endif
let l:command="find ".path." -type f -iname '*".query."*'".ignore
let l:list=system(l:command)
let l:num=strlen(substitute(l:list, "[^\n]", "", "g"))
if l:num < 1
throw "AngularQueryNotFound"
return
endif
if l:num == 1
exe cmd . " " . substitute(l:list, "\n", "", "g")
else
let tmpfile = tempname()
exe "redir! > " . tmpfile
silent echon l:list
redir END
let old_efm = &efm
set efm=%f
if exists(":cgetfile")
execute "silent! cgetfile " . tmpfile
else
execute "silent! cfile " . tmpfile
endif
let &efm = old_efm
" Open the quickfix window below the current window
botright copen
call delete(tmpfile)
endif
endfunction
" Helper
" jacked from abolish.vim (was s:snakecase there). thank you, tim pope.
function! s:dashcase(word) abort
let word = substitute(a:word,'::','/','g')
let word = substitute(word,'\(\u\+\)\(\u\l\)','\1_\2','g')
let word = substitute(word,'\(\l\|\d\)\(\u\)','\1_\2','g')
let word = substitute(word,'_','-','g')
let word = tolower(word)
return word
endfunction
function! s:dashcasewithngtype(word) abort
let word = substitute(a:word,'::','/','g')
let word = substitute(word,'\(\u\+\)\(\u\l\)','\1_\2','g')
let word = substitute(word,'\(\l\|\d\)\(\u\)','\1_\2','g')
let word = substitute(word,'_\([a-zA-Z]\+\)$','.\1','g')
let word = substitute(word,'_','-','g')
let word = tolower(word)
return word
endfunction
function! s:FindFileBasedOnAngularServiceUnderCursor(cmd) abort
let l:fileundercursor = expand('<cfile>')
" Maybe the person actually has the cursor over a file path.
" do more standard gf stuff in that case
if filereadable(l:fileundercursor)
execute "e " . l:fileundercursor
return
endif
" app is the angular 'public root' conventionally.
" this will help us find things like the template here:
" $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
if filereadable("app/" . l:fileundercursor)
execute "e " . "app/" . l:fileundercursor
return
endif
let l:wordundercursor = expand('<cword>')
let l:dashcased = s:dashcase(l:wordundercursor)
let l:ngdotcased = s:dashcasewithngtype(l:wordundercursor)
let l:filethatmayexistverbatim = l:wordundercursor . '.js'
let l:filethatmayexistdashcase = l:dashcased . '.js'
let l:filethatmayexistngdotcase = l:ngdotcased . '.js'
let l:queries = [
\ l:filethatmayexistverbatim,
\ l:filethatmayexistdashcase,
\ l:filethatmayexistngdotcase
\ ]
for query in l:queries
try
call <SID>Find(query, a:cmd)
break
catch 'AngularQueryNotFound'
if (query == l:filethatmayexistngdotcase)
echo "angular.vim says: '".join(l:queries, ', ')."' not found"
endif
endtry
endfor
endfunction
function! s:SubStr(originalstring, pattern, replacement) abort
return substitute(a:originalstring, a:pattern, a:replacement, "")
endfunction
function! s:GenerateTestPaths(currentpath, appbasepath, testbasepath) abort
let l:samefilename = s:SubStr(a:currentpath, a:appbasepath, a:testbasepath)
let l:withcamelcasedspecsuffix = s:SubStr(s:SubStr(a:currentpath, a:appbasepath, a:testbasepath), ".js", "Spec.js")
let l:withdotspecsuffix = s:SubStr(s:SubStr(a:currentpath, a:appbasepath, a:testbasepath), ".js", ".spec.js")
let l:withcoffeescriptdotspecsuffix = s:SubStr(s:SubStr(a:currentpath, a:appbasepath, a:testbasepath), ".js.coffee", ".spec.js.coffee")
let l:withcoffeescriptcamelcasedspecsuffix = s:SubStr(s:SubStr(a:currentpath, a:appbasepath, a:testbasepath), ".js.coffee", "Spec.js.coffee")
return [l:withdotspecsuffix, l:withcamelcasedspecsuffix, l:withcoffeescriptdotspecsuffix, l:withcoffeescriptcamelcasedspecsuffix, l:samefilename]
endfunction
function! s:GenerateSrcPaths(currentpath, appbasepath, testbasepath) abort
return [s:SubStr(s:SubStr(a:currentpath, a:testbasepath, a:appbasepath), "Spec.js", ".js"),
\ s:SubStr(s:SubStr(a:currentpath, a:testbasepath, a:appbasepath), ".spec.js", ".js"),
\ s:SubStr(s:SubStr(a:currentpath, a:testbasepath, a:appbasepath), ".spec.js.coffee", ".js.coffee")]
endfunction
function! s:AngularAlternate(cmd) abort
let l:currentpath = expand('%')
let l:possiblepathsforalternatefile = []
for possiblenewpath in [s:SubStr(l:currentpath, ".js", "_test.js"), s:SubStr(l:currentpath, "_test.js", ".js")]
if possiblenewpath != l:currentpath
let l:possiblepathsforalternatefile = l:possiblepathsforalternatefile + [possiblenewpath]
endif
endfor
" handle a test subdirectory just above the leaf node
let l:possiblenewpath = s:SubStr(l:currentpath, "/test/", "/")
if possiblenewpath != l:currentpath
let l:possiblepathsforalternatefile = l:possiblepathsforalternatefile + [s:SubStr(possiblenewpath, '.spec.js', '.js')]
else
let l:lastslashindex = strridx(l:currentpath, '/')
let l:possibletestpath = strpart(l:currentpath, 0, l:lastslashindex) . '/test' . s:SubStr(strpart(l:currentpath, l:lastslashindex), '.js', '.spec.js')
let l:possiblepathsforalternatefile = l:possiblepathsforalternatefile + [l:possibletestpath]
endif
if exists('g:angular_source_directory')
if type(g:angular_source_directory) == type([])
let l:possiblesrcpaths = g:angular_source_directory
else
let l:possiblesrcpaths = [g:angular_source_directory]
endif
else
let l:possiblesrcpaths = ['app/src', 'app/js', 'app/scripts', 'public/js', 'frontend/src']
endif
if exists('g:angular_test_directory')
if type(g:angular_test_directory) == type([])
let l:possibletestpaths = g:angular_test_directory
else
let l:possibletestpaths = [g:angular_test_directory]
endif
else
let l:possibletestpaths = ['test/unit', 'test/spec', 'test/karma/unit', 'tests/frontend']
endif
for srcpath in l:possiblesrcpaths
if l:currentpath =~ srcpath
for testpath in l:possibletestpaths
let l:possiblepathsforalternatefile = l:possiblepathsforalternatefile + s:GenerateTestPaths(l:currentpath, srcpath, testpath)
endfor
endif
endfor
for testpath in l:possibletestpaths
if l:currentpath =~ testpath
for srcpath in l:possiblesrcpaths
let l:possiblepathsforalternatefile = l:possiblepathsforalternatefile + s:GenerateSrcPaths(l:currentpath, srcpath, testpath)
endfor
endif
endfor
for path in l:possiblepathsforalternatefile
if filereadable(path)
return a:cmd . ' ' . fnameescape(path)
endif
endfor
return 'echoerr '.string("angular.vim says: Couldn't find alternate file")
endfunction
" Helper
" goes to end of line first ($) so it doesn't go the previous
" function if your cursor is sitting right on top of the pattern
function! s:SearchUpForPattern(pattern) abort
execute 'silent normal! ' . '$?' . a:pattern . "\r"
endfunction
function! s:FirstLetterOf(sourcestring) abort
return strpart(a:sourcestring, 0, 1)
endfunction
function! s:AngularRunSpecOrBlock(jasminekeyword) abort
" save cursor position so we can go back
let b:angular_pos = getpos('.')
cal s:SearchUpForPattern(a:jasminekeyword . '(')
let l:wordundercursor = expand('<cword>')
let l:jasmine1 = exists('g:angular_jasmine_version') && g:angular_jasmine_version == 1
if l:jasmine1
let l:additionalletter = s:FirstLetterOf(a:jasminekeyword)
else
let l:additionalletter = 'f'
end
if l:wordundercursor == a:jasminekeyword
" if there was a spec (anywhere in the file) highlighted with "iit" before, revert it to "it"
let l:positionofspectorun = getpos('.')
" this can move the cursor, hence setting the cursor back
if l:jasmine1
%s/ddescribe(/describe(/ge
%s/iit(/it(/ge
else
%s/fdescribe(/describe(/ge
%s/fit(/it(/ge
end
" move cursor back to the spec we want to run
call setpos('.', l:positionofspectorun)
" either change the current spec to "iit" or
" the current block to "ddescribe"
execute 'silent normal! cw' . l:additionalletter . a:jasminekeyword
elseif l:wordundercursor == l:additionalletter . a:jasminekeyword
" either delete the first i in "iit" or
" the first d in "ddescribe"
execute 'silent normal! hx'
endif
update " write the file if modified
" Reset cursor to previous position.
call setpos('.', b:angular_pos)
endfunction
function! s:AngularRunSpecBlock() abort
cal s:AngularRunSpecOrBlock('describe')
endfunction
function! s:AngularRunSpec() abort
cal s:AngularRunSpecOrBlock('it')
endfunction
nnoremap <silent> <Plug>AngularGfJump :<C-U>exe <SID>FindFileBasedOnAngularServiceUnderCursor('open')<CR>
nnoremap <silent> <Plug>AngularGfSplit :<C-U>exe <SID>FindFileBasedOnAngularServiceUnderCursor('split')<CR>
nnoremap <silent> <Plug>AngularGfTabjump :<C-U>exe <SID>FindFileBasedOnAngularServiceUnderCursor('tabedit')<CR>
au BufNewFile,BufRead *.coffee set filetype=coffee
augroup angular_gf
autocmd!
autocmd FileType javascript,coffee,html command! -buffer AngularGoToFile :call s:FindFileBasedOnAngularServiceUnderCursor('open')
autocmd FileType javascript,coffee,html nmap <buffer> gf <Plug>AngularGfJump
autocmd FileType javascript,coffee,html nmap <buffer> <C-W>f <Plug>AngularGfSplit
autocmd FileType javascript,coffee,html nmap <buffer> <C-W><C-F> <Plug>AngularGfSplit
autocmd FileType javascript,coffee,html nmap <buffer> <C-W>gf <Plug>AngularGfTabjump
augroup END
if !exists('g:angular_skip_alternate_mappings')
augroup angular_alternate
autocmd!
autocmd FileType javascript,coffee command! -buffer -bar -bang AA :exe s:AngularAlternate('edit<bang>')
autocmd FileType javascript,coffee command! -buffer -bar AAS :exe s:AngularAlternate('split')
autocmd FileType javascript,coffee command! -buffer -bar AAV :exe s:AngularAlternate('vsplit')
autocmd FileType javascript,coffee command! -buffer -bar AAT :exe s:AngularAlternate('tabedit')
augroup END
endif
augroup angular_run_spec
autocmd!
autocmd FileType javascript,coffee command! -buffer AngularRunSpec :call s:AngularRunSpec()
autocmd FileType javascript,coffee command! -buffer AngularRunSpecBlock :call s:AngularRunSpecBlock()
autocmd FileType javascript,coffee nnoremap <silent><buffer> <Leader>rs :AngularRunSpec<CR>
autocmd FileType javascript,coffee nnoremap <silent><buffer> <Leader>rb :AngularRunSpecBlock<CR>
augroup END