Skip to content
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

Merged
merged 7 commits into from
Jan 18, 2021
Merged

Respect NERDTreeCustomOpenArgs when opening bookmark #1200

merged 7 commits into from
Jan 18, 2021

Conversation

przepompownia
Copy link
Contributor

@przepompownia przepompownia commented Jan 5, 2021

Closes #1201

@PhilRunninger
Copy link
Member

The function you changed in this pull request is the one used by the t key for bookmarks. By design, this does not use the g:NERDTreeCustomOpenArgs. The Custom Open function is linked to the Enter key, and is intended to provide a second, customizable way to open files. Your setting will work as intended if you use <CR> instead of t.

@przepompownia
Copy link
Contributor Author

  • Enter on any bookmark item works as intended
  • OpenBookmark still opens buffer in the current window.

@PhilRunninger
Copy link
Member

Oh. My mistake. This applies to the :OpenBookmark command. I'll have to think about this one. This is going to change the way the command works in some circumstances, but I'm not sure it will make much difference. This seems like a command that doesn't get used very much.

@PhilRunninger PhilRunninger reopened this Jan 10, 2021
Comment on lines 503 to 511
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

@przepompownia przepompownia Jan 11, 2021

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
Copy link
Contributor Author

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.

@PhilRunninger PhilRunninger merged commit 7e17138 into preservim:master Jan 18, 2021
@przepompownia przepompownia deleted the openbookmark-use-custom-open-args branch January 18, 2021 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Respect NERDTreeCustomOpenArgs when opening bookmark
2 participants