Skip to content

Commit db12139

Browse files
authored
feat: support opening file in new window or new tab (#10)
* Added support for the <c-w>f and the <c-w>gf command * Fixed backwards compatiblity issue by preserving the original signature of the VimNPRFindFile function
1 parent b7accf2 commit db12139

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

plugin/vim-npr.vim

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ if !exists("g:vim_npr_default_dirs")
2323
let g:vim_npr_default_dirs = ["src", "lib", "test", "public", "node_modules"]
2424
endif
2525

26-
function! VimNPRFindFile(cmd) abort
26+
function! VimNPRFindFile(cmd)
27+
return s:FindFile(a:cmd, 'same')
28+
endfunction
29+
30+
function! VimNPRFindFile_NewWindow(cmd)
31+
return s:FindFile(a:cmd, 'window')
32+
endfunction
33+
34+
function! VimNPRFindFile_NewTab(cmd)
35+
return s:FindFile(a:cmd, 'tab')
36+
endfunction
37+
38+
function! s:FindFile(cmd, place) abort
2739
if index(g:vim_npr_file_types, expand("%:e")) == -1
2840
return s:print_error("(Error) VimNPR: incorrect file type for to perform resolution within. Please raise an issue at github.com/tomarrell/vim-npr.") " Don't run on filetypes that we don't support
2941
endif
@@ -36,7 +48,7 @@ function! VimNPRFindFile(cmd) abort
3648
let l:possiblePath = expand("%:p:h") . '/' . l:cfile . filename
3749

3850
if filereadable(l:possiblePath)
39-
return s:edit_file(l:possiblePath, a:cmd)
51+
return s:edit_file(l:possiblePath, a:cmd, a:place)
4052
endif
4153
endfor
4254

@@ -74,7 +86,7 @@ function! VimNPRFindFile(cmd) abort
7486

7587
for filename in g:vim_npr_file_names
7688
if filereadable(possiblePath . filename)
77-
return s:edit_file(possiblePath . filename, a:cmd)
89+
return s:edit_file(possiblePath . filename, a:cmd, a:place)
7890
endif
7991
endfor
8092
endfor
@@ -83,8 +95,20 @@ function! VimNPRFindFile(cmd) abort
8395
return s:print_error("(Error) VimNPR: Failed to sensibly resolve file in path. If you believe this to be an error, please log an error at github.com/tomarrell/vim-npr.")
8496
endfunction
8597

86-
function! s:edit_file(path, cmd)
87-
exe "edit" . a:cmd . " " . a:path
98+
99+
function! s:edit_file(path, cmd, place)
100+
"Open in the same window
101+
if a:place == "same"
102+
exe "edit" . a:cmd . " " . a:path
103+
endif
104+
"Open in a new (horizontal) window
105+
if a:place == "window"
106+
exe "sp" . a:cmd . " " . a:path
107+
endif
108+
"Open in a new tab
109+
if a:place == "tab"
110+
exe "tabnew" . a:cmd . " " . a:path
111+
endif
88112
endfunction
89113

90114
function! s:print_error(error)
@@ -102,3 +126,5 @@ autocmd FileType javascript silent! unmap <buffer> <C-w><C-f>
102126

103127
" Automap gf when entering JS/css file types
104128
autocmd BufEnter *.js,*.jsx,*.css,*.coffee nmap <buffer> gf :call VimNPRFindFile("")<CR>
129+
autocmd BufEnter *.js,*.jsx,*.css,*.coffee nmap <buffer> <C-w>f :call VimNPRFindFile_NewWindow("")<CR>
130+
autocmd BufEnter *.js,*.jsx,*.css,*.coffee nmap <buffer> <C-w>gf :call VimNPRFindFile_NewTab("")<CR>

0 commit comments

Comments
 (0)