Add position_in_class: after_doc to keep class doc adjacent to class#337
Add position_in_class: after_doc to keep class doc adjacent to class#337yamat47 wants to merge 2 commits into
position_in_class: after_doc to keep class doc adjacent to class#337Conversation
Closes drwl#201. When a model is documented with a comment block placed directly above the class declaration, the existing `before` / `top` positions push that comment above the schema annotation, away from the class it documents: ```ruby # Source file: # Doc about User class User < ApplicationRecord end # After annotaterb today: # Doc about User # == Schema Information # ... class User < ApplicationRecord end ``` Add `after_doc` as a value for `position_in_class` and the other `position_in_*` options. With this value, the schema annotation is inserted above the contiguous documentation comment block immediately above the class, so the doc stays adjacent to the class: ```ruby # After annotaterb with position_in_class: after_doc: # == Schema Information # ... # Doc about User class User < ApplicationRecord end ``` A "documentation comment" is the contiguous comment block immediately above the class declaration with no blank line between. Recognized magic comments (`encoding`, `frozen_string_literal`, `shareable_constant_value`, `warn_indent`, `typed`, `rbs_inline`, plus Emacs/Vim modelines) are excluded so the annotation still slots between magic comments and the class doc: ```ruby # Source file: # frozen_string_literal: true # Doc about User class User < ApplicationRecord end # With position_in_class: after_doc: # frozen_string_literal: true # == Schema Information # ... # Doc about User class User < ApplicationRecord end ``` The default (`before` / `top`) is unchanged.
| --position | ||
| --pc, --position-in-class [before|top|after|bottom] | ||
| Place the annotations at the top (before) or the bottom (after) of the model file | ||
| -p [before|top|after|bottom|after_doc], |
There was a problem hiding this comment.
With the existing options, the names describe where the annotation appears when reading the file from top to bottom: before places it before the class block, and after places it after the class block.
Following that same logic, after_doc sounds like the annotation would appear after the doc block — in other words, between the doc and the class. But the actual behavior is the opposite.
I think before_doc feels more natural because it reads as “place the annotation before the doc block,” which matches what actually happens:
# == Schema Information ← before the doc
# Doc about User
class User < ApplicationRecord
endThere was a problem hiding this comment.
Thanks!
I confused myself with the naming here. Since the annotation is placed before the documentation comment (consistent with how before/after describe top-to-bottom placement), before_doc makes much more sense. Fixed in e5bb069 — renamed across the option values, CLI help, README, and specs.
The `before`/`after` naming describes where the annotation appears when reading a file top to bottom. The schema annotation is inserted *before* the documentation comment block, so `before_doc` reads more naturally than `after_doc`. Refs: drwl#337 (comment)
Summary
Closes #201.
Add a new
after_docvalue forposition_in_class(and the otherposition_in_*options). When set, the schema annotation is inserted above the contiguous documentation comment block that sits directly above the class declaration, so the human-written doc stays adjacent to the class.The default behavior (
before/top) is unchanged.Problem
With the existing
before/toppositions, a documentation comment placed directly above the class declaration gets pushed above the schema annotation — away from the class it documents:Fix
Setting
position_in_class: after_docslots the annotation above the doc block, keeping the doc next to the class:A "documentation comment" is defined as the contiguous comment block immediately above the class declaration with no blank line between. Recognized magic comments (
encoding,frozen_string_literal,shareable_constant_value,warn_indent,typed,rbs_inline, plus Emacs/Vim modelines) are excluded so the annotation still slots between magic comments and the class doc: