Skip to content

Commit fe77575

Browse files
author
Rogerluo
authored
Fix Vararg printing (#113)
1 parent 9da0885 commit fe77575

File tree

9 files changed

+84
-19
lines changed

9 files changed

+84
-19
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
matrix:
1111
version:
1212
- '1.0'
13-
- '1.5'
13+
- '1'
1414
- 'nightly'
1515
os:
1616
- ubuntu-latest
@@ -48,7 +48,7 @@ jobs:
4848
- uses: actions/checkout@v2
4949
- uses: julia-actions/setup-julia@v1
5050
with:
51-
version: '1.5'
51+
version: '1.6'
5252
- run: |
5353
julia --project=docs -e '
5454
using Pkg

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ docs/build
55
docs/site
66
docs/Manifest.toml
77
Manifest.toml
8+
.vscode

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
| **Documentation** | **Build Status** |
66
|:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|
7-
| [![][docs-stable-img]][docs-stable-url] [![][docs-latest-img]][docs-latest-url] | [![][travis-img]][travis-url] [![][appveyor-img]][appveyor-url] [![][codecov-img]][codecov-url] |
7+
| [![][docs-stable-img]][docs-stable-url] [![][docs-latest-img]][docs-latest-url] | [![CI][github-action-img]][github-action-url] [![][codecov-img]][codecov-url] |
88

99
## Installation
1010

@@ -37,6 +37,9 @@ Contributions are very welcome, as are feature requests and suggestions. Please
3737
[docs-stable-img]: https://img.shields.io/badge/docs-stable-blue.svg
3838
[docs-stable-url]: https://juliadocs.github.io/DocStringExtensions.jl/stable
3939

40+
[github-action-img]: https://github.com/JuliaDocs/DocStringExtensions.jl/actions/workflows/ci.yml/badge.svg
41+
[github-action-url]: https://github.com/JuliaDocs/DocStringExtensions.jl/actions/workflows/ci.yml
42+
4043
[travis-img]: https://travis-ci.org/JuliaDocs/DocStringExtensions.jl.svg?branch=master
4144
[travis-url]: https://travis-ci.org/JuliaDocs/DocStringExtensions.jl
4245

src/DocStringExtensions.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
__precompile__(true)
2-
31
"""
42
*Extensions for the Julia docsystem.*
53

src/abbreviations.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ function format(::MethodSignatures, buf, doc)
320320
local modname = doc.data[:module]
321321
local func = Docs.resolve(binding)
322322
local groups = methodgroups(func, typesig, modname)
323+
323324
if !isempty(groups)
324325
println(buf)
325326
println(buf, "```julia")
@@ -518,10 +519,6 @@ function print_mutable_struct_or_struct(buf, object)
518519
println(buf)
519520
end
520521

521-
@static if VERSION < v"0.7.0"
522-
isprimitivetype(x) = isbitstype(x)
523-
end
524-
525522
function format(::TypeDefinition, buf, doc)
526523
local binding = doc.data[:binding]
527524
local object = gettype(Docs.resolve(binding))

src/templates.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ template for modules found in module `ModName`.
6565
6666
"""
6767
macro template(ex)
68-
# JuliaLang/julia#22064 introduced the __module__ variable and deprecated current_module()
69-
@static if VERSION >= v"0.7.0-DEV.484"
70-
template(__source__, __module__, ex)
71-
else
72-
template(LineNumberNode(0), current_module(), ex)
73-
end
68+
template(__source__, __module__, ex)
7469
end
7570

7671
const TEMP_SYM = gensym("templates")

src/utilities.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,23 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
292292
print(buffer, "(")
293293
local args = arguments(method)
294294
local where_syntax = []
295+
295296
for (i, sym) in enumerate(args)
296297
t = typesig.types[i]
297-
print(buffer, "$sym::$t")
298+
if isvarargtype(t)
299+
elt = vararg_eltype(t)
300+
301+
if elt === Any
302+
print(buffer, "$sym...")
303+
else
304+
print(buffer, "$sym::$elt...")
305+
end
306+
elseif t === Any
307+
print(buffer, sym)
308+
else
309+
print(buffer, "$sym::$t")
310+
end
311+
298312
if i != length(args)
299313
print(buffer, ", ")
300314
end
@@ -317,6 +331,35 @@ printmethod(b, f, m) = String(take!(printmethod(IOBuffer(), b, f, m)))
317331
get_method_source(m::Method) = Base.uncompressed_ast(m)
318332
nargs(m::Method) = m.nargs
319333

334+
function isvarargtype(t)
335+
@static if VERSION > v"1.7-"
336+
t isa Core.TypeofVararg
337+
elseif VERSION > v"1.5-"
338+
t isa Type && t <: Vararg
339+
else
340+
# don't special print Vararg
341+
# below 1.5
342+
false
343+
end
344+
end
345+
346+
function vararg_eltype(t)
347+
@static if VERSION > v"1.7-"
348+
return t.T
349+
elseif VERSION > v"1.5-"
350+
if t isa DataType
351+
return t.parameters[1]
352+
elseif t isa UnionAll
353+
return t.body.parameters[1]
354+
else
355+
# don't know how to handle
356+
# just return Any
357+
return Any
358+
end
359+
else
360+
error("cannot handle Vararg below 1.5")
361+
end
362+
end
320363

321364
"""
322365
$(:SIGNATURES)

test/TestModule/M.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ k_7(x::Union{T,Nothing}, y::T = zero(T)) where {T <: Integer} = x
3333
k_8(x) = x
3434
k_9(x::T where T<:Any) = x
3535
k_10(x::T) where T = x
36+
k_11(x::Int, xs...) = x
37+
k_12(x::Int, xs::Real...) = x
3638

3739
mutable struct T
3840
a

test/tests.jl

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ end
259259
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
260260
str = String(take!(buf))
261261
@test occursin("\n```julia\n", str)
262-
@test occursin("\nk_3(x::Any, y::T, z::U) -> Any\n", str)
262+
@test occursin("\nk_3(x, y::T, z::U) -> Any\n", str)
263263
@test occursin("\n```\n", str)
264264

265265
doc.data = Dict(
@@ -338,7 +338,7 @@ end
338338
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
339339
str = String(take!(buf))
340340
@test occursin("\n```julia\n", str)
341-
@test occursin("\nk_8(x::Any) -> Any\n", str)
341+
@test occursin("\nk_8(x) -> Any\n", str)
342342
@test occursin("\n```\n", str)
343343

344344
doc.data = Dict(
@@ -349,9 +349,35 @@ end
349349
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
350350
str = String(take!(buf))
351351
@test occursin("\n```julia\n", str)
352-
@test occursin("\nk_9(x::Any) -> Any\n", str)
352+
@test occursin("\nk_9(x) -> Any\n", str)
353353
@test occursin("\n```\n", str)
354354

355+
@static if VERSION > v"1.5-" # see JuliaLang/#40405
356+
357+
doc.data = Dict(
358+
:binding => Docs.Binding(M, :k_11),
359+
:typesig => Union{Tuple{Int, Vararg{Any}}},
360+
:module => M,
361+
)
362+
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
363+
str = String(take!(buf))
364+
@test occursin("\n```julia\n", str)
365+
@test occursin("\nk_11(x::Int64, xs...) -> Int64\n", str)
366+
@test occursin("\n```\n", str)
367+
368+
doc.data = Dict(
369+
:binding => Docs.Binding(M, :k_12),
370+
:typesig => Union{Tuple{Int, Vararg{Real}}},
371+
:module => M,
372+
)
373+
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
374+
str = String(take!(buf))
375+
@test occursin("\n```julia\n", str)
376+
@test occursin("\nk_12(x::Int64, xs::Real...) -> Int64\n", str)
377+
@test occursin("\n```\n", str)
378+
379+
end
380+
355381
doc.data = Dict(
356382
:binding => Docs.Binding(M, :k_10),
357383
:typesig => Union{Tuple{T}, Tuple{T}} where T,

0 commit comments

Comments
 (0)