hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.86k stars 393 forks source link

Long nvim-cmp completion popup. Found no way to exclude clangd function prototypes #1913

Closed printfdebugging closed 4 months ago

printfdebugging commented 4 months ago

FAQ

Announcement

Minimal reproducible full config

local cmp = require("cmp")
local luasnip = require("luasnip")
cmp.setup({
    snippet = {
        expand = function(args)
            luasnip.lsp_expand(args.body) -- For `luasnip` users.
        end,
    },
    view = { docs = { auto_open = false } },
    window = {
        completion = {
            scrollbar = false,
        },
    },
    mapping = cmp.mapping.preset.insert({
        ["<C-b>"] = cmp.mapping.scroll_docs(-4),
        ["<C-f>"] = cmp.mapping.scroll_docs(4),
        ["<C-Space>"] = cmp.mapping.complete(),
        ["<C-e>"] = cmp.mapping.abort(),
        ["<Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.confirm({ select = true })
                -- elseif luasnip.expandable() then
                --  luasnip.expand()
                -- elseif luasnip.expand_or_jumpable() then
                --  luasnip.expand_or_jump()
            else
                fallback()
            end
        end, {
            "i",
            "s",
        }),
        ["<S-Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.select_prev_item()
                -- elseif luasnip.jumpable(-1) then
                --  luasnip.jump(-1)
            else
                fallback()
            end
        end, {
            "i",
            "s",
        }),
    }),

    formatting = {
        format = require("lspkind").cmp_format({
            maxwidth = 20,
            ellipsis_char = "...",
        }),
    },
    sources = cmp.config.sources({
        { name = "nvim_lsp" },
        { name = "luasnip" }, -- For luasnip users.
        { name = "buffer" },
        { name = "path" },
    }),
})
require("lspsaga").setup({
    ui = {
        border = "single",
    },
    definition = {
        keys = {
            quit = "q",
        },
    },
    lightbulb = {
        virtual_text = false,
    },
    symbol_in_winbar = {
        -- enable = false,
    },
})
-- Set up lspconfig.
local keymap = vim.keymap
local on_attach = function(client, bufnr)
    local opts = { noremap = true, silent = true, buffer = bufnr }
    keymap.set("n", "gf", "<cmd>Lspsaga lsp_finder<CR>", opts) -- show definition, references
    keymap.set("n", "gD", "<Cmd>Lspsaga goto_definition<CR>", opts) -- got to declaration
    keymap.set("n", "gd", "<cmd>Lspsaga peek_definition<CR>", opts) -- see definition and make edits in window
    keymap.set("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts) -- go to implementation
    keymap.set("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", opts) -- see available code actions
    keymap.set("n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opts) -- smart rename
    keymap.set("n", "<leader>D", "<cmd>Lspsaga show_line_diagnostics<CR>", opts) -- show  diagnostics for line
    keymap.set("n", "<leader>d", "<cmd>Lspsaga show_cursor_diagnostics<CR>", opts) -- show diagnostics for cursor
    keymap.set("n", "[d", "<cmd>Lspsaga diagnostic_jump_prev<CR>", opts) -- jump to previous diagnostic in buffer
    keymap.set("n", "]d", "<cmd>Lspsaga diagnostic_jump_next<CR>", opts) -- jump to next diagnostic in buffer
    keymap.set("n", "K", "<cmd>Lspsaga hover_doc<CR>", opts) -- show documentation for what is under cursor
    keymap.set("n", "<leader>o", "<cmd>LSoutlineToggle<CR>", opts) -- see outline on right hand side
    keymap.set("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
    vim.keymap.set("n", "<C-n>", ":cnext<CR>")
    vim.keymap.set("n", "<C-p>", ":cprev<CR>")
end

local capabilities = require("cmp_nvim_lsp").default_capabilities()
local servers = {
    "clangd",
    "tsserver",
    "lua_ls",
    "html",
    "cssls",
    "pyright",
    "bashls",
    "gopls",
    "emmet_ls",
    "marksman",
}

for _, server in ipairs(servers) do
    require("lspconfig")[server].setup({
        capabilities = capabilities,
        on_attach = on_attach,
    })
end

require("lspconfig")["rust_analyzer"].setup({
    capabilities = capabilities,
    on_attach = on_attach,
    cmd = { "rustup", "run", "stable", "rust-analyzer" },
})

require("lspconfig")["lua_ls"].setup({
    capabilities = capabilities,
    on_attach = on_attach,
    settings = { -- custom settings for lua
        Lua = {
            -- make the language server recognize "vim" global
            diagnostics = {
                globals = { "vim" },
            },
            workspace = {
                -- make language server aware of runtime files
                library = {
                    [vim.fn.expand("$VIMRUNTIME/lua")] = true,
                    [vim.fn.stdpath("config") .. "/lua"] = true,
                },
            },
        },
    },
})

Description

35 I want a way to exclude the clangd function signatures, but couldn't find any on the clangd documentation.

Steps to reproduce

Use clangd, and start writing a C++program, it will get long somewhere in between.

Expected behavior

There should be a way to exclude the function prototypes, such that it doesn't get that long. if not then atleast some way to set the maximum length.

Actual behavior

Long clangd function prototypes show up at the end of each entry increasing the length of the popup window.

Additional context

I found

formatting.format 
  `fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem`
  The function used to customize the appearance of the completion menu. See
  |complete-items|. This value can also be used to modify the `dup` property.
  NOTE: The `vim.CompletedItem` can contain the special properties
  `abbr_hl_group`, `kind_hl_group` and `menu_hl_group`.

in the FAQ, but don't know if related or not. If so, then some pointers to how I can configure it to the desired look would be nice.

mifka01 commented 4 months ago

Hi, I encountered the same issue with clangd that you did. Here’s a simple temporary solution that worked for me.

formatting = {
    ...
    format = lspkind.cmp_format({
        mode = "symbol_text",
        maxwidth = 50,
        ellipsis_char = "...",

        before = function(_, vim_item)
            vim_item.menu = ({ nvim_lsp = "" })["clangd"]
            return vim_item
        end,
    }),
},
printfdebugging commented 4 months ago

@mifka01 Thanks, it works like a charm :)

printfdebugging commented 4 months ago

Hi, I am closing it, since the issue has been resolved and it's look like an issue now since nvim-cmp showed what clangd pushed to it, and there exists a way to control what is shown and what is not.

printfdebugging commented 4 months ago

I meant it doesn't look like an issue :)