Skip to content

Commit 0611106

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

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
@@ -11,7 +11,7 @@ class DeclarationListener
1111
#: Array[String]
1212
attr_reader :indexing_errors
1313

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

lib/ruby_lsp/erb_document.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ERBDocument < Document
66
extend T::Sig
77
extend T::Generic
88

9-
ParseResultType = type_member { { fixed: Prism::ParseResult } }
9+
ParseResultType = type_member { { fixed: Prism::ParseLexResult } }
1010

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

45+
#: -> Prism::ProgramNode
46+
def ast
47+
@parse_result.value.first
48+
end
49+
4550
# @override
4651
#: -> bool
4752
def syntax_error?
@@ -59,7 +64,7 @@ def locate_node(position, node_types: [])
5964
char_position, _ = find_index_by_position(position)
6065

6166
RubyDocument.locate(
62-
@parse_result.value,
67+
ast,
6368
char_position,
6469
code_units_cache: @code_units_cache,
6570
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
@@ -102,7 +102,7 @@ def refactor_variable
102102

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

201201
# Find the closest method declaration node, so that we place the refactor in a valid position
202202
node_context = RubyDocument.locate(
203-
@document.parse_result.value,
203+
@document.ast,
204204
start_index,
205205
node_types: [Prism::DefNode],
206206
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
@@ -37,7 +37,7 @@ def initialize(document, global_state, params, sorbet_level, dispatcher)
3737
delegate_request_if_needed!(global_state, document, char_position)
3838

3939
node_context = RubyDocument.locate(
40-
document.parse_result.value,
40+
document.ast,
4141
char_position,
4242
node_types: [
4343
Prism::CallNode,

lib/ruby_lsp/requests/definition.rb

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

2727
node_context = RubyDocument.locate(
28-
document.parse_result.value,
28+
document.ast,
2929
char_position,
3030
node_types: [
3131
Prism::CallNode,

lib/ruby_lsp/requests/discover_tests.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def perform
3636
# in the index first and then discover the tests, all in the same traversal.
3737
if @index.entries_for(uri.to_s)
3838
Listeners::TestStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
39-
@dispatcher.visit(@document.parse_result.value)
39+
@dispatcher.visit(@document.ast)
4040
else
4141
@global_state.synchronize do
4242
RubyIndexer::DeclarationListener.new(
@@ -52,7 +52,7 @@ def perform
5252
# Dispatch the events both for indexing the test file and discovering the tests. The order here is
5353
# important because we need the index to be aware of the existing classes/modules/methods before the test
5454
# listeners can do their work
55-
@dispatcher.visit(@document.parse_result.value)
55+
@dispatcher.visit(@document.ast)
5656
end
5757
end
5858

lib/ruby_lsp/requests/document_highlight.rb

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

2424
node_context = RubyDocument.locate(
25-
document.parse_result.value,
25+
document.ast,
2626
char_position,
2727
code_units_cache: document.code_units_cache,
2828
)

lib/ruby_lsp/requests/hover.rb

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

3232
node_context = RubyDocument.locate(
33-
document.parse_result.value,
33+
document.ast,
3434
char_position,
3535
node_types: Listeners::Hover::ALLOWED_TARGETS,
3636
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
@@ -23,7 +23,7 @@ def perform
2323
char_position, _ = @document.find_index_by_position(@position)
2424

2525
node_context = RubyDocument.locate(
26-
@document.parse_result.value,
26+
@document.ast,
2727
char_position,
2828
node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode],
2929
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
@@ -27,7 +27,7 @@ def perform
2727
char_position, _ = @document.find_index_by_position(position)
2828

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

lib/ruby_lsp/requests/rename.rb

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

3939
node_context = RubyDocument.locate(
40-
@document.parse_result.value,
40+
@document.ast,
4141
char_position,
4242
node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode],
4343
code_units_cache: @document.code_units_cache,
@@ -139,25 +139,27 @@ def collect_text_edits(target, name)
139139
next if @store.key?(uri)
140140

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

148148
@store.each do |uri, document|
149-
edits = collect_changes(target, document.parse_result, name, document.uri)
149+
next unless document.is_a?(RubyDocument) || document.is_a?(ERBDocument)
150+
151+
edits = collect_changes(target, document.ast, name, document.uri)
150152
changes[uri] = edits unless edits.empty?
151153
end
152154

153155
changes
154156
end
155157

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

162164
finder.references.map do |reference|
163165
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
@@ -26,7 +26,7 @@ def initialize(document)
2626
#: -> (Array[Support::SelectionRange] & Object)
2727
def perform
2828
# [node, parent]
29-
queue = [[@document.parse_result.value, nil]]
29+
queue = [[@document.ast, nil]]
3030

3131
until queue.empty?
3232
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
@@ -14,7 +14,7 @@ def initialize(document, range)
1414
super()
1515
@document = document
1616
@range = range
17-
@tree = T.let(document.parse_result.value, Prism::ProgramNode)
17+
@tree = T.let(document.ast, Prism::ProgramNode)
1818
end
1919

2020
# @override

lib/ruby_lsp/requests/signature_help.rb

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

3333
node_context = RubyDocument.locate(
34-
document.parse_result.value,
34+
document.ast,
3535
char_position,
3636
node_types: [Prism::CallNode],
3737
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
@@ -6,7 +6,7 @@ class RubyDocument < Document
66
extend T::Sig
77
extend T::Generic
88

9-
ParseResultType = type_member { { fixed: Prism::ParseResult } }
9+
ParseResultType = type_member { { fixed: Prism::ParseLexResult } }
1010

1111
METHODS_THAT_CHANGE_DECLARATIONS = [
1212
:private_constant,
@@ -158,11 +158,16 @@ def parse!
158158
return false unless @needs_parsing
159159

160160
@needs_parsing = false
161-
@parse_result = Prism.parse(@source)
161+
@parse_result = Prism.parse_lex(@source)
162162
@code_units_cache = @parse_result.code_units_cache(@encoding)
163163
true
164164
end
165165

166+
#: -> Prism::ProgramNode
167+
def ast
168+
@parse_result.value.first
169+
end
170+
166171
# @override
167172
#: -> bool
168173
def syntax_error?
@@ -200,7 +205,7 @@ def locate_first_within_range(range, node_types: [])
200205
start_position, end_position = find_index_by_position(range[:start], range[:end])
201206

202207
desired_range = (start_position...end_position)
203-
queue = T.let(@parse_result.value.child_nodes.compact, T::Array[T.nilable(Prism::Node)])
208+
queue = T.let(ast.child_nodes.compact, T::Array[T.nilable(Prism::Node)])
204209

205210
until queue.empty?
206211
candidate = queue.shift
@@ -228,7 +233,7 @@ def locate_node(position, node_types: [])
228233
char_position, _ = find_index_by_position(position)
229234

230235
RubyDocument.locate(
231-
@parse_result.value,
236+
ast,
232237
char_position,
233238
code_units_cache: @code_units_cache,
234239
node_types: node_types,

lib/ruby_lsp/server.rb

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

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

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

542542
send_message(Result.new(id: message[:id], response: semantic_highlighting.perform))
543543
end
@@ -563,7 +563,7 @@ def text_document_semantic_tokens_delta(message)
563563
document,
564564
message.dig(:params, :previousResultId),
565565
)
566-
dispatcher.visit(document.parse_result.value)
566+
dispatcher.visit(document.ast)
567567
send_message(Result.new(id: message[:id], response: request.perform))
568568
end
569569

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

@@ -680,7 +680,7 @@ def text_document_document_highlight(message)
680680
end
681681

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

@@ -823,7 +823,7 @@ def text_document_inlay_hint(message)
823823
end
824824

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

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)
@@ -257,7 +257,7 @@ def test_baz; end
257257

258258
dispatcher = Prism::Dispatcher.new
259259
listener = RubyLsp::Requests::CodeLens.new(@global_state, uri, dispatcher)
260-
dispatcher.dispatch(document.parse_result.value)
260+
dispatcher.dispatch(document.ast)
261261
response = listener.perform
262262

263263
assert_equal(6, response.size)

0 commit comments

Comments
 (0)