Skip to content

Commit

Permalink
Merge pull request #200 from FluxML/qq
Browse files Browse the repository at this point in the history
Define @qq
  • Loading branch information
cstjean authored Jan 15, 2024
2 parents 6b3034a + 7547d12 commit 2b843b2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.0'
- '1.6'
- '1'
- 'nightly'
os: [ubuntu-latest, macOS-latest, windows-latest]
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name = "MacroTools"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.12"
version = "0.5.13"

[deps]
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
julia = "1"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
1 change: 1 addition & 0 deletions docs/src/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ MacroTools.combinearg

```@docs
MacroTools.@q
MacroTools.@qq
MacroTools.isexpr
MacroTools.rmlines
MacroTools.unblock
Expand Down
23 changes: 23 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ macro q(ex)
esc(Expr(:quote, striplines(ex)))
end

struct Flag end

to_flag(ex) = MacroTools.prewalk(x->x isa LineNumberNode ? Flag() : x, ex)
to_line(l::LineNumberNode, ex) = MacroTools.prewalk(x->x isa Flag ? l : x, ex)

"""
@qq [expression]
Like the `quote` keyword but replace construction site line numbers with __source__.
Line numbers of interpolated expressions are preserved. The result is that line numbers will be
attributed to the macro usage site, instead of the macro source code.
Only works inside of a macro definition.
See also: [`@q`](@ref)
"""
macro qq(ex)
# This was a difficult macro to write; the round-trip to Flag appears to be necessary.
# Crucially, we want interpolated expressions to preserve their line numbers.
esc(:($MacroTools.to_line(__source__, $(Expr(:quote, to_flag(ex))))))
end


"""
isexpr(x, ts...)
Expand Down
16 changes: 15 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MacroTools: isdef, flatten, striplines
using MacroTools: isdef, flatten, striplines, @qq

@testset "utils" begin
ex1 = :(function foo(a) return a; end)
Expand Down Expand Up @@ -45,3 +45,17 @@ end
@test flatten(ex) |> striplines == ex |> striplines
end
end

## Test for @qq

macro my_fff_def(a)
@qq function fff() $a end
end

@my_fff_def begin # line where fff() is defined
function g() # line where fff()() is defined
22
end
end

@test which(fff,()).line == which(fff(),()).line - 1

2 comments on commit 2b843b2

@cstjean
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/98881

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.13 -m "<description of version>" 2b843b20f271c63158eb07d2940cb73987395aa4
git push origin v0.5.13

Please sign in to comment.