Skip to content

Incorrect or inconsistent Pin::Block#location? #1109

@lekemula

Description

@lekemula

Hi,

I'm coming from lekemula/solargraph-rspec#28.

It looks like we have some inconsistency on what the location range for BlockNode should be:

Currently

 some_method_with_block do
 #^ - block start
 end
 #^ - block end

Expected

 some_method_with_block do
 #                      ^ - block start
 end
 #^ - block end

Reproduction

irb:

# frozen_string_literal: true

require 'solargraph'
code = <<~RUBY
some_method do
  puts 'hello'
end
RUBY

ast = Solargraph::Parser.parse(code)

puts Solargraph::Parser.node_range(ast).inspect
# includes the `some_method` line
# #<Solargraph::Range #<Solargraph::Position 0, 0> to #<Solargraph::Position 2, 3>>
source_map = Solargraph::SourceMap.load_string(code)
puts source_map.source.node_at(0, 0)
# does not include the block:
# (send nil :some_method)
puts source_map.source.node_at(0, 12)
# (block
#   (send nil :some_method)
#   (args)
#   (send nil :puts
#     (str "hello")))

This leads the Solargraph::SourceMap#locate_closure_pin to return the wrong closure when Go-to-definition/Hover.

Would it make sense to fix this here upstream instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions