RRethy/nvim-treesitter-endwise

Endwise does not work for bash/vim with nvim-ts-rainbow and nvim-treesitter-context enabled

nkakouros opened this issue · 7 comments

Describe the bug
When I have nvim-treesitter-endwise, nvim-treesitter-context and nvim-ts-rainbow enabled, then endwise does not work for the filetypes sh and vim. It works for ruby and lua.

I have traced down the behavior to this init.lua:

-- Vim Plug Installation {{{
local packer_path = os.getenv('HOME') .. '/.local/share/nvim/site/pack/packer/start/packer.nvim/'
local f = io.open(packer_path .. "lua/packer.lua", "r")
if f == nil then
  local result = os.execute("git clone --depth 1 https://github.com/wbthomason/packer.nvim " ..
    "~/.local/share/nvim/site/pack/packer/start/packer.nvim")
end
function run_packer()
   require("packer").sync()
end

vim.api.nvim_create_augroup("PackerAutoInstall", {clear=true})
vim.api.nvim_create_autocmd("VimEnter", {group="PackerAutoInstall", callback=run_packer})
-- }}}

plugins = {
  {url='https://github.com/wbthomason/packer.nvim', enable=true},
  {
    description = 'Nvim Treesitter configurations and abstraction layer',
    url = 'https://github.com/nvim-treesitter/nvim-treesitter',
    enabled = true,
    on_update = ":TSUpdate",
    config = function() require('nvim-treesitter.configs').setup ({
      ensure_installed = {"bash", "bibtex", "c", "comment", "cpp", "css", "commonlisp", "dockerfile",
        "html", "http", "java", "javascript", "json", "latex", "lua", "make", "markdown",
        "php", "phpdoc", "python", "r", "regex", "rst", "scss", "toml", "vim", "yaml"},

      rainbow = {
        enable = true,
        extended_mode = true, -- Also highlight non-bracket delimiters like html tags
      },

      endwise = {
        enable = true,
      },

    }) end,
  },
  {
    description = 'Rainbow parentheses for neovim using tree-sitter',
    url = 'https://github.com/romgrk/nvim-treesitter-context',
    enabled = true,
    config = function() require'treesitter-context'.setup({
      patterns = {
        default = {
          'class',
          'function',
          'method',
          'for',
          'while',
          'if',
          'switch',
          'case',
        },
      },
    }) end
  },
  {
    description = 'Rainbow parentheses for neovim using tree-sitter',
    url = 'https://github.com/p00f/nvim-ts-rainbow',
    enabled = true,
  },
  {
    description = 'Wisely add "end" in Ruby, Vimscript, Lua, etc. Tree-sitter aware',
    url = 'https://github.com/RRethy/nvim-treesitter-endwise',
    enabled = true,
  },
  -- }}}
}

function install_plugins(plugins)
  for i, plugin in ipairs(plugins) do
    require("packer").use({
            plugin.url,
            branch=plugin.branch,
            disable=not plugin.enabled,
            requires=plugin.dependencies,
            run=plugin.on_update,
            config=plugin.config,
    })
  end
end

packer = require("packer")
packer.reset()
packer.init({compile_path = packer_db})
install_plugins(plugins)

To Reproduce
Steps to reproduce the behavior with a minimal init.lua:

  1. Start vim with the above init.lua only
  2. Install the plugins using :PackerSync
  3. Restart nvim
  4. Add a function to a lua file, endwise works ok
  5. Add a while loop to a shell script (either ft=sh or ft=bash), endwise does not work
  6. Disable nvim-ts-rainbow and nvim-treesitter-context and repeat, endwise now works for all 4 supported filetypes
  7. Disable only one of the above two conflicting extensions, endwise still does not work

Expected behavior
Endwise should work.

Screengrab
If applicable, add screengrabs which record any weird behaviour.

Additional context
neovim --version:

NVIM v0.7.0-dev+1186-g83fc91433                                                                               Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions     -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security     -fstack-clash-protection -fcf-protection -Wp,-U_FORTIFY_SOURCE -Wp,-D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/tmp/yaourt-tmp-nikos/aur-neovim-git/src/build/config -I/tmp/yaourt-tmp-nikos/aur-neovim-git/src/neovim-git/src -I/usr/include -I/tmp/yaourt-tmp-nikos/aur-neovim-git/src/build/src/nvim/auto -I/tmp/yaourt-tmp-nikos/aur-neovim-git/src/build/include
Compiled by my-machine

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

This there any development on this?

I just found out that in the above mentioned situations the following loop is never run, because no match is found by treesitter.

for _, match, metadata in query:iter_matches(root, bufnr, range[1], range[3] + 1) do

I looked into this a while back and found the issue was on treesitter's end, you can reproduce with a minimal config that only loads nvim-ts-rainbow (iirc) but I never had time to dig further and make an issue in the nvim-treesitter repo (or maybe core?).

I found a new plugin that can endwise.
https://github.com/hrsh7th/nvim-insx

The issue is not present for me with the all those plugins. Perhaps, it was already fixed?

The issue is not present for me with the all those plugins. Perhaps, it was already fixed?

Perhaps, it's not actually an nvim-treesitter-endwise error but rather a nvim-treesitter bug. I'll try to reproduce later and maybe close the issue.

Basically, it's very difficult to try to take advantage of treesitter while you're in the middle of your input.
This is because the AST often becomes invalid in the middle of input, and treesitter will not be able to understand the correct tree structure.