123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- .. _doc_code_style_guidelines:
- Code style guidelines
- =====================
- .. highlight:: shell
- When contributing to Godot's source code, you will be expected to follow the
- style guidelines outlined below. Some of them are checked via the Continuous
- Integration process and reviewers will ask you to fix potential issues, so
- best setup your system as outlined below to ensure all your commits follow the
- guidelines.
- C++ and Objective-C
- -------------------
- There are no written guidelines, but the code style agreed upon by the
- developers is enforced via the `clang-format <https://clang.llvm.org/docs/ClangFormat.html>`__
- code beautifier, which takes care for you of all our conventions.
- To name a few:
- - Indentation and alignment are both tab based (respectively one and two tabs)
- - One space around math and assignments operators as well as after commas
- - Pointer and reference operators are affixed to the variable identifier, not
- to the type name
- - See further down regarding header includes
- The rules used by clang-format are outlined in the
- `.clang-format <https://github.com/godotengine/godot/blob/master/.clang-format>`__
- file of the Godot repository.
- As long as you ensure that your style matches the surrounding code and that you're
- not introducing trailing whitespace or space-based indentation, you should be
- fine. If you plan to contribute regularly, however, we strongly advise that you
- set up clang-format locally to check and automatically fix all your commits.
- .. warning:: Godot's code style should *not* be applied to third-party code,
- i.e. that is included in Godot's source tree but was not written
- specifically for our project. Such code usually comes from
- different upstream projects with their own style guides (or lack
- thereof), and don't want to introduce differences that would make
- syncing with upstream repositories harder.
- Third-party code is usually included in the ``thirdparty/`` folder
- and can thus easily be excluded from formatting scripts. For the
- rare cases where a third-party code snippet needs to be included
- directly within a Godot file, you can use
- ``/* clang-format off */`` and ``/* clang-format on */`` to tell
- clang-format to ignore a chunk of code.
- .. seealso::
- These guidelines only cover code formatting. See :ref:`doc_cpp_usage_guidelines`
- for a list of language features that are permitted in pull requests.
- Using clang-format locally
- ~~~~~~~~~~~~~~~~~~~~~~~~~~
- You need to use **clang-format 17** to be compatible with Godot's format. Later versions might
- be suitable, but previous versions may not support all used options, or format
- some things differently, leading to style issues in pull requests.
- .. _doc_code_style_guidelines_pre_commit_hook:
- Pre-commit hook
- ^^^^^^^^^^^^^^^
- For ease of use, we provide hooks for Git with the `pre-commit <https://pre-commit.com/>`__
- Python framework that will run clang-format automatically on all your commits with the
- correct version of clang-format.
- To set up:
- ::
- pip install pre-commit
- pre-commit install
- You can also run the hook manually with ``pre-commit run``.
- .. note::
- Previously, we supplied a hook in the folder ``misc/hooks``. If you copied the
- script manually, these hooks should still work, but symlinks will be broken.
- If you are using the new system, run ``rm .git/hooks/*`` to remove the old hooks
- that are no longer needed.
- Installation
- ^^^^^^^^^^^^
- Here's how to install clang-format:
- - Linux: It will usually be available out-of-the-box with the clang toolchain
- packaged by your distribution. If your distro version is not the required one,
- you can download a pre-compiled version from the
- `LLVM website <https://releases.llvm.org/download.html>`__, or if you are on
- a Debian derivative, use the `upstream repos <https://apt.llvm.org/>`__.
- - macOS and Windows: You can download precompiled binaries from the
- `LLVM website <https://releases.llvm.org/download.html>`__. You may need to add
- the path to the binary's folder to your system's ``PATH`` environment
- variable to be able to call clang-format out of the box.
- You then have different possibilities to apply clang-format to your changes:
- Manual usage
- ^^^^^^^^^^^^
- You can apply clang-format manually for one or more files with the following
- command:
- ::
- clang-format -i <path/to/file(s)>
- - ``-i`` means that the changes should be written directly to the file (by
- default clang-format would only output the fixed version to the terminal).
- - The path can point to several files, either one after the other or using
- wildcards like in a typical Unix shell. Be careful when globbing so that
- you don't run clang-format on compiled objects (.o and .a files) that are
- in Godot's tree. So better use ``core/*.{cpp,h}`` than ``core/*``.
- IDE plugin
- ^^^^^^^^^^
- Most IDEs or code editors have beautifier plugins that can be configured to run
- clang-format automatically, for example, each time you save a file.
- Here is a non-exhaustive list of beautifier plugins for some IDEs:
- - Qt Creator: `Beautifier plugin <https://doc.qt.io/qtcreator/creator-beautifier.html>`__
- - Visual Studio Code: `Clang-Format <https://marketplace.visualstudio.com/items?itemName=xaver.clang-format>`__
- - Visual Studio: `Clang Power Tools 2022 <https://marketplace.visualstudio.com/items?itemName=caphyon.ClangPowerTools2022>`__
- - vim: `vim-clang-format <https://github.com/rhysd/vim-clang-format>`__
- - CLion: Starting from version ``2019.1``, no plugin is required. Instead, enable
- `ClangFormat <https://www.jetbrains.com/help/clion/clangformat-as-alternative-formatter.html#clion-support>`__
- (Pull requests are welcome to extend this list with tested plugins.)
- .. _doc_code_style_guidelines_header_includes:
- Header includes
- ~~~~~~~~~~~~~~~
- When adding new C++ or Objective-C files or including new headers in existing
- ones, the following rules should be followed:
- - The first lines in the file should be Godot's copyright header and MIT
- license, copy-pasted from another file. Make sure to adjust the filename.
- - In a ``.h`` header, include guards should be used with the form
- ``FILENAME_H``.
- - In a ``.cpp`` file (e.g. ``filename.cpp``), the first include should be the
- one where the class is declared (e.g. ``#include "filename.h"``), followed by
- an empty line for separation.
- - Then come headers from Godot's own code base, included in alphabetical order
- (enforced by ``clang-format``) with paths relative to the root folder. Those
- includes should be done with quotes, e.g. ``#include "core/object.h"``. The
- block of Godot header includes should then be followed by an empty line for
- separation.
- - Finally, third-party headers (either from ``thirdparty`` or from the system's
- include paths) come next and should be included with the < and > symbols, e.g.
- ``#include <png.h>``. The block of third-party headers should also be followed
- by an empty line for separation.
- - Godot and third-party headers should be included in the file that requires
- them, i.e. in the `.h` header if used in the declarative code or in the `.cpp`
- if used only in the imperative code.
- Example:
- .. code-block:: cpp
- /**************************************************************************/
- /* my_new_file.h */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #ifndef MY_NEW_FILE_H
- #define MY_NEW_FILE_H
- #include "core/hash_map.h"
- #include "core/list.h"
- #include "scene/gui/control.h"
- #include <png.h>
- ...
- #endif // MY_NEW_FILE_H
- .. code-block:: cpp
- /**************************************************************************/
- /* my_new_file.cpp */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "my_new_file.h"
- #include "core/math/math_funcs.h"
- #include "scene/gui/line_edit.h"
- #include <zlib.h>
- #include <zstd.h>
- Java
- ----
- Godot's Java code (mostly in ``platform/android``) is also enforced via
- ``clang-format``, so see the instructions above to set it up. Keep in mind that
- this style guide only applies to code written and maintained by Godot, not
- third-party code such as the ``java/src/com/google`` subfolder.
- Python
- ------
- Godot's SCons buildsystem is written in Python, and various scripts included
- in the source tree are also using Python.
- For those, we use the `Ruff linter and code formatter <https://docs.astral.sh/ruff/>`__.
- Using ruff locally
- ~~~~~~~~~~~~~~~~~~~
- First of all, you will need to install Ruff. Ruff requires Python 3.7+ to run.
- Installation
- ^^^^^^^^^^^^
- Here's how to install ruff:
- ::
- pip3 install ruff --user
- You then have different possibilities to apply ruff to your changes:
- Manual usage
- ^^^^^^^^^^^^
- You can apply ``ruff`` manually to one or more files with the following
- command:
- ::
- ruff -l 120 <path/to/file(s)>
- - ``-l 120`` means that the allowed number of characters per line is 120.
- This number was agreed upon by the developers.
- - The path can point to several files, either one after the other or using
- wildcards like in a typical Unix shell.
- Pre-commit hook
- ~~~~~~~~~~~~~~~
- For ease of use, we provide hooks for Git with the `pre-commit <https://pre-commit.com/>`__
- Python framework that will run ``ruff`` automatically on all your commits with the
- correct version of ``ruff``.
- To set up:
- ::
- pip install pre-commit
- pre-commit install
- You can also run the hook manually with ``pre-commit run``.
- .. note::
- Previously, we supplied a hook in the folder ``misc/hooks``. If you copied the
- script manually, these hooks should still work, but symlinks will be broken.
- If you are using the new system, run ``rm .git/hooks/*`` to remove the old hooks
- that are no longer needed.
- Editor integration
- ^^^^^^^^^^^^^^^^^^
- Many IDEs or code editors have beautifier plugins that can be configured to run
- ruff automatically, for example, each time you save a file. For details, you can
- check `Ruff Integrations <https://docs.astral.sh/ruff/integrations/>`__.
- Comment style guide
- -------------------
- This comment style guide applies to all programming languages used within
- Godot's codebase.
- - Begin comments with a space character to distinguish them from disabled code.
- - Use sentence case for comments. Begin comments with an uppercase character and
- always end them with a period.
- - Reference variable/function names and values using backticks.
- - Wrap comments to ~100 characters.
- - You can use ``TODO:``, ``FIXME:``, ``NOTE:``, or ``HACK:`` as admonitions
- when needed.
- **Example:**
- .. code-block:: cpp
- // Compute the first 10,000 decimals of Pi.
- // FIXME: Don't crash when computing the 1,337th decimal due to `increment`
- // being negative.
- Don't repeat what the code says in a comment. Explain the *why* rather than *how*.
- **Bad:**
- .. code-block:: cpp
- // Draw loading screen.
- draw_load_screen();
- You can use Javadoc-style comments above function or macro definitions. It's
- recommended to use Javadoc-style comments *only* for methods which are not
- exposed to scripting. This is because exposed methods should be documented in
- the :ref:`class reference XML <doc_updating_the_class_reference>`
- instead.
- **Example:**
- .. code-block:: cpp
- /**
- * Returns the number of nodes in the universe.
- * This can potentially be a very large number, hence the 64-bit return type.
- */
- uint64_t Universe::get_node_count() {
- // ...
- }
- For member variables, don't use Javadoc-style comments but use single-line comments instead:
- .. code-block:: cpp
- class Universe {
- // The cached number of nodes in the universe.
- // This value may not always be up-to-date with the current number of nodes
- // in the universe.
- uint64_t node_count_cached = 0;
- };
|