From 16d5226725cdc783ab3cc97b07d161d4ce3976e4 Mon Sep 17 00:00:00 2001 From: Michael Hatherly Date: Fri, 6 Oct 2023 09:35:24 +0100 Subject: [PATCH] Switch to using package extensions --- Project.toml | 17 ++++++- ext/InteractiveErrorsCthulhuExt.jl | 12 +++++ ext/InteractiveErrorsDebuggerExt.jl | 10 ++++ ext/InteractiveErrorsJETExt.jl | 17 +++++++ ext/InteractiveErrorsJuliaFormatterExt.jl | 17 +++++++ ext/InteractiveErrorsOhMyREPLExt.jl | 22 +++++++++ src/InteractiveErrors.jl | 57 ++--------------------- 7 files changed, 99 insertions(+), 53 deletions(-) create mode 100644 ext/InteractiveErrorsCthulhuExt.jl create mode 100644 ext/InteractiveErrorsDebuggerExt.jl create mode 100644 ext/InteractiveErrorsJETExt.jl create mode 100644 ext/InteractiveErrorsJuliaFormatterExt.jl create mode 100644 ext/InteractiveErrorsOhMyREPLExt.jl diff --git a/Project.toml b/Project.toml index bfaad83..a69235f 100644 --- a/Project.toml +++ b/Project.toml @@ -8,14 +8,29 @@ AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" FoldingTrees = "1eca21be-9b9b-4ed8-839a-6d8ae26b1781" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e" +PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" Requires = "ae029012-a4dd-5104-9daa-d747884805df" +[weakdeps] +Cthulhu = "f68482b8-f384-11e8-15f7-abe071a5a75f" +Debugger = "31a5f54b-26ea-5ae9-a837-f05ce5417438" +JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" +JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" +OhMyREPL = "5fb14364-9ced-5910-84b2-373655c76a03" + +[extensions] +InteractiveErrorsCthulhuExt = "Cthulhu" +InteractiveErrorsDebuggerExt = "Debugger" +InteractiveErrorsJETExt = "JET" +InteractiveErrorsJuliaFormatterExt = "JuliaFormatter" +InteractiveErrorsOhMyREPLExt = "OhMyREPL" + [compat] AbstractTrees = "0.3, 0.4" FoldingTrees = "1.1" IterTools = "1" -Requires = "1" +PackageExtensionCompat = "1" julia = "1.6" [extras] diff --git a/ext/InteractiveErrorsCthulhuExt.jl b/ext/InteractiveErrorsCthulhuExt.jl new file mode 100644 index 0000000..ca8b173 --- /dev/null +++ b/ext/InteractiveErrorsCthulhuExt.jl @@ -0,0 +1,12 @@ +module InteractiveErrorsCthulhuExt + +import Cthulhu +import InteractiveErrors + +InteractiveErrors.has_cthulhu() = true + +InteractiveErrors.ascend(mi::Core.MethodInstance) = Cthulhu.ascend(mi) + +InteractiveErrors.descend(mi::Core.MethodInstance) = Cthulhu.descend(mi) + +end diff --git a/ext/InteractiveErrorsDebuggerExt.jl b/ext/InteractiveErrorsDebuggerExt.jl new file mode 100644 index 0000000..f14e1e3 --- /dev/null +++ b/ext/InteractiveErrorsDebuggerExt.jl @@ -0,0 +1,10 @@ +module InteractiveErrorsDebuggerExt + +import Debugger +import InteractiveErrors + +InteractiveErrors.has_debugger() = true + +InteractiveErrors.breakpoint(file::AbstractString, line::Integer) = Debugger.breakpoint(file, line) + +end diff --git a/ext/InteractiveErrorsJETExt.jl b/ext/InteractiveErrorsJETExt.jl new file mode 100644 index 0000000..85bed39 --- /dev/null +++ b/ext/InteractiveErrorsJETExt.jl @@ -0,0 +1,17 @@ +module InteractiveErrorsJETExt + +import JET +import InteractiveErrors + +InteractiveErrors.has_jet() = true + +function InteractiveErrors.report_call(mi::Core.MethodInstance) + func = Base.tuple_type_head(mi.specTypes).instance + sig = Base.tuple_type_tail(mi.specTypes) + result = JET.report_call(func, sig) + @info "Press return to continue." + readline() + return result +end + +end diff --git a/ext/InteractiveErrorsJuliaFormatterExt.jl b/ext/InteractiveErrorsJuliaFormatterExt.jl new file mode 100644 index 0000000..443b070 --- /dev/null +++ b/ext/InteractiveErrorsJuliaFormatterExt.jl @@ -0,0 +1,17 @@ +module InteractiveErrorsJuliaFormatterExt + +import JuliaFormatter +import InteractiveErrors + +InteractiveErrors.has_juliaformatter() = true + +function InteractiveErrors.format_julia_source(source::String) + try + return JuliaFormatter.format_text(source) + catch err + @debug "failed to format source" err source + return source + end +end + +end diff --git a/ext/InteractiveErrorsOhMyREPLExt.jl b/ext/InteractiveErrorsOhMyREPLExt.jl new file mode 100644 index 0000000..ba64587 --- /dev/null +++ b/ext/InteractiveErrorsOhMyREPLExt.jl @@ -0,0 +1,22 @@ +module InteractiveErrorsOhMyREPLExt + +import OhMyREPL +import InteractiveErrors + +InteractiveErrors.has_ohmyrepl() = true + +function InteractiveErrors.highlight(source::String) + O = OhMyREPL + tokens = collect(O.tokenize(source)) + crayons = fill(O.Crayon(), length(tokens)) + O.Passes.SyntaxHighlighter.SYNTAX_HIGHLIGHTER_SETTINGS(crayons, tokens, 0, source) + io = IOBuffer() + for (token, crayon) in zip(tokens, crayons) + print(io, crayon) + print(io, O.untokenize(token, source)) + print(io, O.Crayon(reset = true)) + end + return String(take!(io)) +end + +end diff --git a/src/InteractiveErrors.jl b/src/InteractiveErrors.jl index baa81fa..91ee633 100644 --- a/src/InteractiveErrors.jl +++ b/src/InteractiveErrors.jl @@ -2,7 +2,9 @@ module InteractiveErrors using FoldingTrees -using REPL, REPL.TerminalMenus, InteractiveUtils, IterTools, Requires +using REPL, REPL.TerminalMenus, InteractiveUtils, IterTools + +import PackageExtensionCompat export toggle, current_theme, set_theme!, reset_theme!, adjust_theme! @@ -361,7 +363,7 @@ end # -# Requires. +# Extensions, these get extended in the `/ext` modules. # has_cthulhu(args...) = false @@ -380,62 +382,13 @@ format_julia_source(source) = source has_ohmyrepl(args...) = false highlight(source) = style(source, :file_contents) -function requires() - @require Cthulhu = "f68482b8-f384-11e8-15f7-abe071a5a75f" begin - has_cthulhu() = true - ascend(mi::Core.MethodInstance) = Cthulhu.ascend(mi) - descend(mi::Core.MethodInstance) = Cthulhu.descend(mi) - end - @require Debugger = "31a5f54b-26ea-5ae9-a837-f05ce5417438" begin - has_debugger() = true - breakpoint(file::AbstractString, line::Integer) = Debugger.breakpoint(file, line) - end - @require JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" begin - has_jet() = true - function report_call(mi::Core.MethodInstance) - func = Base.tuple_type_head(mi.specTypes).instance - sig = Base.tuple_type_tail(mi.specTypes) - result = JET.report_call(func, sig) - @info "Press return to continue." - readline() - return result - end - end - @require JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" begin - has_juliaformatter() = true - format_julia_source(source::String) = - try - JuliaFormatter.format_text(source) - catch err - source - end - end - @require OhMyREPL = "5fb14364-9ced-5910-84b2-373655c76a03" begin - has_ohmyrepl() = true - function highlight(source::String) - O = OhMyREPL - tokens = collect(O.tokenize(source)) - crayons = fill(O.Crayon(), length(tokens)) - O.Passes.SyntaxHighlighter.SYNTAX_HIGHLIGHTER_SETTINGS(crayons, tokens, 0, source) - io = IOBuffer() - for (token, crayon) in zip(tokens, crayons) - print(io, crayon) - print(io, O.untokenize(token, source)) - print(io, O.Crayon(reset = true)) - end - return String(take!(io)) - end - end -end - - # # Module Initialisation. # function __init__() setup_repl() - requires() + PackageExtensionCompat.@require_extensions end end # module