code_style_guidelines.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. .. _doc_code_style_guidelines:
  2. Code style guidelines
  3. =====================
  4. .. highlight:: shell
  5. When contributing to Godot's source code, you will be expected to follow the
  6. style guidelines outlined below. Some of them are checked via the Continuous
  7. Integration process and reviewers will ask you to fix potential issues, so
  8. best setup your system as outlined below to ensure all your commits follow the
  9. guidelines.
  10. C++ and Objective-C
  11. -------------------
  12. There are no written guidelines, but the code style agreed upon by the
  13. developers is enforced via the `clang-format <http://clang.llvm.org/docs/ClangFormat.html>`_
  14. code beautifier, which takes care for you of all our conventions.
  15. To name a few:
  16. - Indentation and alignment are both tab based (respectively one and two tabs)
  17. - One space around math and assignments operators as well as after commas
  18. - Pointer and reference operators are affixed to the variable identifier, not
  19. to the type name
  20. The rules used by clang-format are outlined in the
  21. `.clang-format <https://github.com/godotengine/godot/blob/master/.clang-format>`_
  22. file of the Godot repository.
  23. As long as you ensure that your style matches the surrounding code and that you
  24. not introducing trailing whitespace or space-based indentation, you should be
  25. fine. If you plan to contribute regularly however, we strongly advise that you
  26. setup clang-format locally to check and automatically fix all your commits.
  27. .. warning:: Godot's code style should *not* be applied to thirdparty code,
  28. i.e. that is included in Godot's source tree but was not written
  29. specifically for our project. Such code usually come from
  30. different upstream projects with their own style guides (or lack
  31. thereof), and don't want to introduce differences that would make
  32. syncing with upstream repositories harder.
  33. Thirdparty code is usually included in the ``thirdparty/`` folder
  34. and can thus easily be excluded from formatting scripts. For the
  35. rare cases where a thirdparty code snippet needs to be included
  36. directly within a Godot file, you can use
  37. ``/* clang-format off */`` and ``/* clang-format on */`` to tell
  38. clang-format to ignore a chunk of code.
  39. Using clang-format locally
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. First of all, you will need to install clang-format. As of now, you need to use
  42. **clang-format 5.x** to be compatible with Godot's format. The upcoming 6.x
  43. branch has not been tested yet and my cause inconsistencies; the previous 3.x
  44. branch is incompatible with the style definitions and will error out.
  45. Installation
  46. ^^^^^^^^^^^^
  47. Here's how to install clang-format:
  48. - Linux: It will usually be available out-of-the-box with the clang toolchain
  49. packaged by your distribution. If your distro version is not the required one,
  50. you can download a pre-compiled version from the
  51. `LLVM website <http://llvm.org/releases/download.html>`__, or if you are on
  52. a Debian derivative, use the `upstream repos <http://apt.llvm.org/>`__.
  53. - macOS and Windows: You can download precompiled binaries from the
  54. `LLVM website <http://llvm.org/releases/download.html>`_. You may need to add
  55. the path to the binary's folder to your system's ``PATH`` environment
  56. variable to be able to call ``clang-format`` out of the box.
  57. You then have different possibilities to apply clang-format to your changes:
  58. Manual usage
  59. ^^^^^^^^^^^^
  60. You can apply clang-format manually one or more files with the following
  61. command:
  62. ::
  63. clang-format -i -style=file <path/to/file(s)>
  64. - ``-i`` means that the changes should be written directly to the file (by
  65. default clang-format would only output the fixed version to the terminal).
  66. - ``-style=file`` tells clang-format to use the ``.clang-format`` file of
  67. Godot's repository as a style guide. Note that the ``file`` part is the
  68. actual "file" word, not a path to a file.
  69. - The path can point to several files, either one after the other or using
  70. wildcards like in a typical Unix shell. Be careful when globbing so that
  71. you don't run clang-format on compiled objects (.o and .a files) that are
  72. in Godot's tree. So better use ``core/*.{cpp,h}`` than ``core/*``.
  73. Pre-commit hook
  74. ^^^^^^^^^^^^^^^
  75. For ease of use, we provide a pre-commit hook for Git that will run
  76. clang-format automatically on all your commits to check them, and let you apply
  77. its changes in the final commit.
  78. This "hook" is a script which can be found in ``misc/hooks``, refer to that
  79. folder's README.md for installation instructions.
  80. If your clang-format is not in the ``PATH``, you may have to edit the
  81. ``pre-commit-clang-format`` to point to the correct binary for it to work.
  82. The hook was tested on Linux and macOS, but should also work in the Git Shell
  83. on Windows.
  84. IDE plugin
  85. ^^^^^^^^^^
  86. Most IDEs or code editors have beautifier plugins that can be configured to run
  87. clang-format automatically, for example each time you save a file.
  88. Here is a non-exhaustive list of beautifier plugins for some IDEs:
  89. - Qt Creator: `Beautifier plugin <http://doc.qt.io/qtcreator/creator-beautifier.html>`__
  90. - Visual Studio Code: `Clang-Format <https://marketplace.visualstudio.com/items?itemName=xaver.clang-format>`__
  91. - Visual Studio: `ClangFormat <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat>`__
  92. - vim: `vim-clang-format <https://github.com/rhysd/vim-clang-format>`__
  93. (Pull requests welcome to extend this list with tested plugins.)
  94. Java
  95. ----
  96. For Godot's Java code (mostly in ``platform/android``), there is currently no
  97. style guide, so for now try to stay consistent with the existing code.
  98. Once a style is decided upon, it could also be enforced via clang-format.
  99. Python
  100. ------
  101. Godot's SCons buildsystem is written in Python 2, and various scripts included
  102. in the source tree are either in Python 2 or Python 3.
  103. For those, we follow the `PEP-8 style guide <https://www.python.org/dev/peps/pep-0008/>`_,
  104. this is however not as strongly enforced as for the C++ code. If you are so
  105. enclined, you can check and format your Python changes using
  106. `autopep8 <https://pypi.python.org/pypi/autopep8>`_.