Mappings from nvim-treesitter-textobjects fail set-up
Opened this issue · 15 comments
Describe the bug
I'm using a recommended config for nvim-treesitter-textobjects
, which defines some mappings which start with i
and a
. However, that makes serendipity's 'insert' and 'append' mappings fail.
To Reproduce
Minimal config
Config for visual.nvim
:
using {
'00sapo/visual.nvim',
opts = { treesitter_textobjects = true },
event = "VeryLazy",
config = function()
require('visual').setup()
end,
}
Config for nvim-treesitter-textobjects
:
using {
'nvim-treesitter/nvim-treesitter-textobjects',
config = function()
require'nvim-treesitter.configs'.setup {
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',
-- You can optionally set descriptions to the mappings (used in the desc parameter of
-- nvim_buf_set_keymap) which plugins like which-key display
['ic'] = { query = '@class.inner', desc = 'Select inner part of a class region' },
},
-- You can choose the select mode (default is charwise 'v')
--
-- Can also be a function which gets passed a table with the keys
-- * query_string: eg '@function.inner'
-- * method: eg 'v' or 'o'
-- and should return the mode ('v', 'V', or '<c-v>') or a table
-- mapping query_strings to modes.
selection_modes = {
['@parameter.outer'] = 'v', -- charwise
['@function.outer'] = 'V', -- linewise
['@class.outer'] = '<c-v>', -- blockwise
},
-- If you set this to `true` (default is `false`) then any textobject is
-- extended to include preceding or succeeding whitespace. Succeeding
-- whitespace has priority in order to act similarly to eg the built-in
-- `ap`.
--
-- Can also be a function which gets passed a table with the keys
-- * query_string: eg '@function.inner'
-- * selection_mode: eg 'v'
-- and should return true of false
include_surrounding_whitespace = true,
},
},
}
end
}
Steps to reproduce the behavior:
- Enter serendipity mode
- Press
a
. It will fail to enter append mode. - Press
i
. It will fail to enter insert mode. - Press
if
,af
,ac
, etc. It will select the function/class. - Press
hi
. It will activate insert mode one character back.
Expected behavior
a
will enter append mode.i
. It will enter insert mode.If
,Af
,Ac
, etc. will select the function/class.
Desktop:
- OS: Windows 10 Home 19045.3324
- Neovim v0.10.0-dev-979+g794981d9b
- visual.nvim: 189baea
I will try to make this clearer. From serendipity mode, the inner and outer operators are I and A. So you should use If and Af. In visual mode, you should use if and af instead.
I don't think I understand what you mean by that.
Yes, I've tried using If
, for example, and they don't work on serendipity mode, and i
/a
gets overwritten by nvim-treesitter-textobjects
mappings.
Could you test If and Af with the minimal config? See here: https://github.com/00sapo/visual.nvim#testing
If it works, it's a compatibility problem with your config and you should give me more info to reproduce the issue. I'm also experiencing something like that in my lunarvim-based config, but I still haven't found the time to check it.
I'm getting this error by Lazy upon trying to execute it:
Error detected while processing User Autocommands for "VeryLazy":
Lua module not found for config of visual.nvim. Please use a `config()` function instead
I think it's because of this line: dir = "/home/sapo/develop/visual.nvim"
.
However, I copied from the test config to mine:
using {
'nvim-treesitter/nvim-treesitter-textobjects',
dependencies = { "nvim-treesitter" },
}
using {
'nvim-treesitter/nvim-treesitter',
build = ':TSUpdate',
config = function()
local configs = require("nvim-treesitter.configs")
configs.setup({
ensure_installed = { "lua" }, -- add here the languages for your test case
sync_install = false,
highlight = { enable = true },
indent = { enable = true },
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
},
},
},
})
end,
}
using {
'00sapo/visual.nvim',
dependencies = { "nvim-treesitter", "nvim-treesitter-textobjects" },
opts = { treesitter_textobjects = true },
event = "VeryLazy",
config = function()
require('visual').setup()
end,
}
However, I'm still getting the same behaviour (even when only loading these 3 plugins).
Edit: I changed the line dir = "/home/sapo/develop/visual.nvim"
to "00sapo/visual.nvim"
, and that made it work. Still, I got the same behaviour.
Great, thanks, I'll fix the minimal config then.
About the tree-sitter error, thanks for reporting! In the meantime, you should be able to use vif and vaf even from serendipity mode
Hi, back on this again. The issue is that your configuration is setting opts
but then it's not using it in config
. If you set opts
, then it will be passed to the config
function, so you should expect it. Your config is basically overriding opts
with config
. See this comment for more info.
IMHO, the solution is to just use either config
or opts
, but not both of them.
IMHO, the solution is to just use either config or opts, but not both of them.
I'm not sure what you mean by that. How should I ammend the following configs:
using {
'00sapo/visual.nvim',
dependencies = { "nvim-treesitter", "nvim-treesitter-textobjects" },
load_type = {'portable', 'lightweight', 'vscode', 'test'},
opts = { treesitter_textobjects = true },
event = "VeryLazy",
config = function(_, opts)
require('visual').setup(opts)
end,
}
using {
'nvim-treesitter/nvim-treesitter',
build = ':TSUpdate',
load_type = {'portable', 'lightweight', 'vscode'},
opts = {
ensure_installed = { "lua" },
sync_install = false,
highlight = { enable = true },
indent = { enable = true },
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
},
},
},
},
config = function(_, opts)
require('nvim-treesitter.configs').setup(opts)
end
}
Removing config
from treesitter seems to not set up the configs at all. Leaving it, leads to the behavior described in the OP.
lazy.nvim will already call the setup
method, so no reason to have the config
parameter. In the case of treesitter, they have the setup
function in a different module from the one lazy.nvim
expects, so you need to keep the config function. About visual.nvim
, I don't see anything wrong. Can you try to remove the config
parameter?
Anyway, which package manager are you using? Where that using
come from? Are you using any IDE-like pre-made configuration?
Also, try with set timeoutlen 500
.
- I'm using lazy.nvim. The
using
function is just a thing I made to collect my plugins before passing them to lazy. set timeout 500
doesn't seem to be a command:E518: Unknown option: 500
- The following still gets the same behaviour:
using {
'00sapo/visual.nvim',
dependencies = { "nvim-treesitter", "nvim-treesitter-textobjects" },
load_type = {'portable', 'lightweight', 'vscode', 'test'},
opts = { treesitter_textobjects = true },
event = "VeryLazy",
}
using {
'nvim-treesitter/nvim-treesitter',
build = ':TSUpdate',
load_type = {'portable', 'lightweight', 'vscode'},
opts = {
ensure_installed = { "lua" },
sync_install = false,
highlight = { enable = true },
indent = { enable = true },
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
},
},
},
},
config = function(_, opts)
require('nvim-treesitter.configs').setup(opts)
end
}
If I did the wrong modification: please give me the full config you want me to try, as I'm not quite getting the point about opts
and config
.
Sorry, I realized I did one commit wrong. Try with the version in main...
Sorry for the delay.
Now the behaviour is a bit better (a and i go into insert mode), but I still couldn't get the Af
, If
, Ic
, etc. mappings to work.
Other mappings however, such as A}
, I)
, Ip
, etc. work fine.
Current config:
using {
'nvim-treesitter/nvim-treesitter',
build = ':TSUpdate',
opts = {
ensure_installed = { "lua" },
sync_install = false,
highlight = { enable = true },
indent = { enable = true },
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
},
},
},
},
config = function(_, opts)
require('nvim-treesitter.configs').setup(opts)
end
}
using { '00sapo/visual.nvim',
dependencies = { "nvim-treesitter", "nvim-treesitter-textobjects" },
opts = { treesitter_textobjects = true },
event = "VeryLazy",
config = function()
require('visual').setup()
end,
}
I also tried removing config:
using { '00sapo/visual.nvim',
dependencies = { "nvim-treesitter", "nvim-treesitter-textobjects" },
opts = { treesitter_textobjects = true },
event = "VeryLazy",
}
and changing the mappings myself:
keymaps = {
["Af"] = "@function.outer",
["If"] = "@function.inner",
["Ac"] = "@class.outer",
["Ic"] = "@class.inner",
["Aa"] = "@parameter.outer",
["Ia"] = "@parameter.inner",
},
I still see both opts
and config
I'm your current visual.nvim configuration.
Anyway, when entering serendipity mode, visual.nvim will remove all visual mappings and re-apply it's own. Treesitter's texobjects.select.keymaps
refer to the visual mode, so they will be removed in the serendipity mode. You need to tell visual.nvim to map them in the serendipity mode and this is achieved via the options mappings.sd_inside
and mappings.sd_around
, by default set to I
and A
. When you set the option treesitter_textobjects=true
, what happens is that the configuration of treesitter is parsed and the textobject's selection mappings are reused with sd_inside
and sd_around
keys. If when it starts the treesitter configuration is not setup, that is an issue. This is the only other issue I can think about, but using dependencies
should fix it.
I still see both
opts
andconfig
I'm your current visual.nvim configuration.
Please read my whole comment. I explicitly stated that I tried removing config
.
This is the only other issue I can think about, but using dependencies should fix it.
Considering that I'm already using dependencies = { "nvim-treesitter", "nvim-treesitter-textobjects" },
, I'll just drop this issue and the plugin from my config.
It's hard debugging this issue because I cannot reproduce it. Do you confirm that it persists with the minimal config? If so, I can fix it. Otherwise, it's likely an issue with other plugins I'm not aware of...
Yep, no prob. I'll try to get a minimal config.
Edit: The plugin was archived, so it won't be posted.