JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.18k stars 35 forks source link

Commenting is slow in large Markdown files #88

Open ribru17 opened 1 year ago

ribru17 commented 1 year ago

Minimal reproducible full config

{
  'JoosepAlviste/nvim-ts-context-commentstring',
  lazy = true,
  main = 'ts_context_commentstring',
  opts = {
    enable_autocmd = false,
  },
},
  {
    'numToStr/Comment.nvim',
    config = function()
      require('Comment').setup {
        pre_hook = require(
          'ts_context_commentstring.integrations.comment_nvim'
        ).create_pre_hook(),
        ignore = function()
          local mode = vim.api.nvim_get_mode()['mode']
          if mode == 'n' then
            return '^$'
          end
          return nil
        end,
      }
    end,
  },
vim.g.skip_ts_context_commentstring_module = true

Description

When operating on large .md files (~1500 lines), it takes over a second to toggle a comment line using Comment.nvim. Removing this plugin while still using Comment.nvim removes the delay.

Steps to reproduce

  1. Open a large Markdown file
  2. Toggle a comment linewise

Expected behavior

It takes a long amount of time just to comment one line

Actual behavior

Commenting a line should be quite quick

Additional context

For reference, the original file I had issues with is https://github.com/ribru17/118cs-notes/blob/master/README.md

JoosepAlviste commented 12 months ago

Thanks for the report! Performance is something I haven't really thought about too much as I haven't had issues with it before.

I tried to reproduce this with some large markdown files, but didn't manage to. I tried the example you gave as well as this large-ish file: https://github.com/romkatv/powerlevel10k/blob/master/README.md.

Could you try commenting out some parts of the code in this plugin to see where exactly the issue is? Cloning the plugin somewhere, setting dev = true, and configuring the dev path for Lazy.nvim:

{
  path = '~/folder/where/the/plugin/is/cloned',
}

should make it rather easy to debug. This is the main entrypoint for the calculation: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/blob/1277b4a1f451b0f18c0790e1a7f12e1e5fdebfee/lua/ts_context_commentstring/internal.lua#L51

ribru17 commented 12 months ago

Thank you for the response! I have made a minimal config file that was able to reproduce the bug that I saw:

local root = vim.fn.fnamemodify('./.repro', ':p')

-- set stdpaths to use .repro
for _, name in ipairs { 'config', 'data', 'state', 'cache' } do
  vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name
end

-- bootstrap lazy
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 = {
  {
    'nvim-treesitter/nvim-treesitter',
    build = ':TSUpdate',
    event = { 'BufReadPost', 'BufNewFile' },
    config = function()
      require('nvim-treesitter.configs').setup {
        ensure_installed = {
          'markdown',
          'markdown_inline',
          'html',
          'comment',
        },
        highlight = {
          enable = true,
        },
      }
    end,
  },
  'folke/tokyonight.nvim',
  -- add this to your lua/plugins.lua, lua/plugins/init.lua,  or the file you keep your other plugins:
  {
    'numToStr/Comment.nvim',
    config = function()
      require('Comment').setup {
        pre_hook = require(
          'ts_context_commentstring.integrations.comment_nvim'
        ).create_pre_hook(),
      }
    end,
    keys = { 'gc' },
  },
  {
    'JoosepAlviste/nvim-ts-context-commentstring',
    main = 'ts_context_commentstring',
    opts = {
      enable_autocmd = false,
    },
    lazy = true,
  },
}
require('lazy').setup(plugins, {
  root = root .. '/plugins',
})

vim.cmd.colorscheme('tokyonight')

Without Treesitter, the commenting is quite fast but with it it is very slow, at least with the file I linked above. I ran it with nvim -u repro.lua

xorander00 commented 3 months ago

Just chiming in to echo that I'm seeing the same issue. In my case, navigating large Markdown files is really slow. When I cleared all CursorMoved autocommands, the issue disappeared. Process of elimination narrowed it down to this plugin. I'll see if I can isolate the specific cause of this.

@ribru17 Was performance in your case impacted any more or less with Markdown folds?