Skip to content

Abort visual commenting if it makes no sense #193

Closed

Description

@RaafatTurki

Hey there, awesome plugin BTW!
This might not be strictly Comment.nvim specific but I'd love a pointer in the right direction.

This issue is about nested filetype support so as the readme says I'm using nvim-ts-context-commentstring which integrates great with the readme pre_hook snippet

Consider the following example

1   <html>
2     <body>
3       <script>
4       let name = "raafat"
5       function print_name(name) {
6         console.log(name)
7       }
8       </script>
9     </body>
10  </html>

Visually selecting lines 1 through 5 and commenting produces an unwanted mess:

<!-- <html> -->
<!--   <body> -->
<!--     <script> -->
<!--     let name = "Raafat" -->
<!--     function print_name(name) { -->
      console.log(name)
    }
    </script>
  </body>
</html>

My aim is to calculate the commentstring at the start and end of the visual selection and only comment if they match.
I've found out that returning %s in the pre_hook results in "aborting" (is there a proper way to do this?).
Let me know what you think.

Activity

  1. numToStr commented on Aug 4, 2022

    @numToStr
    Owner

    Very interesting use case. I tried and the following seems to work

    require('Comment').setup({
        pre_hook = function(ctx)
            local start_range = {
                srow = ctx.range.srow,
                erow = ctx.range.srow,
                scol = ctx.range.scol,
                ecol = ctx.range.scol,
            }
    
            local end_range = {
                srow = ctx.range.erow,
                erow = ctx.range.erow,
                scol = ctx.range.ecol,
                ecol = ctx.range.ecol,
            }
    
            ctx.range = start_range
            local start_cstr = require('Comment.ft').calculate(ctx)
    
            ctx.range = end_range
            local end_cstr = require('Comment.ft').calculate(ctx)
    
            return start_cstr == end_cstr and start_cstr or ''
        end,
    })

    FYI, you don't need https://github.com/JoosepAlviste/nvim-ts-context-commentstring unless you need jsx (which might also get integrated in future #133)

  2. added
    questionQuestion related to integration and other stuff
    on Aug 4, 2022
  3. RaafatTurki commented on Aug 5, 2022

    @RaafatTurki
    Author

    Great snippet, it works nicely!

    The reaon I use nvim_ts_ctx_cs is not jsx, it's embedded js in html:
    Here's a minimal testing snippet

    <html>
      <body>
        <script>
          let name = "raafat"
        </script>
      </body>
    </html>

    Which after a while of linewise commenting turns into:

    <html>
      <body>
        <script>
          <!-- // <!-- // <!-- // <!-- // let name = "raafat" --> --> --> -->
        </script>
      </body>
    </html>

    The same happens with js/ts in svelte files, Is this a known issue or should I debug my nvim setup?

  4. numToStr commented on Aug 5, 2022

    @numToStr
    Owner

    You must be hitting this issue #62 (comment), which will, sadly, always remain a limitation

  5. RaafatTurki commented on Aug 5, 2022

    @RaafatTurki
    Author

    I see.
    How do you reckon a nvim_ts_ctx_cs based version of the snippet you've provided would look like?

  6. numToStr commented on Aug 6, 2022

    @numToStr
    Owner

    Hmmmm I am not sure as I don't use ts-context-commenstring. Maybe ask the plugin author for help. I guess it would be pretty similar to what I suggested earlier; calculate the commenstring twice, first for the starting range and other for the ending range then compare them.

  7. Repository owner locked and limited conversation to collaborators on Aug 8, 2022
  8. converted this issue into a discussion #199 on Aug 8, 2022
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

    questionQuestion related to integration and other stuff

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions