-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect NERDTreeCustomOpenArgs when opening bookmark #1200
Respect NERDTreeCustomOpenArgs when opening bookmark #1200
Conversation
The function you changed in this pull request is the one used by the |
|
Oh. My mistake. This applies to the |
autoload/nerdtree/ui_glue.vim
Outdated
return | ||
endif | ||
|
||
let l:openArgs = get(g:, 'NERDTreeCustomOpenArgs', {}) | ||
let l:options = ( | ||
\ type(l:openArgs) ==# v:t_dict | ||
\ && has_key(l:openArgs, 'file') | ||
\ ) ? l:openArgs.file : {'where': 'p'} | ||
call l:bookmark.open(b:NERDTree, l:options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return | |
endif | |
let l:openArgs = get(g:, 'NERDTreeCustomOpenArgs', {}) | |
let l:options = ( | |
\ type(l:openArgs) ==# v:t_dict | |
\ && has_key(l:openArgs, 'file') | |
\ ) ? l:openArgs.file : {'where': 'p'} | |
call l:bookmark.open(b:NERDTree, l:options) | |
else | |
call l:bookmark.open(b:NERDTree, s:initCustomOpenArgs().file) | |
endif |
There is a simpler way to write this, by using the function set up to initialize the CustomOpenArgs. If you want to move the type validation to that function, I'd be fine with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How it works now:
function! s:initCustomOpenArgs(customOpenArgs) abort
let l:defaultOpenArgs = {'file': {'reuse': 'all', 'where': 'p'}, 'dir': {}}
let l:customOpenArgs = a:customOpenArgs
if !s:validateType(l:customOpenArgs, v:t_dict)
return l:defaultOpenArgs
endif
for l:typeKey in keys(l:defaultOpenArgs)
if !s:validateType(get(l:customOpenArgs, l:typeKey, {}), v:t_dict)
let l:customOpenArgs[l:typeKey] = l:defaultOpenArgs[l:typeKey]
continue
endif
for l:optionName in keys(l:defaultOpenArgs[l:typeKey])
if s:validateType(get(l:customOpenArgs[l:typeKey], l:optionName, v:null), v:t_string)
continue
endif
let l:customOpenArgs[l:typeKey][l:optionName] = l:defaultOpenArgs[l:typeKey][l:optionName]
endfor
endfor
return extend(l:customOpenArgs, l:defaultOpenArgs, 'keep')
endfunction
function! s:validateType(variable, type)
if type(a:variable) is# a:type
return v:true
endif
return v:false
endfunction
function! s:testInitCustomOpenArgs(options, expectedResult)
call assert_equal(a:expectedResult, s:initCustomOpenArgs(a:options))
endfunction
function! s:testValidateType(variable, expectedResult)
call assert_true(s:validateType(a:variable, a:expectedResult))
endfunction
let v:errors = []
let defaultOpenArgs = {'file': {'reuse': 'all', 'where': 'p'}, 'dir': {}}
call s:testValidateType({}, v:t_dict)
call s:testValidateType(defaultOpenArgs['file']['where'], v:t_string)
call s:testInitCustomOpenArgs(1, defaultOpenArgs)
call s:testInitCustomOpenArgs({'file': 1}, defaultOpenArgs)
call s:testInitCustomOpenArgs({'file': {}, 'dir': 'foo'}, defaultOpenArgs)
call s:testInitCustomOpenArgs(
\ {'file': {'reuse': [1], 'where': 'w', 'foo': []}},
\ {'file': {'reuse': 'all', 'where': 'w', 'foo': []}, 'dir': {}}
\ )
call s:testInitCustomOpenArgs(
\ {'file': {'reuse': [1], 'foo': []}},
\ {'file': {'reuse': 'all', 'where': 'p', 'foo': []}, 'dir': {}}
\ )
if !len(v:errors)
echom 'Success!'
finish
endif
echoerr v:errors
@@ -500,9 +530,10 @@ function! nerdtree#ui_glue#openBookmark(name) abort | |||
endtry | |||
if l:bookmark.path.isDirectory | |||
call l:bookmark.open(b:NERDTree) | |||
else | |||
call l:bookmark.open(b:NERDTree, {'where': 'p'}) | |||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left quick returning to avoid unnecessary nesting level.
Closes #1201