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

  • NinmiOPM
    link
    fedilink
    1
    edit-2
    3 years ago

    Instead of making a helpthread for my own problem here, I thought I’d title this as a general thread were people can ask questions and hopefully receive answers as well.

    So I need to turn this thing in to Lua, but I’m new to Lua and have only copy pasted bramscript before.

    function! s:show_documentation()
      if (index(['vim','help'], &filetype) >= 0)
        execute 'h '.expand('<cword>')
      elseif (coc#rpc#ready())
        call CocActionAsync('doHover')
      else
        execute '!' . &keywordprg . " " . expand('<cword>')
      endif
    endfunction
    

    AFAIK, I should be able to call coc#rpc#ready() with vim.fn['coc#rpc#ready'](), but what is index() here? I assume the first argument would be { 'vim', 'help' }, but what would &filetype be in Lua? I assume 'h '.expand() is not a thing in Lua (and the rest of the similar stuff in the else block)?

    E: also, I have no idea what the s: is, but apparently it’s not functioning well anyway.

    The function is taken from CoC’s example config: https://github.com/neoclide/coc.nvim

    • @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