neovim/neovim

Could `f_exists` be modified to also check for the existence of Lua functions?

pauldesmondparker opened this issue · 6 comments

Problem

The exists() function f_exists cannot currently report on whether a Lua function exists.

Under the assumption that both coc#status and v:lua.myLuaFunc exist:

Note
exists('*coc#status') == 1

Warning
exists('*v:lua.myLuaFunc') --> E15: Invalid expression: v:lua

Ref: itchyny/lightline.vim#657 (comment)

Expected behavior

Note
exists('*v:lua.myLuaFunc') == 1

This is unnecessary. All you need to do is:

if myluafunc then
  ...
end

@lewis6991 Did you read the referenced issue?

From what I can tell they want to do this from vimscript but they already have a workaround in place.

Apologies, the confusion is caused by my failure to provide enough context.

I'm configuring a vim plugin from an init.lua (Lazy) context. The plugin is able to accept vim functions as configuration arguments, but not Lua functions, due to the fact that the plugin checks for the existence of the function and gracefully ignores it if it doesn't exist. This is not graceful if the input to exists is v:lua..., as it throws.

For my own immediate utility, I have patched the plugin to not check for the existence of any supplied function. This is working for me.

While it is possible to edit the plugin to add in another level of logic to handle an existence check for Lua functions, it was argued that perhaps this is in fact better handled in the context of exists itself. Perhaps a pure vim plugin is not the place to be adding code specific to neovim?

{ "itchyny/lightline.vim",
      vim.g.lightline = {
        colorscheme = "lightline-transparent",
        active = {
          left = {
            { "mode", "paste" },
            { "readonly", "filename", "modified" },
            { "gitbranch", "gitstatus", "cocstatus" },
          },
          right = {
            { "lineinfo" },
            { "percent" },
            { "filetype" },
          },
        },
        component_function = {
          gitbranch = 'v:lua.gitBranch',  -- This is where an un-patched `lightline.vim` would cause an error.
          gitstatus = 'v:lua.gitStatus',
          cocstatus = 'coc#status',
        },
        component_expand = {
          filetype = "lightline#filetype",
        },
      }
    end,
  }

Perhaps a pure vim plugin is not the place to be adding code specific to neovim?

Makes sense.

Besides the context of the original plugin, as a Vim script plugin author, the expectation here is simple; having a function name f, exists('*' . f) yields 1 if it exists. This applies to user-defined dict functions too (of course).

function! F()
  echo 'F'
endfunction

call F()
echo exists('*F')

let G = {}
function! G.F()
  echo 'G.F'
endfunction

call G.F()
echo exists('*G.F')