Skip to content

Wrap comments #265

Open

Description

@amannn

Besides statements in code, I think it's also desirable when comments are formatted in a way that makes them easy to read. I once hacked together a small atom plugin, that worked for my usecase: format-javascript-comment.

Features that could be interesting for prettier are the following:

  • It wraps lines at a specific line length by taking minimum raggedness into account.
  • It wraps lines before JSDoc tags.
  • Leaves empty lines in multiline comments in place.

Here's a small demo:

It would of course be really cool if this was baked into prettier 🙂.

Activity

  1. jlongster commented on Jan 17, 2017

    @jlongster
    Member

    This is a neat idea, but we need to make comments fully robust first. Currently they are moved around incorrectly and I'm about to rehaul how comments work. Let's get that in first and then we can talk about it.

    I'm a little hesitant to do this because users may put big diagrams or something in comments, and like string templates, it's kind of nice that we don't touch them. Also, users can combine your tool with ours to format both separately. We can keep this issue open for now though.

    Tweaking the title to make it more clear.

  2. changed the title [-]Prettier comments[/-] [+]Automatically wrap comments[/+] on Jan 17, 2017
  3. added
    status:needs discussion
    on Jan 17, 2017
  4. sophistifunk commented on Jan 17, 2017

    @sophistifunk

    I personally would be very wary of reformatting within comments, as it tends to break a lot of things such as ascii diagrams, table-formatted text, and the like.

  5. liddellj commented on Feb 13, 2017

    @liddellj

    What about introducing a wrapComments option, defaulted to false? Although I can see how taking this approach in general could lead to an explosion of options...

  6. erbridge commented on Feb 13, 2017

    @erbridge

    Or just allow the same flag to skip a comment block from being formatted as other code (pending in #671)?

  7. changed the title [-]Automatically wrap comments[/-] [+]Wrap comments[/+] on May 21, 2017
  8. rattrayalex commented on Jul 14, 2017

    @rattrayalex
    Collaborator

    There was a PR adding this functionality to JSX not too long ago. Comments have also largely had the overhaul mentioned, but still need some work.

    If anyone is looking for a fun challenge, this may be a prime candidate!

  9. added
    help wanted
    on Jul 14, 2017
  10. amannn commented on Jul 14, 2017

    @amannn
    Author

    Great to see some people interested in this idea!

    @rattrayalex What would you suggest how this could be integrated? With or without an option? Having it be skippable with prettier-ignore is probably pretty important.

    Btw. I recently discovered something my atom plugin currently doesn't cover: Treating code blocks like {thisIsAProperty: true, hereIsAnotherProperty: false} in comments atomic, meaning they can't be split by white space. I usually mark these parts with back ticks, so they should easily be identifiable (the back ticks are removed from my snippet here in Github).

  11. rattrayalex commented on Jul 14, 2017

    @rattrayalex
    Collaborator

    @amannn it would be great if this could be built well enough that it doesn't need to be an option. At a minimum, that means respecting prettier-ignore as you mentioned. It also means developing good heuristic around things like ascii-text, code examples (as you mentioned), etc.

    Probably the best place to start would be gathering a bunch of examples of comment blocks from around github, and forming a test file with them.

    It's also fantastic that you have an existing atom plugin; building on work & iteration from that would obviously be quite helpful.

  12. azz commented on Jul 15, 2017

    @azz
    Member

    Imagine if we could format JavaScript inside Markdown (#2444) inside JavaScript comments!

    /**
     * ```js
     * if (hasScienceGoneTooFar()) {
     *   throw new Issue("prettier/prettier", "wow");
     * }
     * ```
     */
    function wow() {}
  13. samhunta commented on Aug 17, 2017

    @samhunta

    An issue with making wrapped comments respect // prettier-ignore is if the programmer's intent is to ignore the statement following the API comments it's ignoring. Instead there could be prettier-ignore-comment?

  14. added
    area:comments
    and removed
    status:needs discussion
    on Oct 4, 2017
  15. 22 remaining items

  16. fardad-dev commented on Dec 17, 2020

    @fardad-dev
    Contributor
  17. muuvmuuv commented on Feb 3, 2021

    @muuvmuuv
    Contributor

    Yes, good idea to handle /** */ as Markdown and IMO it would be easy if we would ignore /**/ or //. That way ASCII arts would be safe since that should use the /**/ syntax anyway.

    Additionally, separating blocks for easier reading should be considered as well: fardad-dev/prettier-plugin-jsdoc#33

  18. added a commit that references this issue on Feb 3, 2021
  19. cupidchan commented on Sep 27, 2022

    @cupidchan

    Is there any update on this wrap comments request?

  20. Sanaruca commented on Sep 30, 2023

    @Sanaruca

    In the meantime I am using Rewrap, a VSCode extension.

  21. lkraav commented on Mar 20, 2024

    @lkraav

    TIL YAML (or I guess any) comments are not wrapped yet.

  22. muuvmuuv commented on Oct 15, 2024

    @muuvmuuv
    Contributor

    In the meantime I am using Rewrap, a VSCode extension.

    Could that not be ported to an prettier plugin? Guess that would be a good thing for the time being.

  23. karlhorky commented on Oct 15, 2024

    @karlhorky
    Contributor

    It's F# and C# code - if anyone would be interested in starting to get involved in the Rewrap code, there's an initiative by the community to fork it here (original author is no longer around):

    Eg. to polish it up a bit and potentially port it to TypeScript, before any integration into Prettier is considered.

    An alternative would be to try to adapt that code to Prettier as is, but that seems like multiple steps at once - feels riskier to do it all at once.

  24. muuvmuuv commented on Oct 15, 2024

    @muuvmuuv
    Contributor

    Porting to TS; Sounds like a job for ChatGPT 😅

  25. added theissue type on Mar 3, 2025
  26. vhbui02 commented on Mar 23, 2025

    @vhbui02

    Are there any progress regarding this feature?

  27. AndersonVitaease commented on Nov 26, 2026

    @AndersonVitaease

    Hi! I'd like to work on this and want to agree on the design before writing code, since the main concerns here have been ASCII art, prettier-ignore fatigue and the no-new-options policy.

    Proposal (phase 1, JS/TS only):

    • No new option. Reuse proseWrap. With the default ("preserve") nothing changes for anyone.
    • With proseWrap: "always"/"never", only /** ... */ doc comments are treated as Markdown paragraphs and rewrapped to printWidth (accounting for indentation and the * prefix), reusing the existing Markdown printer.
    • Never touched: // and plain /* */ comments, JSDoc tag lines (@param etc., left as-is in phase 1), fenced or 4-space-indented code, lists, tables, known directives, and anything under prettier-ignore.
    • Idempotent, with fixtures for each case above, and no existing snapshot changes.

    Would the team accept a PR along these lines? Happy to adjust scope (e.g. keep it behind proseWrap: "always" only).

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

    area:commentshelp wanted

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions