Skip to content

Commit

Permalink
Extract correct node from repaired source
Browse files Browse the repository at this point in the history
  • Loading branch information
castwide committed Feb 28, 2025
1 parent 28efa3f commit 16839ba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/solargraph/source/source_chainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def chain
elsif source.parsed? && source.repaired? && end_of_phrase == '.'
node, parent = source.tree_at(fixed_position.line, fixed_position.column)[0..2]
node = Parser.parse(fixed_phrase) if node.nil?
elsif source.repaired?
node = Parser.parse(fixed_phrase)
else
node, parent = source.tree_at(fixed_position.line, fixed_position.column)[0..2] unless source.error_ranges.any?{|r| r.nil? || r.include?(fixed_position)}
# Exception for positions that chain literal nodes in unsynchronized sources
Expand Down
20 changes: 20 additions & 0 deletions spec/source/source_chainer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,24 @@ class Inner2
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, api_map.source_map('test.rb').locals)
expect(type.tag).to eq('Array<String>')
end

it 'extracts correct node from repaired source' do
source = Solargraph::Source.load_string(%(
# @return [Array<String>]
def strings; end
strings.each do |str|
end
), 'test.rb')
updater = Solargraph::Source::Updater.new('test.rb', 1, [
Solargraph::Source::Change.new(Solargraph::Range.from_to(5, 10, 5, 10), 'if s')
])
updated = source.synchronize(updater)
api_map = Solargraph::ApiMap.new
api_map.map updated
chain = Solargraph::Source::SourceChainer.chain(updated, Solargraph::Position.new(5, 14))
expect(chain.node.type).to be(:send)
expect(chain.node.children[1]).to be(:s)
end
end

0 comments on commit 16839ba

Please sign in to comment.