Browse Source

Document support for code folding regions in GDScript reference

Hugo Locurcio 1 year ago
parent
commit
54331c3c8f

+ 68 - 0
tutorials/scripting/gdscript/gdscript_basics.rst

@@ -566,6 +566,74 @@ considered a comment.
 
 
 .. _doc_gdscript_builtin_types:
 .. _doc_gdscript_builtin_types:
 
 
+Code regions
+~~~~~~~~~~~~
+
+Code regions are special types of comments that the script editor understands as
+*foldable regions*. This means that after writing code region comments, you can
+collapse and expand the region by clicking the arrow that appears at the left of
+the comment. This arrow appears within a purple square to be distinguishable
+from standard code folding.
+
+The syntax is as follows:
+
+::
+
+    # Important: There must be *no* space between the `#` and `region` or `endregion`.
+
+    # Region without a description:
+    #region
+    ...
+    #endregion
+
+    # Region with a description:
+    #region Some description that is displayed even when collapsed
+    ...
+    #endregion
+
+.. tip::
+
+    To create a code region quickly, select several lines in the script editor,
+    right-click the selection then choose **Create Code Region**. The region
+    description will be selected automatically for editing.
+
+    It is possible to nest code regions within other code regions.
+
+Here's a concrete usage example of code regions:
+
+::
+
+    # This comment is outside the code region. It will be visible when collapsed.
+    #region Terrain generation
+    # This comment is inside the code region. It won't be visible when collapsed.
+    func generate_lakes():
+        pass
+
+    func generate_hills():
+        pass
+    #endregion
+
+    #region Terrain population
+    func place_vegetation():
+        pass
+
+    func place_roads():
+        pass
+    #endregion
+
+This can be useful to organize large chunks of code into easier to understand
+sections. However, remember that external editors generally don't support this
+feature, so make sure your code is easy to follow even when not relying on
+folding code regions.
+
+.. note::
+
+    Individual functions and indented sections (such as ``if`` and ``for``) can
+    *always* be collapsed in the script editor. This means you should avoid
+    using a code region to contain a single function or indented section, as it
+    won't bring much of a benefit. Code regions work best when they're used to
+    group multiple elements together.
+
 Line continuation
 Line continuation
 ~~~~~~~~~~~~~~~~~
 ~~~~~~~~~~~~~~~~~
 
 

+ 8 - 3
tutorials/scripting/gdscript/gdscript_styleguide.rst

@@ -449,8 +449,13 @@ This can make long expressions easier to read.
 Comment spacing
 Comment spacing
 ~~~~~~~~~~~~~~~
 ~~~~~~~~~~~~~~~
 
 
-Regular comments should start with a space, but not code that you comment out.
-This helps differentiate text comments from disabled code.
+Regular comments (``#``) and documentation comments (``##``) should start with a
+space, but not code that you comment out. Additionally, code region comments
+(``#region``/``#endregion``) must follow that precise syntax, so they should not
+start with a space.
+
+Using a space for regular and documentation comments helps differentiate text
+comments from disabled code.
 
 
 **Good**:
 **Good**:
 
 
@@ -473,7 +478,7 @@ This helps differentiate text comments from disabled code.
 .. note::
 .. note::
 
 
     In the script editor, to toggle the selected code commented, press
     In the script editor, to toggle the selected code commented, press
-    :kbd:`Ctrl + K`. This feature adds a single # sign at the start
+    :kbd:`Ctrl + K`. This feature adds a single ``#`` sign at the start
     of the selected lines.
     of the selected lines.
 
 
 Whitespace
 Whitespace