hrsh7th / nvim-cmp

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

LSP snippets are expanding with incorrect indentation level. #2091

Open Rid1FZ opened 1 week ago

Rid1FZ commented 1 week ago

FAQ

Announcement

Minimal reproducible full config

-- install lazy.nvim if not already installed
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

-- install and setup plugins with lazy.nvim
require("lazy").setup({
    {
        "neovim/nvim-lspconfig",
        lazy = false,
        config = function()
            local lspconfig = require("lspconfig")

            lspconfig.pyright.setup({})
        end,
    },
    {
        "hrsh7th/nvim-cmp",
        dependencies = {
            "onsails/lspkind.nvim",
            "hrsh7th/cmp-nvim-lsp",
        },
        config = function()
            local cmp = require("cmp")
            cmp.setup({
                snippet = {
                    expand = function(args)
                        vim.snippet.expand(args.body)
                    end,
                },

                preselect = cmp.PreselectMode.None,
                confirm_opts = {
                    behavior = cmp.ConfirmBehavior.Replace,
                    select = true,
                },

                mapping = {
                    ["<CR>"] = cmp.mapping(cmp.mapping.confirm({ select = false }), { "i", "c" }),
                    ["<Tab>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i" }),
                },

                sources = cmp.config.sources({ { name = "nvim_lsp" } }),
            })
        end,
    },
})

Description

I noticed that for pyright language server, some LSP snippets were not expanding correctly. It is particularly happening inside classes for some dunders(like __init__ or __str__). What actually happens is it indents the second line incorrectly. I did some research and found that those snippets were expanding accordingly in AstroNvim and LazyVim configs. I copy pasted some lines from those configs, but no luck. An example gif is added demonstrating the issue.

Steps to reproduce

  1. Setup pyright language server with default configs
  2. Open any python file
  3. Create a class
  4. Try to create any dunder(like __init__). cmp will give suggestions. There will be one suggestion which is a snippet provided by pyright language server
  5. Expand that snippet
  6. See the error

Expected behavior

The second line(with pass keyword) should have indented 1 level deeper

Actual behavior

The second line(with pass keyword) is indented at the same level as def keyword which is wrong.

Additional context

This gif demonstrates the actual issue. pyright