Skip to content

Commit 1ab2af2

Browse files
authored
Fix highlighting of empty source ranges (#231)
Highlighting of empty ranges needs special handling at the ends of lines as the source line of the end of the range can be less than the source line at the start of the range.
1 parent 6b947db commit 1ab2af2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/source_files.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,18 @@ function highlight(io::IO, source::SourceFile, range::UnitRange;
183183
context_lines_before=context_lines_before,
184184
context_lines_after=context_lines_inner)
185185
a,b = source_line_range(source, p)
186-
c,d = source_line_range(source, q)
187-
z,w = source_line_range(source, q;
186+
q1 = max(q, p) # Ignore q for empty ranges
187+
c,d = source_line_range(source, q1)
188+
z,w = source_line_range(source, q1;
188189
context_lines_before=context_lines_inner,
189190
context_lines_after=context_lines_after)
190191

191192
p_line = source_line(source, p)
192-
q_line = source_line(source, q)
193+
q_line = source_line(source, q)
193194

194195
marker_line_color = :light_black
195196

196-
if p_line == q_line
197+
if p_line >= q_line
197198
# x-----------------
198199
# a---p-------q----b
199200
# # └───────┘ ── note

test/source_files.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,22 @@ end
5757
αβγδ
5858
+-*/""")
5959

60+
# Empty ranges
61+
@test sprint(highlight, src, 1:0) == "abcd\n\nαβγδ\n+-*/"
62+
@test sprint(highlight, src, 2:1) == "abcd\n#└\nαβγδ\n+-*/"
63+
@test sprint(highlight, src, 3:2) == "abcd\n# └\nαβγδ\n+-*/"
64+
@test sprint(highlight, src, 4:3) == "abcd\n# └\nαβγδ\n+-*/"
65+
@test sprint(highlight, src, 5:4) == "abcd\n# └\nαβγδ\n+-*/"
66+
@test sprint(highlight, src, 6:5) == "abcd\nαβγδ\n\n+-*/"
67+
@test sprint(highlight, src, 19:18) == "abcd\nαβγδ\n+-*/\n# └"
68+
@test sprint(io->highlight(io, src, 1:0, context_lines_after=0, note="hi")) ==
69+
"abcd\n└ ── hi"
70+
71+
# Single line ranges
6072
@test sprint(highlight, src, 1:4) == "abcd\n└──┘\nαβγδ\n+-*/"
6173
@test sprint(highlight, src, 2:4) == "abcd\n#└─┘\nαβγδ\n+-*/"
6274
@test sprint(highlight, src, 3:4) == "abcd\n# └┘\nαβγδ\n+-*/"
6375
@test sprint(highlight, src, 4:4) == "abcd\n# ╙\nαβγδ\n+-*/"
64-
@test sprint(highlight, src, 4:3) == "abcd\n# └\nαβγδ\n+-*/"
6576
@test sprint(highlight, src, 5:5) == "abcd\n# └\nαβγδ\n+-*/"
6677

6778
# multi-byte chars

0 commit comments

Comments
 (0)