Giter Club home page Giter Club logo

Comments (21)

altermo avatar altermo commented on June 18, 2024 4

Currently that specific feature is not possible.
I may decide to add the feature in the future.

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 18, 2024 4

If anyone wants this feature before I implement it:

local cmp=require('cmp')
local Kind=cmp.lsp.CompletionItemKind
cmp.event:on(
  'confirm_done',
  function (evt)
    if vim.tbl_contains({Kind.Function,Kind.Method},evt.entry:get_completion_item().kind) then
      vim.api.nvim_feedkeys('()'..vim.api.nvim_replace_termcodes('<Left>',true,true,true),'n',false)
    end
  end)

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 18, 2024 4

I modified @altermo's temporary solution to exclude lua_ls and rust_analyzer, since they already create pairs themselves.

The pcall is to catch any null pointer exception when doing event.entry.source.source.client.config.name.

local cmp = require('cmp')
local ind = cmp.lsp.CompletionItemKind

local function ls_name_from_event(event)
  return event.entry.source.source.client.config.name
end

-- Add parenthesis on completion confirmation
cmp.event:on('confirm_done', function(event)
  local ok, ls_name = pcall(ls_name_from_event, event)
  if ok and vim.tbl_contains({ 'rust_analyzer', 'lua_ls' }, ls_name) then
    return
  end

  local completion_kind = event.entry:get_completion_item().kind
  if vim.tbl_contains({ ind.Function, ind.Method }, completion_kind) then
    local left = vim.api.nvim_replace_termcodes('<Left>', true, true, true)
    vim.api.nvim_feedkeys('()' .. left, 'n', false)
  end
end)

from ultimate-autopair.nvim.

XXiaoA avatar XXiaoA commented on June 18, 2024 3

Looking forward to this useful feature!

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 18, 2024 3

It will take some time until I come up with a good design, so in the meantime, I created an experimental version.

To use it:

require'ultimate-autopair'.init({
  require'ultimate-autopair'.extend_default{
      --Normal config goes here
  },
  {profile=require'ultimate-autopair.experimental.cmpair'.init},
})

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 18, 2024 2

Yes, use require'ultimate-autopair.maps.cr'.cmpnewline() like so:

cmp.mapping(function()
    if cmp.get_active_entry() then
        cmp.confirm()
    else
        require'ultimate-autopair.maps.cr'.cmpnewline()
    end
end)

from ultimate-autopair.nvim.

hungnguyen1503 avatar hungnguyen1503 commented on June 18, 2024 2

Hello, I'm still looking forward to this useful feature

from ultimate-autopair.nvim.

GersiD avatar GersiD commented on June 18, 2024 1

Yes sir. This is the last reason I still use nvim-autopairs.

from ultimate-autopair.nvim.

xulongwu4 avatar xulongwu4 commented on June 18, 2024

Hi again, thanks for the help! I might have missed something, but I couldn't make it work with your code snippet. In case my request didn't go through: I was asking whether it is possible to add a pair of parenthesis to a cmp completion item which is a function. This was accomplishable by nvim-autopairs as shown in the link I provided above. I am not good at reading lua code or nvim apis, but I didn't see the part related to check whether the completion item is a function and adding the parenthesis etc. in the cmpnewline function. I would appreciate your help again!

from ultimate-autopair.nvim.

xulongwu4 avatar xulongwu4 commented on June 18, 2024

This is a gif showing the effect that I want to achieve:

nvim-autopair

from ultimate-autopair.nvim.

GersiD avatar GersiD commented on June 18, 2024

This feature is indeed very useful, looking forward to it 😄

from ultimate-autopair.nvim.

weilbith avatar weilbith commented on June 18, 2024

Sorry, currently don't find the resources, but doesn't nvim-cmp support inserting parenthesis after the completion by itself?

from ultimate-autopair.nvim.

GersiD avatar GersiD commented on June 18, 2024

If it does then I can't figure out how to. I just switched from LazyVim to Kickstart and I tried getting the function completion to work like lazy had.and lazy doesn't use the cmp:on_event trick but I have no idea how it gets function completion to work otherwise.

from ultimate-autopair.nvim.

weilbith avatar weilbith commented on June 18, 2024

Okay, it seems like this is a feature of the LSP completion where some servers provide this feature by using snippets. Sorry.

from ultimate-autopair.nvim.

GersiD avatar GersiD commented on June 18, 2024

Very wholesome, very useful feature. In the meantime try and configure the LSP you use to send snippets for completion. This varies per LSP. That's how I got around it so far. Clangd, jdtls, luals, and pyright all support it. So thats all I need for now.

from ultimate-autopair.nvim.

weilbith avatar weilbith commented on June 18, 2024

I was actually thinking about exactly such a solution. Probably just need to make it a liiiittle more advanced, so it does not interfere with server which do actually send additional snippets to add them too.

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 18, 2024

@altermo This works great, but I found that it doesn't work on JavaScript/TypeScript arrow functions:

const foo = (bar, baz) => bar + baz;

I realize that this is trickier, but is this something that would be possible to support?

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 18, 2024

How would it work exactly?
What would the trigger be for the auto parentheses?

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 18, 2024

As I was typing out a response to this I realize that it's probably too difficult :)

from ultimate-autopair.nvim.

hungnguyen1503 avatar hungnguyen1503 commented on June 18, 2024

Hi @mawkler, thanks a lot , I have used your config with pylsp very well

from ultimate-autopair.nvim.

Andrew15-5 avatar Andrew15-5 commented on June 18, 2024

Is there an official response why something like this isn't implemented in cmp-nvim directly? Or at least a snippet like this for those that don't want to rely on per-autopair plugin implementation. I don't think that any autopair plugin actually needs to insert that parenthesis pair in a special way. Unless it does?

P.S. I started searching here after I stumbled upon hrsh7th/nvim-cmp#1672. I also had to include tinymist in the list.

from ultimate-autopair.nvim.

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.