For any questions related to configuring Neovim with Lua or migrating over to Lua from Vimscript.

  • @cheer
    link
    3
    edit-2
    3 years ago

    I don’t use CoC and I don’t know every Lua API function so this might be clunky, but the following works on my machine:

    function _G.show_documentation()
      local ft = vim.bo.filetype
      local cword = vim.fn.expand('<cword>')
      if ft == 'help' or ft == 'vim' then
        vim.fn.execute('help ' .. cword)
      --[[ elseif vim.fn['coc#rpc#ready']() then
        vim.fn.call('CocActionAsync', {'doHover'}) ]]
      else
        vim.fn.execute(vim.o.keywordprg .. ' ' .. cword)
      end
    end
    
    vim.api.nvim_set_keymap('n', 'K', "<Cmd>call v:lua.show_documentation()<CR>", {noremap = true, silent = true})
    

    Note that I commented out the elseif statement since I don’t have CoC and the function fails if it can’t call coc#rpc#ready.

    • NinmiOPM
      link
      fedilink
      2
      edit-2
      3 years ago

      Thank you! I’m gonna study this well before I copy paste time around.

      E: it works! Somehow the Lua version is easier to read and understand as well.

      • @cheer
        link
        23 years ago

        Glad to hear it! Had to go digging though the help pages to find out what expand() did in the original and how to get the word under the cursor in Lua