From 5a1bed2b708f6874daa9f04b26b14b3106d2e447 Mon Sep 17 00:00:00 2001
From: Michael Davis <mcarsondavis@gmail.com>
Date: Wed, 2 Nov 2022 09:38:36 -0500
Subject: [PATCH] Add parameter highlights to Erlang

This doesn't work robustly (within pattern matches). Only regular
bindings are highlighted as parameters. In order to highlight all
parameters even in matches, we would need an arbitrary nesting operator
in queries which doesn't exist yet in tree-sitter.
---
 runtime/queries/erlang/highlights.scm | 34 ++++++++++++++++++++++++---
 runtime/queries/erlang/locals.scm     | 30 +++++++++++++++++++++++
 2 files changed, 61 insertions(+), 3 deletions(-)
 create mode 100644 runtime/queries/erlang/locals.scm

diff --git a/runtime/queries/erlang/highlights.scm b/runtime/queries/erlang/highlights.scm
index 050fb613..3f4ef4cb 100644
--- a/runtime/queries/erlang/highlights.scm
+++ b/runtime/queries/erlang/highlights.scm
@@ -65,6 +65,37 @@
 (function_capture module: (atom) @namespace)
 (function_capture function: (atom) @function)
 
+; Ignored variables
+((variable) @comment.discard
+ (#match? @comment.discard "^_"))
+
+; Parameters
+; specs
+((attribute
+   name: (atom) @keyword
+   (stab_clause
+     pattern: (arguments (variable) @variable.parameter)
+     body: (variable)? @variable.parameter))
+ (#match? @keyword "(spec|callback)"))
+; functions
+(function_clause pattern: (arguments (variable) @variable.parameter))
+; anonymous functions
+(stab_clause pattern: (arguments (variable) @variable.parameter))
+; parametric types
+((attribute
+    name: (atom) @keyword
+    (arguments
+      (binary_operator
+        left: (call (arguments (variable) @variable.parameter))
+        operator: "::")))
+ (#match? @keyword "(type|opaque)"))
+; macros
+((attribute
+   name: (atom) @keyword
+   (arguments
+     (call (arguments (variable) @variable.parameter))))
+ (#eq? @keyword "define"))
+
 ; Records
 (record_content
   (binary_operator
@@ -105,9 +136,6 @@
   name: (_) @keyword.directive)
 
 ; Comments
-((variable) @comment.discard
- (#match? @comment.discard "^_"))
-
 (tripledot) @comment.discard
 
 [(comment) (line_comment) (shebang)] @comment
diff --git a/runtime/queries/erlang/locals.scm b/runtime/queries/erlang/locals.scm
new file mode 100644
index 00000000..7379926b
--- /dev/null
+++ b/runtime/queries/erlang/locals.scm
@@ -0,0 +1,30 @@
+; Specs and Callbacks
+(attribute
+  (stab_clause
+    pattern: (arguments (variable) @local.definition)
+    ; If a spec uses a variable as the return type (and later a `when` clause to type it):
+    body: (variable)? @local.definition)) @local.scope
+
+; parametric `-type`s
+((attribute
+    name: (atom) @_type
+    (arguments
+      (binary_operator
+        left: (call (arguments (variable) @local.definition))
+        operator: "::") @local.scope))
+ (#match? @_type "(type|opaque)"))
+
+; macros
+((attribute
+   name: (atom) @_define
+   (arguments
+     (call (arguments (variable) @local.definition)))) @local.scope
+ (#eq? @_define "define"))
+
+; `fun`s
+(anonymous_function (stab_clause pattern: (arguments (variable) @local.definition))) @local.scope
+
+; Ordinary functions
+(function_clause pattern: (arguments (variable) @local.definition)) @local.scope
+
+(variable) @local.reference