Module: Jazzy::SourceKitten
- Defined in:
- lib/jazzy/sourcekitten.rb
Overview
This module interacts with the sourcekitten command-line executable
Class Method Summary collapse
-
.ancestor_name_match(name_part, doc) ⇒ Object
Find the first ancestor of doc whose name matches name_part.
-
.arguments_from_options(options) ⇒ Object
Builds SourceKitten arguments based on Jazzy options.
- .autolink(docs, root_decls) ⇒ Object
-
.autolink_document(html, doc) ⇒ Object
For autolinking external markdown documents.
-
.autolink_text(text, doc, root_decls, after_highlight = false) ⇒ Object
Links recognized top-level declarations within - inlined code within docs - method signatures after they’ve been processed by the highlighter.
- .availability_attribute?(doc) ⇒ Boolean
-
.deduplicate_declarations(declarations) ⇒ Object
Merges multiple extensions of the same entity into a single document.
-
.deduplication_key(decl, root_decls) ⇒ Object
Two declarations get merged if they have the same deduplication key.
- .expand_extension(extension, name_parts, decls) ⇒ Object
-
.expand_extensions(decls) ⇒ Object
Expands extensions of nested types declared at the top level into a tree so they can be deduplicated properly.
- .filter_excluded_files(json) ⇒ Object
- .group_custom_categories(docs) ⇒ Object
-
.group_docs(docs) ⇒ Object
Group root-level docs by custom categories (if any) and type.
- .group_type_categories(docs, type_category_prefix) ⇒ Object
- .make_default_doc_info(declaration) ⇒ Object
-
.make_doc_info(doc, declaration) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity.
-
.make_doc_urls(docs) ⇒ Hash
rubocop:disable Metrics/MethodLength Generate doc URL by prepending its parents URLs.
- .make_group(group, name, abstract, url_name = nil) ⇒ Object
-
.make_source_declarations(docs, parent = nil) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity.
-
.make_substructure(doc, declaration) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity.
-
.mark_members_from_protocol_extension(extensions) ⇒ Object
Protocol methods provided only in an extension and not in the protocol itself are a special beast: they do not use dynamic dispatch.
-
.merge_declarations(decls) ⇒ Object
rubocop:disable Metrics/MethodLength Merges all of the given types and extensions into a single document.
-
.merge_default_implementations_into_protocol(protocol, extensions) ⇒ Object
If any of the extensions provide default implementations for methods in the given protocol, merge those members into the protocol doc instead of keeping them on the extension.
-
.mergeable_objc?(decl, root_decls) ⇒ Boolean
Returns true if an Objective-C declaration is mergeable.
- .name_match(name_part, docs) ⇒ Object
- .name_traversal(name_parts, doc) ⇒ Object
- .objc_arguments_from_options(options) ⇒ Object
- .parameters(doc, discovered) ⇒ Object
-
.parse(sourcekitten_output, min_acl, skip_undocumented, inject_docs) ⇒ Hash
Parse sourcekitten STDOUT output as JSON.
- .process_undocumented_token(doc, declaration) ⇒ Object
-
.rec_path(path) ⇒ Object
returns all subdirectories of specified path.
- .reject_objc_types(docs) ⇒ Object
-
.run_sourcekitten(arguments) ⇒ Object
Run sourcekitten with given arguments and return STDOUT.
- .sanitize_filename(doc) ⇒ Object
-
.should_document?(doc) ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity.
-
.should_mark_undocumented(kind, filepath) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity.
-
.subdir_for_doc(doc) ⇒ Object
Determine the subdirectory in which a doc should be placed.
Class Method Details
.ancestor_name_match(name_part, doc) ⇒ Object
Find the first ancestor of doc whose name matches name_part.
555 556 557 558 559 560 561 562 |
# File 'lib/jazzy/sourcekitten.rb', line 555 def self.ancestor_name_match(name_part, doc) doc.namespace_ancestors.reverse_each do |ancestor| if match = name_match(name_part, ancestor.children) return match end end nil end |
.arguments_from_options(options) ⇒ Object
Builds SourceKitten arguments based on Jazzy options
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/jazzy/sourcekitten.rb', line 178 def self.() arguments = ['doc'] arguments += if .objc_mode () elsif !.module_name.empty? ['--module-name', .module_name, '--'] else ['--'] end arguments + .xcodebuild_arguments end |
.autolink(docs, root_decls) ⇒ Object
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/jazzy/sourcekitten.rb', line 608 def self.autolink(docs, root_decls) @autolink_root_decls = root_decls docs.each do |doc| doc.children = autolink(doc.children, root_decls) doc.return = autolink_text(doc.return, doc, root_decls) if doc.return doc.abstract = autolink_text(doc.abstract, doc, root_decls) (doc.parameters || []).each do |param| param[:discussion] = autolink_text(param[:discussion], doc, root_decls) end if doc.declaration doc.declaration = autolink_text( doc.declaration, doc, root_decls, true ) end if doc.other_language_declaration doc.other_language_declaration = autolink_text( doc.other_language_declaration, doc, root_decls, true ) end end end |
.autolink_document(html, doc) ⇒ Object
For autolinking external markdown documents
635 636 637 |
# File 'lib/jazzy/sourcekitten.rb', line 635 def self.autolink_document(html, doc) autolink_text(html, doc, @autolink_root_decls || []) end |
.autolink_text(text, doc, root_decls, after_highlight = false) ⇒ Object
Links recognized top-level declarations within
-
inlined code within docs
-
method signatures after they’ve been processed by the highlighter
The ‘after_highlight` flag is used to differentiate between the two modes.
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/jazzy/sourcekitten.rb', line 577 def self.autolink_text(text, doc, root_decls, after_highlight = false) text.autolink_block(doc.url, '[^\s]+', after_highlight) do |raw_name| parts = raw_name .split(/(?<!\.)\.(?!\.)/) # dot with no neighboring dots .reject(&:empty?) # First dot-separated component can match any ancestor or top-level doc first_part = parts.shift name_root = ancestor_name_match(first_part, doc) || name_match(first_part, root_decls) # Traverse children via subsequence components, if any name_traversal(parts, name_root) end.autolink_block(doc.url, '[+-]\[\w+(?: ?\(\w+\))? [\w:]+\]', after_highlight) do |raw_name| match = raw_name.match(/([+-])\[(\w+(?: ?\(\w+\))?) ([\w:]+)\]/) # Subject component can match any ancestor or top-level doc subject = match[2].delete(' ') name_root = ancestor_name_match(subject, doc) || name_match(subject, root_decls) if name_root # Look up the verb in the subject’s children name_match(match[1] + match[3], name_root.children) end end.autolink_block(doc.url, '[+-]\w[\w:]*', after_highlight) do |raw_name| name_match(raw_name, doc.children) end end |
.availability_attribute?(doc) ⇒ Boolean
234 235 236 237 238 239 |
# File 'lib/jazzy/sourcekitten.rb', line 234 def self.availability_attribute?(doc) return false unless doc['key.attributes'] !doc['key.attributes'].select do |attribute| attribute.values.first == 'source.decl.attribute.available' end.empty? end |
.deduplicate_declarations(declarations) ⇒ Object
Merges multiple extensions of the same entity into a single document.
Merges extensions into the protocol/class/struct/enum they extend, if it occurs in the same project.
Merges redundant declarations when documenting podspecs.
436 437 438 439 440 441 442 443 444 445 |
# File 'lib/jazzy/sourcekitten.rb', line 436 def self.deduplicate_declarations(declarations) duplicate_groups = declarations .group_by { |d| deduplication_key(d, declarations) } .values duplicate_groups.map do |group| # Put extended type (if present) before extensions merge_declarations(group) end end |
.deduplication_key(decl, root_decls) ⇒ Object
Two declarations get merged if they have the same deduplication key.
455 456 457 458 459 460 461 462 463 464 |
# File 'lib/jazzy/sourcekitten.rb', line 455 def self.deduplication_key(decl, root_decls) if decl.type.swift_extensible? || decl.type.swift_extension? [decl.usr, decl.name] elsif mergeable_objc?(decl, root_decls) name, _ = decl.objc_category_name || decl.name [name, :objc_class_and_categories] else [decl.usr, decl.name, decl.type.kind] end end |
.expand_extension(extension, name_parts, decls) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/jazzy/sourcekitten.rb', line 412 def self.(extension, name_parts, decls) return extension if name_parts.empty? name = name_parts.shift candidates = decls.select { |decl| decl.name == name } SourceDeclaration.new.tap do |decl| make_default_doc_info(decl) decl.name = name decl.type = extension.type decl.mark = extension.mark decl.usr = candidates.first.usr unless candidates.empty? child = (extension, name_parts, candidates.flat_map(&:children).uniq) child.parent_in_code = decl decl.children = [child] end end |
.expand_extensions(decls) ⇒ Object
Expands extensions of nested types declared at the top level into a tree so they can be deduplicated properly
402 403 404 405 406 407 408 409 410 |
# File 'lib/jazzy/sourcekitten.rb', line 402 def self.(decls) decls.map do |decl| next decl unless decl.type.extension? && decl.name.include?('.') name_parts = decl.name.split('.') decl.name = name_parts.pop (decl, name_parts, decls) end end |
.filter_excluded_files(json) ⇒ Object
533 534 535 536 537 538 539 540 541 |
# File 'lib/jazzy/sourcekitten.rb', line 533 def self.filter_excluded_files(json) excluded_files = Config.instance.excluded_files json.map do |doc| key = doc.keys.first doc[key] unless excluded_files.detect do |exclude| File.fnmatch?(exclude, key) end end.compact end |
.group_custom_categories(docs) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jazzy/sourcekitten.rb', line 62 def self.group_custom_categories(docs) group = Config.instance.custom_categories.map do |category| children = category['children'].flat_map do |name| docs_with_name, docs = docs.partition { |doc| doc.name == name } if docs_with_name.empty? STDERR.puts 'WARNING: No documented top-level declarations match ' \ "name \"#{name}\" specified in categories file" end docs_with_name end # Category config overrides alphabetization children.each.with_index { |child, i| child.nav_order = i } make_group(children, category['name'], '') end [group.compact, docs] end |
.group_docs(docs) ⇒ Object
Group root-level docs by custom categories (if any) and type
54 55 56 57 58 59 60 |
# File 'lib/jazzy/sourcekitten.rb', line 54 def self.group_docs(docs) custom_categories, docs = group_custom_categories(docs) type_categories, uncategorized = group_type_categories( docs, custom_categories.any? ? 'Other ' : '' ) custom_categories + type_categories + uncategorized end |
.group_type_categories(docs, type_category_prefix) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/jazzy/sourcekitten.rb', line 79 def self.group_type_categories(docs, type_category_prefix) group = SourceDeclaration::Type.all.map do |type| children, docs = docs.partition { |doc| doc.type == type } make_group( children, type_category_prefix + type.plural_name, "The following #{type.plural_name.downcase} are available globally.", type_category_prefix + type.plural_url_name, ) end [group.compact, docs] end |
.make_default_doc_info(declaration) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/jazzy/sourcekitten.rb', line 225 def self.make_default_doc_info(declaration) # @todo: Fix these declaration.line = nil declaration.column = nil declaration.abstract = '' declaration.parameters = [] declaration.children = [] end |
.make_doc_info(doc, declaration) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/jazzy/sourcekitten.rb', line 306 def self.make_doc_info(doc, declaration) return unless should_document?(doc) declaration.declaration = Highlighter.highlight( doc['key.parsed_declaration'] || doc['key.doc.declaration'], Config.instance.objc_mode ? 'objc' : 'swift', ) if Config.instance.objc_mode && doc['key.swift_declaration'] declaration.other_language_declaration = Highlighter.highlight( doc['key.swift_declaration'], 'swift' ) end unless doc['key.doc.full_as_xml'] return process_undocumented_token(doc, declaration) end declaration.abstract = Markdown.render(doc['key.doc.comment'] || '') declaration.discussion = '' declaration.return = Markdown.rendered_returns declaration.parameters = parameters(doc, Markdown.rendered_parameters) @stats.add_documented end |
.make_doc_urls(docs) ⇒ Hash
rubocop:disable Metrics/MethodLength Generate doc URL by prepending its parents URLs
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/jazzy/sourcekitten.rb', line 118 def self.make_doc_urls(docs) docs.each do |doc| if !doc.parent_in_docs || doc.children.count > 0 # Create HTML page for this doc if it has children or is root-level doc.url = ( subdir_for_doc(doc) + [sanitize_filename(doc) + '.html'] ).join('/') doc.children = make_doc_urls(doc.children) else # Don't create HTML page for this doc if it doesn't have children # Instead, make its link a hash-link on its parent's page if doc.typename == '<<error type>>' warn 'A compile error prevented ' + doc.fully_qualified_name + ' from receiving a unique USR. Documentation may be ' \ 'incomplete. Please check for compile errors by running ' \ '`xcodebuild ' \ "#{Config.instance.xcodebuild_arguments.shelljoin}`." end id = doc.usr unless id id = doc.name || 'unknown' warn "`#{id}` has no USR. First make sure all modules used in " \ 'your project have been imported. If all used modules are ' \ 'imported, please report this problem by filing an issue at ' \ 'https://github.com/realm/jazzy/issues along with your Xcode ' \ 'project. If this token is declared in an `#if` block, please ' \ 'ignore this message.' end doc.url = doc.parent_in_docs.url + '#/' + id end end end |
.make_group(group, name, abstract, url_name = nil) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/jazzy/sourcekitten.rb', line 92 def self.make_group(group, name, abstract, url_name = nil) group.reject! { |doc| doc.name.empty? } unless group.empty? SourceDeclaration.new.tap do |sd| sd.type = SourceDeclaration::Type.overview sd.name = name sd.url_name = url_name sd.abstract = Markdown.render(abstract) sd.children = group end end end |
.make_source_declarations(docs, parent = nil) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/jazzy/sourcekitten.rb', line 347 def self.make_source_declarations(docs, parent = nil) declarations = [] current_mark = SourceMark.new Array(docs).each do |doc| if doc.key?('key.diagnostic_stage') declarations += make_source_declarations( doc['key.substructure'], parent ) next end declaration = SourceDeclaration.new declaration.parent_in_code = parent declaration.type = SourceDeclaration::Type.new(doc['key.kind']) declaration.typename = doc['key.typename'] current_mark = SourceMark.new(doc['key.name']) if declaration.type.mark? if declaration.type.swift_enum_case? # Enum "cases" are thin wrappers around enum "elements". declarations += make_source_declarations( doc['key.substructure'], parent ) next end next unless declaration.type.should_document? unless declaration.type.name raise 'Please file an issue at ' \ 'https://github.com/realm/jazzy/issues about adding support ' \ "for `#{declaration.type.kind}`." end declaration.file = Pathname(doc['key.filepath']) if doc['key.filepath'] declaration.usr = doc['key.usr'] declaration.modulename = doc['key.modulename'] declaration.name = doc['key.name'] declaration.mark = current_mark declaration.access_control_level = SourceDeclaration::AccessControlLevel.from_doc(doc) declaration.line = doc['key.doc.line'] declaration.column = doc['key.doc.column'] declaration.start_line = doc['key.parsed_scope.start'] declaration.end_line = doc['key.parsed_scope.end'] next unless make_doc_info(doc, declaration) make_substructure(doc, declaration) next if declaration.type.extension? && declaration.children.empty? declarations << declaration end declarations end |
.make_substructure(doc, declaration) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity
333 334 335 336 337 338 339 340 341 342 |
# File 'lib/jazzy/sourcekitten.rb', line 333 def self.make_substructure(doc, declaration) declaration.children = if doc['key.substructure'] make_source_declarations( doc['key.substructure'], declaration, ) else [] end end |
.mark_members_from_protocol_extension(extensions) ⇒ Object
Protocol methods provided only in an extension and not in the protocol itself are a special beast: they do not use dynamic dispatch. These get an “Extension method” annotation in the generated docs.
525 526 527 528 529 530 531 |
# File 'lib/jazzy/sourcekitten.rb', line 525 def self.mark_members_from_protocol_extension(extensions) extensions.each do |ext| ext.children.each do |ext_member| ext_member.from_protocol_extension = true end end end |
.merge_declarations(decls) ⇒ Object
rubocop:disable Metrics/MethodLength Merges all of the given types and extensions into a single document.
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/jazzy/sourcekitten.rb', line 468 def self.merge_declarations(decls) extensions, typedecls = decls.partition { |d| d.type.extension? } if typedecls.size > 1 warn 'Found conflicting type declarations with the same name, which ' \ 'may indicate a build issue or a bug in Jazzy: ' + typedecls.map { |t| "#{t.type.name.downcase} #{t.name}" } .join(', ') end typedecl = typedecls.first if typedecl && typedecl.type.swift_protocol? merge_default_implementations_into_protocol(typedecl, extensions) mark_members_from_protocol_extension(extensions) extensions.reject! { |ext| ext.children.empty? } elsif typedecl && typedecl.type.objc_class? # Mark children merged from categories with the name of category # (unless they already have a mark) extensions.each do |ext| _, category_name = ext.objc_category_name ext.children.each { |c| c.mark.name ||= category_name } end end decls = typedecls + extensions decls.first.tap do |merged| merged.children = deduplicate_declarations( decls.flat_map(&:children).uniq, ) merged.children.each do |child| child.parent_in_code = merged end end end |
.merge_default_implementations_into_protocol(protocol, extensions) ⇒ Object
If any of the extensions provide default implementations for methods in the given protocol, merge those members into the protocol doc instead of keeping them on the extension. These get a “Default implementation” annotation in the generated docs.
508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/jazzy/sourcekitten.rb', line 508 def self.merge_default_implementations_into_protocol(protocol, extensions) protocol.children.each do |proto_method| extensions.each do |ext| defaults, ext.children = ext.children.partition do |ext_member| ext_member.name == proto_method.name end unless defaults.empty? proto_method.default_impl_abstract = defaults.flat_map { |d| [d.abstract, d.discussion] }.join end end end end |
.mergeable_objc?(decl, root_decls) ⇒ Boolean
Returns true if an Objective-C declaration is mergeable.
448 449 450 451 452 |
# File 'lib/jazzy/sourcekitten.rb', line 448 def self.mergeable_objc?(decl, root_decls) decl.type.objc_class? \ || (decl.type.objc_category? \ && name_match(decl.objc_category_name[0], root_decls)) end |
.name_match(name_part, docs) ⇒ Object
543 544 545 546 547 548 549 550 551 552 |
# File 'lib/jazzy/sourcekitten.rb', line 543 def self.name_match(name_part, docs) return nil unless name_part wildcard_expansion = Regexp.escape(name_part) .gsub('\.\.\.', '[^)]*') .gsub(/<.*>/, '') whole_name_pat = /\A#{wildcard_expansion}\Z/ docs.find do |doc| whole_name_pat =~ doc.name end end |
.name_traversal(name_parts, doc) ⇒ Object
564 565 566 567 568 569 570 |
# File 'lib/jazzy/sourcekitten.rb', line 564 def self.name_traversal(name_parts, doc) while doc && !name_parts.empty? next_part = name_parts.shift doc = name_match(next_part, doc.children) end doc end |
.objc_arguments_from_options(options) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/jazzy/sourcekitten.rb', line 190 def self.() arguments = [] if .xcodebuild_arguments.empty? arguments += ['--objc', .umbrella_header.to_s, '--', '-x', 'objective-c', '-isysroot', `xcrun --show-sdk-path --sdk #{.sdk}`.chomp, '-I', .framework_root.to_s, '-fmodules'] end # add additional -I arguments for each subdirectory of framework_root unless .framework_root.nil? rec_path(Pathname.new(.framework_root.to_s)).collect do |child| if child.directory? arguments += ['-I', child.to_s] end end end arguments end |
.parameters(doc, discovered) ⇒ Object
294 295 296 297 298 299 300 301 302 |
# File 'lib/jazzy/sourcekitten.rb', line 294 def self.parameters(doc, discovered) (doc['key.doc.parameters'] || []).map do |p| name = p['name'] { name: name, discussion: discovered[name], } end.reject { |param| param[:discussion].nil? } end |
.parse(sourcekitten_output, min_acl, skip_undocumented, inject_docs) ⇒ Hash
Parse sourcekitten STDOUT output as JSON
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/jazzy/sourcekitten.rb', line 656 def self.parse(sourcekitten_output, min_acl, skip_undocumented, inject_docs) @min_acl = min_acl @skip_undocumented = skip_undocumented @stats = Stats.new sourcekitten_json = filter_excluded_files(JSON.parse(sourcekitten_output)) docs = make_source_declarations(sourcekitten_json).concat inject_docs docs = (docs) docs = deduplicate_declarations(docs) if Config.instance.objc_mode docs = reject_objc_types(docs) else # Remove top-level enum cases because it means they have an ACL lower # than min_acl docs = docs.reject { |doc| doc.type.swift_enum_element? } end ungrouped_docs = docs docs = group_docs(docs) make_doc_urls(docs) autolink(docs, ungrouped_docs) [docs, @stats] end |
.process_undocumented_token(doc, declaration) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/jazzy/sourcekitten.rb', line 277 def self.process_undocumented_token(doc, declaration) make_default_doc_info(declaration) filepath = doc['key.filepath'] objc = Config.instance.objc_mode if objc || should_mark_undocumented(doc['key.kind'], filepath) @stats.add_undocumented(declaration) return nil if @skip_undocumented declaration.abstract = @undocumented_abstract else comment = doc['key.doc.comment'] declaration.abstract = Markdown.render(comment) if comment end declaration end |
.rec_path(path) ⇒ Object
returns all subdirectories of specified path
169 170 171 172 173 174 175 |
# File 'lib/jazzy/sourcekitten.rb', line 169 def self.rec_path(path) path.children.collect do |child| if child.directory? rec_path(child) + [child] end end.select { |x| x }.flatten(1) end |
.reject_objc_types(docs) ⇒ Object
639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/jazzy/sourcekitten.rb', line 639 def self.reject_objc_types(docs) enums = docs.map do |doc| [doc, doc.children] end.flatten.select { |child| child.type.objc_enum? }.map(&:name) docs.map do |doc| doc.children = doc.children.reject do |child| child.type.objc_typedef? && enums.include?(child.name) end doc end.reject do |doc| doc.type.objc_unexposed? || (doc.type.objc_typedef? && enums.include?(doc.name)) end end |
.run_sourcekitten(arguments) ⇒ Object
Run sourcekitten with given arguments and return STDOUT
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/jazzy/sourcekitten.rb', line 211 def self.run_sourcekitten(arguments) if swift_version = Config.instance.swift_version unless xcode = XCInvoke::Xcode.find_swift_version(swift_version) raise "Unable to find an Xcode with swift version #{swift_version}." end env = xcode.as_env else env = ENV end bin_path = Pathname(__FILE__) + '../../../bin/sourcekitten' output, = Executable.execute_command(bin_path, arguments, true, env: env) output end |
.sanitize_filename(doc) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/jazzy/sourcekitten.rb', line 105 def self.sanitize_filename(doc) unsafe_filename = doc.url_name || doc.name sanitzation_enabled = Config.instance.use_safe_filenames if sanitzation_enabled && !doc.type.name_controlled_manually? return CGI.escape(unsafe_filename).gsub('_', '%5F').tr('%', '_') else return unsafe_filename end end |
.should_document?(doc) ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/jazzy/sourcekitten.rb', line 243 def self.should_document?(doc) return false if doc['key.doc.comment'].to_s.include?(':nodoc:') # Always document Objective-C declarations. return true if Config.instance.objc_mode # Don't document @available declarations with no USR, since it means # they're unavailable. if availability_attribute?(doc) && !doc['key.usr'] return false end # Document extensions & enum elements, since we can't tell their ACL. type = SourceDeclaration::Type.new(doc['key.kind']) return true if type.swift_enum_element? if type.swift_extension? return Array(doc['key.substructure']).any? do |subdoc| subtype = SourceDeclaration::Type.new(subdoc['key.kind']) !subtype.mark? && should_document?(subdoc) end end acl_ok = SourceDeclaration::AccessControlLevel.from_doc(doc) >= @min_acl acl_ok.tap { @stats.add_acl_skipped unless acl_ok } end |
.should_mark_undocumented(kind, filepath) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity
271 272 273 274 275 |
# File 'lib/jazzy/sourcekitten.rb', line 271 def self.should_mark_undocumented(kind, filepath) source_directory = Config.instance.source_directory.to_s (filepath || '').start_with?(source_directory) && kind != 'source.lang.swift.decl.generic_type_param' end |
.subdir_for_doc(doc) ⇒ Object
Determine the subdirectory in which a doc should be placed
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/jazzy/sourcekitten.rb', line 154 def self.subdir_for_doc(doc) # We always want to create top-level subdirs according to type (Struct, # Class, etc). top_level_decl = doc.namespace_path.first if top_level_decl && top_level_decl.type && top_level_decl.type.name # File program elements under top ancestor’s type (Struct, Class, etc.) [top_level_decl.type.plural_url_name] + doc.namespace_ancestors.map(&:name) else # Categories live in their own directory [] end end |