branch: externals/matlab-mode
commit 43065fb9d651ab035306234c96c18b69d48d101c
Author: John Ciolfi <[email protected]>
Commit: John Ciolfi <[email protected]>

    matlab-ts-mode: improve electric indent of name=value fcn args
---
 matlab-ts-mode--ei.el                              | 35 ++++++++------
 .../electric_indent_name_value_args.m              | 13 ++++++
 .../electric_indent_name_value_args_expected.m     | 13 ++++++
 ...electric_indent_name_value_args_expected_msgs.m | 13 ++++++
 .../electric_indent_classdef_prop_get_set.m        | 20 ++++++++
 ...ctric_indent_classdef_prop_get_set_expected.txt | 53 ++++++++++++++++++++++
 .../electric_indent_name_value_args.m              | 13 ++++++
 .../electric_indent_name_value_args_expected.txt   | 31 +++++++++++++
 8 files changed, 178 insertions(+), 13 deletions(-)

diff --git a/matlab-ts-mode--ei.el b/matlab-ts-mode--ei.el
index 0e70c8da13..122dd42566 100644
--- a/matlab-ts-mode--ei.el
+++ b/matlab-ts-mode--ei.el
@@ -189,6 +189,11 @@
     (,matlab-ts-mode--ei-pad-op-re    "."                                      
                  1)
     ("."                              ,matlab-ts-mode--ei-pad-op-re            
                  1)
 
+    ;; Case arguments name=value syntax, the "=" node is converted to "n=v"
+    ;; TopTester: electric_indent_name_value_args.m
+    ("."                             ,(rx bos "n=v" eos)                       
                  0)
+    (,(rx bos "n=v" eos)             "."                                       
                  0)
+
     ;; Case: string followed by anything, e.g. ["string1" foo(1)]
     (,(rx bos "string" eos)           "."                                      
                  1)
 
@@ -256,43 +261,47 @@ be unary-op even though the node type is \"+\"."
 
   (let* ((node-type (treesit-node-type node))
          (parent (treesit-node-parent node))
-         (parent-type (treesit-node-type parent)))
+         (parent-type (or (treesit-node-type parent) "")))
 
     (cond
      ;; Use string and not the elements of the string
-     ((equal parent-type "string")
+     ((string= parent-type "string")
       (setq node parent
             node-type parent-type))
 
+     ((and (string= node-type "=")
+           (string= parent-type "arguments"))
+      ;; arguments name=value
+      (setq node-type "n=v"))
+
      ;; convert property identifier to property-id node-type
-     ((and (equal node-type "identifier")
+     ((and (string= node-type "identifier")
            (or
             ;; propertyWithOutDot?
-            (and (equal parent-type "property")
+            (and (string= parent-type "property")
                  (equal (treesit-node-child parent 0) node))
             ;; property.nameWithDot?
-            (and (equal parent-type "property_name")
+            (and (string= parent-type "property_name")
                  (equal (treesit-node-child (treesit-node-parent parent) 0) 
parent))))
       (setq node-type "property-id"))
 
      ;; Unary operator sign, + or -, e.g. [0 -e] or g = - e
-     ((and (equal parent-type "unary_operator")
+     ((and (string= parent-type "unary_operator")
            (equal (treesit-node-child parent 0) node))
       (setq node-type "unary-op"))
 
      ;; Super-class constructor call
      ;;  [email protected];
-     ((and (equal node-type "@")
-           (equal parent-type "function_call"))
+     ((and (string= node-type "@")
+           (string= parent-type "function_call"))
       (setq node-type "@-fcn-call"))
 
      ;; Property dimensions
      ;;   foo1 (1, :) {mustBeNumeric, mustBeReal} = [0, 0, 0];
-     ((and (or (equal node-type "number") (equal node-type ":"))
-           (or (equal parent-type "dimensions")
-               (and (equal parent-type "spread_operator")
-                    (equal (treesit-node-type (treesit-node-parent parent))
-                           "dimensions"))))
+     ((and (or (string= node-type "number") (string= node-type ":"))
+           (or (string= parent-type "dimensions")
+               (and (string= parent-type "spread_operator")
+                    (string= (treesit-node-type (treesit-node-parent parent)) 
"dimensions"))))
       (setq node-type "prop-dim"))
      )
 
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args.m
new file mode 100644
index 0000000000..e876dd28fd
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args.m
@@ -0,0 +1,13 @@
+% -*- matlab-ts -*-
+
+r = myFunction(Name1=3,Name2=7)
+
+function result = myFunction(NameValueArgs)
+    arguments
+        NameValueArgs.Name1
+        NameValueArgs.Name2
+    end
+
+    % Function code
+    result = NameValueArgs.Name1 * NameValueArgs.Name2;
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected.m
new file mode 100644
index 0000000000..c3d28d609d
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected.m
@@ -0,0 +1,13 @@
+% -*- matlab-ts -*-
+
+r = myFunction(Name1=3, Name2=7)
+
+function result = myFunction(NameValueArgs)
+    arguments
+        NameValueArgs.Name1
+        NameValueArgs.Name2
+    end
+
+    % Function code
+    result = NameValueArgs.Name1 * NameValueArgs.Name2;
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected_msgs.m
new file mode 100644
index 0000000000..96a79c4287
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected_msgs.m
@@ -0,0 +1,13 @@
+% -*- matlab-ts -*- %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+r = myFunction(Name1=3, Name2=7) %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
+
+function result = myFunction(NameValueArgs) %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
+    arguments %  <{Matched rule: ((parent-is "\\`function_definition\\'") 
parent matlab-ts-mode--set-function-indent-level-for-gp)}>
+        NameValueArgs.Name1 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+        NameValueArgs.Name2 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+    end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
+
+    % Function code %  <{Matched rule: ((parent-is 
"\\`function_definition\\'") parent 
matlab-ts-mode--set-function-indent-level-for-gp)}>
+    result = NameValueArgs.Name1 * NameValueArgs.Name2; %  <{Matched rule: 
((parent-is "\\`function_definition\\'") parent 
matlab-ts-mode--set-function-indent-level-for-gp)}>
+end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_classdef_prop_get_set.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_classdef_prop_get_set.m
new file mode 100644
index 0000000000..5cceaffc47
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_classdef_prop_get_set.m
@@ -0,0 +1,20 @@
+% -*- matlab-ts -*-
+classdef electric_indent_classdef_prop_get_set
+    properties
+      inputMatrix = [1 0;0 1]
+    end
+    
+    methods
+        function obj = set.inputMatrix(obj, val)
+            try chol(val)
+                obj.inputMatrix = val;
+               catch ME
+             error("inputMatrix must be symmetric positive definite."  )
+            end
+        end
+
+     function m = get.inputMatrix(obj)
+         m = obj.inputMatrix;
+      end
+    end
+end
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_classdef_prop_get_set_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_classdef_prop_get_set_expected.txt
new file mode 100644
index 0000000000..e55880d689
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_classdef_prop_get_set_expected.txt
@@ -0,0 +1,53 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,469> (comment[1,20]@{% -*- matlab-ts -*-}@) \n[20,21]
+ (class_definition<21,468> classdef[21,29] name: 
(identifier[30,67]@{electric_indent_classdef_prop_get_set}@) \n[67,68]
+  (properties<72,120> properties[72,82] \n[82,83]
+   (property<89,112> name: (identifier[89,100]@{inputMatrix}@)
+    (default_value<101,112> =[101,102]
+     (matrix<103,112> [[103,104]
+      (row<104,107> (number[104,105]@{1}@) ,[106,106] (number[106,107]@{0}@))
+      (row<108,111> (number[108,109]@{0}@) ,[110,110] (number[110,111]@{1}@))
+      ][111,112])))
+   \n[112,113] end[117,120])
+  \n[120,121] \n[125,126]
+  (methods<130,464> methods[130,137] \n[137,138]
+   (function_definition<146,376> function[146,154]
+    (function_output<155,160> (identifier[155,158]@{obj}@) =[159,160])
+    set.[161,165] name: (identifier[165,176]@{inputMatrix}@)
+    (function_arguments<176,186> ([176,177] arguments: 
(identifier[177,180]@{obj}@) ,[180,181] (identifier[182,185]@{val}@) )[185,186])
+    \n[186,187]
+    (block<199,365>
+     (try_statement<199,364> try[199,202]
+      (block<203,252>
+       (function_call<203,212> name: (identifier[203,207]@{chol}@) ([207,208]
+        (arguments<208,211> argument: (identifier[208,211]@{val}@))
+        )[211,212])
+       \n[212,213]
+       (assignment<229,250>
+        left: (field_expression<229,244> object: (identifier[229,232]@{obj}@) 
.[232,233] field: (identifier[233,244]@{inputMatrix}@))
+        =[245,246] right: (identifier[247,250]@{val}@))
+       ;[250,251] \n[251,252])
+      (catch_clause<267,349> catch[267,272] (identifier[273,275]@{ME}@) 
\n[275,276]
+       (block<289,349>
+        (function_call<289,348> name: (identifier[289,294]@{error}@) ([294,295]
+         (arguments<295,345>
+          argument: (string<295,345> "[295,296] 
(string_content[296,344]@{inputMatrix must be symmetric positive definite.}@) 
"[344,345]))
+         )[347,348])
+        \n[348,349]))
+      end[361,364])
+     \n[364,365])
+    end[373,376])
+   \n[376,378]
+   (function_definition<383,456> function[383,391]
+    (function_output<392,395> (identifier[392,393]@{m}@) =[394,395])
+    get.[396,400] name: (identifier[400,411]@{inputMatrix}@)
+    (function_arguments<411,416> ([411,412] arguments: 
(identifier[412,415]@{obj}@) )[415,416])
+    \n[416,417]
+    (block<426,447>
+     (assignment<426,445> left: (identifier[426,427]@{m}@) =[428,429]
+      right: (field_expression<430,445> object: (identifier[430,433]@{obj}@) 
.[433,434] field: (identifier[434,445]@{inputMatrix}@)))
+     ;[445,446] \n[446,447])
+    end[453,456])
+   \n[456,457] end[461,464])
+  \n[464,465] end[465,468])
+ \n[468,469])
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args.m
new file mode 100644
index 0000000000..e876dd28fd
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args.m
@@ -0,0 +1,13 @@
+% -*- matlab-ts -*-
+
+r = myFunction(Name1=3,Name2=7)
+
+function result = myFunction(NameValueArgs)
+    arguments
+        NameValueArgs.Name1
+        NameValueArgs.Name2
+    end
+
+    % Function code
+    result = NameValueArgs.Name1 * NameValueArgs.Name2;
+end
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected.txt
new file mode 100644
index 0000000000..230e60552d
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_name_value_args_expected.txt
@@ -0,0 +1,31 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,258> (comment[1,20]@{% -*- matlab-ts -*-}@)
+ (assignment<22,53> left: (identifier[22,23]@{r}@) =[24,25]
+  right: 
+   (function_call<26,53> name: (identifier[26,36]@{myFunction}@) ([36,37]
+    (arguments<37,52> (identifier[37,42]@{Name1}@) =[42,43] 
(number[43,44]@{3}@) ,[44,45] (identifier[45,50]@{Name2}@) =[50,51] 
(number[51,52]@{7}@))
+    )[52,53]))
+ \n[53,55]
+ (function_definition<55,257> function[55,63]
+  (function_output<64,72> (identifier[64,70]@{result}@) =[71,72])
+  name: (identifier[73,83]@{myFunction}@)
+  (function_arguments<83,98> ([83,84] arguments: 
(identifier[84,97]@{NameValueArgs}@) )[97,98])
+  \n[98,99]
+  (arguments_statement<103,176> arguments[103,112] \n[112,113]
+   (property<121,140>
+    name: (property_name<121,140> (identifier[121,134]@{NameValueArgs}@) 
.[134,135] (identifier[135,140]@{Name1}@)))
+   \n[140,141]
+   (property<149,168>
+    name: (property_name<149,168> (identifier[149,162]@{NameValueArgs}@) 
.[162,163] (identifier[163,168]@{Name2}@)))
+   \n[168,169] end[173,176])
+  (comment[182,197]@{% Function code}@)
+  (block<202,254>
+   (assignment<202,252> left: (identifier[202,208]@{result}@) =[209,210]
+    right: 
+     (binary_operator<211,252>
+      left: (field_expression<211,230> object: 
(identifier[211,224]@{NameValueArgs}@) .[224,225] field: 
(identifier[225,230]@{Name1}@))
+      *[231,232]
+      right: (field_expression<233,252> object: 
(identifier[233,246]@{NameValueArgs}@) .[246,247] field: 
(identifier[247,252]@{Name2}@))))
+   ;[252,253] \n[253,254])
+  end[254,257])
+ \n[257,258])

Reply via email to