Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prepare Rename Request #2894

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ruby_lsp/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
require "ruby_lsp/requests/inlay_hints"
require "ruby_lsp/requests/on_type_formatting"
require "ruby_lsp/requests/prepare_type_hierarchy"
require "ruby_lsp/requests/prepare_rename"
require "ruby_lsp/requests/range_formatting"
require "ruby_lsp/requests/references"
require "ruby_lsp/requests/rename"
Expand Down
51 changes: 51 additions & 0 deletions lib/ruby_lsp/requests/prepare_rename.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
module Requests
# The
# [prepare_rename](https://microsoft.github.io/language-server-protocol/specification#textDocument_prepareRename)
# # request checks the validity of a rename operation at a given location.
class PrepareRename < Request
extend T::Sig
include Support::Common

sig do
params(
document: RubyDocument,
position: T::Hash[Symbol, T.untyped],
).void
end
def initialize(document, position)
super()
@document = document
@position = T.let(position, T::Hash[Symbol, Integer])
end

sig { override.returns(T.nilable(Interface::Range)) }
def perform
char_position = @document.create_scanner.find_char_position(@position)

node_context = RubyDocument.locate(
@document.parse_result.value,
char_position,
node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode],
code_units_cache: @document.code_units_cache,
)
target = node_context.node
parent = node_context.parent
return if !target || target.is_a?(Prism::ProgramNode)

if target.is_a?(Prism::ConstantReadNode) && parent.is_a?(Prism::ConstantPathNode)
target = determine_target(
target,
parent,
@position,
)
end

range_from_location(target.location)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/rename.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class Rename < Request

class InvalidNameError < StandardError; end

class << self
extend T::Sig

sig { returns(Interface::RenameOptions) }
def provider
Interface::RenameOptions.new(prepare_provider: true)
end
end

sig do
params(
global_state: GlobalState,
Expand Down
23 changes: 22 additions & 1 deletion lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def process_message(message)
text_document_prepare_type_hierarchy(message)
when "textDocument/rename"
text_document_rename(message)
when "textDocument/prepareRename"
text_document_prepare_rename(message)
when "textDocument/references"
text_document_references(message)
when "typeHierarchy/supertypes"
Expand Down Expand Up @@ -237,6 +239,7 @@ def run_initialize(message)
completion_provider = Requests::Completion.provider if enabled_features["completion"]
signature_help_provider = Requests::SignatureHelp.provider if enabled_features["signatureHelp"]
type_hierarchy_provider = Requests::PrepareTypeHierarchy.provider if enabled_features["typeHierarchy"]
rename_provider = Requests::Rename.provider unless @global_state.has_type_checker

response = {
capabilities: Interface::ServerCapabilities.new(
Expand All @@ -263,7 +266,7 @@ def run_initialize(message)
workspace_symbol_provider: enabled_features["workspaceSymbol"] && !@global_state.has_type_checker,
signature_help_provider: signature_help_provider,
type_hierarchy_provider: type_hierarchy_provider,
rename_provider: !@global_state.has_type_checker,
rename_provider: rename_provider,
references_provider: !@global_state.has_type_checker,
document_range_formatting_provider: true,
experimental: {
Expand Down Expand Up @@ -727,6 +730,24 @@ def text_document_rename(message)
send_message(Error.new(id: message[:id], code: Constant::ErrorCodes::REQUEST_FAILED, message: e.message))
end

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def text_document_prepare_rename(message)
params = message[:params]
document = @store.get(params.dig(:textDocument, :uri))

unless document.is_a?(RubyDocument)
send_empty_response(message[:id])
return
end

send_message(
Result.new(
id: message[:id],
response: Requests::PrepareRename.new(document, params[:position]).perform,
),
)
end

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def text_document_references(message)
params = message[:params]
Expand Down
21 changes: 21 additions & 0 deletions test/expectations/prepare_rename/class_and_reference.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"params": {
"textDocument": {
"uri": "file:///fake"
},
"position": {
"line": 3,
"character": 2
}
},
"result": {
"start": {
"line": 3,
"character": 0
},
"end": {
"line": 3,
"character": 3
}
}
}
12 changes: 12 additions & 0 deletions test/expectations/prepare_rename/class_declaration.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"params": {
"textDocument": {
"uri": "file:///fake"
},
"position": {
"line": 1,
"character": 8
}
},
"result": null
}
22 changes: 22 additions & 0 deletions test/requests/prepare_rename_expectations_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# typed: true
# frozen_string_literal: true

require "test_helper"
require_relative "support/expectations_test_runner"

class PrepareRenameExpectationsTest < ExpectationsTestRunner
expectations_tests RubyLsp::Requests::PrepareRename, "prepare_rename"

def run_expectations(source)
position = @__params&.any? ? @__params[:position] : default_position
uri = URI("file://#{@_path}")
document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri)
RubyLsp::Requests::PrepareRename.new(document, position).perform
end

private

def default_position
{ line: 0, character: 0 }
end
end