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

add @code_src #253

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions Project.toml
Expand Up @@ -4,23 +4,26 @@ version = "0.5.10"

[deps]
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JLFzf = "1019f520-868f-41f5-a6de-eb00f4b6a39c"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Tokenize = "0796e94c-ce3b-5d07-9a54-7f471281c624"

[compat]
Crayons = "1, 2, 3, 4"
Tokenize = "0.5"
JLFzf = "^0.1.1"
Tokenize = "0.5"
julia = "1.0"

[extras]
TerminalRegressionTests = "98bfdc55-cc95-5876-a49a-74609291cbe0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
VT100 = "7774df62-37c0-5c21-b34d-f6d7f98f54bc"
CodeTracking="da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"

[targets]
test = ["Test", "TerminalRegressionTests", "VT100"]
test = ["Test", "TerminalRegressionTests", "VT100", "CodeTracking"]
12 changes: 9 additions & 3 deletions docs/Manifest.toml
Expand Up @@ -102,10 +102,10 @@ uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[OhMyREPL]]
deps = ["Crayons", "JLFzf", "Markdown", "Pkg", "Printf", "REPL", "Tokenize"]
deps = ["Crayons", "InteractiveUtils", "JLFzf", "Markdown", "Pkg", "Printf", "REPL", "Requires", "Tokenize"]
path = ".."
uuid = "5fb14364-9ced-5910-84b2-373655c76a03"
version = "0.5.9"
version = "0.5.10"

[[Parsers]]
deps = ["Dates"]
Expand Down Expand Up @@ -137,9 +137,15 @@ deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Requires]]
deps = ["UUIDs"]
git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.3.0"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

Expand Down
15 changes: 15 additions & 0 deletions docs/src/features/code_src.md
@@ -0,0 +1,15 @@
# `@code_src` macro

The macro allows you to display source code of a method in-line,
just like `@code_llvm` and `@code_typed`. Inspired by:
https://github.com/JuliaLang/julia/issues/2625#issuecomment-498840808

```julia-repl
julia> @code_src sum(1:3)
function sum(r::AbstractRange{<:Real})
l = length(r)
# note that a little care is required to avoid overflow in l*(l-1)/2
return l * first(r) + (iseven(l) ? (step(r) * (l-1)) * (l>>1)
: (step(r) * l) * ((l-1)>>1))
end
```
1 change: 1 addition & 0 deletions docs/src/index.md
Expand Up @@ -19,6 +19,7 @@ Pages = [
"features/rainbow_brackets.md",
"features/markdown_highlight.md",
"features/fzf.md",
"features/code_src.md",
"internals/passes.md"
]
Depth = 1
Expand Down
13 changes: 13 additions & 0 deletions src/CodeTracking.jl
@@ -0,0 +1,13 @@
import InteractiveUtils
using .CodeTracking
export @code_src

macro code_src(expr...)
codestr = InteractiveUtils.gen_call_with_extracted_types_and_kwargs(CodeTracking, :code_string, expr)
quote
tokens = collect(Tokenize.tokenize($codestr))
crayons = fill(Crayon(), length(tokens))
OhMyREPL.Passes.SyntaxHighlighter.SYNTAX_HIGHLIGHTER_SETTINGS(crayons, tokens, 0)
OhMyREPL.untokenize_with_ANSI(crayons, tokens, 0)
end
end
2 changes: 2 additions & 0 deletions src/OhMyREPL.jl
Expand Up @@ -7,6 +7,7 @@ module OhMyREPL

using Tokenize
using Crayons
using Requires
if VERSION > v"1.3"
import JLFzf
end
Expand Down Expand Up @@ -86,6 +87,7 @@ const ENABLE_FZF = Ref(true)
enable_fzf(v::Bool) = ENABLE_FZF[] = v

function __init__()
@require CodeTracking="da1fd8a2-8d9e-5ec2-8556-3022fb5608a2" include("CodeTracking.jl")
options = Base.JLOptions()
# command-line
if (options.isinteractive != 1) && options.commands != C_NULL
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Expand Up @@ -9,4 +9,21 @@ withenv("FORCE_COLOR" => true) do
include("test_bracketmatch.jl")
include("test_highlighter.jl")
include("test_rainbowbrackets.jl")

end

using Test, OhMyREPL, CodeTracking
@testset "code_src macro" begin
original_stdout = stdout

(rd, wr) = redirect_stdout();

@code_src sum(1:3)
redirect_stdout(original_stdout)
close(wr)

s = read(rd, String)
redirect_stdout(original_stdout)
@test occursin("\e[", s)
@test occursin("AbstractRange",s)
end