This extension provides rich Ruby language and debugging support for VS Code. It's still in progress (GitHub), please expect frequent updates with breaking changes before 1.0.
- About
- Install
- Debugger
- Linters
- Formatting
- Autocomplete
- Intellisense (Go to/Peek Definition/Symbols)
- TODO
- Contributing
- License
Also see the CHANGELOG.
It started as a personal project of @rebornix, aiming to bring Ruby debugging experience to VS Code. Then it turned to be a community driven project. With his amazing commits, @HookyQR joined as a contributor and brought users Linting/Formatting/etc, made the debugger more robust and more! If you are interested in this project, feel free to join the community: file issues, fork our project and hack it around and send us PRs, or subscribe to our mailing list.
Press F1
, type ext install
then search for ruby
.
Depending on your setup, you may need to manually install gem dependencies like
rubocop
, ruby-debug-ide
or 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 ruby -v
, 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.
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 (
jruby
,ruby_18
,mingw_18
), rungem install ruby-debug-ide
. - If you are using Ruby v1.9.x (
ruby_19
,mingw_19
), rungem install ruby-debug-ide
. Make sureruby-debug-base19x
is installed together withruby-debug-ide
. - If you are using Ruby v2.x
gem install ruby-debug-ide
gem install debase
(orgem install byebug
)
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.
Read following instructions about how to debug ruby/rails/etc locally or remotely
- Debugger installation
- Launching from VS Code
- Attaching to a debugger
- Running gem scripts
- Example configurations
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
.
You will need to install the ruby gem for each of these for linting to work (except ruby -wc of course)
- ruby -wc
- rubocop
- ruby-lint
- reek
- fasterer
- debride
Enable each one in your workspace or user settings:
// Basic settings: turn linter(s) on
"ruby.lint": {
"reek": true,
"rubocop": true,
"ruby": true, //Runs ruby -wc
"fasterer": true,
"debride": true,
"ruby-lint": true
},
// Time (ms) to wait after keypress before running enabled linters. Ensures
// linters are only run when typing has finished and not for every keypress
"ruby.lintDebounceTime": 500,
//advanced: set command line options for some linters:
"ruby.lint": {
"ruby": {
"unicode": true //Runs ruby -wc -Ku
},
"rubocop": {
"only": ["SpaceInsideBlockBraces", "LeadingCommentSpace"],
"lint": true,
"rails": true
},
"reek": true
}
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:
- debride: none
- ruby: none
- reek: *.reek
- fasterer: .fasterer.yml
- ruby-lint: ruby-lint.yml
- rubocop: .rubocop.yml
Settings available (in your VSCode workspace) for each of the linters:
"debride": {
"rails": true //Add some rails call conversions.
}
"ruby"//no settings
"reek" //no settings
"fasterer" //no settings
"ruby-lint": {
"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" ]
}
"rubocop": {
"lint": true, //enable all 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
}
The VS Code Ruby extension can automatically format your Ruby files whenever you save.
Set ruby.format
to rubocop
to enable rubocop formatting on save.
Formatting requires the rubocop gem to be installed. Note that you may have to turn on some of the AutoCorrect functions in your .rubocop.yml
file. See the rubocop documentation.
Important note: VS Code has a timeout that limits file formatters to 750ms. This is often not enough time for rubocop to complete. In the near future VS Code will allow customizing this timeout via the editor.formatOnSaveTimeout
setting. See #43702 for more details.
Rufo is an alternative Ruby formatting tool. See the VS Code Rufo Extension if you want to try it.
The ruby.codeCompletion
setting lets you select a method for code completion and other intellisense features. Valid options are rcodetools
and false
.
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.
Use the ruby.intellisense
setting to select a go to/peek definition/symbol
method. Valid options are rubyLocate
, and false
.
The 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:
"ruby.locate": {
"include": "**/*.rb",
"exclude": "{**/@(test|spec|tmp|.*),**/@(test|spec|tmp|.*)/**,**/*_spec.rb}"
}
The defaults will include all files with the rb
extension, but avoids searching within the test
, spec
, tmp
directories, as well as any directories begining with a .
, AND any files ending with _spec.rb
.
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 erb
.
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 ruby.intellisense
to false
.
- Unit/Integration tests debugging
- Shoulda
- Test::Unit
- Rack
- Rake
- IRB console
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
. - Run
npm run watch
in a shell to get the Typescript compiler running. - Open the repo directory in VS Code.
- Make a code change and test it. This is not hard, see the doc links above.
- Create a branch and submit a PR!
This extension is licensed under the MIT License.