This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symfony.vim
426 lines (337 loc) · 12.4 KB
/
symfony.vim
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
" vim IDE for the symfony PHP framework to speed up development
" Last Change: 27.06.2009
" Maintainer: Jamie Learmonth <jamie at boxlightmedia dot com>
" Maintainer: Nicolas MARTIN <email.de.nicolas.martin at gmail dot com>
if (exists("loaded_symfony"))
finish
endif
let loaded_symfony = 1
" All our mappings
" Create a new Action which take the name of the word under the cursor
map sca :SfCreateAction<CR>
" Show the symfony.vim menu
map sm :SfMenu<CR>
" Switch between action and template
map sv :SfSwitchView<CR>
" Switch to a test file
map st :SfSwitchToTest<CR>
" Run current functional test
map srt :SfRunTest<CR>
" Clear cache
map sc :SfClearCache<CR>
" Displays a menu of available commands
function! SfMenu()
echo "1. Clear cache"
echo "2. Re-generate C-tags"
let input = input("Choose a menu option: ")
if input == 1
call SfClearCache()
elsei input == 2
call SfGenerateCTags()
endif
endfunction
function! SfCreateAction(name)
let l:func_name = "execute" . toupper(a:name[0]) . a:name[1:]
let cur_line = line(".")
exe "normal \<ESC>bdw0i\<TAB>public function " . l:func_name . "(sfWebRequest $request)\<CR>\<ESC>0i\<TAB>{\<CR>\<ESC>mai\<CR>\<ESC>0i\<TAB>}\<ESC>"
" If Php Doc is loaded let's add some comments too
if exists('*PhpDoc')
exec ":normal " . cur_line . "G"
set paste
call PhpDoc()
set nopaste
endif
" Reposition cursor
exec "normal `a"
endfunction
" Generates new c tags and sticks them in the data directory of the project
function! SfGenerateCTags()
if (!exists("g:sf_root_dir"))
call SfPluginLoad()
endif
exec ":cd " . g:sf_root_dir
let ctag_cmd = 'ctags -a -f data/vim.ctags -h ".php" --languages=php -R --exclude="\.svn" --totals=yes --tag-relative=yes --PHP-kinds=+cf --regex-PHP="/abstract class ([^ ]*)/\1/c/" --regex-PHP="/interface ([^ ]*)/\1/c/" --regex-PHP="/(public |private |static |protected )+function ([^ ()]*)/\2/f/"'
let ctag_resp = system(ctag_cmd)
endfunction
" Command line actions
function! SfCallCommandLine(namespace, task, arguments)
exec(':cd ' . g:sf_root_dir)
execute "!symfony " . a:namespace . ":" . a:task . " " . a:arguments
redraw!
endfunction
function! SfClearCache()
call SfCallCommandLine("cache", "clear", "")
echo "Symfony cache cleared"
endfunction
" Determine all the paths
function! ReconfigPaths()
let file = expand('%:p')
if (!IsATest())
let g:sf_app_name = substitute(file, '.*apps\(/\|\\\)\(.\{-\}\)\(/\|\\\).*', '\2', 'g')
let g:sf_module_name = substitute(file, '.*modules\(/\|\\\)\(.\{-\}\)\(/\|\\\).*', '\2', 'g')
else
let g:sf_app_name = substitute(file, '.*functional\/\(.*\)\/.*', '\1', 'g')
let g:sf_module_name = substitute(file, '.*functional\/' . g:sf_app_name . '\/\(.*\)ActionsTest.php', '\1', 'g')
endif
if (exists("g:sf_root_dir"))
call SetAppConfig()
call SetModuleConfig()
endif
endfunction
function! SetProjectConfig()
let g:sf_config = g:sf_root_dir . 'config/'
let g:sf_batch = g:sf_root_dir . 'batch/'
let g:sf_lib = g:sf_root_dir . 'lib/'
let g:sf_lib_model = g:sf_root_dir . 'lib/model/'
let g:sf_model = g:sf_config . 'schema.xml'
let g:sf_data = g:sf_root_dir . 'data/'
let g:sf_ctags = g:sf_data . '/vim.ctags'
endfunction
function! SetAppConfig()
let g:sf_app = g:sf_root_dir . 'apps/' . g:sf_app_name . '/'
let g:sf_app_templates = g:sf_app . 'templates/'
let g:sf_app_modules = g:sf_app . 'modules/'
let g:sf_app_config = g:sf_app . 'config/'
let g:sf_test_dir = g:sf_root_dir . 'test/'
let g:sf_app_test_dir = g:sf_test_dir . '/functional/' . g:sf_app_name . '/'
endfunction
function! SetModuleConfig()
let g:sf_module = g:sf_app_modules . g:sf_module_name .'/'
let g:sf_module_actions = g:sf_module . 'actions/actions.class.php'
let g:sf_module_components = g:sf_module . 'actions/components.class.php'
let g:sf_module_templates = g:sf_module . 'templates/'
let g:sf_module_config = g:sf_module . 'config/'
let g:sf_module_lib = g:sf_module . 'lib/'
endfunction
" find the corresponding template file of the current function surrounding the cursor
function! FindCurrentAction()
call cursor(line('.')+1, 0, 0)
let lineno = search('public function\ \(.*\)(.*)', 'nbe')
let action = matchstr(getline(lineno), '\zsexecute\(.*\)\ze(.*)')
call cursor(line('.')-1, 0, 0)
if (action != '')
"let template_file_name = tolower(action)."Success.php"
"return template_file_name
return action
else
return ''
endif
endfunction
" executeIndex => index
function! GetActionNameFromAction(action_name)
let first_letter = matchstr(a:action_name, '\U\+\zs\u\ze.*')
let remains = matchstr(a:action_name, '\U\+\u\zs.*')
return tolower(first_letter) . remains
endfunction
" indexSuccess.php => index
function! GetActionNameFromActionFileName(action_file_name)
return matchstr(a:action_file_name, '\zs\U*\zeSuccess')
endfunction
" _index.php => index
function! GetComponentNameFromComponentFileName(component_file_name)
return matchstr(a:component_file_name, '_\zs.*\ze\.')
endfunction
" index => indexSuccess.php
function! GetSuccessTemplateFromAction(action_name)
return GetActionNameFromAction(a:action_name)."Success.php"
endfunction
" index => _index.php
function! GetSuccessTemplateFromComponent(component_name)
return "_".GetActionNameFromAction(a:component_name.".php")
endfunction
" index => executeIndex
function! GetExecuteActionNameFromAction(action_name)
return 'execute' . substitute(a:action_name, '^\(.\?\)', '\u\1\E', "g")
endfunction
function! IsAModule()
if (matchstr(expand('%:p'), 'apps\(/\|\\\).\{-}\(/\|\\\)modules\(/\|\\\).\{-}\(/\|\\\)') != '')
return 1
else
return ''
endif
endfunction
function! IsAnAction()
if (matchstr(expand('%:p'), 'apps\(/\|\\\).\{-}\(/\|\\\)modules\(/\|\\\).\{-}\(/\|\\\)actions\(/\|\\\)actions.class.php') != '')
return 1
else
return ''
endif
endfunction
function! IsAComponent()
if (matchstr(expand('%:p'), 'apps\(/\|\\\).\{-}\(/\|\\\)modules\(/\|\\\).\{-}\(/\|\\\)actions\(/\|\\\)components.class.php') != '')
return 1
else
return ''
endif
endfunction
function! IsATest()
if (matchstr(expand('%:t'), 'Test') != '')
return 1
else
return ''
endif
endfunction
function! IsAComponentTemplate()
if (matchstr(expand('%:p'), 'apps\(/\|\\\).\{-}\(/\|\\\)modules\(/\|\\\).\{-}\(/\|\\\)templates\(/\|\\\)_.\{-}.php') != '')
return 1
else
return ''
endif
endfunction
function! IsAnActionTemplate()
endfunction
function! g:EchoError(msg)
echohl errormsg
echo a:msg
echohl normal
endfunction
" switch from the template file to the corresponding function code of the action
" and from the action to the corresponding template
function! Switch()
if (!exists("g:sf_root_dir"))
call SfPluginLoad(getcwd())
endif
if (IsAModule())
if (FindCurrentAction() != '')
" we are in an action file so let's go the success template file
let g:last_action_line = getpos('.')
if (IsAnAction())
if (g:last_template_line != [])
exec 'edit ' . g:sf_module_templates.GetSuccessTemplateFromAction(FindCurrentAction())
call cursor(g:last_template_line[1], g:last_template_line[2], 0)
else
exec 'edit ' . g:sf_module_templates.GetSuccessTemplateFromAction(FindCurrentAction())
endif
elseif (IsAComponent())
if (g:last_template_line != [])
exec 'edit ' . g:sf_module_templates.GetSuccessTemplateFromComponent(FindCurrentAction())
call cursor(g:last_template_line[1], g:last_template_line[2], 0)
else
exec 'edit ' . g:sf_module_templates.GetSuccessTemplateFromComponent(FindCurrentAction())
endif
endif
let g:last_template_line = []
else
" we are in a template or test so let's go the current module action/function
let g:last_template_line = getpos('.')
if (IsAComponentTemplate())
if (g:last_action_line != [])
exec 'edit ' . g:sf_module_components
call cursor(g:last_action_line[1], g:last_action_line[2], 0)
else
exec 'edit +/' . GetExecuteActionNameFromAction(GetComponentNameFromComponentFileName(expand("%:t"))) . ' ' . g:sf_module_components
endif
else
if (g:last_action_line != [])
exec 'edit ' . g:sf_module_actions
call cursor(g:last_action_line[1], g:last_action_line[2], 0)
else
exec 'edit +/' . GetExecuteActionNameFromAction(GetActionNameFromActionFileName(expand("%:t"))) . ' ' . g:sf_module_actions
endif
endif
let g:last_action_line = []
" jump to the last line of the function
"call cursor(search('}')-1, 100, 0)
endif
else
call g:EchoError("Not in a symfony module context, unable to switch view")
return 0
endif
endfunction
function! SfSwitchToTest()
if (IsAComponent() || IsAnAction())
let g:last_action_line = getpos('.')
let g:last_action = expand("%:p")
exec 'edit ' . g:sf_app_test_dir . g:sf_module_name . 'ActionsTest.php'
elsei (IsATest())
let g:last_test_line = getpos('.')
if (exists("g:last_action"))
exec 'edit ' . g:last_action
else
exec 'edit ' . g:sf_module_actions
endif
if (g:last_action_line != [])
call cursor(g:last_action_line[1], g:last_action_line[2], 0)
endif
elsei (IsATest())
let g:last_test_line = getpos('.')
endif
endfunction
function! SfRunTest()
if (!IsATest())
return ''
endif
call SfCallCommandLine('test', 'functional', g:sf_app_name.' '.g:sf_module_name.'Actions')
endfunction
" Loads a template
function! SfLoadTemplate(name)
let g:sf_vim_template_dir=$HOME.'/.vim/symfony/templates/'
let l:template=g:sf_vim_template_dir.a:name.'.tpl'
if !filereadable(l:template)
echo "Cannot find template (" . l:template . ")"
return
endif
exe '0read '.l:template
endfunction
" Substitutes placeholders in a form
function! SfSubstitutePlaceHolder(tag, replacement)
let l:lineno = search(a:tag)
if (0 != l:lineno)
call setline(l:lineno, substitute(getline(l:lineno), a:tag, a:replacement, 'g'))
return 1
else
return 0
endif
endfunction
function! SfLoadFormTemplate()
call SfLoadTemplate('form')
let l:form_name = substitute(expand('%:t'), '\(.*\)Form\(.class\)\?.php', '\1', 'g')
call SfSubstitutePlaceHolder('###FORMNAME###', l:form_name)
endfunction
function! SfLoadValidatorTemplate()
call SfLoadTemplate('validator')
let l:validator_name = substitute(expand('%:t'), '\(.*\)Validator\(.class\)\?.php', '\1', 'g')
call SfSubstitutePlaceHolder('###VALIDATORNAME###', l:validator_name)
endfunction
function! SfPluginLoad(path)
if ( finddir('apps', a:path) != '') "&& (finddir('config', a:path) != '') && (finddir('lib', a:path) != '') && (finddir('web', a:path) != '')
let g:sf_root_dir = a:path.'/'
exec(':cd '.g:sf_root_dir)
call ReconfigPaths()
endif
if exists("g:sf_root_dir")
:call SetProjectConfig()
:call SetAppConfig()
:call SetModuleConfig()
" Use our ctags file if one exists
if exists("g:sf_ctags")
if filereadable(g:sf_ctags)
" All symfony files should be in project directory, so just replace
" the stack
exec 'set tags=' . g:sf_ctags
endif
endif
endif
let g:sf_extra_dir = $HOME."/.vim/symfony"
if filereadable(g:sf_extra_dir."/utils.vim")
source ~/.vim/symfony/utils.vim
endif
endfunction
" Set all commands
command! -complete=dir SfCreateAction :call SfCreateAction(expand('<cword>'))
command! -n=? -complete=dir SfSwitchView :call Switch()
command! SfSwitchToTest :call SfSwitchToTest()
command! -nargs=1 -complete=dir SfPluginLoad :call SfPluginLoad(<args>)
command! SfMenu :call SfMenu()
command! SfClearCache :call SfClearCache()
command! SfRunTest :call SfRunTest()
let g:sf_app_name = ""
let g:sf_module_name = ""
let g:last_template_line = []
let g:last_action_line = []
autocmd BufEnter,BufLeave,BufWipeout * call SfPluginLoad(getcwd()) " Automatically reload .vimrc when changing
" Load Templates
autocmd BufNewFile *Form.class.php call SfLoadFormTemplate()
autocmd BufNewFile *Form.php call SfLoadFormTemplate()
autocmd BufNewFile *Validator.class.php call SfLoadValidatorTemplate()
autocmd BufNewFile *Validator.php call SfLoadValidatorTemplate()