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
229 lines (182 loc) · 7.27 KB
/
Copy pathangular.vim
File metadata and controls
229 lines (182 loc) · 7.27 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
" 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-']
"
" or copy the mechanism used here to ensure you get both your ignores and
" the plugin's ignores.
if !exists('g:syntastic_html_tidy_ignore_errors')
let g:syntastic_html_tidy_ignore_errors = []
endif
let g:syntastic_html_tidy_ignore_errors = g:syntastic_html_tidy_ignore_errors + [
\ ' proprietary attribute "ng-',
\ ' proprietary attribute "ui-view',
\ '<div> proprietary attribute "src'
\ ]
if !exists('g:angular_find_ignore')
let g:angular_find_ignore = []
endif
let g:angular_find_ignore = g:angular_find_ignore + [
\ 'coverage/',
\ '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
echo "'".query."' not found"
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: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:filethatmayexist = l:dashcased . ".js"
call <SID>Find(l:filethatmayexist, a:cmd)
endfunction
function! s:Alternate(cmd) abort
let l:currentpath = expand('%')
let l:newpaths = []
if l:currentpath =~ "test/unit"
let l:newpaths = [
\ substitute(l:currentpath, "test/unit", "app/js", ""),
\ substitute(l:currentpath, "test/unit", "app/src", ""),
\ substitute(substitute(l:currentpath, "test/unit", "app/js", ""), "Spec.js", ".js", ""),
\ substitute(substitute(l:currentpath, "test/unit", "app/src", ""), "Spec.js", ".js", "")
\ ]
elseif l:currentpath =~ "test/karma/unit"
let l:newpaths = [substitute(substitute(l:currentpath, "test/karma/unit", "public/js", ""), ".spec.js", ".js", "")]
elseif l:currentpath =~ "test/spec"
let l:newpaths = [substitute(l:currentpath, "test/spec", "app/scripts", "")]
elseif l:currentpath =~ "app/scripts"
let l:newpaths = [substitute(l:currentpath, "app/scripts", "test/spec", "")]
elseif l:currentpath =~ "app/src"
let l:newpaths = [substitute(l:currentpath, "app/src", "test/unit", ""), substitute(substitute(l:currentpath, "app/src", "test/unit", ""), ".js", "Spec.js", "")]
elseif l:currentpath =~ "app/js"
let l:newpaths = [substitute(l:currentpath, "app/js", "test/unit", ""), substitute(substitute(l:currentpath, "app/js", "test/unit", ""), ".js", "Spec.js", "")]
elseif l:currentpath =~ "public/js"
let l:newpaths = [substitute(l:currentpath, "public/js", "test/karma/unit", ""), substitute(substitute(l:currentpath, "public/js", "test/karma/unit", ""), ".js", ".spec.js", "")]
endif
if l:newpaths != []
for path in l:newpaths
if filereadable(path)
return a:cmd . ' ' . fnameescape(path)
endif
endfor
endif
return 'echoerr '.string("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:AngularRunSpec() abort
" save cursor position so we can go back
let b:angular_pos = getpos('.')
cal s:SearchUpForPattern('it(')
let l:wordundercursor = expand('<cword>')
if l:wordundercursor == "it"
" 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
%s/iit/it/ge
" move cursor back to the spec we want to run
call setpos('.', l:positionofspectorun)
" change the current spec to "iit"
execute 'silent normal! cwiit'
elseif l:wordundercursor == "iit"
" delete the second i in "iit"
execute 'silent normal! x'
endif
update " write the file if modified
" Reset cursor to previous position.
call setpos('.', b:angular_pos)
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>
augroup angular_gf
autocmd!
autocmd FileType javascript,html command! -buffer AngularGoToFile :call s:FindFileBasedOnAngularServiceUnderCursor('open')
autocmd FileType javascript,html nmap <buffer> gf <Plug>AngularGfJump
autocmd FileType javascript,html nmap <buffer> <C-W>f <Plug>AngularGfSplit
autocmd FileType javascript,html nmap <buffer> <C-W><C-F> <Plug>AngularGfSplit
autocmd FileType javascript,html nmap <buffer> <C-W>gf <Plug>AngularGfTabjump
augroup END
augroup angular_alternate
autocmd!
autocmd FileType javascript command! -buffer -bar -bang A :exe s:Alternate('edit<bang>')
autocmd FileType javascript command! -buffer -bar AS :exe s:Alternate('split')
autocmd FileType javascript command! -buffer -bar AV :exe s:Alternate('vsplit')
autocmd FileType javascript command! -buffer -bar AT :exe s:Alternate('tabedit')
augroup END
augroup angular_run_spec
autocmd!
autocmd FileType javascript command! -buffer AngularRunSpec :call s:AngularRunSpec()
autocmd FileType javascript nnoremap <silent><buffer> <Leader>rs :AngularRunSpec<CR>
augroup END