Skip to content

Commit

Permalink
Support /method ?=/ in legacy parser
Browse files Browse the repository at this point in the history
  • Loading branch information
castwide committed Nov 22, 2021
1 parent 744f4c5 commit b52338a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
12 changes: 9 additions & 3 deletions lib/solargraph/parser/legacy/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ def returns_from node
end

def references source, name
if name.end_with?("=")
reg = /#{Regexp.escape name[0..-2]}\s*=/
extract_offset = ->(code, offset) { reg.match(code, offset).offset(0) }
else
extract_offset = ->(code, offset) { [soff = code.index(name, offset), soff + name.length] }
end
inner_node_references(name, source.node).map do |n|
offset = Position.to_offset(source.code, NodeMethods.get_node_start_position(n))
soff = source.code.index(name, offset)
eoff = soff + name.length
rng = Range.from_node(n)
offset = Position.to_offset(source.code, rng.start)
soff, eoff = extract_offset[source.code, offset]
Location.new(
source.filename,
Range.new(
Expand Down
30 changes: 13 additions & 17 deletions lib/solargraph/parser/legacy/node_processors/block_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@ class BlockNode < Parser::NodeProcessor::Base
include Legacy::NodeMethods

def process
if other_class_eval?
other_class = Solargraph::Pin::Namespace.new(
location = get_node_location(node)
parent = if other_class_eval?
Solargraph::Pin::Namespace.new(
location: location,
type: :class,
name: unpack_name(node.children[0].children[0])
)
make_block_in other_class.context
process_children region.update(closure: other_class)
else
make_block_in nil
process_children region.update(closure: pins.last)
region.closure
end
pins.push Solargraph::Pin::Block.new(
location: location,
closure: parent,
receiver: node.children[0],
comments: comments_for(node),
scope: region.scope || region.closure.context.scope
)
process_children region.update(closure: pins.last)
end

private
Expand All @@ -28,17 +35,6 @@ def other_class_eval?
node.children[0].children[1] == :class_eval &&
[:cbase, :const].include?(node.children[0].children[0]&.type)
end

def make_block_in context
pins.push Solargraph::Pin::Block.new(
location: get_node_location(node),
context: context,
closure: region.closure,
receiver: node.children[0],
comments: comments_for(node),
scope: region.scope || region.closure.context.scope
)
end
end
end
end
Expand Down

0 comments on commit b52338a

Please sign in to comment.