Navigate to Ruby function definition in VS Code
Asked Answered
L

6

75

I'm pretty new to Visual Studio Code, and I'm trying to edit some Ruby code. I installed Ruby support, and I enabled the language server, but Ctrl-clicking on a function name doesn't work, and neither does F12. Both of these features work fine for Python code.

How can I navigate from a function call to its definition in Ruby code?

Here's the Ruby code I tried:

def foo
    puts "In foo."
end

foo()

Here are my settings:

{
    "explorer.confirmDelete": false,
    "explorer.confirmDragAndDrop": false,
    "terminal.integrated.fontSize": 15,
    "git.confirmSync": false,
    "ruby.useLanguageServer": true,
    "editor.rulers": [80, 120]
}

Here's the Python code that works fine:

def foo():
    print('In foo.')


foo()
Libation answered 12/3, 2020 at 16:57 Comment(3)
Try setting "ruby.intellisense": "rubyLocate" in your settings.Etch
That didn't work, @Chris, but it gave me a better error message. I added an answer with my complete fix.Libation
add "ruby.intellisense": "rubyLocate" in settings works for me, I'm using vs code 1.61.2. remember: restart vs code.Hatchel
L
148

Thanks to Chris's suggestion, I got a better error message. Following that lead, I found that Ruby's code navigation seems to require a second language server: solargraph. I don't know if you require both, but I can now navigate to Ruby definitions. I also have to autocomplete working.

Update [2022]

Coming back to this with VSCode 1.67.0 on Ubuntu 22.04, I no longer require solargraph. However, I did struggle a bit to figure out the exact steps to get it working:

  1. Open File: Preferences: Extensions.

  2. Search for Ruby by Peng Lv, and install it.

  3. Open File: Preferences: Settings.

  4. Click the button in the top right to open the JSON settings file: enter image description here

  5. Add these settings:

    {
        "ruby.useLanguageServer": true,
        "ruby.intellisense": "rubyLocate"
    }
    
  6. Restart VSCode.

Libation answered 12/3, 2020 at 23:15 Comment(6)
This worked for me except that plugin doesnt navigate to constants definition.Buttonhook
Make sure that the VSCode Ruby extension is also installed and enabledOverride
I just want to make a note, there are some settings that need to be entered in the json and can't be changed with just the GUI, these settings are not though. You can just find the settings by putting "ruby use language server" and "ruby intellisense" in the search. Saying to open the json and just copy paste this text is a much more complete set of instructions though so the answer does make sense this way, just want to let anyone who finds this know if they don't like manually editing the settings json file.Jegar
you are awsome, i wish i had found this before and not paid for rubyminesWaltraudwaltz
Ruby, by Peng Lv, is deprecated now. "Ruby LSP" is the one they suggest using instead, but Go To Definition stopped working for me now with that oneLiturgical
@NickLScott: It's really sad. vim-rails had better features, and a working "go to definition" more than 10 years ago. It would be nice to have a modern, easy to install, full-featured, open-source Ruby IDE.Potboy
S
27
  1. Try setting "ruby.intellisense": "rubyLocate" in your settings.
  2. check folder .vscode file on yout project-> settings.json -> {"ruby.intellisense": "rubyLocate"}
Seline answered 9/6, 2021 at 9:46 Comment(2)
I only needed to do step one. There wasn't a .vscode file in my project. I did have to quit VS Code and reopen it before right clicking revealed "Go to Definition."Ululant
Doesn't work anymoreAetna
S
7

Install Extension Ruby Solargraph

Sweeper answered 12/8, 2021 at 6:52 Comment(0)
T
4

In case this may help anyone else: Based on my own experience, you do need to have BOTH solargraph installed in your Rails repo/enabled in VSCode, AND have rubyLocate specified in your VSCode settings as your Ruby intellisense method. "Go to definition" didn't work for my Rails project with only solargraph installed, but once I set rubyLocate as the intellisense method (it was set to "false" by default) it started working.

Tenorio answered 27/5, 2021 at 22:1 Comment(1)
"It started working". Okay. Does it also go-to-definition from has_many :books to app/models/book.rb? It doesn't seem to work in VSCode+SolarGraph+rubyLocate. vim-rails has been doing it for more than 10 years nowPotboy
S
3

What worked for me in 2024 was this:

Outside your main project, create a folder with these two files:

  • .ruby-version with a version >= 3.0.0
3.0.0
  • Gemfile
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'ruby-lsp'
gem 'ruby-lsp-rails'
gem 'rubocop'
gem 'rubocop-rspec'
gem 'rubocop-rails'
gem 'rubocop-performance'

In your Ruby project, create a VSCode settings file (e.g. .vscode/settings.json)

{
  "rubyLsp.bundleGemfile": "../path/to/above/Gemfile", //or absolute path
  "rubyLsp.formatter": "rubocop",
  "rubyLsp.rubyVersionManager": "rbenv",
}

Install Ruby LSP plugin and restart VSCode.

Saddle answered 28/2 at 0:8 Comment(2)
This should now be the accepted answer. Do note that Ruby 3.0 is the minimum version LSP will work with. Unfortunately but understandably, legacy versions, notably 2.7.8, won't work (which quite a few people in the enterprise space are stuck on). Still, This Is The Way!Mcgruter
This solution worked for me with ruby 3.0.5Epitaph
S
2

In VSCode, the navigating to a Ruby function definition is called "Go to Symbol in Editor...":

vsCodeSymbol

(Windows: Ctrl + Shift + O. Macs: Cmd + Shift + O).

This doesn't work out of the box with VSCode at the moment, but one lightweight extension that implements it is VSCode Ruby Symbols.

VSCodeRubySymbols

Squall answered 11/10, 2021 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.