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

@template broken with Base.@__doc__? #73

Closed
rafaqz opened this issue Feb 19, 2019 · 5 comments · Fixed by #96
Closed

@template broken with Base.@__doc__? #73

rafaqz opened this issue Feb 19, 2019 · 5 comments · Fixed by #96

Comments

@rafaqz
Copy link

rafaqz commented Feb 19, 2019

@template doesn't seem to work when a struct is processed by a macro, and Base.@__doc__ is used in the macro.

The docs otherwise work fine. But the template is not inserted, although the format method does actually run.

@mortenpi
Copy link
Member

Do you have a code snippet of what you mean by any chance?

@rafaqz
Copy link
Author

rafaqz commented Feb 25, 2019

Ok a simple example that doesn't actually need the @__doc__ macro but illustrates the problem:

module DocsCheck

using DocStringExtensions

@template TYPES =
    """
    $(TYPEDEF)
    $(DOCSTRING)
    """


macro documentbreaker(ex)
    :(Base.@__doc__ $(esc(ex)))
end

"Docs don't show typedef"
@documentbreaker struct BrokenDocs <: AbstractString 
    s::String
end


"Docs show typedef ^"
struct WorkingDocs <: AbstractString 
    s::String
end

end

Then:

help?> DocsCheck.WorkingDocs
  struct WorkingDocs <: AbstractString

  Docs show typedef ^

help?> DocsCheck.BrokenDocs
  Docs don't show typedef

julia> 

@marius311
Copy link

I think this is a simpler example:

julia> module Foo

       using DocStringExtensions
       @template (FUNCTIONS, METHODS, MACROS) =
           """
           $(SIGNATURES)
           $(DOCSTRING)
           """

       @doc raw"""
       stuff
       """
       foo() = 1
       end
Main.Foo

help?> Main.Foo.foo
  stuff

I was expecting the docstring at the end to have the signature added. This is DocStringExtensions v0.7.0 and Julia 1.1.0.

More generally, I need raw"..." or doc"..." docstrings since I tend to use lots of Latex. I don't currently see any way to make that work with this package due to this bug, but maybe there's a way I'm missing?

@bzinberg
Copy link

bzinberg commented Aug 9, 2020

@marius311, I think I may have found a workaround:

"""$(raw"""
stuff
""")"""
foo() = 1

My rough understanding of why this works is that the relevant DocStrExtensions code path (namely, a call to DocStrExtensions.format) is used only when the docstring shows up as a :string node in the AST (e.g. (string "x = " x), not a bare literal like "a").

I am combining this with a different workaround, to get Documenter.jl to use the right "view source" link when combined with Base.@kwdef (rather than linking to the source of whatever name happens to first appear in the expansion of @kwdef):

@doc """Fields:
$(FIELDS)$(raw"""
---

Prose documentation with ``\LaTeX``
""")""" MyStruct
@kwdef struct MyStruct
  n::Int
end

@bzinberg
Copy link

bzinberg commented Aug 9, 2020

I wonder if the above workaround (or a better version of it, if known) should be mentioned in the documentation? It seems like an important use case.

MichaelHatherly added a commit to MichaelHatherly/DocStringExtensions.jl that referenced this issue Aug 10, 2020
This moves the expansion of templates from macro expansion time to later
on. This allows us to avoid having to search through expressions looking
for the actual expression type since those may be hidden by "decorator"
macros. It also handles cases where `@doc` is used on a string macro
docstring, such as

    @doc raw"..." f(x) = x

Implementation is based on a new internal abbreviation type called
`Template` which gets prepended and appended to docstrings in modules
where templates are available. Then, during formatting, we expand these
`Template` abbreviations into there definitions.

Fixes JuliaDocs#73.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants