Skip to content

Commit b82d0b3

Browse files
authored
Make Exceptions a sub-grouping inside modules (#1124)
1 parent d5cde30 commit b82d0b3

File tree

10 files changed

+72
-35
lines changed

10 files changed

+72
-35
lines changed

assets/js/autocomplete/suggestions.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const labels = {
2121
}
2222

2323
/**
24-
* Transform an object containing data about a search result and transforms it into a simple
24+
* Takes an object containing data about a search result and transforms it into a simple
2525
* data structure that can be used directly in the autocomplete template.
2626
*
2727
* @param {Object} item Result to be serialized
@@ -44,7 +44,7 @@ function serialize (item, moduleId = null) {
4444
label: label, // 'Callback' or 'Type' - if set it will be displayed next to the title.
4545
matchQuality: item.matchQuality, // 0..1 - How well result matches the search term. Higher is better.
4646
category: category
47-
// 'Module', 'Mix Task', 'Exception' or 'Child'.
47+
// 'Module', 'Mix Task' or 'Child'.
4848
// Used to sort the results according to the 'sortingPriority'.
4949
// 'Child' means an item that belongs to a module (like Function, Callback or Type).
5050
}
@@ -63,10 +63,9 @@ function getSuggestions (term = '') {
6363
const nodes = sidebarNodes
6464

6565
let modules = findIn(nodes.modules, term, 'Module')
66-
let exceptions = findIn(nodes.exceptions, term, 'Exception')
6766
let tasks = findIn(nodes.tasks, term, 'Mix Task')
6867

69-
let results = [...modules, ...exceptions, ...tasks]
68+
let results = [...modules, ...tasks]
7069

7170
results = sort(results)
7271

@@ -82,8 +81,8 @@ function getSuggestions (term = '') {
8281
*/
8382
function sort (items) {
8483
return items.sort(function (item1, item2) {
85-
const weight1 = sortingPriority[item1.category] || -1
86-
const weight2 = sortingPriority[item2.category] || -1
84+
const weight1 = getSortingPriority(item1)
85+
const weight2 = getSortingPriority(item2)
8786

8887
return weight2 - weight1
8988
}).sort(function (item1, item2) {
@@ -115,7 +114,8 @@ function findIn (elements, term, categoryName) {
115114
id: element.id,
116115
match: highlight(titleMatch),
117116
category: categoryName,
118-
matchQuality: matchQuality(titleMatch)
117+
matchQuality: matchQuality(titleMatch),
118+
group: element.group
119119
}, element.id)
120120

121121
results.push(parentResult)
@@ -199,7 +199,33 @@ function findMatchingChildren (elements, parentId, term, key) {
199199
}
200200

201201
/**
202-
* How good th
202+
* Chooses the right sorting priority for the given item.
203+
*
204+
* @param {Object} item Search result item.
205+
*
206+
* @returns {number} Sorting priority for a given item. Higher priority means higher position in search results.
207+
*/
208+
function getSortingPriority (item) {
209+
if (isException(item)) {
210+
return sortingPriority['Exception']
211+
}
212+
213+
return sortingPriority[item.category] || -1
214+
}
215+
216+
/**
217+
* Is the given item an exception?
218+
*
219+
* @param {Object} item Search result item.
220+
*
221+
* @returns {boolean}
222+
*/
223+
function isException (item) {
224+
return item.group === 'Exceptions'
225+
}
226+
227+
/**
228+
* How well search result marches the current query.
203229
*
204230
* @param {(Array|null)} match Information about the matched text (returned by String.match()).
205231
*

assets/js/events.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import sidebarItemsTemplate from './templates/sidebar-items.handlebars'
1616
var SIDEBAR_TYPES = [
1717
'#extras-list',
1818
'#modules-list',
19-
'#exceptions-list',
2019
'#tasks-list',
2120
'#search-list'
2221
]

assets/test/autocomplete/suggestions.spec.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {getSuggestions} from '../../js/autocomplete/suggestions'
33
describe('getSuggestions', () => {
44
before(() => {
55
window.sidebarNodes = {
6-
exceptions: [{
7-
id: 'My exception',
8-
title: 'My exception'
9-
}],
106
modules: [
7+
{
8+
id: 'My exception',
9+
title: 'My exception',
10+
group: 'Exceptions'
11+
},
1112
{
1213
id: 'Map',
1314
title: 'Map',
@@ -62,6 +63,7 @@ describe('getSuggestions', () => {
6263
expect(getSuggestions('Map').length).to.eql(1)
6364
expect(getSuggestions('Ecto.Repo').length).to.eql(1)
6465
expect(getSuggestions('phx.server').length).to.eql(1)
66+
expect(getSuggestions('My exception').length).to.eql(1)
6567
})
6668

6769
it('returns matching functions, callbacks and types', () => {
@@ -82,6 +84,7 @@ describe('getSuggestions', () => {
8284
})
8385

8486
it('is case insensitive', () => {
87+
expect(getSuggestions('My ExCePtIoN').length).to.eql(1)
8588
expect(getSuggestions('My ExCePtIoN')).to.eql(getSuggestions('my exception'))
8689
})
8790

@@ -127,6 +130,12 @@ describe('getSuggestions', () => {
127130
expect(firstResult.label).to.eql('type')
128131
})
129132

133+
it('includes module name in function description', () => {
134+
const firstResult = getSuggestions('get_by')[0]
135+
136+
expect(firstResult.description).to.eql('Ecto.Repo')
137+
})
138+
130139
it('generates a link for each suggestion', () => {
131140
expect(getSuggestions('has_many')[0].link).to.eql('Ecto.Schema.html#t:has_many/1')
132141
expect(getSuggestions('map')[0].link).to.eql('Map.html')
File renamed without changes.

formatters/html/dist/app-02948fd67e8f1d4dd1a5.js renamed to formatters/html/dist/app-a1d27018548e1b91d6b1.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ex_doc/formatter/html.ex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ defmodule ExDoc.Formatter.HTML do
2626
search_items = generate_search_items(project_nodes, extras, config)
2727

2828
nodes_map = %{
29-
modules: filter_list(:module, project_nodes),
30-
exceptions: filter_list(:exception, project_nodes),
29+
modules: filter_list(:module, project_nodes) |> group_exceptions(),
3130
tasks: filter_list(:task, project_nodes)
3231
}
3332

@@ -47,7 +46,6 @@ defmodule ExDoc.Formatter.HTML do
4746
generate_search(nodes_map, config) ++
4847
generate_not_found(nodes_map, config) ++
4948
generate_list(nodes_map.modules, nodes_map, config) ++
50-
generate_list(nodes_map.exceptions, nodes_map, config) ++
5149
generate_list(nodes_map.tasks, nodes_map, config) ++ generate_index(config)
5250

5351
generate_build(all_files, build)
@@ -392,13 +390,25 @@ defmodule ExDoc.Formatter.HTML do
392390
end
393391

394392
def filter_list(:module, nodes) do
395-
Enum.filter(nodes, &(not (&1.type in [:exception, :task])))
393+
Enum.filter(nodes, &(&1.type != :task))
396394
end
397395

398396
def filter_list(type, nodes) do
399397
Enum.filter(nodes, &(&1.type == type))
400398
end
401399

400+
@exceptions_group_name "Exceptions"
401+
defp group_exceptions(module_nodes) do
402+
module_nodes
403+
|> Enum.map(fn module_node ->
404+
case module_node.type do
405+
:exception -> %{module_node | group: @exceptions_group_name}
406+
_ -> module_node
407+
end
408+
end)
409+
|> Enum.sort_by(&(&1.type == :exception))
410+
end
411+
402412
defp generate_list(nodes, nodes_map, config) do
403413
nodes
404414
|> Task.async_stream(&generate_module_page(&1, nodes_map, config), timeout: :infinity)

lib/ex_doc/formatter/html/templates.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ defmodule ExDoc.Formatter.HTML.Templates do
164164
defp logo_path(%{logo: nil}), do: nil
165165
defp logo_path(%{logo: logo}), do: "assets/logo#{Path.extname(logo)}"
166166

167-
defp sidebar_type(:exception), do: "exceptions"
167+
defp sidebar_type(:exception), do: "modules"
168168
defp sidebar_type(:extra), do: "extras"
169169
defp sidebar_type(:module), do: "modules"
170170
defp sidebar_type(:behaviour), do: "modules"

lib/ex_doc/formatter/html/templates/api_reference_template.eex

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@
1414
</section>
1515
<% end %>
1616

17-
<%= if nodes_map.exceptions != [] do %>
18-
<section class="details-list">
19-
<h2 id="exceptions" class="section-heading">Exceptions</h2>
20-
<div class="summary">
21-
<%= for exception_node <- nodes_map.exceptions do
22-
api_reference_entry_template(exception_node)
23-
end %>
24-
</div>
25-
</section>
26-
<% end %>
27-
2817
<%= if nodes_map.tasks != [] do %>
2918
<section class="details-list">
3019
<h2 id="tasks" class="section-heading">Mix Tasks</h2>

lib/ex_doc/formatter/html/templates/sidebar_template.eex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
<li><a id="modules-list" href="#full-list">Modules</a></li>
4747
<% end %>
4848

49-
<%= if nodes_map.exceptions != [] do %>
50-
<li><a id="exceptions-list" href="#full-list">Exceptions</a></li>
51-
<% end %>
52-
5349
<%= if nodes_map.tasks != [] do %>
5450
<li><a id="tasks-list" href="#full-list">Mix Tasks</a></li>
5551
<% end %>

test/ex_doc/formatter/html_test.exs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ defmodule ExDoc.Formatter.HTMLTest do
215215
~r{"id":"Common\.Nesting\.Prefix\.Bar","nested_context":"Common\.Nesting\.Prefix","nested_title":"Bar","title":"Common\.Nesting\.Prefix\.Bar"}ms
216216
end
217217

218+
test "groups exceptions" do
219+
generate_docs(doc_config())
220+
221+
content = read_wildcard!("#{output_dir()}/dist/sidebar_items-*.js")
222+
223+
assert content =~
224+
~r{"group":"Exceptions","id":"RandomError","title":"RandomError"}ms
225+
end
226+
218227
describe "generates logo" do
219228
test "overriding previous entries" do
220229
File.mkdir_p!("#{output_dir()}/assets")
@@ -302,7 +311,6 @@ defmodule ExDoc.Formatter.HTMLTest do
302311

303312
content = read_wildcard!("#{output_dir()}/dist/sidebar_items-*.js")
304313
assert content =~ ~s("modules":[])
305-
assert content =~ ~s("exceptions":[])
306314

307315
assert content =~
308316
~s("extras":[{"group":"","headers":[],"id":"api-reference","title":"API Reference"},)

0 commit comments

Comments
 (0)