Ruby LSP Mongoid
A Ruby LSP add-on that provides enhanced editor features for Mongoid applications.
Similar to ruby-lsp-rails, this add-on enhances Ruby LSP with Mongoid-specific functionality by providing index enhancements for methods generated by Mongoid DSLs.
Note: This project is under active development. See TODO.md for planned features.
Installation
Add this gem to your application's Gemfile:
group :development do
gem "ruby-lsp-mongoid"
endThen run:
bundle installRuby LSP will automatically detect and load the add-on.
Requirements
How It Works
Ruby LSP Mongoid implements the Ruby LSP add-on interface to provide index enhancements. It statically analyzes Mongoid model files to understand which DSL methods are used and registers the generated methods with Ruby LSP's index.
Features
Go-to-Definition & Autocomplete for Mongoid DSL Methods
Ruby LSP Mongoid indexes methods generated by Mongoid DSLs. Once indexed, Ruby LSP's built-in features automatically work:
- Go-to-Definition: Jump to the DSL declaration from method calls
-
Autocomplete: See generated methods in completion suggestions (e.g., type
user.to seeposts,profile, etc.)
Field Accessors
class User
include Mongoid::Document
field :name, type: String
field :email, type: String
field :age, type: Integer
end
user = User.new
user.name # Go-to-definition works! → jumps to `field :name`
user.name= # Writer method also indexedAssociations
class User
include Mongoid::Document
has_many :posts
has_one :profile
belongs_to :organization
has_and_belongs_to_many :roles
end
user.posts # → jumps to `has_many :posts`
user.post_ids # has_many also generates `_ids` accessor
user.profile # → jumps to `has_one :profile`
user.build_profile # builder methods indexed
user.create_profileEmbedded Documents
class Post
include Mongoid::Document
embeds_many :comments
embeds_one :metadata
end
class Comment
include Mongoid::Document
embedded_in :post
end
post.comments # → jumps to `embeds_many :comments`
post.metadata # → jumps to `embeds_one :metadata`
comment.post # → jumps to `embedded_in :post`Scopes
class Post
include Mongoid::Document
scope :published, -> { where(published: true) }
scope :recent, -> { order(created_at: :desc) }
end
Post.published # → jumps to `scope :published`
Post.recent # → jumps to `scope :recent`Hover Information
When hovering over Mongoid DSL declarations, you'll see additional information:
Field Options
Hovering over a field symbol shows its type, alias, and default value:
field :active, type: Boolean, default: true
# ^^^^^^
# Hover shows: "type: Boolean, default: true"Association Target Class
Hovering over an association symbol shows the target class with a clickable link:
has_many :posts
# ^^^^^^
# Hover shows: "has_many: [Post](file:///path/to/post.rb#L1)"This works for all association and embedded document DSLs:
-
has_many,has_one,belongs_to,has_and_belongs_to_many -
embeds_many,embeds_one,embedded_in
Development
After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/riseshia/ruby-lsp-mongoid.
License
The gem is available as open source under the terms of the MIT License.