Skip to content

Commit

Permalink
Merge pull request #13 from t-bltg/doc
Browse files Browse the repository at this point in the history
allow documenting a struct
  • Loading branch information
thautwarm authored Jan 18, 2024
2 parents edebadd + 15ea308 commit 1de9ba0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/compile-time.buildclass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ function codegen(cur_line :: LineNumberNode, cur_mod::Module, type_def::TypeDef)
outer_block = [
[
:(struct $traithead end),
Expr(:struct,
:(Base.@__doc__ $(Expr(:struct,
type_def.isMutable,
:($defhead <: $_merge_shape_types($traithead, $(to_expr.(type_def.bases)...))),
Expr(:block, struct_block...)),
:($ObjectOriented.CompileTime._shape_type(t::$Type{<:$typename}) = $supertype(t)),
Expr(:block, struct_block...)))),
:(Base.@__doc__ $ObjectOriented.CompileTime._shape_type(t::$Type{<:$typename}) = $supertype(t)),

];
[
:($ObjectOriented.RunTime.default_initializers(t::$Type{<:$typename}) = $expr_default_initializers)
:(Base.@__doc__ $ObjectOriented.RunTime.default_initializers(t::$Type{<:$typename}) = $expr_default_initializers)
];
outer_block
]
Expand Down
4 changes: 3 additions & 1 deletion src/compile-time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ include("compile-time.static_dispatch.jl")
macro oodef(ex)
preprocess(x) = Base.macroexpand(__module__, x)
type_def = parse_class(__source__, ex, preprocess=preprocess)
esc(canonicalize_where(codegen(__source__, __module__, type_def)))
quote
Base.@__doc__ $(canonicalize_where(codegen(__source__, __module__, type_def)))
end |> esc
end

end # module
4 changes: 2 additions & 2 deletions src/compile-time.reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function parse_class_body!(ln::LineNumberNode, self::TypeDef, body; preprocess::
x = preprocess(x)
end

if x isa LineNumberNode
if x isa LineNumberNode || x isa String
ln = x
continue
end
Expand All @@ -247,7 +247,7 @@ function parse_class_body!(ln::LineNumberNode, self::TypeDef, body; preprocess::
push!(self.methods, func_info)
continue
end
throw(create_exception(ln, "unrecognised statement in $(self.name) definition: $(x)"))
throw(create_exception(ln, "unrecognised statement in $(self.name) definition: $x"))

end
end
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,25 @@ module structdef
@test some_ref_val[] == 5
@test x.b == "sada"
end

"""
Documented struct fancy docstring
"""
@oodef struct DocumentedStruct
a :: Int
"""
Constructor docstring
"""
function new(x)
@mk a = 2x
end
end

@testset "documented struct" begin
x = DocumentedStruct(123)
@test occursin("Documented struct fancy docstring", string(@doc(DocumentedStruct)))
@test x.a == 246
end
end

include("example.jl")
Expand Down

0 comments on commit 1de9ba0

Please sign in to comment.