Coder Social home page Coder Social logo

Comments (2)

przepompownia avatar przepompownia commented on July 22, 2024 1

I then have updated the example to

vim.api.nvim_create_autocmd('FileType', {
  pattern = 'lua',
  callback = function (args)
    vim.treesitter.start(args.buf, 'lua')
    vim.lsp.start({
      name = 'lua-ls',
      cmd = {'lua-language-server'},
      root_dir = vim.fs.root(args.buf, 'init.lua'),
    })
  end,
})

vim.api.nvim_create_autocmd('LspAttach', {
  callback = function (args)
    vim.lsp.completion.enable(true, args.data.client_id, args.buf, {autotrigger = true})

    vim.keymap.set({'i'}, '<C-Space>', function ()
      vim.lsp.completion.trigger()
    end, {buffer = args.buf})

    vim.api.nvim_create_autocmd('CompleteChanged', {
      buffer = args.buf,
      callback = function ()
        local info = vim.fn.complete_info({'selected'})
        local completionItem = vim.tbl_get(vim.v.completed_item, 'user_data', 'nvim', 'lsp', 'completion_item')
        if nil == completionItem then
          return
        end

        local resolvedItem = vim.lsp.buf_request_sync(
          args.buf,
          vim.lsp.protocol.Methods.completionItem_resolve,
          completionItem,
          500
        )

        local docs = vim.tbl_get(resolvedItem[args.data.client_id], 'result', 'documentation', 'value')
        if nil == docs then
          return
        end

        local winData = vim.api.nvim__complete_set(info['selected'], {info = docs})
        if not winData.winid or not vim.api.nvim_win_is_valid(winData.winid) then
          return
        end

        vim.api.nvim_win_set_config(winData.winid, {border = 'rounded'})
        vim.treesitter.start(winData.bufnr, 'markdown')
        vim.wo[winData.winid].conceallevel = 3

        vim.api.nvim_create_autocmd({'TextChangedI'}, {
          buffer = args.buf,
          callback = function ()
            vim.lsp.completion.trigger()
          end
        })
      end
    })
  end
})

vim.go.completeopt = 'menu,noselect,menuone,popup'

local pumMaps = {
  ['<Tab>'] = '<C-n>',
  ['<Down>'] = '<C-n>',
  ['<S-Tab>'] = '<C-p>',
  ['<Up>'] = '<C-p>',
  ['<CR>'] = '<C-y>',
}

for insertKmap, pumKmap in pairs(pumMaps) do
  vim.keymap.set(
    {'i'},
    insertKmap,
    function ()
      return vim.fn.pumvisible() == 1 and pumKmap or insertKmap
    end,
    {expr = true}
  )
end

from neovim.

glepnir avatar glepnir commented on July 22, 2024

Tips: request is asynchronous, it is best to use a timer to ensure that selected is the same before you invoke nvim__set_complete. and when no result you also need close exist info window.

from neovim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.