Skip to content

Commit 2929aa9

Browse files
committed
Apply syntax highlighting to Exprs in REPL
Large exprs (such as those produced by @macroexpand) can be hard to read and interpret. This is a problem made easier by JuliaSyntaxHighlighting, and with it we can easily apply syntax highlighting when showing exprs. Normally this would be implemented in Base's show method for Exprs, however since JuliaSyntaxHighlighting is a stdlib we must look elsewhere, and REPL seems like a sensible place. To ensure that the IOContext of the REPL IO is properly respected, we change the show invocation within the REPL display method to use show_repl which falls back to just calling show, as before. We then add a show_repl(::IO, ::MIME"text/plain", ::Expr) specialisation, which prints expressions with syntax highlighting.
1 parent b9f68ac commit 2929aa9

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

doc/Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
219219
version = "1.11.0"
220220

221221
[[deps.REPL]]
222-
deps = ["InteractiveUtils", "Markdown", "Sockets", "StyledStrings", "Unicode"]
222+
deps = ["InteractiveUtils", "JuliaSyntaxHighlighting", "Markdown", "Sockets", "StyledStrings", "Unicode"]
223223
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
224224
version = "1.11.0"
225225

stdlib/Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
194194
version = "1.11.0"
195195

196196
[[deps.REPL]]
197-
deps = ["InteractiveUtils", "Markdown", "Sockets", "StyledStrings", "Unicode"]
197+
deps = ["InteractiveUtils", "JuliaSyntaxHighlighting", "Markdown", "Sockets", "StyledStrings", "Unicode"]
198198
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
199199
version = "1.11.0"
200200

stdlib/REPL/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "1.11.0"
44

55
[deps]
66
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
7+
JuliaSyntaxHighlighting = "dc6e5ff7-fb65-4e79-a425-ec3bc9c03011"
78
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
89
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
910
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"

stdlib/REPL/src/REPL.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function __init__()
101101
end
102102

103103
using Base.Meta, Sockets, StyledStrings
104+
using JuliaSyntaxHighlighting
104105
import InteractiveUtils
105106

106107
export
@@ -380,13 +381,20 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x)
380381
# this can override the :limit property set initially
381382
io = foldl(IOContext, d.repl.options.iocontext, init=io)
382383
end
383-
show(io, mime, x[])
384+
show_repl(io, mime, x[])
384385
println(io)
385386
end
386387
return nothing
387388
end
389+
388390
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
389391

392+
show_repl(io::IO, mime::MIME"text/plain", x) = show(io, mime, x)
393+
394+
show_repl(io::IO, ::MIME"text/plain", ex::Expr) =
395+
print(io, JuliaSyntaxHighlighting.highlight(
396+
sprint(show, ex, context=IOContext(io, :color => false))))
397+
390398
function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
391399
repl.waserror = response[2]
392400
with_repl_linfo(repl) do io

0 commit comments

Comments
 (0)