Skip to content

Commit 582883d

Browse files
committed
Enable :confirm A to create alternate
References tpope#135.
1 parent 8dbc101 commit 582883d

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

autoload/rails.vim

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,7 +3158,7 @@ function! s:find(cmd, file) abort
31583158
endif
31593159
let cmd = (empty(a:cmd) ? '' : s:findcmdfor(a:cmd)) . ' '
31603160
if djump =~# '!'
3161-
if empty(a:cmd) || file !~# '\%(^\|:\)[\/]'
3161+
if empty(a:cmd) || file !~# '\%(^.\=\|:\)[\/]'
31623162
throw "Cannot create directory here"
31633163
else
31643164
if !isdirectory(fnamemodify(file, ':h'))
@@ -3179,7 +3179,7 @@ function! s:edit(cmd, file) abort
31793179
return s:open(s:editcmdfor(a:cmd), a:file)
31803180
endfunction
31813181

3182-
function! s:Alternate(cmd,line1,line2,count,...)
3182+
function! s:Alternate(cmd,line1,line2,count,...) abort
31833183
if a:0
31843184
if a:1 =~# '^#\h' && a:cmd !~# 'D'
31853185
return s:jump(a:1[1:-1], a:cmd)
@@ -3200,11 +3200,32 @@ function! s:Alternate(cmd,line1,line2,count,...)
32003200
if empty(file)
32013201
let file = rails#buffer().alternate(a:count)
32023202
endif
3203-
if !empty(file)
3204-
return s:edit(a:cmd, file)
3205-
else
3206-
call s:warn("No alternate file is defined")
3203+
let has_path = !empty(file) && rails#app().has_path(file)
3204+
let g:confirm = histget(':', -1)
3205+
let confirm = &confirm || (histget(':', -1) =~# '\%(^\||\)\s*conf\%[irm]\>')
3206+
if confirm && !a:count && !has_path
3207+
let projected = rails#buffer().projected_with_raw('alternate')
3208+
call filter(projected, 'rails#app().has_path(matchstr(v:val[1], "^[^{}]*/"))')
3209+
if len(projected)
3210+
let choices = ['Create alternate file?']
3211+
let i = 0
3212+
for [alt, _] in projected
3213+
let i += 1
3214+
call add(choices, i.' '.alt)
3215+
endfor
3216+
let i = inputlist(choices)
3217+
if i > 0 && i <= len(projected)
3218+
let file = projected[i-1][0] . '!'
3219+
else
3220+
return ''
3221+
endif
3222+
endif
3223+
endif
3224+
if empty(file)
3225+
call s:error("No alternate file defined")
32073226
return ''
3227+
else
3228+
return s:find(a:cmd, './' . file)
32083229
endif
32093230
endif
32103231
endfunction
@@ -4829,7 +4850,7 @@ function! s:expand_placeholders(string, placeholders)
48294850
return value =~# "\001" ? '' : value
48304851
endfunction
48314852

4832-
function! s:readable_projected(key, ...) dict abort
4853+
function! s:readable_projected_with_raw(key, ...) dict abort
48334854
let f = self.name()
48344855
let all = self.app().projections()
48354856
let mine = []
@@ -4853,13 +4874,17 @@ function! s:readable_projected(key, ...) dict abort
48534874
if suffix =~# '\.js\>'
48544875
let ph.S = s:gsub(ph.S, '::', '.')
48554876
endif
4856-
let mine += map(s:getlist(all[pattern], a:key), 's:expand_placeholders(v:val, ph)')
4877+
let mine += map(s:getlist(all[pattern], a:key), '[s:expand_placeholders(v:val, ph), v:val]')
48574878
endif
48584879
endfor
4859-
return filter(mine, '!empty(v:val)')
4880+
return filter(mine, '!empty(v:val[0])')
4881+
endfunction
4882+
4883+
function! s:readable_projected(key, ...) dict abort
4884+
return map(self.projected_with_raw(a:key, a:0 ? a:1 : {}), 'v:val[0]')
48604885
endfunction
48614886

4862-
call s:add_methods('readable', ['projected'])
4887+
call s:add_methods('readable', ['projected', 'projected_with_raw'])
48634888

48644889
function! s:Set(bang,...)
48654890
call s:warn('Rset is obsolete and has no effect')

0 commit comments

Comments
 (0)