Skip to content

Commit

Permalink
MM: Identify module source for extensions
Browse files Browse the repository at this point in the history
This is to resolve the issue of merging extensions that provide
protocol compliance -- in this case there may be no members to annotate
and show the source module.

A bit messy and not entirely complete because of the way the code is
structured -- really we should rework 'declaration' to be a list of
stuff for the templating layer but it is already tangled up with html
because of Rouge.

Good enough though, covers everything, and in reality these are unusual
cases.
  • Loading branch information
johnfairh committed Mar 6, 2024
1 parent b7674ae commit 2d9512c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
37 changes: 30 additions & 7 deletions lib/jazzy/sourcekitten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -895,17 +895,40 @@ def self.move_merged_extension_marks(decls)
# declaration: public protocol conformances and, for top-level extensions,
# further conditional extensions of the same type.
def self.merge_code_declaration(decls)
first = decls.first

declarations = decls[1..].select do |decl|
decl.type.swift_extension? &&
(decl.other_inherited_types?(@inaccessible_protocols) ||
(first.type.swift_extension? && decl.constrained_extension?))
end.map(&:declaration)

unless declarations.empty?
first.declaration = declarations.prepend(first.declaration).uniq.join
(decls.first.type.swift_extension? && decl.constrained_extension?))
end.prepend(decls.first)

html_declaration = ''
until declarations.empty?
module_decls, declarations = next_doc_module_group(declarations)
first = module_decls.first
if need_doc_module_note?(first, html_declaration)
html_declaration += "<span class='declaration-note'>From #{first.doc_module_name}:</span>"
end
html_declaration += module_decls.map(&:declaration).uniq.join
end

# Must preserve `nil` for edge cases
decls.first.declaration = html_declaration unless html_declaration.empty?
end

# Grab all the extensions from the same doc module
def self.next_doc_module_group(decls)
decls.partition { _1.doc_module_name == decls.first.doc_module_name }
end

# Does this extension/type need a note explaining which doc module it is from?
# Only for extensions, if there actually are multiple modules.
# Last condition avoids it for simple 'extension Array'.
def self.need_doc_module_note?(decl, html_declaration)
Config.instance.multiple_modules? &&
decl.type.swift_extension? &&
!(html_declaration.empty? &&
!decl.constrained_extension? &&
!decl.inherited_types?)
end

#
Expand Down
11 changes: 6 additions & 5 deletions lib/jazzy/themes/apple/assets/css/jazzy.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,12 @@ header {
.discouraged {
text-decoration: line-through;
}
.declaration-note {
font-size: .85em;
color: rgba(128,128,128,1);
font-style: italic;
}
}

.declaration-note {
font-size: .85em;
color: rgba(128,128,128,1);
font-style: italic;
}

.pointer-container {
Expand Down
10 changes: 5 additions & 5 deletions lib/jazzy/themes/fullwidth/assets/css/jazzy.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,12 @@ pre code {
margin-left: 20px;
font-size: 1rem;
}
}

.declaration-note {
font-size: .85em;
color: #808080;
font-style: italic;
}
.declaration-note {
font-size: .85em;
color: #808080;
font-style: italic;
}

.pointer-container {
Expand Down
11 changes: 6 additions & 5 deletions lib/jazzy/themes/jony/assets/css/jazzy.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,12 @@ header {
.token-open {
margin-left: 45px;
}
.declaration-note {
font-size: .85em;
color: rgba(128,128,128,1);
font-style: italic;
}
}

.declaration-note {
font-size: .85em;
color: rgba(128,128,128,1);
font-style: italic;
}

.pointer-container {
Expand Down

0 comments on commit 2d9512c

Please sign in to comment.