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

Comments get lost when generating annotations #951

Open
cmer opened this issue Apr 29, 2022 · 7 comments
Open

Comments get lost when generating annotations #951

cmer opened this issue Apr 29, 2022 · 7 comments

Comments

@cmer
Copy link

cmer commented Apr 29, 2022

When there are already comments above a class definition, AnnotateModels deletes them when regenerating annotations.

Here's a short video showing the issue: https://cln.sh/Iw7NsPk7yEfmkyPWtIim

Version

  • annotate version: 3.2
  • rails version: 5.2.6.2
  • ruby version: 2.7.5
@joevandyk
Copy link

running into this as well

@alexeyr-ci1
Copy link

It works to add a blank line above the comment before annotating, but you need to find them first (I spotted it accidentally when reviewing a PR).

@cmer
Copy link
Author

cmer commented Jul 29, 2022 via email

@alexeyr-ci1
Copy link

Sorry, it's actually worse than I thought. It looked like it worked only because the comment wasn't getting regenerated. If I use --force (or do a schema change which forces a regeneration), then the first time eats the empty line and leaves the comment intact (so really hard to note in code review), and the next time eats the comment 🤣.

@filipesperandio
Copy link

Is there a workaround for this?

@drwl
Copy link
Collaborator

drwl commented Feb 17, 2024

Hey folks, I believe I was able to solve this bug in my gem drwl/annotaterb#72

drwl added a commit to drwl/annotaterb that referenced this issue Feb 17, 2024
This PR attempts to fix a long standing bug that was carried over from
the original gem. Comments near an annotation would get removed when
adding or updating annotations.

ctran/annotate_models#951,
https://github.com/ctran/annotate_models/#warning

**Context**
Prior to this change, the AnnotateRb and the old gem Annotate would
annotate model files in 1 of 2 ways:
1. The "update" method, existing annotations would be found using a
regex and then new annotations would be changed in place. If a human
made comment was added after the annotation block **without** a new
line, it would get removed during the update process.
2. The "overwrite" method, which would happen when annotations did not
exist OR if the `:force` option was set to true. A regex would be used
to find annotation, remove it from the file, and then add annotations
again to the file depending on options such as `:position`.

Relying on regexes to identify annotations in a file made it hard to
reason about and hard to change, so most of them were removed and
replaced with a custom Ruby file parser using Ripper [1], which makes
working with Ruby files deterministic.

This PR makes the following behavioral changes:
- When updating annotations (i.e. annotations exist && `force: false`),
it only updates the annotations and should no longer delete any human
comments that are before or after an annotation block
- When generating (no annotations exist in the file) or re-generating
annotations (`force: true`), it removes the annotation block and
surrounding whitespace, if it exists, and then depending on the
`position` option, will add annotations before the class declaration or
after the class declaration. In the case of re-generating annotations,
any surrounding whitespace will be preserved assuming annotations are
being written to the same position.

[1] A great write up on Ripper:
https://kddnewton.com/2022/02/14/formatting-ruby-part-1.html
@cmer-sh
Copy link

cmer-sh commented Feb 17, 2024

I wasn't aware of the fork. I'll definitely check it out. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants