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

    matlab-ts-mode: fix indent cell/row issue
---
 matlab-ts-mode.el                                  | 34 ++++++++++++++++++----
 .../indent_matrix.m                                | 11 ++++++-
 .../indent_matrix_expected.m                       | 11 ++++++-
 3 files changed, 49 insertions(+), 7 deletions(-)

diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 76286b6388..59b1ab40dc 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -934,6 +934,33 @@ cell or matrix row."
         0
       matlab-ts-mode--indent-level)))
 
+(defun matlab-ts-mode--row-indent-level (node parent _bol &rest _)
+  "Indent level for a NODE in PARENT cell or matrix."
+
+  ;; first-entry is the 2nd element, i.e. child 1. Child 0 is is the "[" or 
"{".
+  ;; first-entry could be "line_continuation" or "row"
+  ;; "row" can also be empty, e.g. start-point == end-point as in
+  ;;       C = [
+  ;;             ...
+  ;;   TAB>      1 ...
+  ;;           ]
+  (let* ((first-entry (treesit-node-child parent 1))
+         first-start)
+    (if (and (not (equal node first-entry)) ;; point is not on first row
+             (equal (treesit-node-type first-entry) "row")
+             (not (= (setq first-start (treesit-node-start first-entry))
+                     (treesit-node-end first-entry))))
+        ;;         c = [2, 3;
+        ;;  TAB>        3, 4];
+        (let ((first-column (save-excursion
+                              (goto-char first-start)
+                              (current-column)))
+              (array-column (save-excursion
+                              (goto-char (treesit-node-start parent))
+                              (current-column))))
+          (- first-column array-column))
+      matlab-ts-mode--array-indent-level)))
+
 (defvar matlab-ts-mode--indent-rules
   `((matlab
 
@@ -1085,7 +1112,8 @@ cell or matrix row."
 
      ;; I-Rule:  a = [   ...    |    a = { ...
      ;; <TAB>          2 ...    |          2 ...
-     ((parent-is ,(rx bos (or "matrix" "cell") eos)) parent 
,matlab-ts-mode--array-indent-level)
+     ;; See: tests/test-matlab-ts-mode-indent-files/indent_matrix.m
+     ((parent-is ,(rx bos (or "cell" "matrix") eos)) parent 
,#'matlab-ts-mode--row-indent-level)
 
      ;; I-Rule:  function [   ...              |    function name (   ...
      ;; <TAB>              a, ... % comment    |                   a, ... % 
comment
@@ -1813,10 +1841,6 @@ is t, add the following to an Init File (e.g. 
`user-init-file' or
     ;; TODO update --indent-rules to have See: test file comments.
     ;;
     ;; TODO indent
-    ;;      mat = [1, 2; ...
-    ;;              3, 4];         <== TAB should align
-    ;;
-    ;; TODO indent
     ;;      mat = [1, 2
     ;;             ^               <== RET on previous line or TAB should be 
here
     ;;
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_matrix.m 
b/tests/test-matlab-ts-mode-indent-files/indent_matrix.m
index c0c4fbdea4..99bdac410e 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_matrix.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_matrix.m
@@ -1,14 +1,20 @@
 % -*- matlab-ts -*-
-function a = indent_matrix
+function indent_matrix
     a = [ ...
           1 ...
           + ...
           2
         ];
+    disp(a);
 
     a = [    2 ...
              1 ...
         ];
+    disp(a);
+
+    a = [1, 2;
+         3, 4];
+    disp(a);
 
     a = [ ...
           2 + [ 3
@@ -18,16 +24,19 @@ function a = indent_matrix
                     ]
               ]
         ];
+    disp(a);
 
     a = [ ...
           1; ...
           2 ...
         ];
+    disp(a);
 
     long_variable_a = ...
         [
           2, 123, 456
           3,   2    7
         ];
+    disp(long_variable_a);
 
 end
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected.m 
b/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected.m
index c0c4fbdea4..99bdac410e 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected.m
@@ -1,14 +1,20 @@
 % -*- matlab-ts -*-
-function a = indent_matrix
+function indent_matrix
     a = [ ...
           1 ...
           + ...
           2
         ];
+    disp(a);
 
     a = [    2 ...
              1 ...
         ];
+    disp(a);
+
+    a = [1, 2;
+         3, 4];
+    disp(a);
 
     a = [ ...
           2 + [ 3
@@ -18,16 +24,19 @@ function a = indent_matrix
                     ]
               ]
         ];
+    disp(a);
 
     a = [ ...
           1; ...
           2 ...
         ];
+    disp(a);
 
     long_variable_a = ...
         [
           2, 123, 456
           3,   2    7
         ];
+    disp(long_variable_a);
 
 end

Reply via email to