Is there a way to make use of two custom, complete functions in Vimscript?

后端 未结 2 1742
离开以前
离开以前 2021-02-20 14:01

Is there a way to achieve the following in Vim?

command! -nargs=* -complete=customlist,CustomFunc1 -complete=customlist,CustomFunc2 Foo call MyFunction(

        
2条回答
  •  故里飘歌
    2021-02-20 14:57

    There's no built-in way for vim to do this. What I'd do in this situation is embed the logic into the completion function. When you set -complete=customlist,CompletionFunction, the specified function is invoked with three arguments, in this order:

    • The current argument
    • The entire command line up to this point
    • The cursor position

    So, you can analyze these and call another function depending on whether it looks like you're on the second parameter. Here's an example:

    command! -nargs=* -complete=customlist,FooComplete Foo call Foo()
    function! Foo(...)
      " ...
    endfunction
    
    function! FooComplete(current_arg, command_line, cursor_position)
      " split by whitespace to get the separate components:
      let parts = split(a:command_line, '\s\+')
    
      if len(parts) > 2
        " then we're definitely finished with the first argument:
        return SecondCompletion(a:current_arg)
      elseif len(parts) > 1 && a:current_arg =~ '^\s*$'
        " then we've entered the first argument, but the current one is still blank:
        return SecondCompletion(a:current_arg)
      else
        " we're still on the first argument:
        return FirstCompletion(a:current_arg)
      endif
    endfunction
    
    function! FirstCompletion(arg)
      " ...
    endfunction
    
    function! SecondCompletion(arg)
      " ...
    endfunction
    

    One problem with this example is that it would fail for completions that contain whitespace, so if that's a possibility, you're going to have to make more careful checks.

提交回复
热议问题