annotaterb icon indicating copy to clipboard operation
annotaterb copied to clipboard

Default settings does not indent correctly inside a module

Open wmakley opened this issue 4 months ago • 5 comments

Gem version: 4.19.0 Command: bundle exec annotaterb models

I had to anonymize the code (sorry).

Before:

# frozen_string_literal: true

module SomeModule
  class Action < ApplicationRecord
    # ...
  end
end

After bundle exec annotaterb models:

# frozen_string_literal: true

module SomeModule
# == Schema Information
#
# Table name: action
#
#  created_at          :datetime         not null
#  description         :string(4000)
#  updated_at          :datetime         not null
#  name                :string(50)       not null
#  action_id           :integer          not null, primary key
#  create_user_id      :integer          not null
#  last_update_user_id :integer          not null
#
  class Action < ApplicationRecord
    # ...
  end
end

Settings:

---
:position: before
:position_in_additional_file_patterns: before
:position_in_class: before
:position_in_factory: before
:position_in_fixture: before
:position_in_routes: before
:position_in_serializer: before
:position_in_test: before
:classified_sort: true
:exclude_controllers: true
:exclude_factories: false
:exclude_fixtures: false
:exclude_helpers: true
:exclude_scaffolds: true
:exclude_serializers: false
:exclude_sti_subclasses: false
:exclude_tests: false
:force: false
:format_markdown: false
:format_rdoc: false
:format_yard: false
:frozen: false
:grouped_polymorphic: false
:ignore_model_sub_dir: false
:ignore_unknown_models: false
:include_version: false
:show_check_constraints: false
:show_complete_foreign_keys: false
:show_foreign_keys: true
:show_indexes: true
:show_indexes_include: false
:simple_indexes: false
:sort: false
:timestamp: false
:trace: true
:with_comment: true
:with_column_comments: true
:with_table_comments: true
:position_of_column_comment: :with_name
:active_admin: false
:command:
:debug: false
:hide_default_column_types: ''
:hide_limit_column_types: ''
:timestamp_columns:
- created_at
- updated_at
:ignore_columns:
:ignore_routes:
:models: true
:routes: false
:skip_on_db_migrate: false
:target_action: :do_annotations
:wrapper:
:wrapper_close:
:wrapper_open:
:classes_default_to_s: []
:additional_file_patterns: []
:model_dir:
- app/models
:require: []
:root_dir:
- ''

wmakley avatar Sep 23 '25 16:09 wmakley

Hi @wmakley

Have you tried using the --nested-position option? It should make the annotation appear directly above the nested class (Action) instead of inside the module.

OdenTakashi avatar Oct 21 '25 12:10 OdenTakashi

I just tried the bundle exec annotaterb models --nested-position (on 4.20) an can confirm that it's not formatting correctly.

sirwolfgang avatar Oct 30 '25 22:10 sirwolfgang

@sirwolfgang Thanks for checking and confirming!

It might be that the existing annotation isn’t indented properly, so when it’s updated, the indentation isn’t applied.

Could you try running the following command to regenerate the annotations with proper indentation?

bundle exec annotaterb models --nested-position --force

Does this fix the indentation for existing annotations?

OdenTakashi avatar Nov 06 '25 13:11 OdenTakashi

That command wipes a lot, like new lines under frozen_string_literal comments; but it did leave my module'd class properly indented. So yeah, it looks like the issue is with updates.

sirwolfgang avatar Nov 06 '25 17:11 sirwolfgang

I'm running into this issue as well (on v4.20.0). It looks like bundle exec annotaterb models --nested-position will generate the initial annotations fine:

module SomeModule 
  class Action < ApplicationRecord
    # ...
  end
end
# frozen_string_literal: true

module SomeModule
  # == Schema Information
  #
  # Table name: actions
  # Database name: domain
  #
  #  id                    :uuid             not null, primary key
  #  created_at            :datetime         not null
  #  updated_at            :datetime         not null
  #  rule_id :uuid
  #
  # Indexes
  #
  #  index_action_on_rule_id  (rule_id)
  #
  class Action < ApplicationRecord
    # ...
  end
end

But subsequent calls to annotaterb models remove the indentations, even when the table hasn't changed:

# frozen_string_literal: true

module SomeModule
# == Schema Information
#
# Table name: actions
# Database name: domain
#
#  id                    :uuid             not null, primary key
#  created_at            :datetime         not null
#  updated_at            :datetime         not null
#  rule_id :uuid
#
# Indexes
#
#  index_action_on_rule_id  (rule_id)
#
  class Action < ApplicationRecord
    # ...
  end
end

At least for the settings our application uses, this seems to be fixable by tweaking the regexes used in AnnotationDiffGenerator, but I haven't thoroughly vetted that this doesn't break anything else yet:

AnnotateRb::ModelAnnotator::AnnotationDiffGenerator.send(:remove_const, :HEADER_PATTERN) # /(^# Table name:.*?\n(#.*\r?\n)*\r?)/
AnnotateRb::ModelAnnotator::AnnotationDiffGenerator.send(:remove_const, :COLUMN_PATTERN) # /^#[\t ]+[[\p{L}\p{N}_]*.`\[\]():]+(?:\(.*?\))?[\t ]+.+$/
AnnotateRb::ModelAnnotator::AnnotationDiffGenerator::HEADER_PATTERN = /(# Table name:.*?\n( *#.*\r?\n)*\r?)/
AnnotateRb::ModelAnnotator::AnnotationDiffGenerator::COLUMN_PATTERN = /#[\t ]+[[\p{L}\p{N}_]*.`\[\]():]+(?:\(.*?\))?[\t ]+.+$/

andrewts129 avatar Nov 06 '25 23:11 andrewts129

It does look like there is an issue specifically in the update flow — I can reproduce the indentation breaking when updating existing annotations.

However, I wasn’t able to reproduce the behavior originally described by @wmakley.
In my environment, when I run bundle exec annotaterb models on a file without any existing annotation, the generated annotation is not nested at all, rather than being nested with incorrect indentation.
So the initial indentation issue doesn’t seem to happen for me.

So in summary:

  • The indentation problem during updates does seem to exist
  • The initial generation problem reported by @wmakley was not reproducible for me

It looks like the update flow may need some further investigation.

OdenTakashi avatar Nov 27 '25 11:11 OdenTakashi