Document code
YARD and RDoc are the most popular documentation generation tools used in multiple libraries for documenting code. RubyMine allows you to view documentation written in a YARD or RDoc syntax using Quick Documentation Lookup. Moreover, RubyMine provides extended capabilities to work with YARD tags:
Create missing YARD tags
Check the validity of YARD tags and fix them
Utilize YARD tags for better code insight (for example, determining an object type, viewing parameter information for methods, and so on)
Here we'll show you how to document a method that takes several parameters. In our case, the method takes two arguments and returns the product of these values.
def multiply(val1, val2)
end
We'll see on how to annotate this method using YARD tags and fix incorrect tags.
Place a caret at the method name and press AltEnter.
In the invoked popup, select the Add @param tag action.
RubyMine will add the corresponding comment above the method and will suggest that you specify the type of each parameter value. Start typing Integer and then press CtrlSpace to view suggestions. Select Integer, press Enter to confirm your choice and press Enter one more time to specify the type of the second parameter.
By default, RubyMine generates the parameter name after the parameter type (for example, @param [Integer] val1
). To change this behavior and set the parameter name before its type (@param val1 [Integer]
), do the following:
Open the Settings dialog (CtrlAlt0S).
Open the Editor | Inspections page and select the Method parameter missing '@param' tag inspection in the Ruby group.
Specify the desired order using the Tag syntax format option.
When you edit YARD tags, RubyMine checks whether or not any wrong tags exist. For example, this can be a duplicated tag or tag does not have a corresponding parameter in code. To remove an incorrect YARD tag:
Place the caret at the highlighted line containing the desired tag and press AltEnter.
In the invoked popup, select the Remove tag action.
RubyMine utilizes YARD type annotations for various code insight features, for example, determining an object type, using the obtained object type in code completion, viewing parameter information for methods, and so on. Let's see on several examples.
RubyMine uses YARD tags such as @return
, @param
, or @yieldparam
, to determine object types.
Note that RubyMine suggests completion results corresponding to the specified type.
tip
Apart from YARD tags, RubyMine can process the custom @type tag to receive information about the object type.
RubyMine can check whether or not the actual return and parameter types for a method match corresponding types provided using the @return
and @param
annotations. For example, if the method return type does not match the @return
type, the editor will show you a warning.
You can manage these warnings using the Mismatched parameter type and Mismatched return type inspections.
RubyMine understands the @overload
tag and will suggest to you all the declared overloads when showing parameter information.
Apart from standard YARD and RDoc tags, RubyMine can process the @type tag to receive information about the variable type. For example, you can use this tag for local variables (with or without variable names) and block parameters:
Local variables
# @type [Integer] customer_id = 1 # @type [String] customer_name customer_name = "Andrew Fuller"
Block parameters
# @type [Integer] number [1, 2, 3].each do |number| puts "#{number}" end
RubyMine allows you to render YARD/RDoc comments in the editor. Rendered comments are easier to read, and they don't overload your code with extra tags. You can click links to go to the referenced web pages, or view quick documentation for the referenced topics.
RubyMine also recognizes YARD macro extensions and provides general coding assistance for them. When rendering macros, RubyMine displays the macro data in place of its reference. This includes any variable substitutions that may be used in the macro data ($1, $2, and so on).
Click in the gutter next to the necessary documentation comment (or press CtrlAlt0Q) to toggle the rendered view; click to edit the comment.
(Optional) To change the font size, right-click a comment in the editor and select Adjust Font Size from the context menu. Note that the rendered comments use the same font size as the quick documentation popup.
You can configure the IDE to always render comments in the editor.
Right-click the icon in the gutter ( or ) and enable the Render All option.
Alternatively, in the Settings dialog CtrlAlt0S, select Editor | General | Appearance and enable the Render documentation comments option.
To edit a rendered comment, click the icon in the gutter next to the comment.
Thanks for your feedback!