Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.54k stars 146 forks source link

Hover Definitions for Ruby Keywords #2227

Open jamesmengo opened 3 months ago

jamesmengo commented 3 months ago

Set up the tooltips

Add tooltips to these keywords. The markdown files should have the description below and a newbie-friendly description.

Try to avoid having tooltips which are over-zealous (e.g. DefNode will work on any keyword within a method definition).

### Tooltips
- [ ] `__ENCODING__` - The script encoding of the current file.
- [ ] `__LINE__` - The line number of this keyword in the current file.
- [ ] `__FILE__` - The path to the current file.
- [ ] `BEGIN` - Runs before any other code in the current file.
- [ ] `END` - Runs after any other code in the current file.
- [x] `alias` - Creates an alias between two methods (and other things).
- [x] `and` - Short-circuit Boolean and with lower precedence than `&&`.
- [x] `begin` - Starts an exception handling block.
- [x] `break` - Leaves a block early.
- [x] `case` - Starts a case expression.
- [x] `class` - Creates or opens a class.
- [x] `def` - Defines a method.
- [x] `defined?` - Returns a string describing its argument.
- [x] ~`do` - Starts a block.~
- [x] `else` - The unhandled condition in `case`, `if` and `unless` expressions.
- [ ] `elsif` - An alternate condition for an `if` expression.
- [ ] `end` - The end of a syntax block. Used by classes, modules, methods, exception handling and control expressions.
- [x] `ensure` - Starts a section of code that is always run when an exception is raised.
- [x] `false` - Boolean false.
- [x] `for` - A loop that is similar to using the `each` method.
- [x] `if` - Used for `if` and modifier `if` expressions.
- [x] `in` - Used to separate the iterable object and iterator variable in a `for` loop.
- [x] `module` - Creates or opens a module.
- [x] `next` - Skips the rest of the block.
- [x] `nil` - A false value usually indicating “no value” or “unknown”.
- [ ] `not` - Inverts the following boolean expression. Has a lower precedence than `!`.
- [x] `or` - Boolean or with lower precedence than `||`.
- [x] `redo` - Restarts execution in the current block.
- [x] `rescue` - Starts an exception section of code in a `begin` block.
- [x] `retry` - Retries an exception block.
- [x] `return` - Exits a method.
- [x] `self` - The object the current method is attached to.
- [x] `super` - Calls the current method in a superclass.
- [ ] `then` - Indicates the end of conditional blocks in control structures.
- [x] `true` - Boolean true.
- [x] `undef` - Prevents a class or module from responding to a method call.
- [x] `unless` - Used for `unless` and modifier `unless` expressions.
- [x] `until` - Creates a loop that executes until the condition is true.
- [x] `when` - A condition in a `case` expression.
- [x] `while` - Creates a loop that executes while the condition is true.
- [x] `yield` - Starts execution of the block sent to the current method.

Write long-form documentation

Write newbie-friendly documentation in .md form for each of the following keywords. Once written, link to it from the tooltip. The link should open the markdown file locally in the editor.

Please see the north star comment from @vinistock below:

Expand thoughts on newbie-friendly documentation > I would expand on certain things and try to be super exhaustive with all aspects of the language. For example, in the methods documentation, what is a method? In the modules documentation, we could explain that they are Ruby's way of supporting multiple inheritance, since it does not allow for more than on parent class. We should also mention the differences between prepending, including or extending a module > The goal of these docs is to make someone who is new to Ruby (or new to programming altogether) be able to gather a better understanding of how Ruby works from inside the editor. So a good exercise is trying to put yourself into the position of a newbie. What could be confusing? What was unclear when you first started with Ruby?
### Long-form docs
- [ ] `__ENCODING__` - The script encoding of the current file.
- [ ] `__LINE__` - The line number of this keyword in the current file.
- [ ] `__FILE__` - The path to the current file.
- [ ] `BEGIN` - Runs before any other code in the current file.
- [ ] `END` - Runs after any other code in the current file.
- [ ] `alias` - Creates an alias between two methods (and other things).
- [ ] `and` - Short-circuit Boolean and with lower precedence than `&&`.
- [ ] `begin` - Starts an exception handling block.
- [ ] `break` - Leaves a block early.
- [ ] `case` - Starts a case expression.
- [ ] `class` - Creates or opens a class.
- [ ] `def` - Defines a method.
- [ ] `defined?` - Returns a string describing its argument.
- [ ] `do` - Starts a block.
- [ ] `else` - The unhandled condition in `case`, `if` and `unless` expressions.
- [ ] `elsif` - An alternate condition for an `if` expression.
- [ ] `end` - The end of a syntax block. Used by classes, modules, methods, exception handling and control expressions.
- [ ] `ensure` - Starts a section of code that is always run when an exception is raised.
- [ ] `false` - Boolean false.
- [ ] `for` - A loop that is similar to using the `each` method.
- [ ] `if` - Used for `if` and modifier `if` expressions.
- [ ] `in` - Used to separate the iterable object and iterator variable in a `for` loop.
- [ ] `module` - Creates or opens a module.
- [ ] `next` - Skips the rest of the block.
- [ ] `nil` - A false value usually indicating “no value” or “unknown”.
- [ ] `not` - Inverts the following boolean expression. Has a lower precedence than `!`.
- [ ] `or` - Boolean or with lower precedence than `||`.
- [ ] `redo` - Restarts execution in the current block.
- [ ] `rescue` - Starts an exception section of code in a `begin` block.
- [ ] `retry` - Retries an exception block.
- [ ] `return` - Exits a method.
- [ ] `self` - The object the current method is attached to.
- [ ] `super` - Calls the current method in a superclass.
- [ ] `then` - Indicates the end of conditional blocks in control structures.
- [ ] `true` - Boolean true.
- [ ] `undef` - Prevents a class or module from responding to a method call.
- [ ] `unless` - Used for `unless` and modifier `unless` expressions.
- [ ] `until` - Creates a loop that executes until the condition is true.
- [ ] `when` - A condition in a `case` expression.
- [ ] `while` - Creates a loop that executes while the condition is true.
- [ ] `yield` - Starts execution of the block sent to the current method.
### Special Cases
- [ ] Introduce a nicer pattern in `requests/hover.rb` rather than constantly adding `elsif` statements
- [ ] Ensure all Prism Nodes have been included + handled
jamesmengo commented 3 months ago

Following branch contains starters for long-form descriptions + tooltip descriptions https://github.com/Shopify/ruby-lsp/compare/jm/hd

Stivaros commented 3 months ago

I've rebased your changes into my branch, thanks!

andyw8 commented 3 months ago

A false value usually indicating “no value” or “unknown”.

I'd suggest "falsey" rather than false.

Stivaros commented 3 months ago

A false value usually indicating “no value” or “unknown”.

I'd suggest "falsey" rather than false.

We are pulling these from the official docs, @andyw8. We will hopefully be improving these in our newbie-friendly extended docs, though.

Or do we know the right people to update the official docs with the improved definition?

Stivaros commented 3 months ago

Spoke to @vinistock and we won't document cases without a corresponding Prism::XNode. For example, the do keyword tends to belong to a CallNode in Prism rather than as a DoNode. In such cases, we will just not document it, but also ensure that hovering over do doesn't show the tooltip for a CallNode.