Visual Studio Code Ruby Extension
This extension provides Ruby language and debugging support for VS Code.
Table of Contents
Also see the CHANGELOG.
ext install then search for
Depending on your setup, you may need to manually install gem dependencies like
solargraph. This can be complicated because
there are many different ways to use Ruby itself - system ruby, rbenv, chruby,
rvm, bundler, etc. Your results may also vary depending on how you start VS Code
and the environment variables present at that time.
The important thing is that if VS Code can't find
rubocop, our extension can't
either. One way to debug these problems is to investigate within VS Code's
Integrated Terminal. (View > Integrated Terminal). Try
gem env gemdir,
gem list | grep rubocop,
which rubocop and then
rubocop -v. This
might shed some light on why a gem dependency isn't working.
As of version v0.22.0, a new language server has been introduced into the extension. This server provides the following features:
- Multi-root support
- Balanced pairs (eg
if-end, etc) highlighting
- Syntax aware code folding
- Diagnostics support (eg lint) with rubocop, reek, and standard
- Document/selection formatting support for rubocop, standard, and rufo
DocumentSymbol support which enables features line the Outline view
The server is designed to slowly replace existing extension functionality. Users can choose to replace that functionality with that of the language server's via the
useLanguageServer configuration option (set it to
true). You can verify the server is running via the "Output" tab in VSCode (next to the Terminal) and selecting "Ruby Language Server" from the dropdown to the right.
The language server is under active development. You can check Issue 317 to see the currently proposed feature list. Please do not open GitHub issues for unimplemented features that are already on that list!
Install Ruby Dependencies
In this extension, we implement ruby debug ide protocol to allow VS Code to communicate with ruby debug, it requires
ruby-debug-ide to be installed on your machine. This is also how RubyMine/NetBeans does by default.
- If you are using JRuby or Ruby v1.8.x (
gem install ruby-debug-ide.
- If you are using Ruby v1.9.x (
gem install ruby-debug-ide. Make sure
ruby-debug-base19x is installed together with
- If you are using Ruby v2.x
gem install ruby-debug-ide
gem install debase (or
gem install byebug)
Add VS Code config to your project
Go to the debugger view of VS Code and hit the gear icon. Choose Ruby or Ruby Debugger from the prompt window, then you'll get the sample launch config in
.vscode/launch.json. The sample launch configurations include debuggers for RSpec (complete, and active spec file) and Cucumber runs. These examples expect that
bundle install --binstubs has been called.
Detailed instruction for debugging Ruby Scripts/Rails/etc
Read following instructions about how to debug ruby/rails/etc locally or remotely
Conditional breakpoint doesn't work
You need use Ruby
2.0 or above and you need to update
debase to latest beta version
gem install debase -v 0.2.2.beta10.
Available Linter hooks
You will need to install the ruby gem for each of these for linting to work (except ruby -wc of course)
- ruby -wc
Enable each one in your workspace or user settings:
// Basic settings: turn linter(s) on
"ruby": true, //Runs ruby -wc
// Time (ms) to wait after keypress before running enabled linters. Ensures
// linters are only run when typing has finished and not for every keypress
//advanced: set command line options for some linters:
"unicode": true //Runs ruby -wc -Ku
"only": ["SpaceInsideBlockBraces", "LeadingCommentSpace"],
By default no linters are turned on.
Each linter runs only on the newly opened or edited file. This excludes some of the linters functionality, and makes some overly chatty - such as ruby-lint reporting undefined methods. The usual configuration file for each linter will be use as they would be when running from the command line, however settings that include/exclude files will not likely be followed.
Relevant configuration files:
Settings available (in your VSCode workspace) for each of the linters:
"rails": true //Add some rails call conversions.
"reek" //no settings
"fasterer" //no settings
"levels": [/* a subset of these */ "error","warning","info"],
"classes":[ /* a subset of these */ "argument_amount", "loop_keywords", "pedantics", "shadowing_variables", "undefined_methods", "undefined_variables", "unused_variables", "useless_equality_checks" ]
"lint": true, //Run only lint cops.
"only": [/* array: Run only the specified cop(s) and/or cops in the specified departments. */],
"except": [/* array: Run all cops enabled by configuration except the specified cop(s) and/or departments. */],
"forceExclusion": true, //Add --force-exclusion option
"require": [/* array: Require Ruby files. */],
"rails": true //Run extra rails cops. Note [this was removed in RuboCop 0.72.0](https://github.com/rubocop-hq/rubocop/issues/5976)
The VS Code Ruby extension can automatically format your Ruby files whenever you save.
Steps to enable rubocop formatting.
- Install rubocop with
gem install rubocop. Note that you may have to turn on some of the AutoCorrect functions in your
.rubocop.yml file. See the rubocop documentation.
- Add the following to your settings.json:
Note: VS Code has a timeout that limits file formatters to 750ms which is often not enough time for rubocop to complete, which is why the setting is adjusted above. You may want to tweak this setting to meet your needs. See #43702 for more details.
Rufo is an alternative Ruby formatting tool. See the VS Code Rufo Extension if you want to try it.
endwise is a Visual Studio Code plugin that automatically inserts the
end keyword where appropriate. See the VS Code endwise extension.
ruby.codeCompletion setting lets you select a method for code completion and other intellisense features. Valid options are
To enable method completion in Ruby, run
gem install rcodetools. You may need to restart Visual Studio Code the first time.
[1, 2, 3].e #<= Press CTRL-Space here
Solargraph is an alternative Ruby code completion tool. See the Solargraph extension if you want to try it.
For more information about using Solargraph, refer to the Solargraph extension.
Intellisense (Go to/Peek Definition/Symbols)
ruby.intellisense setting to select a
go to/peek definition/symbol method. Valid options are
rubyLocate option includes workspace parsing functionality. It allows VS Code to
go to definition,
peak definition and provides
symbols for modules, classes, and methods defined within the same workspace. You can set glob patterns to match including and excluding particular files. The exclude match also runs against directories on initial load, to reduce latency.
rubyLocate uses ruby-method-locate to parse symbols.
The default settings are:
The defaults will include all files with the
rb extension, but avoids searching within the
tmp directories, as well as any directories begining with a
., AND any files ending with
If you change these settings, currently you will need to reload your workspace.
We now provide go to definition within
erb files, as well as syntax highlighting for
Solargraph now includes go to/peek definition and other language features. See the Solargraph extension for more information.
When using Solargraph it is recommended to set
Feel free to open issues or PRs! We welcome all contributions, even from beginners. If you want to get started with a PR, please do the following:
- Check out the VS Code Extension Docs, especially Running and Debugging Extensions.
- Fork this repo.
- Install dependencies with
npm install. You'll also need to install dependencies in the
npm run watch in a shell to get the Typescript compiler running.
npm run watch:client to compile the client and
npm run watch:server to compile the server. You'll need all three running to do development on the extension.
- Open the repo directory in VS Code.
- Make a code change and test it. You can use the Debug tab and the
Launch Extension configuration to help.
- Create a branch and submit a PR!
This extension is licensed under the MIT License.