Skip to content

[Solved] Blockwise comments broken with context-commentstring #10

Closed

Description

@sQVe

👋🏼,

Cheers for making a great commenting plugin!

I'm running the following config:

require('Comment').setup({
    ignore = '^$',
    pre_hook = function()
        return
            require('ts_context_commentstring.internal').calculate_commentstring()
    end
})

Take the following TypeScript snippet:

if (text == null) {
  text = await readTextFromStdin();
}

Running gbc on the if line gives me:

// if (text == null) {

when it really should give me:

/* if (text == null) { */

Activity

  1. numToStr commented on Oct 8, 2021

    @numToStr
    Owner

    The problem might be context-commenstring returning a linewise commentstring while running gbc and as you already know pre_hook overrides the commentstring.

    Someone was also facing this dilemma https://www.reddit.com/r/neovim/comments/q35328/comment/hfstsld/?utm_source=share&utm_medium=web2x&context=3

  2. sQVe commented on Oct 8, 2021

    @sQVe
    ContributorAuthor

    That seems like the same issue, yeah.

    This issue makes this look sort of broken though. Especially since it also just comments the first line if I have many selected.

  3. sQVe commented on Oct 8, 2021

    @sQVe
    ContributorAuthor

    This is an example:

    simplescreenrecorder-2021-10-08_12.21.29.mp4
  4. numToStr commented on Oct 8, 2021

    @numToStr
    Owner

    Yeah, I figured that much. As we know block comments should have both LHS and RHS part in the comments. But line comments only have RHS.

    -- Javascript block comments
    --  {*               %s                *}
    --  |                 |                 |
    -- lhs           placeholder           rhs
    
    -- Javascript line comments
    --  //               %s
    --  |                 |
    -- lhs           placeholder

    And to be honest, IDK how to resolve this situation but I am planning to update the pre_hook method to give you the comment type ie. line/block, and mode ie. toggle/comment/uncomment as the arguments. I still don't know after this whether anyone we'll be able to fully resolve this or not.

  5. numToStr commented on Oct 9, 2021

    @numToStr
    Owner

    @sQVe I was wondering, Would something like this work?

    {
        pre_hook = function(ctype)
            local U = require('Comment.utils')
    
            if ctype == U.ctype.line then
                -- This will only run if you do linewise comment
                return require('ts_context_commentstring.internal').calculate_commentstring()
            end
        end,
    }
  6. changed the title [-]Blockwise commenting broken with commentstring[/-] [+]Blockwise comments broken with context-commentstring[/+] on Oct 9, 2021
  7. pinned this issue on Oct 9, 2021
  8. added
    integration
    on Oct 9, 2021
  9. sQVe commented on Oct 9, 2021

    @sQVe
    ContributorAuthor

    That would be an improvement but would obviously disable the proper comment string when doing block commenting. gb would still not work for JSX.

  10. numToStr commented on Oct 9, 2021

    @numToStr
    Owner

    It's challenging to make both plugins sing together 😆 . As you can tell, this is more like an issue with context-commenstring itself, you can ask its maintainer for this use case.

  11. JoosepAlviste commented on Oct 14, 2021

    @JoosepAlviste
    Contributor

    Hey!

    Would it solve the problem if nvim-ts-context-commentstring could be configured with multi-line and single-line comments explicitly?

    Something like:

    typescript = {single = '// %s', multi = '/* %s */'},

    Then, we could accept a preferred commenting style in calculate_commentstring (default to single, for example):

    return require('ts_context_commentstring.internal').calculate_commentstring('multi')
  12. numToStr commented on Oct 14, 2021

    @numToStr
    Owner

    @JoosepAlviste Surely, that should fix this. As the pre_hook is now aware of the type of the comment so anyone can do this.

    {
        pre_hook = function(ctx)
            local U = require('Comment.utils')
            local ty = ctx.ctype == U.ctype.line and 'single' or 'multi'
            return require('ts_context_commentstring.internal').calculate_commentstring(ty)
        end,
    }
  13. sQVe commented on Oct 18, 2021

    @sQVe
    ContributorAuthor

    I just tested this some more and it's working quite nicely but it does have some quirks. It seems like the commentstring is evaluated on the line where the cursor is.

    Given this code:

    <button
      type="button"
      onClick={() => {
        actions.copyStringsToClipboard();
      }}
    >

    If I select everything from <button to }} I would assume that it would set the commentstring to {/* and not //. Only when I select down to > it uses the correct {/*.

  14. xeluxee commented on Oct 29, 2021

    @xeluxee

    @sQVe @numToStr I think I've found a solution to this problem using internal ts-context-commentstring functions:

    pre_hook = function(ctx)
      local tsccu = require('ts_context_commentstring.utils')
      local tscci = require('ts_context_commentstring.internal')
      local node, language_tree = tsccu.get_node_at_cursor_start_of_line(
        vim.tbl_keys(tscci.config)
      )
    
      if not node and not language_tree then
        return nil
      end
    
      local language = language_tree:lang()
      local language_config = require("Comment.ft").get(language, ctx.ctype)
    
      return tscci.check_node(node, language_config)
    end
    

    The above code is partially copied by calculate_commentstring function from ts-context-commentstring

  15. JoosepAlviste commented on Nov 2, 2021

    @JoosepAlviste
    Contributor

    Hey! I added a configuration option to nvim-ts-context-commentstring that would allow to do this: JoosepAlviste/nvim-ts-context-commentstring#26

    The way to use it with Comment.nvim would be like so:

    require('Comment').setup {
      pre_hook = function(ctx)
        local U = require 'Comment.utils'
        local type = ctx.ctype == U.ctype.line and '__default' or '__multiline'
        return require('ts_context_commentstring.internal').calculate_commentstring {
          key = type,
        }
      end,
    }

    Please take a look at the branch and let me know if it works for your use-case!

    PS: I'm also planning to add a way to customize the location where the treesitter tree traversal is started (currently only on the first non-blank char on the cursor's line). This would make it possible to comment something like this in JSX when running gciW:

    <View /* style={styles.foobar} */ />

    CC @sQVe @numToStr

  16. sQVe commented on Nov 2, 2021

    @sQVe
    ContributorAuthor

    @JoosepAlviste That works perfectly for my use-case in jsx! 🎉

  17. numToStr commented on Nov 3, 2021

    @numToStr
    Owner

    @JoosepAlviste That's wonderful, thanks. I personally don't use nvim-ts-context-commentstring but it seems to fix the issue as @sQVe said.


    @sQVe Feel free to close the issue.

  18. changed the title [-]Blockwise comments broken with context-commentstring[/-] [+][Solved] Blockwise comments broken with context-commentstring[/+] on Nov 3, 2021
  19. unpinned this issue on Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    integration

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions