Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply syntax highlighting to Exprs in REPL #54446

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

tecosaur
Copy link
Contributor

As discussed on slack a while ago, it's now near trivial to make inspecting large Exprs much more pleasant with JuliaSyntax highlighting.

See the commit message for more details.

Screenshots

image

image

@tecosaur tecosaur added stdlib:REPL Julia's REPL (Read Eval Print Loop) domain:display and printing Aesthetics and correctness of printed representations of objects. labels May 12, 2024
@tecosaur
Copy link
Contributor Author

If anyone's up for it, I think this should be a pretty quick and easy review & merge 🙂

Copy link
Sponsor Member

@KristofferC KristofferC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done without type piracy and respect the REPL IOContext:

Before:

julia> :(quote 123.32132132132 end)
:($(Expr(:quote, quote
    #= REPL[1]:1 =#
    123.32132132132
end)))

julia> Base.active_repl.options.iocontext[:compact] = true
true

julia> :(quote 123.32132132132 end)
:($(Expr(:quote, quote
    #= REPL[3]:1 =#
    123.321
end)))

After

julia> :(quote 123.32132132132 end)
:($(Expr(:quote, quote
    #= REPL[1]:1 =#
    123.32132132132
end)))

julia> Base.active_repl.options.iocontext[:compact] = true
true

julia> :(quote 123.32132132132 end)
:($(Expr(:quote, quote
    #= REPL[3]:1 =#
    123.32132132132
end)))

@tecosaur
Copy link
Contributor Author

@fredrikekre / @KristofferC any thoughts on the current version of the code?

@@ -23,6 +23,7 @@ using Base.Meta

import Markdown
import StyledStrings
import JuliaSyntaxHighlighting
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure, there was some conversation I had (on Slack) which concluded with the import StyledStrings line being needed in #51869 even though it's not used in precompile.jl, so I've just "followed the pattern" here.

I could just try dropping the line and seeing what happens?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a mechanism for how it should be needed so it's probably good to try to remove it and see what happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying... 🤞

@KristofferC
Copy link
Sponsor Member

The current version seems ok to me at least. Does it still obey --color=no?

@tecosaur
Copy link
Contributor Author

Yep, since it's just printing via. StyledStrings now, and StyledStrings respects the colour property as expected.

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.
Copy link
Contributor

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me now, but it'd be great if @KristofferC and/or @fredrikekre could have a final look (and then perhaps directly merge it if they are happy)

@tecosaur
Copy link
Contributor Author

Bump @KristofferC / @fredrikekre

@fingolfin
Copy link
Contributor

This looks really simple and straight-forward to me, I hope it can be merged soon -- it's been ready (at least from my POV) for over 2 months

@fingolfin fingolfin merged commit 9dd49c0 into JuliaLang:master Jul 24, 2024
7 checks passed
@tecosaur tecosaur deleted the highlight-expr-in-repl branch August 11, 2024 04:46
lazarusA pushed a commit to lazarusA/julia that referenced this pull request Aug 17, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects. stdlib:REPL Julia's REPL (Read Eval Print Loop)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants