nvim-treesitter / nvim-treesitter-textobjects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keymap fail on python

fecet opened this issue · comments

I find the latest version doesn't correctly add keymap for python(good for lua, doesn't test other filetype)

A git bisect tell me ec85f5f is the first bad commit

Hi, I also use Python but I don't see this issue. Can you show me your configuration?

Do you mean all keymaps fail or just certain queries fail to map?

Sorry for late, it take me a while to prepare a minimal config:
init.lua

local g = vim.g
g.mapleader = " "
vim.opt.termguicolors = true
local fn = vim.fn

local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
	fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
	vim.cmd("packadd packer.nvim")
end

vim.cmd([[packadd packer.nvim]])
local function use_packages(use)
	use({
		"nvim-treesitter/nvim-treesitter",
		opt = true,
		run = ":TSUpdate",
		-- after="nvim-lsp-installer",
		event = { "BufRead" },
		-- event="VimEnter",
		config = function()
			require("nvim-treesitter.configs").setup({
				ensure_installed = "all", -- one of "all", "maintained" (parsers with maintainers), or a list of languages
				textobjects = {
					select = {
						enable = true,
						-- Automatically jump forward to textobj, similar to targets.vim
						lookahead = true,
						keymaps = {
							-- You can use the capture groups defined in textobjects.scm
							["af"] = "@function.outer",
							["if"] = "@function.inner",
							["ac"] = "@class.outer",
							["ic"] = "@class.inner",
						},
					},
					move = {
						enable = true,
						set_jumps = true, -- whether to set jumps in the jumplist
						goto_next_start = {
							["]["] = "@function.outer",
							["]m"] = "@class.outer",
						},
						goto_next_end = {
							["]]"] = "@function.outer",
							["]M"] = "@class.outer",
						},
						goto_previous_start = {
							["[["] = "@function.outer",
							["[m"] = "@class.outer",
						},
						goto_previous_end = {
							["[]"] = "@function.outer",
							["[M"] = "@class.outer",
						},
					},
				},
			})
		end,
		-- setup = function() require('plugins.treesitter') end,
	})

	use({ "nvim-treesitter/nvim-treesitter-textobjects", opt = true, after = "nvim-treesitter" })
end

require("packer").startup({
	use_packages,
	config = {
		compile_path = vim.fn.stdpath("config") .. "/lua/packer_compiled.lua",
	},
})
require("packer_compiled")

I have an error with packer_compiled and I don't use packer so I don't know why it doesn't work..

I made a lazy.nvim equivalent:

local g = vim.g
g.mapleader = " "
vim.opt.termguicolors = true
local fn = vim.fn

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)

local lazy = {
  {
    "nvim-treesitter/nvim-treesitter",
    lazy = true,
    build = ":TSUpdate",
    -- after="nvim-lsp-installer",
    event = { "BufRead" },
    -- event="VimEnter",
    config = function()
      require("nvim-treesitter.configs").setup {
        ensure_installed = "all", -- one of "all", "maintained" (parsers with maintainers), or a list of languages
        textobjects = {
          select = {
            enable = true,
            -- Automatically jump forward to textobj, similar to targets.vim
            lookahead = true,
            keymaps = {
              -- You can use the capture groups defined in textobjects.scm
              ["af"] = "@function.outer",
              ["if"] = "@function.inner",
              ["ac"] = "@class.outer",
              ["ic"] = "@class.inner",
            },
          },
          move = {
            enable = true,
            set_jumps = true, -- whether to set jumps in the jumplist
            goto_next_start = {
              ["]["] = "@function.outer",
              ["]m"] = "@class.outer",
            },
            goto_next_end = {
              ["]]"] = "@function.outer",
              ["]M"] = "@class.outer",
            },
            goto_previous_start = {
              ["[["] = "@function.outer",
              ["[m"] = "@class.outer",
            },
            goto_previous_end = {
              ["[]"] = "@function.outer",
              ["[M"] = "@class.outer",
            },
          },
        },
      }
    end,
    -- setup = function() require('plugins.treesitter') end,
  },
  "nvim-treesitter/nvim-treesitter-textobjects",
}

require("lazy").setup(lazy, {
  dev = {
    path = "~/project",
    -- patterns = { "kiyoon", "nvim-treesitter-textobjects" },
  },
})

Here I confirmed that the python bindings work.

Which nvim version do you use?

Are you sure you wanted ]m etc. to be class instead of function? Maybe you mapped it wrong?

Sorry for confusion, I just test textobject maps like ac and af, maps about move coming from a copy-paste I have not use them much..

Could you confirm ac and af works on your config? Maybe the problem is due to lazyload with packer, I've experienced several odd issues it caused.

Update: Here is my neovim version info

NVIM v0.8.2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Yes select also work for me, but the packer config gives me error so I cannot test well with that config. How do I generate the packer_compiled.lua?

Have you do ":PackerCompile"?

Don't know where you live now, but I wish you a happy Lunar New Year btw and apologize for bothering you on this special day

Okay, I compiled and loaded your config, and everything works well on my side. Have you updated nvim-treesitter and run :TSUpdate?

And thank you. Happy Lunar New Year to you as well!

Keymaps work for my mini config after I reinstall treesitters however still fail for my whole config. Guess it's time to migrate to lazy.nvim..

What are the keymaps for your whole config? Maybe you need to run :PackerCompile again? Actually, lazy.nvim doesn't need compilation and I like it because of that.

What are the keymaps for your whole config? Maybe you need to run :PackerCompile again? Actually, lazy.nvim doesn't need compilation and I like it because of that.

The whole config is the same with my mini one, but I suspect there is some plugin conflict exists. In fact, have no idea how and why but deleting https://github.com/quarto-dev/quarto-nvim solve this.

@fecet Thanks for identifying the issue. In the quarto-nvim plugin, there's queries/python/textobjects.scm which completely overwrites the nvim-treesitter-textobjects's queries. If you want to extend the queries, you need to have it on after/queries/python/textobjects.scm. For some reason, the plugin just uses both files, and maybe the previous version of this plugin ignored this issue.

Thank you for the kind and detail explanation! If this problem persists, I will try to fix it.

No worries. If you ask them to remove the queries folder (or if you do that by yourself) you can still use the plugin with this.