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

    matlab-ts-mode--ei: align multi-line struct's and matrices in properties
---
 matlab-ts-mode--ei.el                              | 32 ++++++++++++++--------
 .../electric_indent_struct_and_matrix_in_prop.m    | 14 ++++++++++
 ...ric_indent_struct_and_matrix_in_prop_expected.m | 14 ++++++++++
 ...ndent_struct_and_matrix_in_prop_expected_msgs.m | 14 ++++++++++
 .../electric_indent_struct_in_prop.m               | 14 ++++++++++
 .../electric_indent_struct_in_prop_expected.m      | 14 ++++++++++
 .../electric_indent_struct_in_prop_expected_msgs.m | 14 ++++++++++
 .../electric_indent_struct_and_matrix_in_prop.m    | 14 ++++++++++
 ...c_indent_struct_and_matrix_in_prop_expected.txt | 23 ++++++++++++++++
 .../electric_indent_struct_in_prop.m               | 14 ++++++++++
 .../electric_indent_struct_in_prop_expected.txt    | 26 ++++++++++++++++++
 11 files changed, 181 insertions(+), 12 deletions(-)

diff --git a/matlab-ts-mode--ei.el b/matlab-ts-mode--ei.el
index 95732dfd47..43c2f19251 100644
--- a/matlab-ts-mode--ei.el
+++ b/matlab-ts-mode--ei.el
@@ -147,8 +147,8 @@
     ;;       @(x) ((ischar(x) || isstring(x)));
     (,(rx bos ")" eos)                ,(rx bos (or "identifier" "{" "(") eos)  
                  1)
 
-    ;; Case: property identifier: propName (1,1) double
-    (,(rx bos "property-id" eos)      "."                                      
                  1)
+    ;; Case: property identifier (the prop or class): propName (1,1) double
+    (,(rx bos (or "prop-id" "prop-class-id") eos)   "."                        
                  1)
 
     ;; Case: padded operators, e.g.: a || b
     (,matlab-ts-mode--ei-pad-op-re    "."                                      
                  1)
@@ -237,14 +237,16 @@ be unary-op even though the node type is \"+\"."
       ;; arguments name=value
       (setq node-type "n=v"))
 
-     ;; property-id, enum-id, attribute-id
+     ;; prop-id, prop-class-id, enum-id, attribute-id
      ((string= node-type "identifier")
-      (cond ((or
-              (and (string= parent-type "property") ;; propertyWithOutDot?
-                   (equal (treesit-node-child parent 0) node))
-              (and (string= parent-type "property_name") ;; 
property.nameWithDot?
-                   (equal (treesit-node-child (treesit-node-parent parent) 0) 
parent)))
-             (setq node-type "property-id"))
+      (cond ((string= parent-type "property") ;; propertyWithOutDot?
+             (if (equal (treesit-node-child parent 0) node)
+                 (setq node-type "prop-id")
+               (setq node-type "prop-class-id")))
+            ((string= parent-type "property_name") ;; property.nameWithDot?
+             (if (equal (treesit-node-child (treesit-node-parent parent) 0) 
parent)
+                 (setq node-type "prop-id")
+               (setq node-type "prop-class-id")))
             ((string= parent-type "enum")
              (setq node-type "enum-id"))
             ((string= parent-type "attribute")
@@ -1064,7 +1066,12 @@ Note, \\='m-struct returns (cons assignment-node 
max-field-width) or nil."
         (let ((eq-node (treesit-node-at (point))))
           ;; First "=" must be an assignment (assumptions elsewhere require 
this).
           (when (and (equal (treesit-node-type eq-node) "=")
-                     (equal (treesit-node-type (treesit-node-parent eq-node)) 
"assignment"))
+                     (if (string= (treesit-node-type assign-node) "assignment")
+                         (equal (treesit-node-type (treesit-node-parent 
eq-node))
+                                "assignment")
+                       (equal (treesit-node-type (treesit-node-parent
+                                                  (treesit-node-parent 
eq-node)))
+                              "property")))
             (cond
              ;; Single-line assignment? Example: v1 = [1, 2];
              ((eq a-type 'single-line)
@@ -1112,7 +1119,8 @@ line, each row is on the same line, with same number of 
columns:
   m = [100   2;
          3 400];"
   (let* ((first-node-in-line (nth 3 ei-info))
-         (assign-node (treesit-parent-until first-node-in-line (rx bos 
"assignment" eos))))
+         (assign-node (treesit-parent-until first-node-in-line
+                                            (rx bos (or "property" 
"assignment") eos))))
     (when assign-node
       (save-excursion
         (goto-char (treesit-node-start assign-node))
@@ -1190,7 +1198,7 @@ Returns nil if not a property, enum field, or argument 
node."
   (let* ((first-node-in-line (nth 3 ei-info))
          (modified-node-type (cdr (matlab-ts-mode--ei-get-node-to-use 
first-node-in-line)))
          (prop-node (pcase modified-node-type
-                      ((or "property-id" "enum-id")
+                      ((or "prop-id" "enum-id")
                        first-node-in-line)
                       ("property_name"
                        (treesit-node-parent first-node-in-line)))))
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop.m
new file mode 100644
index 0000000000..1835d50efa
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes
+
+classdef electric_indent_struct_and_matrix_in_prop
+    properties
+        s = struct( ...
+         'field1', 1, ...
+             'otherField2', 2);
+        m = [100, 2
+               3, 400];
+    end
+
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected.m
new file mode 100644
index 0000000000..3b3a1651fc
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes
+
+classdef electric_indent_struct_and_matrix_in_prop
+    properties
+        s = struct( ...
+            'field1'     , 1, ...
+            'otherField2', 2);
+        m = [100,   2
+               3, 400];
+    end
+
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected_msgs.m
new file mode 100644
index 0000000000..a2919fddf3
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected_msgs.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*- %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+classdef electric_indent_struct_and_matrix_in_prop %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
+    properties %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+        s = struct( ... %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+            'field1'     , 1, ... %  <{Matched rule: ((n-p-gp 
"\\`\\(?:)\\|arguments\\|line_continuation\\)\\'" "\\`function_call\\'" 
"\\`default_value\\'") great-grand-parent 4)}>
+            'otherField2', 2); %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
+        m = [100,   2 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+               3, 400]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+    end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
+
+end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop.m
new file mode 100644
index 0000000000..6d2707c1bb
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes
+
+classdef electric_indent_struct_in_prop
+    properties
+        s(1,1)struct{mustBeNonempty}=struct( ...
+                   'field1'                      , 1, ...
+             'otherField2',2);
+        m = [100, 2
+               3, 400];
+    end
+
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected.m
new file mode 100644
index 0000000000..9f6bcd28ee
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes
+
+classdef electric_indent_struct_in_prop
+    properties
+        s (1,1) struct {mustBeNonempty} = struct( ...
+            'field1'     , 1, ...
+            'otherField2', 2);
+        m = [100,   2
+               3, 400];
+    end
+
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected_msgs.m
new file mode 100644
index 0000000000..a4a6a656ac
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected_msgs.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*- %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+classdef electric_indent_struct_in_prop %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
+    properties %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+        s (1,1) struct {mustBeNonempty} = struct( ... %  <{Matched rule: 
((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+            'field1'     , 1, ... %  <{Matched rule: ((n-p-gp 
"\\`\\(?:)\\|arguments\\|line_continuation\\)\\'" "\\`function_call\\'" 
"\\`default_value\\'") great-grand-parent 4)}>
+            'otherField2', 2); %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
+        m = [100,   2 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+               3, 400]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+    end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
+
+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_struct_and_matrix_in_prop.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop.m
new file mode 100644
index 0000000000..1835d50efa
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes
+
+classdef electric_indent_struct_and_matrix_in_prop
+    properties
+        s = struct( ...
+         'field1', 1, ...
+             'otherField2', 2);
+        m = [100, 2
+               3, 400];
+    end
+
+end
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected.txt
new file mode 100644
index 0000000000..4f3bbfede0
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_and_matrix_in_prop_expected.txt
@@ -0,0 +1,23 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,319> (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,112]@{% 
t-utils-test-indent: no-line-by-line-indent - li...}@) \n[112,114]
+ (class_definition<114,318> classdef[114,122] name: 
(identifier[123,164]@{electric_indent_struct_and_matrix_in_prop}@) \n[164,165]
+  (properties<169,313> properties[169,179] \n[179,180]
+   (property<188,260> name: (identifier[188,189]@{s}@)
+    (default_value<190,260> =[190,191]
+     (function_call<192,260> name: (identifier[192,198]@{struct}@) ([198,199] 
(line_continuation[200,204]@{...\n}@)
+      (arguments<213,259>
+       argument: (string<213,221> '[213,214] 
(string_content[214,220]@{field1}@) '[220,221])
+       ,[221,222] (number[223,224]@{1}@) ,[224,225] 
(line_continuation[226,230]@{...\n}@)
+       (string<243,256> '[243,244] (string_content[244,255]@{otherField2}@) 
'[255,256])
+       ,[256,257] (number[258,259]@{2}@))
+      )[259,260])))
+   ;[260,261] \n[261,262]
+   (property<270,304> name: (identifier[270,271]@{m}@)
+    (default_value<272,304> =[272,273]
+     (matrix<274,304> [[274,275]
+      (row<275,281> (number[275,278]@{100}@) ,[278,279] (number[280,281]@{2}@))
+      (row<297,303> (number[297,298]@{3}@) ,[298,299] (number[300,303]@{400}@))
+      ][303,304])))
+   ;[304,305] \n[305,306] end[310,313])
+  \n[313,315] end[315,318])
+ \n[318,319])
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop.m
new file mode 100644
index 0000000000..6d2707c1bb
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop.m
@@ -0,0 +1,14 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - line-by-line typing results in 
error nodes
+
+classdef electric_indent_struct_in_prop
+    properties
+        s(1,1)struct{mustBeNonempty}=struct( ...
+                   'field1'                      , 1, ...
+             'otherField2',2);
+        m = [100, 2
+               3, 400];
+    end
+
+end
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected.txt
new file mode 100644
index 0000000000..1423c1f233
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_struct_in_prop_expected.txt
@@ -0,0 +1,26 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,364> (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,112]@{% 
t-utils-test-indent: no-line-by-line-indent - li...}@) \n[112,114]
+ (class_definition<114,363> classdef[114,122] name: 
(identifier[123,153]@{electric_indent_struct_in_prop}@) \n[153,154]
+  (properties<158,358> properties[158,168] \n[168,169]
+   (property<177,305> name: (identifier[177,178]@{s}@)
+    (dimensions<178,183> ([178,179] (number[179,180]@{1}@) ,[180,181] 
(number[181,182]@{1}@) )[182,183])
+    (identifier[183,189]@{struct}@)
+    (validation_functions<189,205> {[189,190] 
(identifier[190,204]@{mustBeNonempty}@) }[204,205])
+    (default_value<205,305> =[205,206]
+     (function_call<206,305> name: (identifier[206,212]@{struct}@) ([212,213] 
(line_continuation[214,218]@{...\n}@)
+      (arguments<237,304>
+       argument: (string<237,245> '[237,238] 
(string_content[238,244]@{field1}@) '[244,245])
+       ,[267,268] (number[269,270]@{1}@) ,[270,271] 
(line_continuation[272,276]@{...\n}@)
+       (string<289,302> '[289,290] (string_content[290,301]@{otherField2}@) 
'[301,302])
+       ,[302,303] (number[303,304]@{2}@))
+      )[304,305])))
+   ;[305,306] \n[306,307]
+   (property<315,349> name: (identifier[315,316]@{m}@)
+    (default_value<317,349> =[317,318]
+     (matrix<319,349> [[319,320]
+      (row<320,326> (number[320,323]@{100}@) ,[323,324] (number[325,326]@{2}@))
+      (row<342,348> (number[342,343]@{3}@) ,[343,344] (number[345,348]@{400}@))
+      ][348,349])))
+   ;[349,350] \n[350,351] end[355,358])
+  \n[358,360] end[360,363])
+ \n[363,364])

Reply via email to