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

Syntax highlighted code source #87

Closed
tecosaur opened this issue Aug 17, 2021 · 5 comments
Closed

Syntax highlighted code source #87

tecosaur opened this issue Aug 17, 2021 · 5 comments
Labels
wontfix This will not be worked on

Comments

@tecosaur
Copy link

tecosaur commented Aug 17, 2021

Hey, this seemed nifty so I made a variant macro @code_src that prints a syntax-highlighted @code_string using OhMyREPL.
I have no idea if optional dependencies are easy, or if this of interest, but just in case here's what I have:

using Tokenize, Crayons, InteractiveUtils
"""
    @code_src
Use *InteractiveUtils* to try to find the source for the function or macro call,
and colourise it using *Tokenize*, *Crayons*, and *OhMyREPL*.

```julia-repl
julia> @code_src 2^2
^(x::T, p::T) where {T<:Integer} = power_by_squaring(x,p)
```
"""
macro code_src(expr...)
    codestr = InteractiveUtils.gen_call_with_extracted_types_and_kwargs(CodeTracking, :code_string, expr)
    quote
        tokens = collect(Tokenize.tokenize($codestr))
        crayons = Vector{Crayon}(undef, length(tokens))
        fill!(crayons, Crayon())
        OhMyREPL.Passes.SyntaxHighlighter.SYNTAX_HIGHLIGHTER_SETTINGS(crayons, tokens, 0)
        print(stdout, "  ")
        OhMyREPL.untokenize_with_ANSI(crayons, tokens, 2)
    end
end
@timholy
Copy link
Owner

timholy commented Aug 18, 2021

Very cool!

It can't go in this package, though, because this needs to stay extremely lightweight. It's the foundation for JuliaInterpreter, which is then a foundation for both the debuggers and for Revise. But an add-on package would make sense, or perhaps contributing that under a @require to OhMyREPL?

@Moelf
Copy link

Moelf commented Dec 7, 2021

KristofferC/OhMyREPL.jl#253

@timholy timholy added the wontfix This will not be worked on label Feb 4, 2023
@timholy
Copy link
Owner

timholy commented Feb 4, 2023

Closing this simply because of the need to keep this package very lightweight, otherwise it's quite cool.

@timholy timholy closed this as completed Feb 4, 2023
@tecosaur
Copy link
Author

tecosaur commented Feb 4, 2023

With weak deps I feel better about turning this into a PR based on weakdeps. Let's see if I can get around to that 🙂

@tecosaur
Copy link
Author

tecosaur commented May 2, 2024

Update: now that JuliaSyntaxHighlighting is about to be part of 1.11 (and I plan on making a compat release to General), it seems like the obvious lightweight way to do this.

As a weakdep, it will automatically be loaded in the REPL in 1.11+ (REPL > Markdown > JuliaSyntaxHighlighting), which makes this seem like a very obvious/simple approach to take.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants