'Navigate to Ruby function definition in VS Code

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()


Solution 1:[1]

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 autocomplete working.

Update

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.

  5. Add these settings:

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

Solution 2:[2]

  1. Try setting "ruby.intellisense": "rubyLocate" in your settings.
  2. check folder .vscode file on yout project-> settings.json -> {"ruby.intellisense": "rubyLocate"}

Solution 3:[3]

Install Extension Ruby Solargraph

Solution 4:[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.

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ratan Tirkey
Solution 3 Eric.Hu0111
Solution 4 refrigerizer
Solution 5 stwr667