Skip to content

Commit 8dfcb85

Browse files
committed
Move locate_first_within_range to RubyDocument
1 parent ac928e0 commit 8dfcb85

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

lib/ruby_lsp/document.rb

-34
Original file line numberDiff line numberDiff line change
@@ -213,40 +213,6 @@ def locate(node, char_position, node_types: [])
213213
NodeContext.new(closest, parent, nesting_nodes, call_node)
214214
end
215215

216-
sig do
217-
params(
218-
range: T::Hash[Symbol, T.untyped],
219-
node_types: T::Array[T.class_of(Prism::Node)],
220-
).returns(T.nilable(Prism::Node))
221-
end
222-
def locate_first_within_range(range, node_types: [])
223-
scanner = create_scanner
224-
start_position = scanner.find_char_position(range[:start])
225-
end_position = scanner.find_char_position(range[:end])
226-
desired_range = (start_position...end_position)
227-
queue = T.let(@parse_result.value.child_nodes.compact, T::Array[T.nilable(Prism::Node)])
228-
229-
until queue.empty?
230-
candidate = queue.shift
231-
232-
# Skip nil child nodes
233-
next if candidate.nil?
234-
235-
# Add the next child_nodes to the queue to be processed. The order here is important! We want to move in the
236-
# same order as the visiting mechanism, which means searching the child nodes before moving on to the next
237-
# sibling
238-
T.unsafe(queue).unshift(*candidate.child_nodes)
239-
240-
# Skip if the current node doesn't cover the desired position
241-
loc = candidate.location
242-
243-
if desired_range.cover?(loc.start_offset...loc.end_offset) &&
244-
(node_types.empty? || node_types.any? { |type| candidate.class == type })
245-
return candidate
246-
end
247-
end
248-
end
249-
250216
class Scanner
251217
extend T::Sig
252218

lib/ruby_lsp/requests/code_action_resolve.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Error < ::T::Enum
3838
end
3939
end
4040

41-
sig { params(document: Document, code_action: T::Hash[Symbol, T.untyped]).void }
41+
sig { params(document: RubyDocument, code_action: T::Hash[Symbol, T.untyped]).void }
4242
def initialize(document, code_action)
4343
super()
4444
@document = document

lib/ruby_lsp/ruby_document.rb

+34
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,39 @@ def sorbet_level
5050
SorbetLevel::None
5151
end
5252
end
53+
54+
sig do
55+
params(
56+
range: T::Hash[Symbol, T.untyped],
57+
node_types: T::Array[T.class_of(Prism::Node)],
58+
).returns(T.nilable(Prism::Node))
59+
end
60+
def locate_first_within_range(range, node_types: [])
61+
scanner = create_scanner
62+
start_position = scanner.find_char_position(range[:start])
63+
end_position = scanner.find_char_position(range[:end])
64+
desired_range = (start_position...end_position)
65+
queue = T.let(@parse_result.value.child_nodes.compact, T::Array[T.nilable(Prism::Node)])
66+
67+
until queue.empty?
68+
candidate = queue.shift
69+
70+
# Skip nil child nodes
71+
next if candidate.nil?
72+
73+
# Add the next child_nodes to the queue to be processed. The order here is important! We want to move in the
74+
# same order as the visiting mechanism, which means searching the child nodes before moving on to the next
75+
# sibling
76+
T.unsafe(queue).unshift(*candidate.child_nodes)
77+
78+
# Skip if the current node doesn't cover the desired position
79+
loc = candidate.location
80+
81+
if desired_range.cover?(loc.start_offset...loc.end_offset) &&
82+
(node_types.empty? || node_types.any? { |type| candidate.class == type })
83+
return candidate
84+
end
85+
end
86+
end
5387
end
5488
end

lib/ruby_lsp/server.rb

+6
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ def code_action_resolve(message)
536536
params = message[:params]
537537
uri = URI(params.dig(:data, :uri))
538538
document = @store.get(uri)
539+
540+
unless document.is_a?(RubyDocument)
541+
send_message(Notification.window_show_error("Code actions are currently only available for Ruby documents"))
542+
raise Requests::CodeActionResolve::CodeActionError
543+
end
544+
539545
result = Requests::CodeActionResolve.new(document, params).perform
540546

541547
case result

0 commit comments

Comments
 (0)