Skip to content

Commit c1bcf5d

Browse files
author
José Valim
committed
Support - lists in ANSIDocs, closes #1937
1 parent 11ab91c commit c1bcf5d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lib/iex/lib/iex/ansi_docs.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule IEx.ANSIDocs do
22
@moduledoc false
33

4+
@bullets [?*, ?-]
5+
46
@doc """
57
Prints the head of the documentation (i.e. the function signature)
68
"""
@@ -64,7 +66,7 @@ defmodule IEx.ANSIDocs do
6466
defp process([line | rest], indent) do
6567
{ stripped, count } = strip_spaces(line, 0)
6668
case stripped do
67-
"* " <> item ->
69+
<<bullet, ?\s, item :: binary >> when bullet in @bullets ->
6870
process_list(item, rest, count, indent)
6971
_ ->
7072
process_text(rest, [line], indent, false)
@@ -117,7 +119,7 @@ defmodule IEx.ANSIDocs do
117119
end
118120
end
119121

120-
defp process_list_next(["* " <> _ | _] = rest, _count, _done, acc) do
122+
defp process_list_next([<<bullet, ?\s, _ :: binary>> | _] = rest, _count, _done, acc) when bullet in @bullets do
121123
{ Enum.reverse(acc), rest, false }
122124
end
123125

@@ -129,7 +131,7 @@ defmodule IEx.ANSIDocs do
129131
{ Enum.reverse(acc), rest, done }
130132
end
131133

132-
defp list_next("* " <> _, 0), do: :done
134+
defp list_next(<<bullet, ?\s, _ :: binary>>, 0) when bullet in @bullets, do: :done
133135
defp list_next(line, 0), do: chop(line, 2)
134136
defp list_next(" " <> line, acc), do: list_next(line, acc - 1)
135137
defp list_next(line, _acc), do: line
@@ -151,7 +153,7 @@ defmodule IEx.ANSIDocs do
151153
defp process_text([line | rest], para, indent, true) do
152154
{ stripped, count } = strip_spaces(line, 0)
153155
case stripped do
154-
"* " <> item ->
156+
<<bullet, ?\s, item :: binary>> when bullet in @bullets ->
155157
write_text(Enum.reverse(para), indent, true)
156158
process_list(item, rest, count, indent)
157159
_ ->

lib/iex/test/iex/ansi_docs_test.exs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,56 @@ defmodule IEx.AnsiDocsTest do
4949
assert result == "line\n\e[0m\n\e[36m\e[1m┃ code\n┃ code2\e[0m\n\e[0m\nline2\n\e[0m"
5050
end
5151

52-
test "list is converted" do
52+
test "* list is converted" do
5353
result = format("* one\n* two\n* three\n")
5454
assert result == "• one\n• two\n• three\n\e[0m"
5555
end
5656

57-
test "list surrounded by text is converted" do
57+
test "* list surrounded by text is converted" do
5858
result = format("Count:\n\n* one\n* two\n* three\n\nDone")
5959
assert result == "Count:\n\e[0m\n• one\n• two\n• three\n\e[0m\nDone\n\e[0m"
6060
end
6161

62-
test "list with continuation is converted" do
62+
test "* list with continuation is converted" do
6363
result = format("* one\n two\n three\n* four")
6464
assert result == "• one two three\n• four"
6565
end
6666

67-
test "nested lists are converted" do
67+
test "* nested lists are converted" do
6868
result = format("* one\n * one.one\n * one.two\n* two")
6969
assert result == "• one\n • one.one\n • one.two\n• two"
7070
end
7171

72-
test "lists with spaces are converted" do
72+
test "* lists with spaces are converted" do
7373
result = format(" * one\n * two\n * three")
7474
assert result == "• one\n• two\n• three"
7575
end
7676

77+
test "- list is converted" do
78+
result = format("- one\n- two\n- three\n")
79+
assert result == "• one\n• two\n• three\n\e[0m"
80+
end
81+
82+
test "- list surrounded by text is converted" do
83+
result = format("Count:\n\n- one\n- two\n- three\n\nDone")
84+
assert result == "Count:\n\e[0m\n• one\n• two\n• three\n\e[0m\nDone\n\e[0m"
85+
end
86+
87+
test "- list with continuation is converted" do
88+
result = format("- one\n two\n three\n- four")
89+
assert result == "• one two three\n• four"
90+
end
91+
92+
test "- nested lists are converted" do
93+
result = format("- one\n - one.one\n - one.two\n- two")
94+
assert result == "• one\n • one.one\n • one.two\n• two"
95+
end
96+
97+
test "- lists with spaces are converted" do
98+
result = format(" - one\n - two\n - three")
99+
assert result == "• one\n• two\n• three"
100+
end
101+
77102
test "paragraphs are split" do
78103
result = format("para1\n\npara2")
79104
assert result == "para1\n\e[0m\npara2\n\e[0m"

0 commit comments

Comments
 (0)