Define left- and right-hand filetypes via global config variable

pull/17/head
mandlm 2019-10-20 15:39:32 +02:00
parent b842eb4a48
commit cd2d198447
2 changed files with 49 additions and 8 deletions

View File

@ -1,3 +1,22 @@
function s:invert_dict(source_dict)
let result = {}
for [key, value] in items(a:source_dict)
let result[value] = key
endfor
return result
endfunc
let s:extensions = g:splitopen_extensions
let s:extensions_rev = s:invert_dict(s:extensions)
function s:isLeftSideExtension(extension)
return has_key(s:extensions, a:extension)
endfunc
function s:isRightSideExtension(extension)
return has_key(s:extensions_rev, a:extension)
endfunc
function s:getFileExtension(filename) function s:getFileExtension(filename)
return fnamemodify(a:filename, ":e") return fnamemodify(a:filename, ":e")
endfunction endfunction
@ -6,23 +25,36 @@ function s:getFileRoot(filename)
return fnamemodify(a:filename, ":r") return fnamemodify(a:filename, ":r")
endfunc endfunc
function s:getSwitchExtension(extension) function s:getSwitchExtension(filename)
if (a:extension == "cpp") let extension = s:getFileExtension(a:filename)
return "h" if s:isLeftSideExtension(extension)
elseif (a:extension == "h") return s:extensions[extension]
return "cpp" elseif s:isRightSideExtension(extension)
return s:extensions_rev[extension]
endif endif
endfunc endfunc
function s:getSwitchFile(filename) function s:getSwitchFile(filename)
return s:getFileRoot(a:filename) . "." . s:getSwitchExtension(s:getFileExtension(a:filename)) return s:getFileRoot(a:filename) . "." . s:getSwitchExtension(a:filename)
endfunc endfunc
function s:addLeftSplit(filename) function s:addLeftSplit(filename)
execute("vsplit " . s:getSwitchFile(a:filename)) execute("vsplit " . s:getSwitchFile(a:filename))
execute("wincmd l")
endfunc
function s:addRightSplit(filename)
execute("vsplit " . s:getSwitchFile(a:filename))
execute("wincmd L")
execute("wincmd h")
endfunc endfunc
function splitopen#SplitOpenFile(filename) function splitopen#SplitOpenFile(filename)
execute("tabedit " . a:filename) execute("tabedit " . a:filename)
call s:addLeftSplit(a:filename) let extension = s:getFileExtension(a:filename)
if s:isLeftSideExtension(extension)
call s:addRightSplit(a:filename)
elseif s:isRightSideExtension(extension)
call s:addLeftSplit(a:filename)
endif
endfunction endfunction

View File

@ -1,5 +1,13 @@
command -nargs=1 SplitOpen :call splitopen#SplitOpenFile("<args>") command -nargs=1 SplitOpen :call splitopen#SplitOpenFile("<args>")
" map left-split / right-split file extensions
if !exists("g:splitopen_extensions")
let g:splitopen_extensions = {
\ "h": "cpp",
\ "H": "CPP",
\ }
endif
if !exists('g:splitopen_set_fzf_keys') if !exists('g:splitopen_set_fzf_keys')
let g:splitopen_set_fzf_keys = 0 let g:splitopen_set_fzf_keys = 0
endif endif
@ -9,5 +17,6 @@ if g:splitopen_set_fzf_keys
\ 'ctrl-s': 'SplitOpen', \ 'ctrl-s': 'SplitOpen',
\ 'ctrl-t': 'tab split', \ 'ctrl-t': 'tab split',
\ 'ctrl-x': 'split', \ 'ctrl-x': 'split',
\ 'ctrl-v': 'vsplit' } \ 'ctrl-v': 'vsplit',
\ }
endif endif