JoosepAlviste / nvim-ts-context-commentstring

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

mini.comment integration pre hook - require("ts_context_commentstring.internal").update_commentstring() requires 1 argument #68

Closed krishna-bala closed 1 year ago

krishna-bala commented 1 year ago

I'm trying to set the pre hook for mini.comment per the instructions on the wiki for Integrations. My LSP is telling me that require("ts_context_commentstring.internal").update_commentstring()

requires 1 argument(s) but instead it is receiving 0.

Ignoring that warning doesn't cause any error messages when loading my plugins, but it does not seem to update the comment string as expected (it leaves it unchanged).

I'm using the LazyVim distribution, so it may be some plugin configuration issue on my end. But I just wanted to double check that wiki Integration page is still correct for mini.comment before asking for help on the LazyVim discussion board.

krishna-bala commented 1 year ago

Update: it appears I am calling the pre hook correctly, as I am able to print out a debug message when I attempt to comment a message. Here's what my mini.comment plugin looks like:

-- lua/plugins/minicomment.lua
return {
  "echasnovski/mini.comment",
  dependencies = {
    "nvim-treesitter/nvim-treesitter",
  },
  opts = {
    hooks = {
      pre = function()
        print("Successfully calling pre hook function...")
        require("ts_context_commentstring.internal").update_commentstring()
      end,
    },
  },
}

and here is my nvim-treesitter plugin customization:

-- lua/plugins/nvim-treesitter.lua
return {
  "nvim-treesitter/nvim-treesitter",
  dependencies = {
    "JoosepAlviste/nvim-ts-context-commentstring",
  },
  opts = {
    context_commentstring = {
      enable = true,
      enable_autocmd = false,
      config = {
        c = "// %s",
        cpp = "// %s",
      },
    },
  },
}

So I am inclined to think my call to require("ts_context_commentstring.internal").update_commentstring() isn't working as intended.

JoosepAlviste commented 1 year ago

Hey @krishna-bala! The configuration looks correct to me. I haven't used LazyVim though, so I'm not sure if there are any special steps that need to be taken.

update_commentstring() does accept 1 argument with extra options, but it isn't required. I think that we should update the function docs to set it as optional. The code docs could be improved in general.

Here are some questions and things to try:

  1. What file type are you trying this out in?
  2. If you manually call :=require('ts_context_commentstring.internal').calculate_commentstring(), then does that output the correct commentstring?
krishna-bala commented 1 year ago

Hi @JoosepAlviste, thanks for the reply. I'm trying this on cpp files (*.h, *.cc). Manually calling :=require('ts_context_commentstring.internal').calculate_commentstring() in a *.cc file outputs the desired commentstring, // %s. However, when I use mini.comment to comment out a line it still implements the "default" commentstring that comes with mini.comment, which is /* %s */. I suspect I don't fully understand how the plugins are loaded in LazyVim and am implementing this incorrectly.

That being said, the mini.comment maintainer suggested a workaround for my use case (described below).

Create a file after/ftplugin/<some-filetype>.lua inside your Neovim config (e.g. ~/.config/nvim/) and put this line there: vim.bo.commentstring = '<some-commentstring>'

e.g.

-- ~/.config/nvim/after/ftplugin/c.lua
vim.bo.commentstring = '<some-commentstring>'
victornswd commented 1 year ago

Not really sure if this is helpful for this situation, but I was having issues with mini.comment in the past weeks as well. It changed to commentstring from tree-sitter which caused some issues with some filetypes. I opened an issue on this and I got a recommended a new way of setting up nvim-ts-context-commentstring: https://github.com/echasnovski/mini.nvim/issues/342

JoosepAlviste commented 1 year ago

Thanks for letting me know, @victornswd! I've updated the integration instructions in the wiki: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/wiki/Integrations#minicomment

Hopefully this solves the issue. Feel free to re-open if there are still problems with this.