module Solargraph::ComplexType::TypeMethods
Methods for accessing type data available from both ComplexType and UniqueType.
@abstract This mixin relies on these -
instance variables: @name: String @subtypes: Array<ComplexType> @rooted: boolish methods: transform() all_params() rooted?() can_root_name?()
Constants
- PARAMETERS_TYPE_BY_STARTING_TAG
-
@type [Hash{String => Symbol}]
Attributes
@return [String]
@return [Symbol, nil]
@return [Array<ComplexType>]
Public Instance Methods
Source
# File lib/solargraph/complex_type/type_methods.rb, line 195 def == other return false unless self.class == other.class # @sg-ignore https://github.com/castwide/solargraph/pull/1114 tag == other.tag end
@param other [Object]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 68 def defined? !undefined? end
Source
# File lib/solargraph/complex_type/type_methods.rb, line 51 def duck_type? @duck_type ||= name.start_with?('#') end
@return [Boolean]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 222 def each_unique_type &block return enum_for(__method__) unless block_given? yield self end
@yieldparam [UniqueType] @return [Enumerator<UniqueType>]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 78 def erase_generics(generics_to_erase) transform do |type| if type.name == ComplexType::GENERIC_TAG_NAME if type.all_params.length == 1 && generics_to_erase.include?(type.all_params.first.to_s) ComplexType::UNDEFINED else type end else type end end end
@param generics_to_erase [Enumerable<String>] @return [self]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 108 def fixed_parameters? parameters_type == :fixed end
@return [Boolean]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 168 def generate_substring_from(&to_str) key_types_str = key_types.map(&to_str).join(', ') subtypes_str = subtypes.map(&to_str).join(', ') if key_types.none?(&:defined?) && subtypes.none?(&:defined?) '' elsif key_types.empty? && subtypes.empty? '' elsif hash_parameters? "{#{key_types_str} => #{subtypes_str}}" elsif fixed_parameters? "(#{subtypes_str})" else if name == 'Hash' "<#{key_types_str}, #{subtypes_str}>" else "<#{key_types_str}#{subtypes_str}>" end end end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 113 def hash_parameters? parameters_type == :hash end
@return [Boolean]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 46 def interface? name.start_with?('_') end
Source
# File lib/solargraph/complex_type/type_methods.rb, line 123 def key_types @key_types end
@return [Array<ComplexType>]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 103 def list_parameters? parameters_type == :list end
@return [Boolean]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 128 def namespace # if priority higher than ||=, old implements cause unnecessary check @namespace ||= lambda do return 'Object' if duck_type? return 'NilClass' if nil_type? return (name == 'Class' || name == 'Module') && !subtypes.empty? ? subtypes.first.name : name end.call end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 138 def namespace_type return ComplexType.parse('::Object') if duck_type? return ComplexType.parse('::NilClass') if nil_type? return subtypes.first if (name == 'Class' || name == 'Module') && !subtypes.empty? self end
@return [self]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 56 def nil_type? @nil_type ||= (name.casecmp('nil') == 0) end
@return [Boolean]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 206 def qualify api_map, context = '' transform do |t| next t if t.name == GENERIC_TAG_NAME next t if t.duck_type? || t.void? || t.undefined? recon = (t.rooted? ? '' : context) fqns = api_map.qualify(t.name, recon) if fqns.nil? next UniqueType::BOOLEAN if t.tag == 'Boolean' next UniqueType::UNDEFINED end t.recreate(new_name: fqns, make_rooted: true) end end
Generate a ComplexType that fully qualifies this type’s namespaces.
@param api_map [ApiMap] The ApiMap that performs qualification @param context [String] The namespace from which to resolve names @return [self, ComplexType, UniqueType] The generated ComplexType
Source
# File lib/solargraph/complex_type/type_methods.rb, line 152 def rooted_name return name unless @rooted && can_root_name? "::#{name}" end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 146 def rooted_namespace return namespace unless rooted? && can_root_name?(namespace) "::#{namespace}" end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 163 def rooted_substring @rooted_substring = generate_substring_from(&:rooted_tags) end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 42 def rooted_tag @rooted_tag ||= rooted_name + rooted_substring end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 189 def scope @scope ||= :instance if duck_type? || nil_type? @scope ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? :class : :instance end
@return [::Symbol] :class or :instance
Source
# File lib/solargraph/complex_type/type_methods.rb, line 158 def substring @substring ||= generate_substring_from(&:tags) end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 37 def tag @tag ||= "#{name}#{substring}" end
@return [String]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 60 def tuple? @tuple_type ||= (name == 'Tuple') || (name == 'Array' && subtypes.length >= 1 && fixed_parameters?) end
Source
# File lib/solargraph/complex_type/type_methods.rb, line 72 def undefined? name == 'undefined' end
Source
# File lib/solargraph/complex_type/type_methods.rb, line 118 def value_types @subtypes end
@return [Array<ComplexType>]
Source
# File lib/solargraph/complex_type/type_methods.rb, line 64 def void? name == 'void' end