echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.45k stars 171 forks source link

[mini.ai] No textobject "af"/"if" found #947

Closed Allaman closed 3 weeks ago

Allaman commented 4 weeks ago

Contributing guidelines

Module(s)

mini.ai

Description

Hi! I want to migrate from treesitter-textobjects to mini.ai. However, I can't get @function.inner and @function.outer to work. This drives me crazy. Any help/hint is much appreciated

Minimal not working config:

-- From https://github.com/folke/lazy.nvim
local root = vim.fn.fnamemodify("./.repro", ":p")

for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  "nvim-treesitter/nvim-treesitter",
  "echasnovski/mini.ai",
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

require("nvim-treesitter.configs").setup({
  ensure_installed = { "go" },
  highlight = {
    enable = true,
  },
})

local ai = require("mini.ai")
require("mini.ai").setup({
  n_lines = 500,
  custom_textobjects = {
    f = ai.gen_spec.treesitter({ a = "@function.outer", i = "@function.inner" }), -- function
  },
})

vim.cmd.colorscheme("tokyonight")

Neovim version

v0.11.0-dev-4684+gae0d56264-Homebrew

Steps to reproduce

nvim -nu minimal.lua main.go

Write some Golang code:

package main

func main() {
    i := 100
}

Then inside the main func hit dif or daf.

This treesitter-textobjects config works with the same Go code:

-- From https://github.com/folke/lazy.nvim
local root = vim.fn.fnamemodify("./.repro", ":p")

for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
  "folke/tokyonight.nvim",
  "nvim-treesitter/nvim-treesitter",
  "nvim-treesitter/nvim-treesitter-textobjects",
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

require("nvim-treesitter.configs").setup({
  ensure_installed = { "go" },
  highlight = {
    enable = true,
  },
  textobjects = {
    select = {
      enable = true,
      lookahead = true,
      keymaps = {
        ["af"] = "@function.outer",
        ["if"] = "@function.inner",
      },
    },
  },
})

vim.cmd.colorscheme("tokyonight")

Expected behavior

Operate on the outer/inner function

Actual behavior

(mini.ai) No textobject "af" found covering region within 500 lines and search_method = 'cover_or_next'. (mini.ai) No textobject "if" found covering region within 500 lines and search_method = 'cover_or_next'.

echasnovski commented 3 weeks ago

The problem here is that tree-sitter based textobjects from gen_spec.treesitter() need proper language query files to work. They do not come with 'mini.ai' directly, but should be handled separately by the user. The currently easiest way to do so is to just install 'nvim-treesitter-textobjects' without enabling its functionality. For more information see help for gen_spec.treesitter().

This particular reproduction example can be resolved by updating installed plugins to be the following:

local plugins = {
  "folke/tokyonight.nvim",
  "nvim-treesitter/nvim-treesitter",
  "nvim-treesitter/nvim-treesitter-textobjects",
  "echasnovski/mini.ai",
}
Allaman commented 3 weeks ago

Thank you for the explanation. Adding nvim-treesitter-texobjects does the job.