vscode-docs/docs/languages/ruby.md

222 строки
12 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

2023-03-24 01:36:41 +03:00
---
Order: 15
2023-03-24 01:36:41 +03:00
Area: languages
TOCTitle: Ruby
ContentId: 33c079a7-f8d5-48fc-9d92-16be760b42ab
2023-03-24 01:36:41 +03:00
PageTitle: Ruby with Visual Studio Code
DateApproved: 19/09/2024
2023-03-24 01:36:41 +03:00
MetaDescription: Learn about Visual Studio Code editor features (code completion, debugging, snippets, linting) for Ruby.
---
# Ruby in Visual Studio Code
[Ruby](https://www.ruby-lang.org) is a dynamic, open-source programming language known for its simplicity and productivity. With an expressive and elegant syntax, part of the Ruby philosophy is to make developers happy. It is often used for web development with a range of different frameworks, and for scripting, allowing for fast iterations when building prototypes.
2023-03-24 01:36:41 +03:00
This topic goes into detail about setting up and using Ruby within Visual Studio Code, with the
[Ruby LSP](https://marketplace.visualstudio.com/items?itemName=Shopify.ruby-lsp) extension.
![Ruby extension banner](images/ruby/ruby_lsp_extension.png)
## Installation
### Install Ruby through a version manager
2023-03-24 01:36:41 +03:00
While Ruby is installed by default on some operating systems (such as macOS and some distributions of Linux), we recommend using a version manager such as [rbenv](https://github.com/rbenv/rbenv) to be able to access newer versions of Ruby on Windows, macOS, and Linux. Follow the [installation guidance](https://github.com/rbenv/rbenv#installation) for your platform.
2023-03-24 01:36:41 +03:00
>**Note**: As with installing any new toolset on your machine, you'll want to make sure to restart your terminal/Command Prompt and VS Code instances to use the updated toolset location in your platform's PATH variable.
2023-03-24 01:36:41 +03:00
### Install the Ruby LSP extension in VS Code
2023-03-24 01:36:41 +03:00
You can find and install the Ruby LSP extension from within VS Code via the Extensions view
(`kb(workbench.view.extensions)`) and searching for 'Ruby LSP'.
![Ruby LSP extension in the Extensions view](images/ruby/ruby_lsp_extensions_view.png)
We'll discuss many of Ruby LSP features in this topic but you can also refer to the extension's [documentation](https://shopify.github.io/ruby-lsp) and [repository](https://github.com/Shopify/ruby-lsp).
2023-03-24 01:36:41 +03:00
### Check your installation
After installing, check the language status item to see the status of the Ruby LSP server. If the version manager has been configured, it should display the right Ruby version for your project. The server status should display starting or running, but not error.
2023-03-24 01:36:41 +03:00
![Ruby LSP language status center](images/ruby/ruby_lsp_status_center.png)
The extension generates a `.ruby-lsp` folder automatically with a custom bundle that includes the language server gem `ruby-lsp`. No configuration should be required.
2023-03-24 01:36:41 +03:00
By default, the extension tries to automatically detect the Ruby version manager you're using and use the right versions and paths accordingly. If you want to customize that behavior, set the following configuration in your user [settings](/docs/getstarted/settings.md):
2023-03-24 01:36:41 +03:00
```json
{
"rubyLsp.rubyVersionManager": {
"identifier": "rbenv"
}
}
2023-03-24 01:36:41 +03:00
```
The extension will automatically try to update the `ruby-lsp` language server gem periodically; if you want to force that to happen, use the Command Palette (`kb(workbench.action.showCommands)`) to execute **Ruby LSP: Update language server gem**.
2023-03-24 01:36:41 +03:00
If you have any problems, see [troubleshooting](https://shopify.github.io/ruby-lsp/troubleshooting.html) for next steps.
2023-03-24 01:36:41 +03:00
## Main features
### Navigation and IntelliSense
2023-03-24 01:36:41 +03:00
The Ruby LSP provides several navigation and IntelliSense related features, such as go to definition, hover, workspace
symbol, document symbol, completion and signature help.
2023-03-24 01:36:41 +03:00
<video src="images/ruby/navigation.mp4" placeholder="images/ruby/navigation-placeholder.png" autoplay loop controls
muted title="Demo of navigation and intellisense features">
Sorry, your browser doesn't support HTML 5 video.
</video>
2023-03-24 01:36:41 +03:00
To learn more about moving quickly through your source code with VS Code, check out [Code Navigation](/docs/editor/editingevolved.md).
### Inlay hints
Ruby LSP is able to display useful information about inferred or implicit values in your code. In the example below, you can see `StandardError` being shown as the implicit exception class being rescued in an empty `rescue` call.
2023-03-24 01:36:41 +03:00
![Ruby program with inlay hints displayed](images/ruby/ruby_lsp_inlay_hints.png)
While inlay hints can be helpful for understanding your code, you can also disable the feature via the **Editor > Inlay Hints: Enabled** setting (`setting(editor.inlayHints.enabled)`) or use the following to disable this feature only for Ruby LSP:
2023-03-24 01:36:41 +03:00
```json
"rubyLsp.enabledFeatures": {
"inlayHint": false,
}
```
### Semantic syntax highlighting
Ruby LSP is able to use [semantic syntax highlighting](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview) and styling due to its rich understanding of a project source code.
For example, it can highlight:
* Method invocations consistently, without confusing it with local variables.
* Local arguments (such as method, block or lambda arguments) consistently inside the scope in which they exist.
2023-03-24 01:36:41 +03:00
![Ruby LSP semantic highlighting](images/ruby/ruby_lsp_semantic_highlighting.png)
>**Note**: This screenshot is using the Spinel theme included in the [Ruby extension pack](https://marketplace.visualstudio.com/items?itemName=Shopify.ruby-extensions-pack). Themes must use the information surfaced by the Ruby LSP in order to provide rich highlighting for Ruby files.
2023-03-24 01:36:41 +03:00
To use this feature, the editor must have semantic highlighting enabled.
```json
"editor.semanticHighlighting.enabled": true,
```
### Linting and formatting
By default, Ruby LSP provides linting and formatting through an integration with [RuboCop](https://github.com/rubocop/rubocop). You can format your Ruby file using `kb(editor.action.formatDocument)` or by running the **Format Document** command from the Command Palette (`kb(workbench.action.showCommands)`) or the context menu in the editor.
2023-03-24 01:36:41 +03:00
If your project does not use RuboCop, the Ruby LSP will format files using [SyntaxTree](https://ruby-syntax-tree.github.io/syntax_tree).
2023-03-24 01:36:41 +03:00
![Linting a Ruby file](images/ruby/ruby_lsp_linting.png)
You can also run the formatter on each save (**Editor: Format On Save**) to keep your Ruby code properly formatted automatically while you are working. To do that, you must enable format on save.
2023-03-24 01:36:41 +03:00
```json
"editor.formatOnSave": true
```
The Ruby LSP extension also provides some convenient completions using format on type. For example, it will auto-continue comment lines and auto-close `end` tokens, pipes, or string interpolation curly braces. To use format on type, make sure it's enabled in the editor with:
2023-03-24 01:36:41 +03:00
```json
"editor.formatOnType": true
```
### Quick Fixes
When the linter finds errors and warnings in your source code, Ruby LSP can often provide suggested Quick Fixes (also called Code Actions), which are available via a light bulb hover in the editor. You can quickly open available Quick Fixes via the `kb(editor.action.quickFix)`.
2023-03-24 01:36:41 +03:00
![Quick Fixes for linting violations](images/ruby/ruby_lsp_quickfix.png)
Additionally, **Code Action Widget: Include Nearby Quick Fixes** (`setting(editor.codeActionWidget.includeNearbyQuickFixes)`) is a setting that is enabled on default, which will activate the nearest Quick Fix in a line from `kb(editor.action.quickFix)` (command ID `editor.action.quickFix`), no matter where your cursor is in that line.
The command highlights the source code that will be refactored or fixed with Quick Fixes. Normal Code Actions and non-fix refactorings can still be activated at the cursor location.
2023-03-24 01:36:41 +03:00
### Refactoring
In addition to Quick Fixes, the Ruby LSP also provides refactor options through Code Actions. For example, it can extract a Ruby expression into a local variable with a single click.
2023-03-24 01:36:41 +03:00
![Refactor extract to variable](images/ruby/ruby_lsp_refactor.png)
## Debugging
The Ruby LSP extension supports debugging using the debug gem (Ruby's official debugger). Alternatively, developers can
also install the [VS Code RDBG](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg) extension
to get debugging functionality.
The following documentation is for the Ruby LSP's debugger client. Please refer to [RDBG's README](https://github.com/ruby/vscode-rdbg)
for instructions on how to configure it.
2023-03-24 01:36:41 +03:00
### Debugging tests
2023-03-24 01:36:41 +03:00
The Ruby LSP adds CodeLens buttons on top of unit tests that enable you to run examples in the Test Explorer, run them
in a new terminal or launch the debugger. For these uses, no configuration is required.
2023-03-24 01:36:41 +03:00
![Test running code lenses](images/ruby/ruby_lsp_code_lens.png)
2023-03-24 01:36:41 +03:00
### Debugging through launch tasks
2023-03-24 01:36:41 +03:00
To use the debugger through launch tasks, you will need to create [debugging configurations](/docs/editor/debugging.md#launch-configurations)
in a `launch.json` file. The configuration lets you configure the program to be executed.
2023-03-24 01:36:41 +03:00
To create a `launch.json` for a Ruby program:
1. In the Debug view (`kb(workbench.view.debug)`), select the **create a launch.json file** link.
2. This displays a dropdown with several default launch configuration types. You can pick the first option, but we'll add more configurations.
3. We can now edit the created `.vscode/launch.json` file to add more ways to launch your Ruby program for debugging.
2023-03-24 01:36:41 +03:00
Example:
```json
{
"version": "0.2.0",
"configurations": [
// Launch the debugger for any given program. In this case, it will run the current file using Ruby
2023-03-24 01:36:41 +03:00
{
"type": "ruby_lsp",
"name": "Debug",
2023-03-24 01:36:41 +03:00
"request": "launch",
"program": "ruby ${file}",
2023-03-24 01:36:41 +03:00
},
// Launch the debugger for the current test file
2023-03-24 01:36:41 +03:00
{
"type": "ruby_lsp",
2023-03-24 01:36:41 +03:00
"request": "launch",
"name": "Debug test file",
"program": "ruby -Itest ${relativeFile}"
2023-03-24 01:36:41 +03:00
},
// Attach the debugger client to an existing Ruby process that has already been launched with the debugger
// server
2023-03-24 01:36:41 +03:00
{
"type": "ruby_lsp",
"request": "attach",
"name": "Attach to existing server",
2023-03-24 01:36:41 +03:00
}
]
}
```
After adding the launch configurations, we can debug Ruby programs by adding breakpoints and executing our launch tasks.
1. Open a Ruby file and click the left gutter in the editor to set a break point. It should display as a red dot.
![Red breakpoint dot in the left gutter of the editor](images/ruby/ruby_lsp_breakpoint.png)
2023-03-24 01:36:41 +03:00
2. Start debugging by selecting the desired task under **Run and Debug** and clicking the start debugging button (default keyboard shortcut `kb(workbench.action.debug.start)`).
2023-03-24 01:36:41 +03:00
![Debug session stopped at breakpoint](images/ruby/ruby_lsp_debugging_session.png)
2023-03-24 01:36:41 +03:00
## Next steps
This has been a brief overview showing the Ruby LSP extension features within VS Code. For more information, see the details provided in the Ruby LSP [documentation](https://shopify.github.io/ruby-lsp), including how to tune specific [VS Code editor](https://github.com/Shopify/ruby-lsp/blob/main/vscode/README.md) configurations.
2023-03-24 01:36:41 +03:00
To stay up to date on the latest features/bug fixes for the Ruby LSP extension, see the Releases page for the [monorepo](https://github.com/Shopify/ruby-lsp/releases), which includes both the server and VS Code extension implementations.
2023-03-24 01:36:41 +03:00
If you have any issues or feature requests, feel free to log them in the Ruby LSP's [GitHub repo](https://github.com/Shopify/ruby-lsp/issues).
2023-03-24 01:36:41 +03:00
If you'd like to learn more about VS Code, try these topics:
* [Basic Editing](/docs/editor/codebasics.md) - A quick introduction to the basics of the VS Code editor.
* [Install an Extension](/docs/editor/extension-marketplace.md) - Learn about other extensions are available in the [Marketplace](https://marketplace.visualstudio.com/vscode).
2023-03-24 01:36:41 +03:00
* [Code Navigation](/docs/editor/editingevolved.md) - Move quickly through your source code.