sphinx-doc/sphinx

Sphinx should add a way to use autoformatters

Open

#11.401 aberto em 5 de mai. de 2023

Ver no GitHub
 (10 comments) (0 reactions) (0 assignees)Python (1.985 forks)batch import
help wanted

Métricas do repositório

Stars
 (5.625 stars)
Métricas de merge de PR
 (Mesclagem média 10d 11h) (11 fundiu PRs em 30d)

Description

Is your feature request related to a problem? Please describe. Languages often have multiple different formatting styles, and it would be good to have the code in documentation be formatted in the same style as the rest of the project. Although I haven't used it, I think reStructured Text can load source files, so code blocks can probably be formatted by using those.

To my knowledge, there isn't a way to do the same for API directives, which can create both inconsistencies and unreadable code. For the latter, consider this C++ standard library function.

.. cpp:function:: template <class T>                                                 \
                  constexpr conditional_t<not is_nothrow_constructible_v<T, T&&> and \
                                              is_constructible_v<T, T const&>,       \
                                          T const&, T&&>                             \
                  std::move_if_noexcept(T &x);

It ends up being formatted as

template<class T>
constexpr conditional_t<not is_nothrow_constructible_v<T, T&&> and is_constructible_v<T, T const&>, T const&, T&&> std::move_if_noexcept(T &x);

Unfortunately, due to my theme's apparent 80ish column limit, it ends up looking like this in my browser:

template<class T>
constexpr conditional_t<not is_nothrow_constructible_v<T, T&&> and
is_constructible_v<T, T const&>, T const&, T&&> std::move_if_noexcept(T &x);

Neither of these are particularly readable formats, and I think potential readers would be well within their rights to complain about this. Ideally, it would be rendered as it was styled in the directive.

Describe the solution you'd like It would be good if Sphinx could be pointed at a formatting tool (possibly with arguments) so that documentation authors can ensure that API directives are both consistent with project style and are legible. Possibly something like this in conf.py:

autoformat = {'path': '/path/to/clang-format', 'args': ['-style=file']}

If autoformat is None or not set, then Sphinx's internal formatting becomes the default.

Describe alternatives you've considered Formatting could alternatively follow what's prescribed in the directive verbatim, but the backslashes might prevent that from being a successful endeavour.

Guia do colaborador