Skip to content

Commit 0303145

Browse files
committed
The new parser no longer needs to do shallow parsing.
1 parent 5cfa5c1 commit 0303145

4 files changed

Lines changed: 9 additions & 31 deletions

File tree

lib/ruby_language_server/code_file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def update_text(new_text)
108108
update(text: new_text, refresh_root_scope: true)
109109
end
110110

111-
def refresh_scopes_if_needed(shallow: false)
111+
def refresh_scopes_if_needed
112112
return unless refresh_root_scope
113113

114114
RubyLanguageServer.logger.debug("Asking about root_scope for #{uri}")
@@ -117,7 +117,7 @@ def refresh_scopes_if_needed(shallow: false)
117117
self.class.transaction do
118118
scopes.clear
119119
variables.clear
120-
new_root = ScopeParser.new(text, shallow).root_scope
120+
new_root = ScopeParser.new(text).root_scope
121121
RubyLanguageServer.logger.debug("new_root.children #{new_root.children.as_json}") if new_root&.children
122122
raise ActiveRecord::Rollback if new_root.nil? || new_root.children.blank?
123123

lib/ruby_language_server/project_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def scan_all_project_files
115115
begin
116116
ActiveRecord::Base.connection_pool.with_connection do |_connection|
117117
update_document_content(host_uri, text)
118-
code_file_for_uri(host_uri).refresh_scopes_if_needed(shallow: false)
118+
code_file_for_uri(host_uri).refresh_scopes_if_needed
119119
end
120120
rescue StandardError => e
121121
RubyLanguageServer.logger.warn("Error updating: #{e}\n#{e.backtrace * "\n"}")

lib/ruby_language_server/scope_parser.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ class PrismProcessor < Prism::Visitor
1818

1919
attr_reader :current_scope, :lines
2020

21-
def initialize(lines = 1, shallow = false)
21+
def initialize(lines = 1)
2222
@lines = lines
23-
@shallow = shallow
2423
@root_scope = nil
2524
@current_scope = nil
2625
@block_names = {}
@@ -87,12 +86,7 @@ def visit_def_node(node)
8786
# Process parameters (adds them as variables in scope)
8887
visit_parameters(node.parameters) if node.parameters
8988

90-
# Process body only if not shallow
91-
if @shallow
92-
# Skip body processing
93-
else
94-
super
95-
end
89+
super
9690

9791
pop_scope
9892
scope
@@ -418,7 +412,6 @@ def extract_command_rest(node)
418412
end
419413

420414
def add_variable(name, line, column, scope = @current_scope)
421-
return if @shallow
422415
return if scope.nil?
423416

424417
scope.variables.where(name:).first_or_create!(
@@ -467,18 +460,18 @@ def pop_scope
467460
class ScopeParser
468461
attr_reader :root_scope
469462

470-
def initialize(text, shallow = false)
463+
def initialize(text)
471464
text ||= '' # empty is the same as nil - but it doesn't crash
472465
begin
473466
result = Prism.parse(text)
474-
processor = PrismProcessor.new(text.split("\n").length, shallow)
467+
processor = PrismProcessor.new(text.split("\n").length)
475468
processor.root_scope # Initialize root scope
476469
result.value.accept(processor)
477470
@root_scope = processor.root_scope
478471
rescue StandardError => e
479472
RubyLanguageServer.logger.error("Exception in prism parsing: #{e} for text: #{text}")
480473
# Create an empty root scope on error
481-
processor = PrismProcessor.new(text.split("\n").length, shallow)
474+
processor = PrismProcessor.new(text.split("\n").length)
482475
@root_scope = processor.root_scope
483476
end
484477
end

spec/lib/ruby_language_server/scope_parser_spec.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,12 @@
99
end
1010

1111
describe 'Small file' do
12-
describe 'shallow parsing' do
13-
before do
14-
@parser = RubyLanguageServer::ScopeParser.new(@code_file_lines, true)
15-
end
16-
17-
# Life is unfair. `private` does not start a block - it just sets a flag. So I may circle back to this.
18-
# it 'does not find private methods' do
19-
# bar = @parser.root_scope.children.first.children.detect { |child| child.full_name == 'Foo::Bar' }
20-
# assert_equal(%w[baz], bar.children.map(&:name).sort)
21-
# end
22-
it 'does not add any variables at any scope' do
23-
assert_equal(RubyLanguageServer::ScopeData::Variable.all.count, 0)
24-
end
25-
end
26-
2712
describe 'normal parsing' do
2813
before do
2914
@parser = RubyLanguageServer::ScopeParser.new(@code_file_lines)
3015
end
3116

32-
it 'records all the variables (as opposed to shallow)' do
17+
it 'records all the variables' do
3318
assert_equal(RubyLanguageServer::ScopeData::Variable.order(:name).pluck(:name), [
3419
"@biz", "@bottom", "@niz", "bing", "bogus", "ning", "paf", "par", "par", "pax", "zang", "zing"
3520
])

0 commit comments

Comments
 (0)