nvim-treesitter / nvim-treesitter-textobjects

Apache License 2.0
2.2k stars 200 forks source link

[main] moving between functions does not work #649

Closed przepompownia closed 3 months ago

przepompownia commented 4 months ago

require('nvim-treesitter-textobjects.move').goto_next_start('@function.outer', 'textobjects') seems to not work.

With init.lua

local thisInitFile = debug.getinfo(1).source:match('@?(.*)')
local cwd = vim.fs.dirname(thisInitFile)
local appname = vim.env.NVIM_APPNAME or 'nvim'

vim.env.XDG_CONFIG_HOME = cwd
vim.env.XDG_DATA_HOME = vim.fs.joinpath(cwd, '.xdg', 'data')
vim.env.XDG_STATE_HOME = vim.fs.joinpath(cwd, '.xdg', 'state')
vim.env.XDG_CACHE_HOME = vim.fs.joinpath(cwd, '.xdg', 'cache')
vim.fn.mkdir(vim.fs.joinpath(vim.env.XDG_CACHE_HOME, appname), 'p')
local stdPathConfig = vim.fn.stdpath('config')

vim.opt.runtimepath:prepend(stdPathConfig)
vim.opt.packpath:prepend(stdPathConfig)

local function gitClone(url, installPath, branch)
  if vim.fn.isdirectory(installPath) ~= 0 then
    return
  end

  local command = {'git', 'clone', '--', url, installPath}
  if branch then
    table.insert(command, 3, '--branch')
    table.insert(command, 4, branch)
  end
  local sysObj = vim.system(command, {}):wait()
  if sysObj.code ~= 0 then
    error(sysObj.stderr)
  end
  vim.notify(sysObj.stdout)
  vim.notify(sysObj.stderr, vim.log.levels.WARN)
end

local pluginsPath = 'nvim/pack/plugins/opt'
vim.fn.mkdir(pluginsPath, 'p')
pluginsPath = vim.uv.fs_realpath(pluginsPath)

--- @type table<string, {url:string, branch: string?}>
local plugins = {
  ['nvim-treesitter'] = {url = 'https://github.com/nvim-treesitter/nvim-treesitter', branch = 'main'},
  ['nvim-treesitter-textobjects'] = {url = 'https://github.com/nvim-treesitter/nvim-treesitter-textobjects', branch = 'main'}
}

for name, repo in pairs(plugins) do
  local installPath = vim.fs.joinpath(pluginsPath, name)
  gitClone(repo.url, installPath, repo.branch)
  vim.cmd.packadd({args = {name}, bang = true})
end

local treesitterLib = vim.fs.joinpath(cwd, 'treesitter')
vim.fn.mkdir(treesitterLib, 'p')
vim.opt.runtimepath:append(treesitterLib)

local tsLangMap = {
  php = 'php_only',
  phpdoc = 'phpdoc',
}

for filetype, lang in pairs(tsLangMap) do
  if filetype ~= lang then
    vim.treesitter.language.register(lang, filetype)
  end
end

require('nvim-treesitter').setup({
  install_dir = treesitterLib,
})

require('nvim-treesitter.install').install(vim.tbl_values(tsLangMap), {skip = {installed = true}}, function ()
  vim.api.nvim_exec_autocmds('User', {pattern = 'TSInstallFinished'})
end)

vim.api.nvim_create_autocmd('FileType', {
  pattern = vim.tbl_keys(tsLangMap),
  callback = function (event)
    vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
    local ok, error = pcall(vim.treesitter.start, event.buf, tsLangMap[event.match])
    if not ok and error:find('no parser') then
      vim.api.nvim_create_autocmd('User', {
        buffer = event.buf,
        callback = function (tsinstallEvent)
          print(vim.inspect(tsinstallEvent))
          if not tsinstallEvent.match == 'TSInstallFinished' then
            return
          end
          vim.treesitter.start(event.buf, tsLangMap[event.match])
        end
      })
    end
  end
})

local tsSelectKeymaps = {
  ['ap'] = '@parameter.outer',
  ['ip'] = '@parameter.inner',
  ['af'] = '@function.outer',
  ['if'] = '@function.inner',
}

for input, capture in pairs(tsSelectKeymaps) do
  vim.keymap.set({'x', 'o'}, input, function ()
    require 'nvim-treesitter-textobjects.select'.select_textobject(capture, 'textobjects')
  end)
end

vim.keymap.set({'n', 'x', 'o'}, '[[', function ()
  require('nvim-treesitter-textobjects.move').goto_previous_start('@function.outer', 'textobjects')
end)
vim.keymap.set({'n', 'x', 'o'}, '[m', function ()
  require('nvim-treesitter-textobjects.move').goto_previous_start('@class.outer', 'textobjects')
end)
vim.keymap.set({'n', 'x', 'o'}, ']]', function ()
  require('nvim-treesitter-textobjects.move').goto_next_start('@function.outer', 'textobjects')
end)
vim.keymap.set({'n', 'x', 'o'}, ']m', function ()
  require('nvim-treesitter-textobjects.move').goto_next_start('@class.outer', 'textobjects')
end)

try nvim --clean -u init.lua init.lua and try ]] keymap or :lua require('nvim-treesitter-textobjects.move').goto_next_start('@function.outer', 'textobjects'). Nothing happens for me.

Expected behavior Moving works on main.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check() Requirements ~ - OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). - OK tree-sitter 0.22.6 (/home/user/.cargo/bin/tree-sitter) - OK curl 8.8.0 (/usr/bin/curl) curl 8.8.0 (x86_64-pc-linux-gnu) libcurl/8.8.0 GnuTLS/3.8.6 zlib/1.3.1 brotli/1.1.0 zstd/1.5.6 libidn2/2.3.7 libpsl/0.21.2 libssh2/1.11.0 nghttp2/1.62.1 ngtcp2/1.5.0 nghttp3/1.3.0 librtmp/2.3 OpenLDAP/2.5.18 Release-Date: 2024-05-22, security patched: 8.8.0-4 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd - OK tar 1.35.0 (/usr/bin/tar) OS Info ~ - sysname: Linux - version: #1 SMP PREEMPT_DYNAMIC Debian 6.9.9-1 (2024-07-13) - machine: x86_64 - release: 6.9.9-amd64 Install directory for parsers and queries ~ - /home/user/dev/user/nvim/envs/nvim-treesitter-textobjects-main/treesitter/ - OK is writable. - OK is in runtimepath. Installed languages H L F I J ~ - php_only ✓ ✓ ✓ ✓ ✓ - phpdoc ✓ . . . .

Output of nvim --version

NVIM v0.11.0-dev-474+g2a24d0a43
Build type: RelWithDebInfo
LuaJIT 2.1.1720049189

Additional context To observe the same for PHP parser (included there too) we need to disable built-in php ftplugin first (it defines its own ]] keymap).

ofseed commented 3 months ago

Try https://github.com/nvim-treesitter/nvim-treesitter-textobjects/pull/659. I should fix it there.

przepompownia commented 3 months ago

Now it works. Thank you.

clason commented 3 months ago

Please keep reporting regressions; there's a lot...