Skip to content

Commit c8965fa

Browse files
committed
Use parse_lex instead of parse for Ruby and ERB documents
1 parent f596f38 commit c8965fa

24 files changed

+71
-59
lines changed

lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DeclarationListener
99
#: Array[String]
1010
attr_reader :indexing_errors
1111

12-
#: (Index index, Prism::Dispatcher dispatcher, Prism::ParseResult parse_result, URI::Generic uri, ?collect_comments: bool) -> void
12+
#: (Index index, Prism::Dispatcher dispatcher, Prism::ParseLexResult | Prism::ParseResult parse_result, URI::Generic uri, ?collect_comments: bool) -> void
1313
def initialize(index, dispatcher, parse_result, uri, collect_comments: false)
1414
@index = index
1515
@uri = uri

lib/ruby_lsp/erb_document.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RubyLsp
55
class ERBDocument < Document
66
extend T::Generic
77

8-
ParseResultType = type_member { { fixed: Prism::ParseResult } }
8+
ParseResultType = type_member { { fixed: Prism::ParseLexResult } }
99

1010
#: String
1111
attr_reader :host_language_source
@@ -34,11 +34,16 @@ def parse!
3434
@host_language_source = scanner.host_language
3535
# Use partial script to avoid syntax errors in ERB files where keywords may be used without the full context in
3636
# which they will be evaluated
37-
@parse_result = Prism.parse(scanner.ruby, partial_script: true)
37+
@parse_result = Prism.parse_lex(scanner.ruby, partial_script: true)
3838
@code_units_cache = @parse_result.code_units_cache(@encoding)
3939
true
4040
end
4141

42+
#: -> Prism::ProgramNode
43+
def ast
44+
@parse_result.value.first
45+
end
46+
4247
# @override
4348
#: -> bool
4449
def syntax_error?
@@ -56,7 +61,7 @@ def locate_node(position, node_types: [])
5661
char_position, _ = find_index_by_position(position)
5762

5863
RubyDocument.locate(
59-
@parse_result.value,
64+
ast,
6065
char_position,
6166
code_units_cache: @code_units_cache,
6267
node_types: node_types,

lib/ruby_lsp/requests/code_action_resolve.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def refactor_variable
101101

102102
# Find the closest statements node, so that we place the refactor in a valid position
103103
node_context = RubyDocument
104-
.locate(@document.parse_result.value,
104+
.locate(@document.ast,
105105
start_index,
106106
node_types: [
107107
Prism::StatementsNode,
@@ -199,7 +199,7 @@ def refactor_method
199199

200200
# Find the closest method declaration node, so that we place the refactor in a valid position
201201
node_context = RubyDocument.locate(
202-
@document.parse_result.value,
202+
@document.ast,
203203
start_index,
204204
node_types: [Prism::DefNode],
205205
code_units_cache: @document.code_units_cache,

lib/ruby_lsp/requests/completion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def initialize(document, global_state, params, sorbet_level, dispatcher)
3333
delegate_request_if_needed!(global_state, document, char_position)
3434

3535
node_context = RubyDocument.locate(
36-
document.parse_result.value,
36+
document.ast,
3737
char_position,
3838
node_types: [
3939
Prism::CallNode,

lib/ruby_lsp/requests/definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(document, global_state, position, dispatcher, sorbet_level)
2323
delegate_request_if_needed!(global_state, document, char_position)
2424

2525
node_context = RubyDocument.locate(
26-
document.parse_result.value,
26+
document.ast,
2727
char_position,
2828
node_types: [
2929
Prism::CallNode,

lib/ruby_lsp/requests/discover_tests.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def perform
4343
addon.create_discover_tests_listener(@response_builder, @dispatcher, @document.uri)
4444
end
4545

46-
@dispatcher.visit(@document.parse_result.value)
46+
@dispatcher.visit(@document.ast)
4747
else
4848
@global_state.synchronize do
4949
RubyIndexer::DeclarationListener.new(
@@ -64,7 +64,7 @@ def perform
6464
# Dispatch the events both for indexing the test file and discovering the tests. The order here is
6565
# important because we need the index to be aware of the existing classes/modules/methods before the test
6666
# listeners can do their work
67-
@dispatcher.visit(@document.parse_result.value)
67+
@dispatcher.visit(@document.ast)
6868
end
6969
end
7070

lib/ruby_lsp/requests/document_highlight.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(global_state, document, position, dispatcher)
2020
delegate_request_if_needed!(global_state, document, char_position)
2121

2222
node_context = RubyDocument.locate(
23-
document.parse_result.value,
23+
document.ast,
2424
char_position,
2525
code_units_cache: document.code_units_cache,
2626
)

lib/ruby_lsp/requests/hover.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def initialize(document, global_state, position, dispatcher, sorbet_level)
2727
delegate_request_if_needed!(global_state, document, char_position)
2828

2929
node_context = RubyDocument.locate(
30-
document.parse_result.value,
30+
document.ast,
3131
char_position,
3232
node_types: Listeners::Hover::ALLOWED_TARGETS,
3333
code_units_cache: document.code_units_cache,

lib/ruby_lsp/requests/prepare_rename.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def perform
2222
char_position, _ = @document.find_index_by_position(@position)
2323

2424
node_context = RubyDocument.locate(
25-
@document.parse_result.value,
25+
@document.ast,
2626
char_position,
2727
node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode],
2828
code_units_cache: @document.code_units_cache,

lib/ruby_lsp/requests/references.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def perform
2626
char_position, _ = @document.find_index_by_position(position)
2727

2828
node_context = RubyDocument.locate(
29-
@document.parse_result.value,
29+
@document.ast,
3030
char_position,
3131
node_types: [
3232
Prism::ConstantReadNode,

lib/ruby_lsp/requests/rename.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def perform
3434
char_position, _ = @document.find_index_by_position(@position)
3535

3636
node_context = RubyDocument.locate(
37-
@document.parse_result.value,
37+
@document.ast,
3838
char_position,
3939
node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode],
4040
code_units_cache: @document.code_units_cache,
@@ -136,25 +136,27 @@ def collect_text_edits(target, name)
136136
next if @store.key?(uri)
137137

138138
parse_result = Prism.parse_file(path)
139-
edits = collect_changes(target, parse_result, name, uri)
139+
edits = collect_changes(target, parse_result.value, name, uri)
140140
changes[uri.to_s] = edits unless edits.empty?
141141
rescue Errno::EISDIR, Errno::ENOENT
142142
# If `path` is a directory, just ignore it and continue. If the file doesn't exist, then we also ignore it.
143143
end
144144

145145
@store.each do |uri, document|
146-
edits = collect_changes(target, document.parse_result, name, document.uri)
146+
next unless document.is_a?(RubyDocument) || document.is_a?(ERBDocument)
147+
148+
edits = collect_changes(target, document.ast, name, document.uri)
147149
changes[uri] = edits unless edits.empty?
148150
end
149151

150152
changes
151153
end
152154

153-
#: (RubyIndexer::ReferenceFinder::Target target, Prism::ParseResult parse_result, String name, URI::Generic uri) -> Array[Interface::TextEdit]
154-
def collect_changes(target, parse_result, name, uri)
155+
#: (RubyIndexer::ReferenceFinder::Target target, Prism::Node ast, String name, URI::Generic uri) -> Array[Interface::TextEdit]
156+
def collect_changes(target, ast, name, uri)
155157
dispatcher = Prism::Dispatcher.new
156158
finder = RubyIndexer::ReferenceFinder.new(target, @global_state.index, dispatcher, uri)
157-
dispatcher.visit(parse_result.value)
159+
dispatcher.visit(ast)
158160

159161
finder.references.map do |reference|
160162
adjust_reference_for_edit(name, reference)

lib/ruby_lsp/requests/selection_ranges.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(document)
2525
#: -> (Array[Support::SelectionRange] & Object)
2626
def perform
2727
# [node, parent]
28-
queue = [[@document.parse_result.value, nil]]
28+
queue = [[@document.ast, nil]]
2929

3030
until queue.empty?
3131
node, parent = queue.shift

lib/ruby_lsp/requests/show_syntax_tree.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(document, range)
1212
super()
1313
@document = document
1414
@range = range
15-
@tree = document.parse_result.value #: Prism::ProgramNode
15+
@tree = document.ast #: Prism::ProgramNode
1616
end
1717

1818
# @override

lib/ruby_lsp/requests/signature_help.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def initialize(document, global_state, position, context, dispatcher, sorbet_lev
2727
delegate_request_if_needed!(global_state, document, char_position)
2828

2929
node_context = RubyDocument.locate(
30-
document.parse_result.value,
30+
document.ast,
3131
char_position,
3232
node_types: [Prism::CallNode],
3333
code_units_cache: document.code_units_cache,

lib/ruby_lsp/ruby_document.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RubyLsp
55
class RubyDocument < Document
66
extend T::Generic
77

8-
ParseResultType = type_member { { fixed: Prism::ParseResult } }
8+
ParseResultType = type_member { { fixed: Prism::ParseLexResult } }
99

1010
METHODS_THAT_CHANGE_DECLARATIONS = [
1111
:private_constant,
@@ -142,11 +142,16 @@ def parse!
142142
return false unless @needs_parsing
143143

144144
@needs_parsing = false
145-
@parse_result = Prism.parse(@source)
145+
@parse_result = Prism.parse_lex(@source)
146146
@code_units_cache = @parse_result.code_units_cache(@encoding)
147147
true
148148
end
149149

150+
#: -> Prism::ProgramNode
151+
def ast
152+
@parse_result.value.first
153+
end
154+
150155
# @override
151156
#: -> bool
152157
def syntax_error?
@@ -184,7 +189,7 @@ def locate_first_within_range(range, node_types: [])
184189
start_position, end_position = find_index_by_position(range[:start], range[:end])
185190

186191
desired_range = (start_position...end_position)
187-
queue = @parse_result.value.child_nodes.compact #: Array[Prism::Node?]
192+
queue = ast.child_nodes.compact #: Array[Prism::Node?]
188193

189194
until queue.empty?
190195
candidate = queue.shift
@@ -212,7 +217,7 @@ def locate_node(position, node_types: [])
212217
char_position, _ = find_index_by_position(position)
213218

214219
RubyDocument.locate(
215-
@parse_result.value,
220+
ast,
216221
char_position,
217222
code_units_cache: @code_units_cache,
218223
node_types: node_types,

lib/ruby_lsp/server.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,11 @@ def run_combined_requests(message)
499499
@global_state.index.handle_change(uri) do |index|
500500
index.delete(uri, skip_require_paths_tree: true)
501501
RubyIndexer::DeclarationListener.new(index, dispatcher, parse_result, uri, collect_comments: true)
502-
dispatcher.dispatch(parse_result.value)
502+
dispatcher.dispatch(document.ast)
503503
end
504504
end
505505
else
506-
dispatcher.dispatch(parse_result.value)
506+
dispatcher.dispatch(document.ast)
507507
end
508508

509509
# Store all responses retrieve in this round of visits in the cache and then return the response for the request
@@ -538,7 +538,7 @@ def text_document_semantic_tokens_full(message)
538538

539539
dispatcher = Prism::Dispatcher.new
540540
semantic_highlighting = Requests::SemanticHighlighting.new(@global_state, dispatcher, document, nil)
541-
dispatcher.visit(document.parse_result.value)
541+
dispatcher.visit(document.ast)
542542

543543
send_message(Result.new(id: message[:id], response: semantic_highlighting.perform))
544544
end
@@ -564,7 +564,7 @@ def text_document_semantic_tokens_delta(message)
564564
document,
565565
message.dig(:params, :previousResultId),
566566
)
567-
dispatcher.visit(document.parse_result.value)
567+
dispatcher.visit(document.ast)
568568
send_message(Result.new(id: message[:id], response: request.perform))
569569
end
570570

@@ -593,7 +593,7 @@ def text_document_semantic_tokens_range(message)
593593
nil,
594594
range: range.dig(:start, :line)..range.dig(:end, :line),
595595
)
596-
dispatcher.visit(document.parse_result.value)
596+
dispatcher.visit(document.ast)
597597
send_message(Result.new(id: message[:id], response: request.perform))
598598
end
599599

@@ -681,7 +681,7 @@ def text_document_document_highlight(message)
681681
end
682682

683683
request = Requests::DocumentHighlight.new(@global_state, document, params[:position], dispatcher)
684-
dispatcher.dispatch(document.parse_result.value)
684+
dispatcher.dispatch(document.ast)
685685
send_message(Result.new(id: message[:id], response: request.perform))
686686
end
687687

@@ -824,7 +824,7 @@ def text_document_inlay_hint(message)
824824
end
825825

826826
request = Requests::InlayHints.new(document, hints_configurations, dispatcher)
827-
dispatcher.visit(document.parse_result.value)
827+
dispatcher.visit(document.ast)
828828
result = request.perform
829829
document.cache_set("textDocument/inlayHint", result)
830830

test/requests/code_lens_expectations_test.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def run_expectations(source)
1414
dispatcher = Prism::Dispatcher.new
1515
stub_test_library("minitest")
1616
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
17-
dispatcher.dispatch(document.parse_result.value)
17+
dispatcher.dispatch(document.ast)
1818
listener.perform
1919
end
2020

@@ -31,7 +31,7 @@ def test_bar; end
3131

3232
dispatcher = Prism::Dispatcher.new
3333
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
34-
dispatcher.dispatch(document.parse_result.value)
34+
dispatcher.dispatch(document.ast)
3535
response = listener.perform
3636

3737
assert_equal(6, response.size)
@@ -63,7 +63,7 @@ class FooTest < MiniTest::Spec
6363

6464
dispatcher = Prism::Dispatcher.new
6565
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
66-
dispatcher.dispatch(document.parse_result.value)
66+
dispatcher.dispatch(document.ast)
6767
response = listener.perform
6868

6969
assert_equal(9, response.size)
@@ -98,7 +98,7 @@ def test_command_generation_for_minitest_spec_handles_specify_alias_for_it
9898

9999
dispatcher = Prism::Dispatcher.new
100100
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
101-
dispatcher.dispatch(document.parse_result.value)
101+
dispatcher.dispatch(document.ast)
102102
response = listener.perform
103103

104104
# 3 for the describe, 3 for the specify
@@ -118,7 +118,7 @@ def test_bar; end
118118

119119
dispatcher = Prism::Dispatcher.new
120120
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
121-
dispatcher.dispatch(document.parse_result.value)
121+
dispatcher.dispatch(document.ast)
122122
response = listener.perform
123123

124124
assert_equal(6, response.size)
@@ -145,7 +145,7 @@ def test_bar; end
145145
dispatcher = Prism::Dispatcher.new
146146
stub_test_library("unknown")
147147
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
148-
dispatcher.dispatch(document.parse_result.value)
148+
dispatcher.dispatch(document.ast)
149149
response = listener.perform
150150

151151
assert_empty(response)
@@ -164,7 +164,7 @@ def test_bar; end
164164
dispatcher = Prism::Dispatcher.new
165165
stub_test_library("rspec")
166166
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
167-
dispatcher.dispatch(document.parse_result.value)
167+
dispatcher.dispatch(document.ast)
168168
response = listener.perform
169169

170170
assert_empty(response)
@@ -183,7 +183,7 @@ def test_bar; end
183183
dispatcher = Prism::Dispatcher.new
184184
stub_test_library("minitest")
185185
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
186-
dispatcher.dispatch(document.parse_result.value)
186+
dispatcher.dispatch(document.ast)
187187
response = listener.perform
188188

189189
assert_empty(response)
@@ -202,7 +202,7 @@ def test_no_code_lens_for_unsaved_specs
202202
dispatcher = Prism::Dispatcher.new
203203
stub_test_library("minitest")
204204
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
205-
dispatcher.dispatch(document.parse_result.value)
205+
dispatcher.dispatch(document.ast)
206206
response = listener.perform
207207

208208
assert_empty(response)
@@ -253,7 +253,7 @@ def test_baz; end
253253

254254
dispatcher = Prism::Dispatcher.new
255255
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
256-
dispatcher.dispatch(document.parse_result.value)
256+
dispatcher.dispatch(document.ast)
257257
response = listener.perform
258258

259259
assert_equal(6, response.size)

0 commit comments

Comments
 (0)