Browse Source

Add highlighting support for %Unique nodes in NodePaths (#403)

Co-authored-by: Hugo Locurcio <[email protected]>
Daelon Suzuka 3 years ago
parent
commit
ec95c21b84
2 changed files with 85 additions and 6 deletions
  1. 64 6
      syntaxes/GDScript.tmLanguage.json
  2. 21 0
      syntaxes/examples/gdscript1.gd

+ 64 - 6
syntaxes/GDScript.tmLanguage.json

@@ -181,13 +181,19 @@
                 {
                     "begin": "[\\\"\\']",
                     "end": "[\\\"\\']",
-                    "name": "constant.character.escape"
+                    "name": "constant.character.escape",
+                    "patterns": [
+                        {
+                            "match": "%",
+                            "name": "keyword.control.flow"
+                        }
+                    ]
                 }
             ]
         },
         "nodepath_function": {
             "name": "meta.literal.nodepath.gdscript",
-            "begin": "(get_node_or_null|has_node|find_node|get_node)\\s*(?:\\()",
+            "begin": "(get_node_or_null|has_node|has_node_and_resource|find_node|get_node)\\s*(?:\\()",
             "beginCaptures": {
                 "1": {
                     "name": "entity.name.function.gdscript"
@@ -198,7 +204,13 @@
                 {
                     "begin": "[\\\"\\']",
                     "end": "[\\\"\\']",
-                    "name": "constant.character.escape"
+                    "name": "constant.character.escape",
+                    "patterns": [
+                        {
+                            "match": "%",
+                            "name": "keyword.control.flow"
+                        }
+                    ]
                 }
             ]
         },
@@ -404,16 +416,62 @@
             "name": "support.function.builtin.gdscript"
         },
         "builtin_get_node_shorthand": {
-            "match": "(\\$)([\\\"\\'].*[\\\"\\']|(?:[a-zA-Z_][a-zA-Z_0-9]*/?)*)",
+            "patterns": [
+                {
+                    "include": "#builtin_get_node_shorthand_quoted"
+                },
+                {
+                    "include": "#builtin_get_node_shorthand_bare"
+                }
+            ]
+        },
+        "builtin_get_node_shorthand_quoted": {
+            "begin": "(\\$)([\\\"\\'])",
+            "end": "([\\\"\\'])",
             "name": "support.function.builtin.shorthand.gdscript",
-            "captures": {
+            "beginCaptures": {
                 "1": {
                     "name": "keyword.control.flow"
                 },
                 "2": {
                     "name": "constant.character.escape"
                 }
-            }
+            },
+            "endCaptures": {
+                "1": {
+                    "name": "constant.character.escape"
+                }
+            },
+            "patterns": [
+                {
+                    "match": "%",
+                    "name": "keyword.control.flow"
+                },
+                {
+                    "match": "[^%]*",
+                    "name": "constant.character.escape"
+                }
+            ]
+        },
+        "builtin_get_node_shorthand_bare": {
+            "begin": "(\\$)",
+            "end": "[^\\w%]",
+            "name": "support.function.builtin.shorthand.gdscript",
+            "beginCaptures": {
+                "1": {
+                    "name": "keyword.control.flow"
+                }
+            },
+            "patterns": [
+                {
+                    "match": "[a-zA-Z_][a-zA-Z_0-9]*/?",
+                    "name": "constant.character.escape"
+                },
+                {
+                    "match": "%[a-zA-Z_][a-zA-Z_0-9]*/?",
+                    "name": "invalid.illegal.escape.gdscript"
+                }
+            ]
         },
         "decorators": {
             "match": "(@)(export|export_color_no_alpha|export_dir|export_enum|export_exp_easing|export_file|export_flags|export_flags_2d_navigation|export_flags_2d_physics|export_flags_2d_render|export_flags_3d_navigation|export_flags_3d_physics|export_flags_3d_render|export_global_dir|export_global_file|export_multiline|export_node_path|export_placeholder|export_range|icon|onready|rpc|tool|warning_ignore)\\b",

+ 21 - 0
syntaxes/examples/gdscript1.gd

@@ -127,6 +127,27 @@ onready var node_h = get_node("../Sibling")
 if has_node('Child') and get_node('Child').has_node('GrandChild'):
 	pass
 
+#! NOTE: scene unique nodes can only appear inside quoted nodepaths, not
+#! naked ones using the $ operator
+
+onready var bad_unique_nodepath_a = $%Unique
+onready var bad_unique_nodepath_b = $Child/%Unique
+onready var bad_unique_nodepath_c = $Child/GrandChild/%Unique
+onready var bad_unique_nodepath_c = $Child/%Unique/ChildOfUnique
+
+onready var node_i = $"%Unique"
+onready var node_ii = get_node("%Unique")
+onready var node_iii = NodePath("%Unique")
+onready var node_j = $'%Unique/Child'
+onready var node_jj = get_node('%Unique/Child')
+onready var node_jjj = NodePath('%Unique/Child')
+onready var node_k = $"%Unique/%UniqueChild"
+onready var node_kk = get_node("%Unique/%UniqueChild")
+onready var node_kkk = NodePath("%Unique/%UniqueChild")
+
+if has_node('%Unique') and get_node('%Child').has_node('%GrandChild'):
+	pass
+
 onready var node_i = $badlyNamedChild
 onready var node_j = $badlyNamedChild/badly_named_grandchild