nvim-autopairs icon indicating copy to clipboard operation
nvim-autopairs copied to clipboard

Treesitter conditions not working with comments

Open SGauvin opened this issue 3 months ago • 1 comments

Description

I'm trying to have a setup where single quotes don't get auto-paired when writing a comment. I took the example in the README, but even this doesn't seem to work for comments.

Mapping bug

No response

Steps to reproduce

See that the comment node is properly detected by treesitter: image

When entering "%" at the end of the comment, it doesn't get a matching one (but it should) Screenshot from 2024-03-08 15-59-02

When entering "%" inside the string, it gets a matching one like expected. Screenshot from 2024-03-08 15-59-49

For some reason the "comment" node doesn't seem to be working

Minimal config

-- config using lazy


-- [[ Install `lazy.nvim` plugin manager ]]
--    See `:help lazy.nvim.txt` or https://github.com/folke/lazy.nvim for more info
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  local lazyrepo = "https://github.com/folke/lazy.nvim.git"
  vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
end ---@diagnostic disable-next-line: undefined-field
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  {
    "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate",
    config = function()
      ---@diagnostic disable-next-line: missing-fields
      require("nvim-treesitter.configs").setup({
        ensure_installed = {"lua"},
        auto_install = true,
        highlight = { enable = true },
        indent = { enable = true },
      })
    end,
  },
  {
    "windwp/nvim-autopairs",
    event = "InsertEnter",
    config = function()
      local npairs = require("nvim-autopairs")
      local Rule = require("nvim-autopairs.rule")

      npairs.setup({
        check_ts = true,
      })

      local ts_conds = require("nvim-autopairs.ts-conds")

      require("nvim-autopairs").get_rules("'")[1]:with_pair(ts_conds.is_not_ts_node({ "comment" }))

      -- press % => %% only while inside a comment or string
      npairs.add_rules({
        Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node({ "string", "comment" })),
        Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node({ "function" })),
      })
    end,
  },
})

SGauvin avatar Mar 08 '24 21:03 SGauvin