Skip to content

Commit d748823

Browse files
committed
Fill in template on :A file creation
This incidentally provides the undocumented feature of :AD to re-apply a template.
1 parent 457006c commit d748823

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

autoload/rails.vim

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,8 +2004,10 @@ endfunction
20042004

20052005
function! s:jumpargs(file, jump) abort
20062006
let file = fnameescape(a:file)
2007-
if empty(a:jump) || a:jump ==# '!'
2007+
if empty(a:jump)
20082008
return file
2009+
elseif a:jump ==# '!'
2010+
return '+AD ' . file
20092011
elseif a:jump =~# '^\d\+$'
20102012
return '+' . a:jump . ' ' . file
20112013
else
@@ -3129,21 +3131,20 @@ function! s:readable_open_command(cmd, argument, name, projections) dict abort
31293131
call mkdir(fnamemodify(file, ':h'), 'p')
31303132
endif
31313133
if has_key(projection, 'template')
3132-
let template = s:split(projection.template)
3133-
let ph = {
3134-
\ 'match': root,
3135-
\ 'file': file,
3136-
\ 'project': self.app().path(),
3137-
\ 'S': rails#camelize(root),
3138-
\ 'h': toupper(root[0]) . tr(rails#underscore(root), '_', ' ')[1:-1]}
3134+
let template = s:split(projection.template)
3135+
let ph = {
3136+
\ 'match': root,
3137+
\ 'file': file,
3138+
\ 'project': self.app().path(),
3139+
\ 'S': rails#camelize(root),
3140+
\ 'h': toupper(root[0]) . tr(rails#underscore(root), '_', ' ')[1:-1]}
31393141
call map(template, 's:expand_placeholders(v:val, ph)')
3142+
call map(template, 's:gsub(v:val, "\t", " ")')
3143+
let file = fnamemodify(simplify(file), ':.')
3144+
return cmd . ' ' . s:fnameescape(file) . '|call setline(1, '.string(template).')' . '|set nomod'.s:r_warning(a:cmd)
31403145
else
3141-
let projected = self.app().file(relative).projected('template')
3142-
let template = s:split(get(projected, 0, ''))
3146+
return cmd . ' +AD ' . s:fnameescape(file) . s:r_warning(a:cmd)
31433147
endif
3144-
call map(template, 's:gsub(v:val, "\t", " ")')
3145-
let file = fnamemodify(simplify(file), ':.')
3146-
return cmd . ' ' . s:fnameescape(file) . '|call setline(1, '.string(template).')' . '|set nomod'.s:r_warning(a:cmd)
31473148
endif
31483149
endfor
31493150
return 'echoerr '.string("Couldn't find destination directory for ".a:name.' '.a:argument)
@@ -3154,19 +3155,15 @@ call s:add_methods('readable', ['open_command'])
31543155
function! s:find(cmd, file) abort
31553156
let djump = matchstr(a:file,'!.*\|#\zs.*\|:\zs\d*\ze\%(:in\)\=$')
31563157
let file = s:sub(a:file,'[#!].*|:\d*%(:in)=$','')
3157-
if file =~# '^\.\.\=\%([\/]\|$\)' && getcwd() !=# rails#app().path()
3158-
let file = rails#app().path() . s:sub(file[1:-1], '^\.\+', '')
3158+
if file =~# '^\.\.\=\%([\/]\|$\)'
3159+
let file = simplify(rails#app().path() . s:sub(file[1:-1], '^\.', '/..'))
31593160
endif
31603161
let cmd = (empty(a:cmd) ? '' : s:findcmdfor(a:cmd)) . ' '
31613162
if djump =~# '!'
3162-
if empty(a:cmd) || file !~# '\%(^.\=\|:\)[\/]'
3163-
throw "Cannot create directory here"
3164-
else
3165-
if !isdirectory(fnamemodify(file, ':h'))
3166-
call mkdir(fnamemodify(file, ':h'), 'p')
3167-
endif
3168-
return s:editcmdfor(cmd) . s:fnameescape(file)
3163+
if !isdirectory(fnamemodify(file, ':h'))
3164+
call mkdir(fnamemodify(file, ':h'), 'p')
31693165
endif
3166+
return s:editcmdfor(cmd) . s:jumpargs(fnamemodify(file, ':~:.'), djump)
31703167
else
31713168
return cmd . s:jumpargs(file, djump)
31723169
endif
@@ -3182,8 +3179,8 @@ endfunction
31823179

31833180
function! s:Alternate(cmd,line1,line2,count,...) abort
31843181
if a:0
3185-
if a:1 =~# '^#\h' && a:cmd !~# 'D'
3186-
return s:jump(a:1[1:-1], a:cmd)
3182+
if a:1 =~# '^#\h'
3183+
return s:jump(a:1[1:-1], s:sub(a:cmd, 'D', 'E'))
31873184
elseif a:count && a:cmd !~# 'D'
31883185
return call('s:Find',[1,a:line1.a:cmd]+a:000)
31893186
else
@@ -3196,6 +3193,20 @@ function! s:Alternate(cmd,line1,line2,count,...) abort
31963193
let file = a:{i}
31973194
return s:find(cmd, file)
31983195
endif
3196+
elseif a:cmd =~# 'D'
3197+
let modified = &l:modified
3198+
let template = s:split(get(rails#buffer().projected('template'), 0, []))
3199+
call map(template, 's:gsub(v:val, "\t", " ")')
3200+
if a:line2 == a:count
3201+
call append(a:line2, template)
3202+
else
3203+
silent %delete_
3204+
call setline(1, template)
3205+
if !modified && !filereadable(expand('%'))
3206+
setlocal nomodified
3207+
endif
3208+
endif
3209+
return ''
31993210
else
32003211
let file = get(b:, a:count ? 'rails_related' : 'rails_alternate')
32013212
if empty(file)
@@ -4456,10 +4467,14 @@ function! s:app_engines() dict abort
44564467
return self.cache.get('engines')[0]
44574468
endfunction
44584469

4459-
function! s:extend_projection(dest, src)
4470+
function! s:extend_projection(dest, src) abort
44604471
let dest = copy(a:dest)
44614472
for key in keys(a:src)
4462-
if !has_key(dest, key) || key ==# 'affinity'
4473+
if !has_key(dest, key) && key ==# 'template'
4474+
let dest[key] = [s:split(a:src[key])]
4475+
elseif key ==# 'template'
4476+
let dest[key] += [s:split(a:src[key])]
4477+
elseif !has_key(dest, key) || key ==# 'affinity'
44634478
let dest[key] = a:src[key]
44644479
elseif type(a:src[key]) == type({}) && type(dest[key]) == type({})
44654480
let dest[key] = extend(copy(dest[key]), a:src[key])
@@ -4827,8 +4842,8 @@ function! s:expand_placeholder(placeholder, expansions) abort
48274842
endfunction
48284843

48294844
function! s:expand_placeholders(string, placeholders)
4830-
if type(a:string) !=# type('')
4831-
return a:string
4845+
if type(a:string) ==# type({}) || type(a:string) == type([])
4846+
return map(copy(a:string), 's:expand_placeholders(v:val, a:placeholders)')
48324847
endif
48334848
let ph = extend({'%': '%'}, a:placeholders)
48344849
let value = substitute(a:string, '{[^{}]*}', '\=s:expand_placeholder(submatch(0), ph)', 'g')

0 commit comments

Comments
 (0)