Skip to content

Add position_in_class: after_doc to keep class doc adjacent to class#337

Open
yamat47 wants to merge 2 commits into
drwl:mainfrom
yamat47:feat/before-doc-position
Open

Add position_in_class: after_doc to keep class doc adjacent to class#337
yamat47 wants to merge 2 commits into
drwl:mainfrom
yamat47:feat/before-doc-position

Conversation

@yamat47
Copy link
Copy Markdown
Contributor

@yamat47 yamat47 commented May 11, 2026

Summary

Closes #201.

Add a new after_doc value for position_in_class (and the other position_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 / top positions, a documentation comment placed directly above the class declaration gets pushed above the schema annotation — away from the class it documents:

# Source file:
# Doc about User
class User < ApplicationRecord
end

# After annotaterb today:
# Doc about User
# == Schema Information
# ...
class User < ApplicationRecord
end

Fix

Setting position_in_class: after_doc slots the annotation above the doc block, keeping the doc next to the class:

# With position_in_class: after_doc:
# == Schema Information
# ...
# Doc about User
class User < ApplicationRecord
end

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:

# 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

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.
@yamat47 yamat47 marked this pull request as ready for review May 11, 2026 22:30
Copy link
Copy Markdown
Collaborator

@OdenTakashi OdenTakashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!
One thing that still feels a little confusing to me is the name after_doc.

Comment thread README.md Outdated
--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],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OdenTakashi

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)
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

Successfully merging this pull request may close these issues.

annotaterb is moving my class doc

2 participants