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

[BUG] When I try to comment linewise in visual mode, unrelated or extra code gets commented #427

Open
Rizhiy opened this issue Jan 9, 2024 · 5 comments · May be fixed by #432
Open

[BUG] When I try to comment linewise in visual mode, unrelated or extra code gets commented #427

Rizhiy opened this issue Jan 9, 2024 · 5 comments · May be fixed by #432

Comments

@Rizhiy
Copy link

Rizhiy commented Jan 9, 2024

Here is my config (using lazy):

return {
    "numToStr/Comment.nvim",
    keys = {
        {
            "<C-_>",
            function()
                require("Comment.api").toggle.linewise.current()
                require("rizhiy.keys").press("j", "n")
            end,
            desc = "Comment line",
        },
        {
            "<C-_>",
            function() require("Comment.api").toggle.linewise(vim.fn.visualmode()) end,
            mode = "v",
            desc = "Comment selection",
        },
    },
    opts = {
        mappings = {
            basic = false,
        },
    },
}

When I try to comment in visual mode (using C-/), different code rather than selection gets commented, sometimes the outer scope, sometimes completely unrelated code altogether.

This config was working previously, not sure what changed.

vim version:

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1692716794
@Rizhiy
Copy link
Author

Rizhiy commented Jan 9, 2024

Hmm, when I try to use default keymaps nothing happens.

@jade-tea-h
Copy link

jade-tea-h commented Feb 3, 2024

I'm having similar behavior, it seems to me that it toggles the previous selection in visual/visual block mode, ignoring the visual line mode selection.

ETA: I don't know why I thought this, but it looks like the plugin expects you to leave visual mode before calling require("Comment.api").toggle.linewise(vim.fn.visualmode()) as it uses vim.api.nvim_buf_get_mark(0, '<'), which doesn't get the current selection, but the previous one. Also, I don't believe it works correctly with visual block mode, as it doesn't recognize ^V as a visual mode.

@Rizhiy
Copy link
Author

Rizhiy commented Feb 3, 2024

Surprisingly, I just had a similar dive into how to get visual region.

Yeah, this plugin is also making the same mistake, so either nvim changed it recently or just common misconception.

Need to either change to . and v marks or modify function to exit visual mode before calling toggle.linewise():

{
    "<C-_>",
    function()
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, true, true), "n", true)
        require("Comment.api").toggle.linewise(vim.fn.visualmode())
    end,
    mode = "v",
    desc = "Comment selection",
},

@jade-tea-h
Copy link

I just came to the same conclusion. I don't think this plugin is making a mistake so much as just expecting you to exit visual mode first. Trying to adjust the plugin to use . and v positions.

@franklouwers
Copy link

@Rizhiy for some reason that's not working here.

I have the following in my keybinds.lua:

vim.keymap.set('v', "«", function()
  vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, true, true), "n", true)
  require('Comment.api').toggle.linewise(vim.fn.visualmode())
end)

(for some reason option-\ ends up as being « on my system, don't ask).

It still comments strange parts of the file in visual mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants