andrewferrier / wrapping.nvim

Plugin to make it easier to switch between 'soft' and 'hard' line wrapping in NeoVim
MIT License
123 stars 4 forks source link

Use treesitter to toggle hard wrapping in code comments #44

Closed nianiam closed 3 months ago

nianiam commented 5 months ago

As I mentioned in #40 I would like to see the ability to auto-wrap code comments. I had a go at implementing this and, whilst it works, I am far from proficient at lua and using neovim's APIs so I wasn't sure how to properly test my implementation.

It's a relatively simple implementation, using tsutils to get the node under the cursor and then toggling hard wrap + adding a text width when the cursor is in a comment. I added the following to the options:

 require("wrapping").setup({
        ...
        hard_wrap_comments = {
            notify = false, -- This will override "notify_on_switch"
            width = 79, -- Default textwidth if not set for the filetype
        }
    })

So that it was possible to disable wrap notifications specifically for this mode (and any other in the future!) and allow the user to set their preferred textwidth if it is not set in an ftpluin.

I left the plugin disabled by default.

Either way, the actual implementation probably needs some refinement, but the logic is there.

andrewferrier commented 5 months ago

@nianiam thanks for this. So I'm trying to understand the use case for this. Is there an implication here that you normally use soft wrapping for your code? That's not what I do, which is why I'm a bit confused by this.

I think in order to merge this either I or you would probably also need to add some tests. Are you up for trying that? :)

nianiam commented 5 months ago

@andrewferrier I don’t soft wrap my code. In fact, I don't have any form of wrapping (ie. wrap = false and textwidth is not set) in my code files. For the most part I let programmatic formatting tools like Prettier do that to keep my code consistent. However, one thing they don’t tend you do is wrap your code comments. Which means I need to manually hard wrap them to keep the text width consistent.

This is where my little addition comes in. It enforces hard wrapping (in case wrapping mode is set to soft or is not set) and sets a text width when the cursor is in a comment.

When the cursor is not in a code comment it attempts to revert the wrapping mode back to what it was originally along with removing the text width. Unfortunately it has to do this check on every cursor movement, but I think my logic prevents too much overhead.

If this makes sense, I can have a go at writing some tests. However, I don't actually know how to run the tests! It's not clear how you're running them from the repo alone.

andrewferrier commented 3 months ago

Hi - I haven't tried this pull request yet, but I don't fully understand it. If the 'default' mode you use a piece of code in is soft wrapping (or no wrapping), isn't it going to be a bit jarring when you move over a comment and the whole file flips into hard wrap mode? Presumably all the long code lines which up to that point were soft-wrapped will suddenly jump up to just be one screen line long?

andrewferrier commented 3 months ago

I guess fundamentally I do also just have concerns here about how well suited a plugin like this is to handling code ... it was certainly originally designed for text-like filetypes.

Would it not make more sense to work on adding capability to code formatters like prettier to format comments also?

nianiam commented 3 months ago

I understand and agree that ultimately this should be the responsibility of prettier. However, as I linked in a previous comment, there is a longstanding issue asking for this to be implemented. I also appreciate that this is your project and wouldn't want to force your to add anything. I can just maintain my fork if necessary.

However, here's a short video of it working. Because I toggle the line width the whole file doesn't wrap suddenly:

https://imgur.com/a/AbckXxY

It works pretty well for me (although it needs little bit of polish).

andrewferrier commented 3 months ago

Fair point, and you're right, perhaps I didn't really evaluate this properly originally. I will try and find some time to try out your fork properly and maybe add some tests. Appreciate your patience and thanks for all your work on this.

nianiam commented 3 months ago

I’m happy to get started on the testing, if perhaps you could guide me on what tools you’re using to actually run the tests. I’m not that clued up with lua tooling.

There’s a small bug whereby if you start on a blank line and type a comment, you never trigger CursorMove so hard wrapping doesn’t get enabled. I think an additional auto command on the TextChangedI event attempting to enable the mode should sort this out.

Additionally, I modified some of your functions in my implementation. I think I’ll revert them and write my own so that my fork is sandboxed a bit better.

andrewferrier commented 3 months ago

OK, you did ask before - sorry - to run the tests, just run 'make' from the base of the repository - there's a Makefile which runs them.

I'm trying to understand better what you've done here, I'd hate for you to go to a lot of effort and then for me to reject the pull request.

I don't really understand your video, it looks like normal out-of-the-box behaviour to me, I'm not sure what either wrapping.nvim or your change is doing there. I tried opening a .ts file, but it seems that NeoVim already hard wraps comments when you enter them; whereas it doesn't hard wrap lines of code (this is with wrapping.nvim completely disabled). This may be something to do with the formatoptions option, I suspect.

So I guess I'm still a bit unclear what the change does?

nianiam commented 3 months ago

It would appear I didn’t understand the formatoptions settings properly, and you’re right, it does automatically hard wrap only comments with the c flag. Which, is pretty much exactly I wanted.

I can understand your confusion now.

Well, at least I enjoyed a random coding challenge! But I guess Treesitter was my only tool…

Thanks for pointing this out and apologies for wasting some of your time! 😅

andrewferrier commented 3 months ago

It's alright, bit of learning all round :) Thanks for taking the time to contribute.