Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ruby_language_server/code_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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?

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_language_server/project_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}")
Expand Down
17 changes: 5 additions & 12 deletions lib/ruby_language_server/scope_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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
Expand Down
24 changes: 5 additions & 19 deletions spec/lib/ruby_language_server/scope_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down