hrsh7th / nvim-cmp

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

Completion doesn't close in .astro files #1471

Open bushblade opened 1 year ago

bushblade commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

-- setup lazy for plugins
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)

require("lazy").setup({
  {
    "neovim/nvim-lspconfig",
    config = function()
      require("lspconfig").emmet_ls.setup({
        filetypes = { "html", "css", "javascriptreact", "typescriptreact", "vue", "astro" },
      })
      require("lspconfig").astro.setup({
        init_options = {
          typescript = {
            -- needs to be installed in ~ by using `pnpm i typescript`. NOTE: do not use -g flag!
            serverPath = os.getenv("HOME") .. "/.npm-packages/lib/node_modules/typescript/lib/typescript.js",
          },
        },
      })
    end,
  },
  {
    "hrsh7th/nvim-cmp",
    dependencies = {
      "hrsh7th/cmp-nvim-lsp", -- lsp completions
      "saadparwaiz1/cmp_luasnip", -- snippets completions
      "L3MON4D3/LuaSnip",
      "rafamadriz/friendly-snippets",
    },
    config = function()
      local cmp = require("cmp")
      local luasnip = require("luasnip")
      local has_words_before = function()
        unpack = unpack or table.unpack
        local line, col = unpack(vim.api.nvim_win_get_cursor(0))
        return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
      end

      cmp.setup({
        snippet = {
          expand = function(args)
            require("luasnip").lsp_expand(args.body)
          end,
        },
        mapping = {
          ["<C-p>"] = cmp.mapping.select_prev_item(),
          ["<C-n>"] = cmp.mapping.select_next_item(),
          ["<C-d>"] = cmp.mapping.scroll_docs(-4),
          ["<C-f>"] = cmp.mapping.scroll_docs(4),
          ["<C-e>"] = cmp.mapping.close(),
          ["<CR>"] = cmp.mapping.confirm({
            select = true,
          }),
          ["<Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
              cmp.select_next_item()
            elseif luasnip.expand_or_jumpable() then
              luasnip.expand_or_jump()
            elseif has_words_before() then
              cmp.complete()
            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" }),
        },
        sources = cmp.config.sources({
          { name = "nvim_lsp" },
          { name = "luasnip" },
        }),
      })
    end,
  },
})

Description

In *.astro files the completion doesn't close after accepting a completion and shows another completion for the closing tag. Subsequently pressing <CR> accepts the next completions suggestion.

Steps to reproduce

Create any .astro file and try an write some markup.

Expected behavior

Completion closes and <CR> goes to a new line, same behaviour as in .jsx/.tsx/.vue/.html etc.

Actual behavior

For example here I start typing

Screenshot_2023-03-02_15-42-09

And hit <CR> to accept the completion...

Screenshot_2023-03-02_15-42-46

Which then shows another completion, so if I hit <CR> instead of the expected behaviour of going to a new line, it accepts the next suggestion...

Screenshot_2023-03-02_15-44-29

Which inserts another closing tag and again offers the same completion, this just keeps repeating as long as you press <CR>

Additional context

No response

chalop commented 1 year ago

Might be unrelated, but cmp does not show does not show astro's component props.

bushblade commented 1 year ago

Hi @chalop It does show completion on component props for me.

Screenshot_2023-04-09_16-53-49

Are you using JavaScript or TypeScript?

chalop commented 1 year ago

Sorry, I meant when invoking / using a component. Here's some examples:

The component has the following interface: Screenshot 2023-04-09 at 15 56 34

Nvim does not show the full list of available props: Screenshot 2023-04-09 at 15 54 41

While vscode does: Screenshot 2023-04-09 at 15 57 38

The lsp does give me the proper errors when I don't implement any required properties though, hence why I believe its an issue with nvim-cmp.