# Removing LineNumberNodes when displaying expressions with macros

**URL:** https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222
**Category:** General Usage
**Created:** [January 29, 2019, 9:23am UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222 "2019-01-29T09:23:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [January 29, 2019, 9:23am UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/1 "2019-01-29T09:23:24Z")

</div>

I’m trying to define a function to filter `LineNumberNodes` from an expression to pretty-print it without annotations:

```julia
filterLNN(x) = deepcopy(x)

function filterLNN(e::Expr)
    Expr(e.head, filterLNN.(filter(x->!isa(x, LineNumberNode), e.args))...)
end

```

```julia
julia> quote
         let acc = 1
           for i in 1:3
             acc += i
           end
           acc
         end
       end |> filterLNN

quote
    let acc = 1
        for i = 1:3
            acc += i
        end
        acc
    end
end

```

So far, so good, but my problem is that macro calls in the expression aren’t displayed anymore:

```julia
julia> quote
         let acc = 1
           @simd for i in 1:3
             acc += i
           end
           acc
         end
       end |> filterLNN

quote
    let acc = 1
        @simd
        acc
    end
end

```

I can display macro calls again if I leave at least their “accompanying” `LineNumberNode` in the expression:

```julia
function filterLNN(e::Expr)
    if e.head == :macrocall
        Expr(e.head, e.args[1:2]..., filterLNN.(filter(x->!isa(x, LineNumberNode), e.args[3:end]))...)
    else
        Expr(e.head, filterLNN.(filter(x->!isa(x, LineNumberNode), e.args))...)
    end
end

```

```julia
julia> quote
         let acc = 1
           @simd for i in 1:3
             acc += i
           end
           acc
         end
       end |> filterLNN

quote
    let acc = 1
        #= REPL[26]:3 =# @simd for i = 1:3
                acc += i
            end
        acc
    end
end

```

Is this expected behavior? Or should I look for a bug in the implementation of `display` for macro calls?

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [January 29, 2019, 11:05am UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/2 "2019-01-29T11:05:46Z")

</div>

There is [MacroTools.rmlines](https://github.com/MikeInnes/MacroTools.jl/blob/master/src/utils.jl) which doesn’t actually seem to work on your quote. My hack was [here](https://github.com/mcabbott/TensorCast.jl/blob/master/src/pretty.jl) simply to regex the string version… but surely there is a nicer way.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [January 29, 2019, 11:32am UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/3 "2019-01-29T11:32:53Z")

</div>

You can also use my tool `SyntaxTree.linefilter!` from [SyntaxTree.jl](https://github.com/chakravala/SyntaxTree.jl) for that

```nohighlight
julia> using SyntaxTree

julia> quote
           x = 1
           y = x+2
       end
quote
    #= REPL[2]:2 =#
    x = 1
    #= REPL[2]:3 =#
    y = x + 2
end

julia> linefilter!(ans)
quote
    x = 1
    y = x + 2
end

```

The `linefilter!` method is faster than MacroTools because it is recursively mutating

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [January 29, 2019, 12:19pm UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/4 "2019-01-29T12:19:52Z")

</div>

> [@mcabbott](#):
>
> There is [MacroTools.rmlines](https://github.com/MikeInnes/MacroTools.jl/blob/master/src/utils.jl) which doesn’t actually seem to work on your quote

Thanks! It looks like `rmlines` removes only one level of `LineNumberNode`s; [`striplines`](https://github.com/MikeInnes/MacroTools.jl/blob/master/src/utils.jl#L72) is recursive. Both these seem to handle macro calls like in my implementation, but at least it’s an improvement to use an already written implementation instead of having to write mine.

> [@mcabbott](#):
>
> My hack was [here](https://github.com/mcabbott/TensorCast.jl/blob/master/src/pretty.jl) simply to regex the string version… but surely there is a nicer way.

Thanks. I’ll consider doing that too if I don’t find a nicer way.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [January 29, 2019, 12:24pm UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/5 "2019-01-29T12:24:48Z")

</div>

> [@chakravala](#):
>
> You can also use my tool `SyntaxTree.linefilter!` from [SyntaxTree.jl](https://github.com/chakravala/SyntaxTree.jl) for that.

Thanks, but this function does not seem to handle macro calls in any special way, and therefore produces the same output as my first attempt above:

```julia
julia> quote
           let acc = 1
               @simd for i in 1:3
                   acc += i
               end
               acc
           end
       end |> SyntaxTree.linefilter

quote
    let acc = 1
        @simd
        acc
    end
end

```

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [January 29, 2019, 1:13pm UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/6 "2019-01-29T13:13:55Z")

</div>

Okay, I didnt test that, I will fix it later today since that’s a bug for me.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [January 29, 2019, 1:43pm UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/7 "2019-01-29T13:43:29Z")

</div>

> [@ffevotte](#):
>
> Thanks, but this function does not seem to handle macro calls in any special way, and therefore produces the same output as my first attempt above:

It is fixed in the `master` branch now, it can handle it correctly

```Julia
julia> quote
           let acc = 1
               @simd for i in 1:3
                   acc += i
               end
               acc
           end
       end |> SyntaxTree.linefilter!

quote
    let acc = 1
        @simd for i = 1:3
                acc += i
            end
        acc
    end
end

```

The problem is that `:macrocall` expressions now require a `LineNumberNode` or something to fill its place, so the solution is to simply put `nothing` in its place

> <https://github.com/chakravala/SyntaxTree.jl/blob/1570e0e9ee60fa0112718d92b87a5c9514a6766b/src/SyntaxTree.jl#L32-L34>

This bug fix will be tagged as an updated release today

---

<div class="post-metadata">

### Author: ![Clemapfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clemapfel/32/32261_2.png) [@Clemapfel](https://discourse.julialang.org/u/Clemapfel)
#### Post date: [January 8, 2022, 4:58pm UTC](https://discourse.julialang.org/t/removing-linenumbernodes-when-displaying-expressions-with-macros/20222/8 "2022-01-08T16:58:46Z")

</div>

I realize this is solved but for sport I figured I’ll write my own solution of `linefilter!` also using recursion, only tested in 1.7.1, maybe someone stumbling across this thread years later will find it useful:

Code:

```julia
macro unquote(expr::Expr)

        function aux!(args::Vector{Any}) ::Nothing

            to_delete = Vector{Integer}()
            for (i, x) in enumerate(args)
                if x isa LineNumberNode
                    push!(to_delete, i)
                elseif x isa Expr
                    aux!(x.args)
                end
            end

            n_deleted = 0;
            for i in to_delete
                deleteat!(args, i - n_deleted)
                n_deleted += 1
            end
        end

        aux!(expr.args)
        return Expr(expr.head, :($(expr.args...)))
    end

```

Usage:

```julia
julia> quoted = quote module m end; struct s end; i = 1; end

```

```julia
quote
    #= REPL[15]:1 =#
    module m
    #= REPL[15]:1 =#
    #= REPL[15]:1 =#
    end
    #= REPL[15]:1 =#
    struct s
        #= REPL[15]:1 =#
    end
    #= REPL[15]:1 =#
    i = 1
end

```

```julia
julia> unquoted = @unquote quote module m end; struct s end; i = 1; end

```

```julia
quote
    module m
    end
    struct s
    end
    i = 1
end

```

It is part of [jluna](https://github.com/Clemapfel/jluna) and can be used freely
