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

[gc/]gc and [gb/]gb to navigate to the next/previous line/block comments #169

Open
k-times-c opened this issue Jun 15, 2022 · 7 comments
Open
Labels
feature New feature help wanted Extra attention is needed

Comments

@k-times-c
Copy link

Thanks for the awesome plugin!

Given Comment.nvim already uses treesitter, and ships with a number of optional mappings. I love to see this one as an option as well. I tried adding [gc and ]gc to my treesitter.textobjects config, but the 3 character plugins don't work all that well. Perhaps that's different here?

If you're swamped, I can take a stab at it! Just would most likely need some help implementing/understanding the code base.

I look forward to your thoughts.

@k-times-c k-times-c changed the title [gc/]gc and [gb/[gc to navigate to the next/previous code blocks [gc/]gc and [gb/[gb to navigate to the next/previous line/block comments Jun 15, 2022
@k-times-c k-times-c changed the title [gc/]gc and [gb/[gb to navigate to the next/previous line/block comments [gc/]gc and [gb/]gb to navigate to the next/previous line/block comments Jun 15, 2022
@k-times-c
Copy link
Author

I'm just seeing that there is a lot of talk surrounding "text objects" implementation(s). I think the feature is in the same vein. So let me know if you prefer this subject be merged with another issue, or simply closed since it not really feasible, etc.

@numToStr
Copy link
Owner

I am open to ideas, but I am still not sure whether we should add text-objects of any kind inside the plugin. My concern stems from couple of reasons

  1. comment node is different for couple of languages like comment in js but in rust it's line_comment and block_comment. Maintaining multiple queries for multiple languages, poses a maintenance burden.
  2. Neovim currently doesn't support + quantifier which is necessary for querying sibling nodes. This is a fundamental blocker for comment text-object.

I am hoping for other plugins which provides text-objects, to provide robust text-objects for comments so that we could leverage them after neovim supports + quantifier.


I think the feature is in the same vein. So let me know if you prefer this subject be merged with another issue, or simply closed since it not really feasible, etc.

I'll let it sit open, so we can reference it in the future.

@numToStr numToStr added good first issue Good for newcomers feature New feature help wanted Extra attention is needed and removed good first issue Good for newcomers labels Jun 15, 2022
@k-times-c
Copy link
Author

All that sounds good.

I am hoping for other plugins which provides text-objects, to provide robust text-objects for comments so that we could leverage them after neovim supports + quantifier.

I do know that nvim-treesitter-textobjects does offer an @comment textobject for a number of mainstream languages. Instead of creating the queries within Comment.nvim, perhaps these navigation keymappings in theory could leverage that plugin?

@numToStr
Copy link
Owner

perhaps these navigation keymappings in theory could leverage that plugin?

Yes, that's the idea. And we also have https://github.com/RRethy/nvim-treesitter-textsubjects

But nothing will work properly until + quantifier support.

@k-times-c
Copy link
Author

gotcha - and is there an issue or pull-request to track the conversation and subsequent work on adding + quantifier?

Separately, in preparation for getting more involved on such work, is there any content you'd recommend outside of the treesitter website and tree-sitter playground to get up-to-speed?

@numToStr
Copy link
Owner

is there an issue or pull-request to track the conversation and subsequent work on adding + quantifier?

Follow #133, i already linked couple of PR there.

Separately, in preparation for getting more involved on such work, is there any content you'd recommend outside of the treesitter website and tree-sitter playground to get up-to-speed?

There isn't much, maybe read other TS based plugins and neovim's core TS api (this is marked as experimental so there is much to improve)

@fernandocanizo
Copy link

fernandocanizo commented Dec 26, 2022

Let me jump in with my two cents: the idea of vi/vim/nvim is to have composable commands. Introducing text-objects mappings would impose an unnecessary maintainability burden. You can just map the toggles to single keys instead of default mappings, and compose whatever movement mapping Neovim already offers with visual mode and your mapped keys.

For example, I can comment the following Javascript code:

for (let i = 0; i < 10; i++) {
	count += 2
	console.log(count)
}

with v]]<F1> (I chose <F1> key for line-toggle, but it could be whatever you prefer). Also V$%<F1> would do the trick.

The thing is we have more than enough for whatever needs you may have. Below I share my numToStr/Comment.nvim configuration, you can copy & paste it under stdpath("config") .. after/plugin so it loads automatically:

-- Setup https://github.com/numToStr/Comment.nvim
-- See `:h comment-nvim`
require('Comment').setup({
    -- Ignore empty lines
    ignore = '^$',
    mappings = {
        -- Disable basic mappings. I'm gonna do my own, see below
        basic = false,
        -- Disable extra mappings, they are unnecessary
        extra = false,
    },
})

-- Toggle current line or with count
vim.keymap.set('n', '<F1>', function()
  local toggle_current_line = '<Plug>(comment_toggle_linewise_current)j'
  local toggle_count_lines = '<Plug>(comment_toggle_linewise_count)' .. vim.v.count .. 'j'
  return vim.v.count == 0 and toggle_current_line
    or toggle_count_lines
end, { expr = true })

-- Toggle visual, line comment
vim.keymap.set('x', '<F1>', '<Plug>(comment_toggle_linewise_visual)')

-- Toggle count, block comment
vim.keymap.set('n', '<F2>', function()
  local toggle_count_block = '<Plug>(comment_toggle_blockwise_count)' .. vim.v.count .. 'j'
  return toggle_count_block
end, { expr = true })

-- Toggle visual, block comment
vim.keymap.set('x', '<F2>', '<Plug>(comment_toggle_blockwise_visual)')

Hope you find them useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants