Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commenting is slow in large Markdown files #88

Open
ribru17 opened this issue Nov 22, 2023 · 2 comments
Open

Commenting is slow in large Markdown files #88

ribru17 opened this issue Nov 22, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@ribru17
Copy link

ribru17 commented Nov 22, 2023

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

@ribru17 ribru17 added the bug Something isn't working label Nov 22, 2023
@JoosepAlviste
Copy link
Owner

JoosepAlviste commented Nov 30, 2023

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:

function M.calculate_commentstring(args)

@ribru17
Copy link
Author

ribru17 commented Dec 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants