Skip to content

Commit

Permalink
Merge pull request #22 from MichaelHatherly/mh/pkgexts
Browse files Browse the repository at this point in the history
Switch to using package extensions
  • Loading branch information
MichaelHatherly authored Oct 6, 2023
2 parents a52cde7 + 16d5226 commit 718650a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 53 deletions.
17 changes: 16 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions ext/InteractiveErrorsCthulhuExt.jl
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions ext/InteractiveErrorsDebuggerExt.jl
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions ext/InteractiveErrorsJETExt.jl
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions ext/InteractiveErrorsJuliaFormatterExt.jl
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions ext/InteractiveErrorsOhMyREPLExt.jl
Original file line number Diff line number Diff line change
@@ -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
57 changes: 5 additions & 52 deletions src/InteractiveErrors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down Expand Up @@ -361,7 +363,7 @@ end


#
# Requires.
# Extensions, these get extended in the `/ext` modules.
#

has_cthulhu(args...) = false
Expand All @@ -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

0 comments on commit 718650a

Please sign in to comment.