timholy / CodeTracking.jl

It's editing-time, do you know where your methods are?
MIT License
132 stars 10 forks source link

Syntax highlighted code source #87

Closed tecosaur closed 1 year ago

tecosaur commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

https://github.com/KristofferC/OhMyREPL.jl/pull/253

timholy commented 1 year ago

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

tecosaur commented 1 year ago

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 commented 5 months ago

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.