Formatting code samples for developer docs

Throughout the GLib/GTK documentation, the code samples follow a very predictable and standard style. What tool can I use to ensure my code samples conform to the coding style? (Maybe clang-format?)

Thank you.

1 Like

Hi,

GLib and Gtk have a .clang-format in their source code (example), that you can reuse.

Does that help?

Unfortunately, the GTK coding style cannot be automated with existing tools. The closest you can get is by using uncrustify, but it still won’t format the arguments in a function properly, e.g.

bool
some_example_method (SomeExample  *self,
                     const char  **an_array,
                     size_t        array_length,
                     GError      **error)

GTK and GLib have a local .clang-format file in their repository because they have a CI job that checks contributions, but it’s mainly an advisory pass because clang-format does not cover all the conformant coding style.

This is unfortunate, and my recommendation is to use a coding style that can be automated, at least for your own projects and documentation. The GTK coding style should be reserved for contributing to GTK.

3 Likes

Emmanuele is right. If you’re super-keen then one other option would be to improve clang-format (or uncrustify) so that it is flexible enough to replicate the GTK coding style, then everyone benefits. I don’t think it would need many new options to be able to replicate the style. There’s a lot of existing code out there which follows this style and which would benefit from automated style checking/enforcement. :upside_down_face:

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.