Class: Jazzy::SourceDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/jazzy/source_declaration.rb,
lib/jazzy/source_declaration/type.rb,
lib/jazzy/source_declaration/access_control_level.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

SourceDocument

Defined Under Namespace

Classes: AccessControlLevel, Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#abstractObject

Returns the value of attribute abstract.



122
123
124
# File 'lib/jazzy/source_declaration.rb', line 122

def abstract
  @abstract
end

#access_control_levelObject

Returns the value of attribute access_control_level.



130
131
132
# File 'lib/jazzy/source_declaration.rb', line 130

def access_control_level
  @access_control_level
end

#asyncObject

Returns the value of attribute async.



141
142
143
# File 'lib/jazzy/source_declaration.rb', line 141

def async
  @async
end

#childrenObject

counterpart of parent_in_docs



44
45
46
# File 'lib/jazzy/source_declaration.rb', line 44

def children
  @children
end

#columnObject

Returns the value of attribute column.



114
115
116
# File 'lib/jazzy/source_declaration.rb', line 114

def column
  @column
end

#declarationObject

Returns the value of attribute declaration.



120
121
122
# File 'lib/jazzy/source_declaration.rb', line 120

def declaration
  @declaration
end

#default_impl_abstractObject

Returns the value of attribute default_impl_abstract.



123
124
125
# File 'lib/jazzy/source_declaration.rb', line 123

def default_impl_abstract
  @default_impl_abstract
end

#deprecatedObject

Returns the value of attribute deprecated.



135
136
137
# File 'lib/jazzy/source_declaration.rb', line 135

def deprecated
  @deprecated
end

#deprecation_messageObject

Returns the value of attribute deprecation_message.



136
137
138
# File 'lib/jazzy/source_declaration.rb', line 136

def deprecation_message
  @deprecation_message
end

#discussionObject

Returns the value of attribute discussion.



125
126
127
# File 'lib/jazzy/source_declaration.rb', line 125

def discussion
  @discussion
end

#end_lineObject

Returns the value of attribute end_line.



132
133
134
# File 'lib/jazzy/source_declaration.rb', line 132

def end_line
  @end_line
end

#fileObject

Returns the value of attribute file.



112
113
114
# File 'lib/jazzy/source_declaration.rb', line 112

def file
  @file
end

#from_protocol_extensionObject

Returns the value of attribute from_protocol_extension.



124
125
126
# File 'lib/jazzy/source_declaration.rb', line 124

def from_protocol_extension
  @from_protocol_extension
end

#generic_requirementsObject

Returns the value of attribute generic_requirements.



139
140
141
# File 'lib/jazzy/source_declaration.rb', line 139

def generic_requirements
  @generic_requirements
end

#inherited_typesObject

Returns the value of attribute inherited_types.



140
141
142
# File 'lib/jazzy/source_declaration.rb', line 140

def inherited_types
  @inherited_types
end

#lineObject

Returns the value of attribute line.



113
114
115
# File 'lib/jazzy/source_declaration.rb', line 113

def line
  @line
end

#markObject

Returns the value of attribute mark.



129
130
131
# File 'lib/jazzy/source_declaration.rb', line 129

def mark
  @mark
end

#modulenameObject

Returns the value of attribute modulename.



117
118
119
# File 'lib/jazzy/source_declaration.rb', line 117

def modulename
  @modulename
end

#nameObject

Returns the value of attribute name.



118
119
120
# File 'lib/jazzy/source_declaration.rb', line 118

def name
  @name
end

Returns the value of attribute nav_order.



133
134
135
# File 'lib/jazzy/source_declaration.rb', line 133

def nav_order
  @nav_order
end

#objc_nameObject

Returns the value of attribute objc_name.



119
120
121
# File 'lib/jazzy/source_declaration.rb', line 119

def objc_name
  @objc_name
end

#other_language_declarationObject

Returns the value of attribute other_language_declaration.



121
122
123
# File 'lib/jazzy/source_declaration.rb', line 121

def other_language_declaration
  @other_language_declaration
end

#parametersObject

Returns the value of attribute parameters.



127
128
129
# File 'lib/jazzy/source_declaration.rb', line 127

def parameters
  @parameters
end

#parent_in_codeObject

Element containing this declaration in the code



37
38
39
# File 'lib/jazzy/source_declaration.rb', line 37

def parent_in_code
  @parent_in_code
end

#parent_in_docsObject

Logical parent in the documentation. May differ from parent_in_code because of top-level categories and merged extensions.



41
42
43
# File 'lib/jazzy/source_declaration.rb', line 41

def parent_in_docs
  @parent_in_docs
end

#returnObject

Returns the value of attribute return.



126
127
128
# File 'lib/jazzy/source_declaration.rb', line 126

def return
  @return
end

#start_lineObject

Returns the value of attribute start_line.



131
132
133
# File 'lib/jazzy/source_declaration.rb', line 131

def start_line
  @start_line
end

#typeObject

kind of declaration (e.g. class, variable, function)



10
11
12
# File 'lib/jazzy/source_declaration.rb', line 10

def type
  @type
end

#type_usrObject

Returns the value of attribute type_usr.



116
117
118
# File 'lib/jazzy/source_declaration.rb', line 116

def type_usr
  @type_usr
end

#typenameObject

static type of declared element (e.g. String.Type -> ())



12
13
14
# File 'lib/jazzy/source_declaration.rb', line 12

def typename
  @typename
end

#unavailableObject

Returns the value of attribute unavailable.



137
138
139
# File 'lib/jazzy/source_declaration.rb', line 137

def unavailable
  @unavailable
end

#unavailable_messageObject

Returns the value of attribute unavailable_message.



138
139
140
# File 'lib/jazzy/source_declaration.rb', line 138

def unavailable_message
  @unavailable_message
end

#urlObject

Returns the value of attribute url.



128
129
130
# File 'lib/jazzy/source_declaration.rb', line 128

def url
  @url
end

#url_nameObject

Returns the value of attribute url_name.



134
135
136
# File 'lib/jazzy/source_declaration.rb', line 134

def url_name
  @url_name
end

#usrObject

Returns the value of attribute usr.



115
116
117
# File 'lib/jazzy/source_declaration.rb', line 115

def usr
  @usr
end

Instance Method Details

#abstract_globObject



215
216
217
218
219
220
221
# File 'lib/jazzy/source_declaration.rb', line 215

def abstract_glob
  return [] unless
    Config.instance.abstract_glob_configured &&
    Config.instance.abstract_glob

  Config.instance.abstract_glob.select { |e| File.file? e }
end

#alternative_abstractObject



202
203
204
205
206
# File 'lib/jazzy/source_declaration.rb', line 202

def alternative_abstract
  if file = alternative_abstract_file
    Pathname(file).read
  end
end

#alternative_abstract_fileObject



208
209
210
211
212
213
# File 'lib/jazzy/source_declaration.rb', line 208

def alternative_abstract_file
  abstract_glob.select do |f|
    # allow Structs.md or Structures.md
    [name, url_name].include?(File.basename(f).split('.').first)
  end.first
end

#constrained_extension?Boolean

Returns:

  • (Boolean)


161
162
163
164
# File 'lib/jazzy/source_declaration.rb', line 161

def constrained_extension?
  type.swift_extension? &&
    generic_requirements
end

#declaration_noteObject

Info text for contents page by collapsed item name



195
196
197
198
199
200
# File 'lib/jazzy/source_declaration.rb', line 195

def declaration_note
  notes = [default_impl_abstract ? 'default implementation' : nil,
           from_protocol_extension ? 'extension method' : nil,
           async ? 'asynchronous' : nil].compact
  notes.join(', ').humanize unless notes.empty?
end

#display_declarationObject



101
102
103
104
105
# File 'lib/jazzy/source_declaration.rb', line 101

def display_declaration
  return declaration if swift?

  Config.instance.hide_objc? ? other_language_declaration : declaration
end

#display_languageObject

The language in the templates for display



95
96
97
98
99
# File 'lib/jazzy/source_declaration.rb', line 95

def display_language
  return 'Swift' if swift?

  Config.instance.hide_objc? ? 'Swift' : 'Objective-C'
end

#display_other_language_declarationObject



107
108
109
110
# File 'lib/jazzy/source_declaration.rb', line 107

def display_other_language_declaration
  other_language_declaration unless
    Config.instance.hide_objc? || Config.instance.hide_swift?
end

#docs_filenameObject

Base filename (no extension) for the item



152
153
154
155
156
157
158
159
# File 'lib/jazzy/source_declaration.rb', line 152

def docs_filename
  result = url_name || name
  # Workaround functions sharing names with
  # different argument types (f(a:Int) vs. f(a:String))
  return result unless type.swift_global_function?

  result + "_#{type_usr}"
end

#filepathObject



147
148
149
# File 'lib/jazzy/source_declaration.rb', line 147

def filepath
  CGI.unescape(url)
end

#fully_qualified_nameObject



65
66
67
# File 'lib/jazzy/source_declaration.rb', line 65

def fully_qualified_name
  namespace_path.map(&:name).join('.')
end

#fully_qualified_name_regexpObject

:name doesn’t include any generic type params. This regexp matches any generic type params in parent names.



71
72
73
74
75
# File 'lib/jazzy/source_declaration.rb', line 71

def fully_qualified_name_regexp
  Regexp.new(namespace_path.map(&:name)
                           .map { |n| Regexp.escape(n) }
                           .join('(?:<.*?>)?\.'))
end

#highlight_languageObject



25
26
27
# File 'lib/jazzy/source_declaration.rb', line 25

def highlight_language
  swift? ? Highlighter::SWIFT : Highlighter::OBJC
end

#inherited_types?Boolean

Returns:

  • (Boolean)


174
175
176
177
# File 'lib/jazzy/source_declaration.rb', line 174

def inherited_types?
  inherited_types &&
    !inherited_types.empty?
end

#mark_for_childrenObject



166
167
168
169
170
171
172
# File 'lib/jazzy/source_declaration.rb', line 166

def mark_for_children
  if constrained_extension?
    SourceMark.new_generic_requirements(generic_requirements)
  else
    SourceMark.new
  end
end

#namespace_ancestorsObject



57
58
59
60
61
62
63
# File 'lib/jazzy/source_declaration.rb', line 57

def namespace_ancestors
  if parent_in_code
    parent_in_code.namespace_path
  else
    []
  end
end

#namespace_pathObject

Chain of parent_in_code from top level to self. (Includes self.)



53
54
55
# File 'lib/jazzy/source_declaration.rb', line 53

def namespace_path
  namespace_ancestors + [self]
end

#objc_category_nameObject

If this declaration is an objc category, returns an array with the name of the extended objc class and the category name itself, i.e. [“NSString”, “MyMethods”], nil otherwise.



80
81
82
# File 'lib/jazzy/source_declaration.rb', line 80

def objc_category_name
  name.split(/[()]/) if type.objc_category?
end

#omit_content_from_parent?Boolean

When referencing this item from its parent category, include the content or just link to it directly?

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/jazzy/source_declaration.rb', line 31

def omit_content_from_parent?
  Config.instance.separate_global_declarations &&
    render_as_page?
end

#other_inherited_types?(unwanted) ⇒ Boolean

Is there at least one inherited type that is not in the given list?

Returns:

  • (Boolean)


180
181
182
183
184
# File 'lib/jazzy/source_declaration.rb', line 180

def other_inherited_types?(unwanted)
  return false unless inherited_types?

  inherited_types.any? { |t| !unwanted.include?(t) }
end

#render_as_page?Boolean

Give the item its own page or just inline into parent?

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/jazzy/source_declaration.rb', line 15

def render_as_page?
  children.any? ||
    (Config.instance.separate_global_declarations &&
     type.global?)
end

#swift?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/jazzy/source_declaration.rb', line 21

def swift?
  type.swift_type?
end

#swift_extension_objc_nameObject



88
89
90
91
92
# File 'lib/jazzy/source_declaration.rb', line 88

def swift_extension_objc_name
  return unless type.swift_extension? && usr

  usr.split('(cs)').last
end

#swift_objc_extension?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/jazzy/source_declaration.rb', line 84

def swift_objc_extension?
  type.swift_extension? && usr && usr.start_with?('c:objc')
end

#type_from_doc_module?Boolean

Pre-Swift 5.6: SourceKit only sets modulename for imported modules Swift 5.6+: modulename is always set

Returns:

  • (Boolean)


188
189
190
191
192
# File 'lib/jazzy/source_declaration.rb', line 188

def type_from_doc_module?
  !type.extension? ||
    (swift? && usr &&
      (modulename.nil? || modulename == Config.instance.module_name))
end

#usage_discouraged?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/jazzy/source_declaration.rb', line 143

def usage_discouraged?
  unavailable || deprecated
end