Class: Jazzy::SourceDeclaration::Type

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

MARKDOWN_KIND =
'document.markdown'.freeze
TYPES =
{
  # Markdown
  MARKDOWN_KIND => {
    jazzy: 'Guide',
    dash: 'Guide',
  }.freeze,

  'Overview' => {
    jazzy: nil,
    dash: 'Section',
  }.freeze,

  # Objective-C
  'sourcekitten.source.lang.objc.decl.unexposed' => {
    jazzy: 'Unexposed',
    dash: 'Unexposed',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.category' => {
    jazzy: 'Category',
    dash: 'Extension',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.class' => {
    jazzy: 'Class',
    dash: 'Class',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.constant' => {
    jazzy: 'Constant',
    dash: 'Constant',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.enum' => {
    jazzy: 'Enumeration',
    url: 'Enum',
    dash: 'Enum',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.enumcase' => {
    jazzy: 'Enumeration Case',
    dash: 'Case',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.initializer' => {
    jazzy: 'Initializer',
    dash: 'Initializer',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.method.class' => {
    jazzy: 'Class Method',
    dash: 'Method',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.method.instance' => {
    jazzy: 'Instance Method',
    dash: 'Method',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.property' => {
    jazzy: 'Property',
    dash: 'Property',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.protocol' => {
    jazzy: 'Protocol',
    dash: 'Protocol',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.typedef' => {
    jazzy: 'Type Definition',
    dash: 'Type',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.mark' => {
    jazzy: 'Mark',
    dash: 'Mark',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.function' => {
    jazzy: 'Function',
    dash: 'Function',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.struct' => {
    jazzy: 'Structure',
    url: 'Struct',
    dash: 'Struct',
    global: true,
  }.freeze,
  'sourcekitten.source.lang.objc.decl.field' => {
    jazzy: 'Field',
    dash: 'Field',
  }.freeze,
  'sourcekitten.source.lang.objc.decl.ivar' => {
    jazzy: 'Instance Variable',
    dash: 'Ivar',
  }.freeze,
  'sourcekitten.source.lang.objc.module.import' => {
    jazzy: 'Module',
    dash: 'Module',
  }.freeze,

  # Swift
  'source.lang.swift.decl.function.accessor.address' => {
    jazzy: 'Addressor',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.accessor.didset' => {
    jazzy: 'didSet Observer',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.accessor.getter' => {
    jazzy: 'Getter',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.accessor.mutableaddress' => {
    jazzy: 'Mutable Addressor',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.accessor.setter' => {
    jazzy: 'Setter',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.accessor.willset' => {
    jazzy: 'willSet Observer',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.operator' => {
    jazzy: 'Operator',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.operator.infix' => {
    jazzy: 'Infix Operator',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.operator.postfix' => {
    jazzy: 'Postfix Operator',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.operator.prefix' => {
    jazzy: 'Prefix Operator',
    dash: 'Function',
  }.freeze,
  'source.lang.swift.decl.function.method.class' => {
    jazzy: 'Class Method',
    dash: 'Method',
  }.freeze,
  'source.lang.swift.decl.var.class' => {
    jazzy: 'Class Variable',
    dash: 'Variable',
  }.freeze,
  'source.lang.swift.decl.class' => {
    jazzy: 'Class',
    dash: 'Class',
    global: true,
  }.freeze,
  'source.lang.swift.decl.function.constructor' => {
    jazzy: 'Initializer',
    dash: 'Constructor',
  }.freeze,
  'source.lang.swift.decl.function.destructor' => {
    jazzy: 'Deinitializer',
    dash: 'Method',
  }.freeze,
  'source.lang.swift.decl.var.global' => {
    jazzy: 'Global Variable',
    dash: 'Global',
    global: true,
  }.freeze,
  'source.lang.swift.decl.enumcase' => {
    jazzy: 'Enumeration Case',
    dash: 'Case',
  }.freeze,
  'source.lang.swift.decl.enumelement' => {
    jazzy: 'Enumeration Element',
    dash: 'Element',
  }.freeze,
  'source.lang.swift.decl.enum' => {
    jazzy: 'Enumeration',
    url: 'Enum',
    dash: 'Enum',
    global: true,
  }.freeze,
  'source.lang.swift.decl.extension' => {
    jazzy: 'Extension',
    dash: 'Extension',
    global: true,
  }.freeze,
  'source.lang.swift.decl.extension.class' => {
    jazzy: 'Class Extension',
    dash: 'Extension',
    global: true,
  }.freeze,
  'source.lang.swift.decl.extension.enum' => {
    jazzy: 'Enumeration Extension',
    dash: 'Extension',
    global: true,
  }.freeze,
  'source.lang.swift.decl.extension.protocol' => {
    jazzy: 'Protocol Extension',
    dash: 'Extension',
    global: true,
  }.freeze,
  'source.lang.swift.decl.extension.struct' => {
    jazzy: 'Structure Extension',
    dash: 'Extension',
    global: true,
  }.freeze,
  'source.lang.swift.decl.function.free' => {
    jazzy: 'Function',
    dash: 'Function',
    global: true,
  }.freeze,
  'source.lang.swift.decl.function.method.instance' => {
    jazzy: 'Instance Method',
    dash: 'Method',
  }.freeze,
  'source.lang.swift.decl.var.instance' => {
    jazzy: 'Instance Variable',
    dash: 'Property',
  }.freeze,
  'source.lang.swift.decl.var.local' => {
    jazzy: 'Local Variable',
    dash: 'Variable',
  }.freeze,
  'source.lang.swift.decl.var.parameter' => {
    jazzy: 'Parameter',
    dash: 'Parameter',
  }.freeze,
  'source.lang.swift.decl.protocol' => {
    jazzy: 'Protocol',
    dash: 'Protocol',
    global: true,
  }.freeze,
  'source.lang.swift.decl.function.method.static' => {
    jazzy: 'Static Method',
    dash: 'Method',
  }.freeze,
  'source.lang.swift.decl.var.static' => {
    jazzy: 'Static Variable',
    dash: 'Variable',
  }.freeze,
  'source.lang.swift.decl.struct' => {
    jazzy: 'Structure',
    url: 'Struct',
    dash: 'Struct',
    global: true,
  }.freeze,
  'source.lang.swift.decl.function.subscript' => {
    jazzy: 'Subscript',
    dash: 'Method',
  }.freeze,
  'source.lang.swift.decl.typealias' => {
    jazzy: 'Type Alias',
    url: 'Typealias',
    dash: 'Alias',
    global: true,
  }.freeze,
  'source.lang.swift.decl.generic_type_param' => {
    jazzy: 'Generic Type Parameter',
    dash: 'Parameter',
  }.freeze,
  'source.lang.swift.decl.associatedtype' => {
    jazzy: 'Associated Type',
    dash: 'Alias',
  }.freeze,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind) ⇒ Type

Returns a new instance of Type.



13
14
15
16
# File 'lib/jazzy/source_declaration/type.rb', line 13

def initialize(kind)
  @kind = kind
  @type = TYPES[kind]
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

Class Method Details

.allObject



7
8
9
# File 'lib/jazzy/source_declaration/type.rb', line 7

def self.all
  TYPES.keys.map { |k| new(k) }.reject { |t| t.name.nil? }
end

.markdownObject



156
157
158
# File 'lib/jazzy/source_declaration/type.rb', line 156

def self.markdown
  Type.new(MARKDOWN_KIND)
end

.overviewObject



150
151
152
# File 'lib/jazzy/source_declaration/type.rb', line 150

def self.overview
  Type.new('Overview')
end

Instance Method Details

#==(other) ⇒ Object



169
170
171
# File 'lib/jazzy/source_declaration/type.rb', line 169

def ==(other)
  other && kind == other.kind
end

#dash_typeObject



18
19
20
# File 'lib/jazzy/source_declaration/type.rb', line 18

def dash_type
  @type && @type[:dash]
end

#declaration?Boolean

Returns:

  • (Boolean)


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

def declaration?
  kind.start_with?('source.lang.swift.decl',
                   'sourcekitten.source.lang.objc.decl')
end

#equalsObject



168
# File 'lib/jazzy/source_declaration/type.rb', line 168

alias equals ==

#extension?Boolean

Returns:

  • (Boolean)


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

def extension?
  swift_extension? || objc_category?
end

#generic_type_param?Boolean

Returns:

  • (Boolean)


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

def generic_type_param?
  kind == 'source.lang.swift.decl.generic_type_param'
end

#global?Boolean

kinds that are ‘global’ and should get their own pages with –separate-global-declarations

Returns:

  • (Boolean)


28
29
30
# File 'lib/jazzy/source_declaration/type.rb', line 28

def global?
  @type && @type[:global]
end

#hashObject



164
165
166
# File 'lib/jazzy/source_declaration/type.rb', line 164

def hash
  kind.hash
end

#mark?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/jazzy/source_declaration/type.rb', line 61

def mark?
  objc_mark? || swift_mark?
end

#markdown?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/jazzy/source_declaration/type.rb', line 160

def markdown?
  kind == MARKDOWN_KIND
end

#nameObject



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

def name
  @type && @type[:jazzy]
end

#name_controlled_manually?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/jazzy/source_declaration/type.rb', line 37

def name_controlled_manually?
  !kind.start_with?('source')
  # "'source'.lang..." for Swift
  # or "'source'kitten.source..." for Objective-C
  # but not "Overview" for navigation groups.
end

#objc_category?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/jazzy/source_declaration/type.rb', line 78

def objc_category?
  kind == 'sourcekitten.source.lang.objc.decl.category'
end

#objc_class?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/jazzy/source_declaration/type.rb', line 82

def objc_class?
  kind == 'sourcekitten.source.lang.objc.decl.class'
end

#objc_enum?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/jazzy/source_declaration/type.rb', line 70

def objc_enum?
  kind == 'sourcekitten.source.lang.objc.decl.enum'
end

#objc_mark?Boolean

Returns:

  • (Boolean)


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

def objc_mark?
  kind == 'sourcekitten.source.lang.objc.mark'
end

#objc_typedef?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/jazzy/source_declaration/type.rb', line 74

def objc_typedef?
  kind == 'sourcekitten.source.lang.objc.decl.typedef'
end

#objc_unexposed?Boolean

Returns:

  • (Boolean)


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

def objc_unexposed?
  kind == 'sourcekitten.source.lang.objc.decl.unexposed'
end

#param?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'lib/jazzy/source_declaration/type.rb', line 131

def param?
  # SourceKit strangely categorizes initializer parameters as local
  # variables, so both kinds represent a parameter in jazzy.
  kind == 'source.lang.swift.decl.var.parameter' ||
    kind == 'source.lang.swift.decl.var.local'
end

#plural_nameObject



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

def plural_name
  name.pluralize
end

#plural_url_nameObject



48
49
50
# File 'lib/jazzy/source_declaration/type.rb', line 48

def plural_url_name
  url_name.pluralize
end

#should_document?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/jazzy/source_declaration/type.rb', line 98

def should_document?
  declaration? && !param? && !generic_type_param?
end

#swift_enum_case?Boolean

Returns:

  • (Boolean)


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

def swift_enum_case?
  kind == 'source.lang.swift.decl.enumcase'
end

#swift_enum_element?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/jazzy/source_declaration/type.rb', line 94

def swift_enum_element?
  kind == 'source.lang.swift.decl.enumelement'
end

#swift_extensible?Boolean

Returns:

  • (Boolean)


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

def swift_extensible?
  kind =~ /^source\.lang\.swift\.decl\.(class|struct|protocol|enum)$/
end

#swift_extension?Boolean

Returns:

  • (Boolean)


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

def swift_extension?
  kind =~ /^source\.lang\.swift\.decl\.extension.*/
end

#swift_global_function?Boolean

Returns:

  • (Boolean)


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

def swift_global_function?
  kind == 'source.lang.swift.decl.function.free'
end

#swift_mark?Boolean

covers MARK: TODO: FIXME: comments

Returns:

  • (Boolean)


57
58
59
# File 'lib/jazzy/source_declaration/type.rb', line 57

def swift_mark?
  kind == 'source.lang.swift.syntaxtype.comment.mark'
end

#swift_protocol?Boolean

Returns:

  • (Boolean)


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

def swift_protocol?
  kind == 'source.lang.swift.decl.protocol'
end

#swift_type?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/jazzy/source_declaration/type.rb', line 86

def swift_type?
  kind.include? 'swift'
end

#swift_typealias?Boolean

Returns:

  • (Boolean)


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

def swift_typealias?
  kind == 'source.lang.swift.decl.typealias'
end

#swift_variable?Boolean

Returns:

  • (Boolean)


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

def swift_variable?
  kind.start_with?('source.lang.swift.decl.var')
end

#task_mark?(name) ⇒ Boolean

mark that should start a new task section

Returns:

  • (Boolean)


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

def task_mark?(name)
  objc_mark? || (swift_mark? && name.start_with?('MARK: '))
end

#url_nameObject

name to use for type subdirectory in URLs for back-compatibility



33
34
35
# File 'lib/jazzy/source_declaration/type.rb', line 33

def url_name
  @type && (@type[:url] || @type[:jazzy])
end