diff --git a/lib/ruby_language_server/code_file.rb b/lib/ruby_language_server/code_file.rb index 44bc49b..1df5c0c 100644 --- a/lib/ruby_language_server/code_file.rb +++ b/lib/ruby_language_server/code_file.rb @@ -108,7 +108,7 @@ def update_text(new_text) update(text: new_text, refresh_root_scope: true) end - def refresh_scopes_if_needed(shallow: false) + def refresh_scopes_if_needed return unless refresh_root_scope RubyLanguageServer.logger.debug("Asking about root_scope for #{uri}") @@ -117,7 +117,7 @@ def refresh_scopes_if_needed(shallow: false) self.class.transaction do scopes.clear variables.clear - new_root = ScopeParser.new(text, shallow).root_scope + new_root = ScopeParser.new(text).root_scope RubyLanguageServer.logger.debug("new_root.children #{new_root.children.as_json}") if new_root&.children raise ActiveRecord::Rollback if new_root.nil? || new_root.children.blank? diff --git a/lib/ruby_language_server/project_manager.rb b/lib/ruby_language_server/project_manager.rb index 85b920a..6ed30eb 100644 --- a/lib/ruby_language_server/project_manager.rb +++ b/lib/ruby_language_server/project_manager.rb @@ -115,7 +115,7 @@ def scan_all_project_files begin ActiveRecord::Base.connection_pool.with_connection do |_connection| update_document_content(host_uri, text) - code_file_for_uri(host_uri).refresh_scopes_if_needed(shallow: false) + code_file_for_uri(host_uri).refresh_scopes_if_needed end rescue StandardError => e RubyLanguageServer.logger.warn("Error updating: #{e}\n#{e.backtrace * "\n"}") diff --git a/lib/ruby_language_server/scope_parser.rb b/lib/ruby_language_server/scope_parser.rb index 19342de..a2a441d 100644 --- a/lib/ruby_language_server/scope_parser.rb +++ b/lib/ruby_language_server/scope_parser.rb @@ -18,9 +18,8 @@ class PrismProcessor < Prism::Visitor attr_reader :current_scope, :lines - def initialize(lines = 1, shallow = false) + def initialize(lines = 1) @lines = lines - @shallow = shallow @root_scope = nil @current_scope = nil @block_names = {} @@ -87,12 +86,7 @@ def visit_def_node(node) # Process parameters (adds them as variables in scope) visit_parameters(node.parameters) if node.parameters - # Process body only if not shallow - if @shallow - # Skip body processing - else - super - end + super pop_scope scope @@ -418,7 +412,6 @@ def extract_command_rest(node) end def add_variable(name, line, column, scope = @current_scope) - return if @shallow return if scope.nil? scope.variables.where(name:).first_or_create!( @@ -467,18 +460,18 @@ def pop_scope class ScopeParser attr_reader :root_scope - def initialize(text, shallow = false) + def initialize(text) text ||= '' # empty is the same as nil - but it doesn't crash begin result = Prism.parse(text) - processor = PrismProcessor.new(text.split("\n").length, shallow) + processor = PrismProcessor.new(text.split("\n").length) processor.root_scope # Initialize root scope result.value.accept(processor) @root_scope = processor.root_scope rescue StandardError => e RubyLanguageServer.logger.error("Exception in prism parsing: #{e} for text: #{text}") # Create an empty root scope on error - processor = PrismProcessor.new(text.split("\n").length, shallow) + processor = PrismProcessor.new(text.split("\n").length) @root_scope = processor.root_scope end end diff --git a/spec/lib/ruby_language_server/scope_parser_spec.rb b/spec/lib/ruby_language_server/scope_parser_spec.rb index 700024f..1e74a76 100644 --- a/spec/lib/ruby_language_server/scope_parser_spec.rb +++ b/spec/lib/ruby_language_server/scope_parser_spec.rb @@ -9,30 +9,16 @@ end describe 'Small file' do - describe 'shallow parsing' do - before do - @parser = RubyLanguageServer::ScopeParser.new(@code_file_lines, true) - end - - # Life is unfair. `private` does not start a block - it just sets a flag. So I may circle back to this. - # it 'does not find private methods' do - # bar = @parser.root_scope.children.first.children.detect { |child| child.full_name == 'Foo::Bar' } - # assert_equal(%w[baz], bar.children.map(&:name).sort) - # end - it 'does not add any variables at any scope' do - assert_equal(RubyLanguageServer::ScopeData::Variable.all.count, 0) - end - end - describe 'normal parsing' do before do @parser = RubyLanguageServer::ScopeParser.new(@code_file_lines) end - it 'records all the variables (as opposed to shallow)' do - assert_equal(RubyLanguageServer::ScopeData::Variable.order(:name).pluck(:name), [ - "@biz", "@bottom", "@niz", "bing", "bogus", "ning", "paf", "par", "par", "pax", "zang", "zing" - ]) + it 'records all the variables' do + assert_equal( + RubyLanguageServer::ScopeData::Variable.order(:name).pluck(:name), + ["@biz", "@bottom", "@niz", "bing", "bogus", "ning", "paf", "par", "par", "pax", "zang", "zing"] + ) end describe 'class << self' do