From 8c6754e7b41a1ec11e933e9220f585687c6f2008 Mon Sep 17 00:00:00 2001 From: Mohith26 <109003762+Mohith26@users.noreply.github.com> Date: Wed, 4 Nov 2026 08:12:38 -0500 Subject: [PATCH] Add references, rename and definition for local variables Resolve local variables per scope with a Prism visitor that uses the depth Prism assigns to each local variable node. This covers reads, writes, parameters, block locals, pattern matching, regexp named captures and hash/keyword shorthands, and only looks at the current document since locals can't be referenced from other files. --- jekyll/index.markdown | 17 +- lib/ruby_lsp/internal.rb | 1 + lib/ruby_lsp/requests/definition.rb | 19 + lib/ruby_lsp/requests/prepare_rename.rb | 13 + lib/ruby_lsp/requests/references.rb | 23 + lib/ruby_lsp/requests/rename.rb | 57 +++ .../requests/support/local_variable_finder.rb | 437 ++++++++++++++++++ test/requests/definition_expectations_test.rb | 55 ++- .../prepare_rename_expectations_test.rb | 27 ++ test/requests/references_test.rb | 87 ++++ test/requests/rename_test.rb | 177 +++++++ .../support/local_variable_finder_test.rb | 349 ++++++++++++++ 12 files changed, 1256 insertions(+), 6 deletions(-) create mode 100644 lib/ruby_lsp/requests/support/local_variable_finder.rb create mode 100644 test/requests/support/local_variable_finder_test.rb diff --git a/jekyll/index.markdown b/jekyll/index.markdown index 5c8a57fc39..882d238745 100644 --- a/jekyll/index.markdown +++ b/jekyll/index.markdown @@ -133,7 +133,8 @@ Sorry, your browser doesn't support embedded videos. This video demonstrates the ### Go-to-Definition Go-to-definition allows users to navigate to the target constant or method's definition, -whether they're defined in your project or its dependencies. +whether they're defined in your project or its dependencies. For local variables, it jumps to the parameter or the first +assignment that declares the variable in the current scope. In VS Code this feature can be triggered by one of the following methods: @@ -479,7 +480,14 @@ Rename allows developers to rename all occurrences of the entity under the curso renaming can be triggered by right clicking the entity to rename or by pressing F2 on it. You can also preview the edits that will be applied by pressing CTRL/CMD + Enter after typing the desired new name. -Rename is currently only supported for constants, module names and class names. +Rename is currently supported for constants, module names, class names and local variables (including method and +block parameters). + +Local variables are renamed only within the scope that declares them, so a variable with the same name in another method +or block is left untouched. When the variable is used as a shorthand hash value or keyword argument (`{ name: }` or +`foo(name:)`), the key is kept and only the value is renamed (`{ name: new_name }`). Renaming is rejected if the new name +is already used by another local or by a method call in the same scope, since that would change the program's behavior. +Keyword parameters can't be renamed because callers refer to them by name.