JoosepAlviste / nvim-ts-context-commentstring

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

ts_context_commentstring.integrations.comment_nvim module not found #58

Closed SmithJson closed 1 year ago

SmithJson commented 1 year ago

Environment

Question Description

Thank you very much for the plugin. I encountered a "ts_context_commentstring.integrations.comment_nvim not found" error during use, but I can find the module in the source code of the nvim-ts-context-commentstring plugin

image image image image

JoosepAlviste commented 1 year ago

Hey!

Could you show how you installed the plugin? Just to make sure it is installed correctly with the plugin manager (if you're using one).

I don't think that this is related to your error, but one thing I noticed in your configuration is that configuring the plugin inside treesitter config setup function is incorrect. In your screenshot, the key is config, but it should be context_commentstring:

require'nvim-treesitter.configs'.setup {
  context_commentstring = {
    enable = true,
    enable_autocmd = false,
  }
}

I don't think that that would cause the error though since requiring a module should still work. But it makes sense to try to fix it anyways πŸ˜„

UtkarshVerma commented 1 year ago

I am also facing this issue.

SmithJson commented 1 year ago

Hey!

Could you show how you installed the plugin? Just to make sure it is installed correctly with the plugin manager (if you're using one).

I don't think that this is related to your error, but one thing I noticed in your configuration is that configuring the plugin inside treesitter config setup function is incorrect. In your screenshot, the key is config, but it should be context_commentstring:

require'nvim-treesitter.configs'.setup {
  context_commentstring = {
    enable = true,
    enable_autocmd = false,
  }
}

I don't think that that would cause the error though since requiring a module should still work. But it makes sense to try to fix it anyways πŸ˜„

I use modules packaged by other developers based on wbthomason/packer.nvim

This is the file address:https://github.com/SmithJson/nvim/blob/master/lua/core/pack.lua The parameters passed in by calling are the same as those required by packer.nvim

NyCodeGHG commented 1 year ago

I also have this issue trying to use the plugin using lazy.nvim

SmithJson commented 1 year ago

Thanks you

JoosepAlviste commented 1 year ago

Looks like this is an issue for quite a few people πŸ˜•

I can't really reproduce it and I'm using lazy.nvim now and previously used packer. Maybe you could help with a way to minimally reproduce the issue?

It seems like the Lua runtime path is maybe not correctly set up for some weird reason? Are you maybe using Lazy.nvim or Packer in a non-standard-ish way? I couldn't really fully understand the config from @SmithJson , but it looks like there's some funky things going on there. Maybe it could be reproduced with a smaller config that uses the same techniques?

Finally, does your set rtp show anything interesting? It should include the nvim-ts-context-commentstring folder. If I understand correctly, then require() should try to import files from the lua/ folders inside the rtp folders.

NyCodeGHG commented 1 year ago

I think i've fixed the issue with lazy.nvim in my config. I was trying to require the plugin before it was loaded.

This is my configuration now which works for me:

return {
  {
    'numToStr/Comment.nvim',
    dependencies = {
      'JoosepAlviste/nvim-ts-context-commentstring',
    },
    opts = function()
      return {
        pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
      }
    end,
  },
}

But this is basically the same config as @SmithJson uses so I think this might be a packer issue, but I'm not sure

SmithJson commented 1 year ago

Looks like this is an issue for quite a few people πŸ˜•

I can't really reproduce it and I'm using lazy.nvim now and previously used packer. Maybe you could help with a way to minimally reproduce the issue?

It seems like the Lua runtime path is maybe not correctly set up for some weird reason? Are you maybe using Lazy.nvim or Packer in a non-standard-ish way? I couldn't really fully understand the config from @SmithJson , but it looks like there's some funky things going on there. Maybe it could be reproduced with a smaller config that uses the same techniques?

Finally, does your set rtp show anything interesting? It should include the nvim-ts-context-commentstring folder. If I understand correctly, then require() should try to import files from the lua/ folders inside the rtp folders.

πŸ‘ @NyCodeGHG @JoosepAlviste Thanks a lot for the ideas provided. The reason for the error is that there is a problem with the loading order of the package. I put comment.nvim load after nvim-ts-context-commentstring and it worked fine

image image
JoosepAlviste commented 1 year ago

That's great to hear that it works now!

It's interesting though, that requiring a Lua file also needs to be done after the plugin is loaded. I suppose Packer/Lazy hadn't added the plugin to the runtime path yet when Comment.nvim was initialized.

Does this fix the issue for you too, @UtkarshVerma?

UtkarshVerma commented 1 year ago

Yes.

ArchitBhonsle commented 1 year ago

I actually came across this same issue but mostly because I messed up the plugin spec. It's a pretty simple mistake but might help you out.

If you are using lazy.nvim like me, do not use the opts field:

    {
        'numToStr/Comment.nvim',
        event = 'VeryLazy',
        dependencies = { 'nvim-treesitter' },
        opts = {
            pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
        },
    },

It is evaluated when the file is read. At this point ts_context_commentstring is not loaded. Instead, use config like this (or use a function to define opts):

    {
        'numToStr/Comment.nvim',
        event = 'VeryLazy',
        dependencies = { 'nvim-treesitter' },
        config = function()
            require('Comment').setup({
                pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
            })
        end,
    }
dpetka2001 commented 1 year ago

You don't actually need to define opts as a function. You just need to define pre_hook as a function, so that it becomes a closure and doesn't get evaluated when the file is parsed. I have the following in my config and works just fine

return {
  {
    "numToStr/Comment.nvim",
    opts = {
      pre_hook = function()
        require("ts_context_commentstring.integrations.comment_nvim").create_pre_hook()
      end,
    },
  },
}

You can of course define opts as a function in general to change some default behavior, but in this case it's not necessary.

jasper-clarke commented 4 months ago

For anyone coming here in the future this is the final bit of code that will make it work on lazy.nvim

  {
    "numToStr/Comment.nvim",
    dependencies = {
      {
        "JoosepAlviste/nvim-ts-context-commentstring",
        config = function()
          require("ts_context_commentstring").setup({
            enable_autocmd = false,
          })
        end,
      },
    },
    config = function(_, opts)
      require("Comment").setup({
        pre_hook = require("ts_context_commentstring.integrations.comment_nvim").create_pre_hook(),
      })
    end,
  },