Skip to content

Commit

Permalink
Apply syntax highlighting to Exprs in REPL (JuliaLang#54446)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tecosaur authored and lazarusA committed Aug 17, 2024
1 parent f6d57c5 commit 0f59ed7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
version = "1.11.0"

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

Expand Down
2 changes: 1 addition & 1 deletion stdlib/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
version = "1.11.0"

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

Expand Down
1 change: 1 addition & 0 deletions stdlib/REPL/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.11.0"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JuliaSyntaxHighlighting = "dc6e5ff7-fb65-4e79-a425-ec3bc9c03011"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
Expand Down
10 changes: 9 additions & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function __init__()
end

using Base.Meta, Sockets, StyledStrings
using JuliaSyntaxHighlighting
import InteractiveUtils

export
Expand Down Expand Up @@ -507,13 +508,20 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x)
# this can override the :limit property set initially
io = foldl(IOContext, d.repl.options.iocontext, init=io)
end
show(io, mime, x[])
show_repl(io, mime, x[])
println(io)
end
return nothing
end

display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)

show_repl(io::IO, mime::MIME"text/plain", x) = show(io, mime, x)

show_repl(io::IO, ::MIME"text/plain", ex::Expr) =
print(io, JuliaSyntaxHighlighting.highlight(
sprint(show, ex, context=IOContext(io, :color => false))))

function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
repl.waserror = response[2]
with_repl_linfo(repl) do io
Expand Down

0 comments on commit 0f59ed7

Please sign in to comment.