Skip to content

Commit

Permalink
Fix anonymous class doc_namespace to use named superclass
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Dec 29, 2023
1 parent c315b80 commit d86f41c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/repl_type_completor/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,38 @@ def method_doc(type, name)
type = type.types.find { _1.all_methods.include? name.to_sym }
case type
when Types::SingletonType
"#{Types.class_name_of(type.module_or_class)}.#{name}"
"#{doc_class_name(type.module_or_class)}.#{name}"
when Types::InstanceType
"#{Types.class_name_of(type.klass)}##{name}"
"#{doc_class_name(type.klass)}##{name}"
end
end

def call_or_const_doc(type, name)
if name =~ /\A[A-Z]/
type = type.types.grep(Types::SingletonType).find { _1.module_or_class.const_defined?(name) }
type.module_or_class == Object ? name : "#{Types.class_name_of(type.module_or_class)}::#{name}" if type
type.module_or_class == Object ? name : "#{doc_class_name(type.module_or_class)}::#{name}" if type
else
method_doc(type, name)
end
end

def doc_class_name(module_or_class)
return Methods::MODULE_NAME_METHOD.bind_call module_or_class unless module_or_class.is_a?(Class)
while true
name = Methods::MODULE_NAME_METHOD.bind_call module_or_class
return name if name
module_or_class = module_or_class.superclass
end
end

def value_doc(type)
return unless type
type.types.each do |t|
case t
when Types::SingletonType
return Types.class_name_of(t.module_or_class)
return doc_class_name(t.module_or_class)
when Types::InstanceType
return Types.class_name_of(t.klass)
return doc_class_name(t.klass)
end
end
nil
Expand Down
6 changes: 6 additions & 0 deletions test/repl_type_completor/test_repl_type_completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ def bo.foo; end
end
end

def test_anonymous_class
bind = eval('s = Class.new(Struct.new).new; binding')
assert_completion('s.', binding: bind, include: 'each_pair')
assert_doc_namespace('s.each_pair', 'Struct#each_pair', binding: bind)
end

DEPRECATED_CONST = 1
deprecate_constant :DEPRECATED_CONST
def test_deprecated_const_without_warning
Expand Down

0 comments on commit d86f41c

Please sign in to comment.