From 21b5a2befc9345e5850c8e254aff7b61320f0e07 Mon Sep 17 00:00:00 2001
From: Jummit <jummit@web.de>
Date: Fri, 4 Aug 2023 15:25:36 +0100
Subject: [PATCH] Improve wren support (#7819)

---
 book/src/generated/lang-support.md   |  2 +-
 languages.toml                       |  2 +-
 runtime/queries/wren/highlights.scm  | 10 ++++++----
 runtime/queries/wren/indents.scm     | 29 ++++++++++++++++++++++++++++
 runtime/queries/wren/injections.scm  |  3 +++
 runtime/queries/wren/locals.scm      | 21 ++++++++++++++++++++
 runtime/queries/wren/textobjects.scm |  4 ++++
 7 files changed, 65 insertions(+), 6 deletions(-)
 create mode 100644 runtime/queries/wren/indents.scm
 create mode 100644 runtime/queries/wren/injections.scm
 create mode 100644 runtime/queries/wren/locals.scm

diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md
index 62a1d4b9a36a..22689c9c6405 100644
--- a/book/src/generated/lang-support.md
+++ b/book/src/generated/lang-support.md
@@ -167,7 +167,7 @@
 | webc | ✓ |  |  |  |
 | wgsl | ✓ |  |  | `wgsl_analyzer` |
 | wit | ✓ |  | ✓ |  |
-| wren | ✓ | ✓ |  |  |
+| wren | ✓ | ✓ | ✓ |  |
 | xit | ✓ |  |  |  |
 | xml | ✓ |  | ✓ |  |
 | yaml | ✓ |  | ✓ | `yaml-language-server` |
diff --git a/languages.toml b/languages.toml
index a58e5b5f102b..967bbea72220 100644
--- a/languages.toml
+++ b/languages.toml
@@ -2704,7 +2704,7 @@ grammar = "html"
 
 [[grammar]]
 name = "wren"
-source = { git = "https://git.sr.ht/~jummit/tree-sitter-wren", rev = "7f576e8ccadac226f6a37cbefe95be3fee9f0a66"}
+source = { git = "https://git.sr.ht/~jummit/tree-sitter-wren", rev = "793d58266924e6efcc40e411663393e9d72bec87"}
 
 [[language]]
 name = "wren"
diff --git a/runtime/queries/wren/highlights.scm b/runtime/queries/wren/highlights.scm
index 5454ce25b1b5..c37bc3bc5720 100644
--- a/runtime/queries/wren/highlights.scm
+++ b/runtime/queries/wren/highlights.scm
@@ -1,11 +1,11 @@
-((identifier) @variable.builtin
+((name) @variable.builtin
  (#match? @variable.builtin "^(Bool|Class|Fiber|Fn|List|Map|Null|Num|Object|Range|Sequence|String|System)$"))
 
 (call_expression
-  (identifier) @function)
+  (name) @function)
 
 (method_definition
-  (identifier) @function.method)
+  (name) @function.method)
 
 ((parameter) @variable.parameter)
 
@@ -13,7 +13,9 @@
 (string) @string
 (raw_string) @string
 (number) @constant.numeric.integer
-(identifier) @variable
+(name) @variable
+(field) @variable
+(static_field) @variable
 (null) @constant.builtin
 (boolean) @constant.builtin.boolean
 
diff --git a/runtime/queries/wren/indents.scm b/runtime/queries/wren/indents.scm
new file mode 100644
index 000000000000..ff64a7e27c50
--- /dev/null
+++ b/runtime/queries/wren/indents.scm
@@ -0,0 +1,29 @@
+[
+	(list)
+	(map)
+	(call_body)
+	(parameter_list)
+	(call_expression)
+	(getter_definition)
+	(setter_definition)
+	(prefix_operator_definition)
+	(subscript_operator_definition)
+	(subscript_setter_definition)
+	(infix_operator_definition)
+	(method_definition)
+	(constructor)
+	(static_method_definition)
+	(static_getter_definition)
+	(attribute)
+	(conditional)
+	(class_body)
+	(if_statement)
+	(for_statement)
+	(while_statement)
+] @indent
+
+[
+	"}"
+	"]"
+	")"
+] @outdent
diff --git a/runtime/queries/wren/injections.scm b/runtime/queries/wren/injections.scm
new file mode 100644
index 000000000000..fca94d58fab7
--- /dev/null
+++ b/runtime/queries/wren/injections.scm
@@ -0,0 +1,3 @@
+((comment) @injection.content
+ (#set! injection.language "comment")
+ (#set! injection.include-children))
diff --git a/runtime/queries/wren/locals.scm b/runtime/queries/wren/locals.scm
new file mode 100644
index 000000000000..38f39d913ae4
--- /dev/null
+++ b/runtime/queries/wren/locals.scm
@@ -0,0 +1,21 @@
+[
+	(method_definition)
+] @local.scope
+
+(name) @local.reference
+(field) @local.reference
+(static_field) @local.reference
+
+(for_statement
+	loop_variable: (name) @local.definition)
+
+(variable_definition
+	name: (name) @local.definition)
+
+(assignment
+	left: (field) @local.definition)
+
+(assignment
+	left: (static_field) @local.definition)
+
+(parameter) @local.definition
diff --git a/runtime/queries/wren/textobjects.scm b/runtime/queries/wren/textobjects.scm
index 248981240fba..486b5b48cc1a 100644
--- a/runtime/queries/wren/textobjects.scm
+++ b/runtime/queries/wren/textobjects.scm
@@ -1,6 +1,10 @@
 (class_definition
   (class_body) @class.inside) @class.around
 
+(call_expression
+  (call_body
+    (_) @function.inside) @function.around)
+
 (method_definition
   body: (_) @function.inside) @function.around