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

    matlab-ts-mode: add electric indent matrix column alignment
---
 matlab-ts-mode.el                                  | 655 +++++++++++++++++----
 .../electric_ends_cases_expected.org               |  34 +-
 .../electric_indent_matrix_basic.m                 |  18 +
 .../electric_indent_matrix_basic_expected.m        |  18 +
 .../electric_indent_matrix_basic_expected_msgs.m   |  18 +
 .../electric_indent_matrix_cols.m                  |  97 +++
 .../electric_indent_matrix_cols_expected.m         |  97 +++
 .../electric_indent_matrix_cols_expected_msgs.m    |  97 +++
 .../electric_indent_matrix_cols_with_error.m       |   4 +
 ...ectric_indent_matrix_cols_with_error_expected.m |   4 +
 ...c_indent_matrix_cols_with_error_expected_msgs.m |   4 +
 .../electric_indent_matrix_expected_msgs.m         |  10 +-
 .../electric_indent_matrix_n_levels.m              |  20 +
 .../electric_indent_matrix_n_levels_expected.m     |  20 +
 ...electric_indent_matrix_n_levels_expected_msgs.m |  20 +
 ...ctric_indent_with_syntax_errors_expected_msgs.m |   6 +-
 .../electric_indent_xr_switch.m                    |  19 +
 .../electric_indent_xr_switch_expected.org         | 118 ++++
 tests/test-matlab-ts-mode-electric-indent-xr.el    |   2 +-
 .../indent_cell_expected_msgs.m                    |  10 +-
 .../indent_matrix_expected_msgs.m                  |   6 +-
 .../indent_old_indents_expected_msgs.m             |  32 +-
 .../indent_old_indents_for_typing_expected_msgs.m  |  32 +-
 .../electric_indent_matrix_basic.m                 |  18 +
 .../electric_indent_matrix_basic_expected.txt      |  31 +
 .../electric_indent_matrix_cols.m                  |  97 +++
 .../electric_indent_matrix_cols_expected.txt       | 193 ++++++
 .../electric_indent_matrix_cols_with_error.m       |   4 +
 ...tric_indent_matrix_cols_with_error_expected.txt |  13 +
 .../electric_indent_matrix_n_levels.m              |  20 +
 .../electric_indent_matrix_n_levels_expected.txt   |  29 +
 .../electric_indent_xr_switch.m                    |  19 +
 .../electric_indent_xr_switch_expected.txt         |   2 +
 33 files changed, 1579 insertions(+), 188 deletions(-)

diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 9b0b067b07..1a705904ab 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -93,12 +93,36 @@ You can also install via use-package or other methods."
   :type 'boolean)
 
 (defcustom matlab-ts-mode-electric-indent t
-  "*If t, indent elements within lines of code.
-This will adjust the whitespace within elements of code.
-Consider
-    a = b+ c *d ;
-When electric indent is active, this will be indented to be:
-    a = b + c * d;"
+  "*If t, indent (format) language elements within code.
+- Canonicalize language elements spacing
+     Example                        |  Result
+     -----------------------------  |  -----------------------------
+     a = b+ c *d ;                  |  a = b + c * d;
+
+- Align consecutive assignments
+     Example                        |  Result
+     -----------------------------  |  -----------------------------
+     width = 100;                   |  width  = 100;
+     length = 200;                  |  length = 200;
+     area=width * length;           |  area   = width * length;
+
+- Align trailing comments
+     Example                        |  Result
+     -----------------------------  |  -----------------------------
+     a = myFcn1(1, 2); % comment 1  |  a = myFcn1(1, 2); % comment 1
+     a = a + 5; % comment 2         |  a = a + 2;        % comment 2
+
+- Align matrix columns
+     Example                        |  Result
+     -----------------------------  |  -----------------------------
+     m = [2,4000                    |  m = [   2, 4000
+            3000,1]                 |       3000,    1]
+
+- Align properties
+   Example:
+      TODO
+   is indented as:
+      TODO"
   :type 'boolean)
 
 (defvar matlab-ts-mode--electric-indent-verbose nil)
@@ -2440,31 +2464,56 @@ Example:
   "Return anchor for `matlab-ts-mode--i-arg-namespace-fcn-prop-matcher'."
   matlab-ts-mode--i-arg-namespace-fcn-prop-anchor-value)
 
-(defvar matlab-ts-mode--i-row-anchor-value nil)
+(defvar matlab-ts-mode--i-row-matcher-pair nil)
 
-(defun matlab-ts-mode--i-row-matcher (node parent _bol &rest _)
+(cl-defun matlab-ts-mode--i-row-matcher (node parent _bol &rest _)
   "Is NODE, PARENT in a matrix with first row on the \"[\" or \"{\" line?
 Example:
-   m = [1, 2, ...
+   m = [1, 2
         3, 4]       <== TAB to here."
-  (and node
-       (string= (treesit-node-type node) "row")
-       (string-match-p (rx bos (or "cell" "matrix") eos) (treesit-node-type 
parent))
-       (save-excursion
-         (goto-char (treesit-node-start parent))
-         (forward-char)
-         (when (and (looking-at "[ \t]")
-                    (re-search-forward "[^ \t]" (line-end-position) t))
-           (backward-char))
-         ;; Have something after the "[", e.g. "[  123" or "[ ..."?
-         (when (and (looking-at "[^ \t\r\n]")
-                    (not (equal (treesit-node-type (treesit-node-at (point))) 
"line_continuation")))
-           (setq matlab-ts-mode--i-row-anchor-value (treesit-node-start 
(treesit-node-at (point))))
-           t))))
-
-(defun matlab-ts-mode--i-row-anchor (_node _parent _bol &rest _)
+
+  (when (or (not node)
+            (not (string= (treesit-node-type node) "row")))
+    (cl-return-from matlab-ts-mode--i-row-matcher))
+
+  (when (and matlab-ts-mode-electric-indent
+             (string= "matrix" (treesit-node-type parent))
+             (matlab-ts-mode--ei-is-m-matrix parent))
+    ;; Align to first column width
+    ;;       m1 = [   23      |    m2 = [45678      |    m3 = [  23
+    ;; TAB>        45678]     |             23]     |           333
+    ;;                                              |          4444]
+    (let* ((first-col-extra (matlab-ts-mode--ei-m-matrix-first-col-extra 
parent))
+           (column-widths (matlab-ts-mode--ei-m-matrix-col-widths parent 
first-col-extra t))
+           (el-width (save-excursion
+                       (goto-char (treesit-node-start node))
+                       (let ((el (treesit-node-at (point))))
+                         (- (treesit-node-end el) (treesit-node-start el)))))
+           (el-spaces (- (alist-get 1 column-widths) el-width)))
+      (setq matlab-ts-mode--i-row-matcher-pair (cons (treesit-node-start 
parent) (1+ el-spaces)))
+      (cl-return-from matlab-ts-mode--i-row-matcher t)))
+
+  (when (string-match-p (rx bos (or "cell" "matrix") eos) (treesit-node-type 
parent))
+    (save-excursion
+      (goto-char (treesit-node-start parent))
+      (forward-char)
+      (when (and (looking-at "[ \t]")
+                 (re-search-forward "[^ \t]" (line-end-position) t))
+        (backward-char))
+      ;; Have something after the "[", e.g. "[  123" or "[ ..."?
+      (let ((node-at-pt (treesit-node-at (point))))
+        (when (and (looking-at "[^ \t\r\n]")
+                   (not (equal (treesit-node-type node-at-pt) 
"line_continuation")))
+          (setq matlab-ts-mode--i-row-matcher-pair (cons (treesit-node-start 
node-at-pt) 0))
+          t)))))
+
+(defun matlab-ts-mode--i-row-matcher-anchor (&rest _)
   "Return anchor for `matlab-ts-mode--i-row-matcher'."
-  matlab-ts-mode--i-row-anchor-value)
+  (car matlab-ts-mode--i-row-matcher-pair))
+
+(defun matlab-ts-mode--i-row-matcher-offset (&rest _)
+  "Return offset for `matlab-ts-mode--i-row-matcher'."
+  (cdr matlab-ts-mode--i-row-matcher-pair))
 
 (defvar matlab-ts-mode--i-ret-pair)
 
@@ -2714,8 +2763,8 @@ Example:
      ;; See: tests/test-matlab-ts-mode-indent-files/indent_matrix.m
      ;; See: tests/test-matlab-ts-mode-indent-files/indent_cell.m
      (,#'matlab-ts-mode--i-row-matcher
-      ,#'matlab-ts-mode--i-row-anchor
-      0)
+      ,#'matlab-ts-mode--i-row-matcher-anchor
+      ,#'matlab-ts-mode--i-row-matcher-offset)
 
      ;; I-Rule:  a = [   ...    |    a = { ...
      ;; <TAB>          2 ...    |          2 ...
@@ -3085,21 +3134,34 @@ Assumes point is at of current node or beginning of 
line."
       (error "Assert: line-node-types mismatch \"%s\" !EQ \"%s\" at line %d in 
%s"
              curr-line-node-types line-node-types (line-number-at-pos (point)) 
(buffer-name)))))
 
-(defun matlab-ts-mode--concat-ei-line (ei-line node extra-chars 
n-spaces-to-append)
+(defun matlab-ts-mode--concat-ei-line (ei-line node extra-chars &optional 
n-spaces-to-append)
   "Return concat EI-LINE with NODE text.
 NODE-END is the NODE end accounting for ignored nodes (semicolons).
 EXTRA-CHARS are appended to EL-LINE.
-N-SPACES-TO-APPEND is the number of spaces to append."
+N-SPACES-TO-APPEND is the number of spaces to append between nodes."
 
-  (let ((node-end (treesit-node-end node)))
+  (let* ((node-end (treesit-node-end node))
+         (last-pt (if (< node-end (line-end-position)) node-end 
(line-end-position))))
     (concat ei-line
-            (if (< node-end (line-end-position))
-                (buffer-substring (treesit-node-start node) node-end)
-              (string-trim-right
-               (buffer-substring (treesit-node-start node) 
(line-end-position))))
+            (buffer-substring (treesit-node-start node) last-pt)
             extra-chars
-            (when (> n-spaces-to-append 0)
-              (make-string n-spaces-to-append ? )))))
+            (if (not n-spaces-to-append) ;; last node?
+                ;; Add trailing whitespace when in an ERROR node. Consider
+                ;;    switch a
+                ;;      case                    ;; One trailing whitespace
+                ;;    end
+                ;; TopTester: electric_indent_xr_switch.m
+                (when (and (treesit-parent-until node (rx bos "ERROR" eos))
+                           (< last-pt (line-end-position)))
+                  (save-excursion
+                    (end-of-line)
+                    (when (re-search-backward "[^ \t]" 
(line-beginning-position) t)
+                      (forward-char)
+                      (when (not (= (point) (line-end-position)))
+                        (buffer-substring (point) (line-end-position))
+                        ))))
+              (when (> n-spaces-to-append 0)
+                (make-string n-spaces-to-append ? ))))))
 
 (defun matlab-ts-mode--no-elements-to-indent ()
   "Return t if no elements in the current line to indent.
@@ -3162,12 +3224,12 @@ Assumes that current point is at `back-to-indentation'."
 
 (cl-defun matlab-ts-mode--ei-get-new-line (&optional start-node start-offset)
   "Get new line content with element spacing adjusted.
-Optional START-NODE and START-OFFSET are used to compute new line-offset
-to restore point after updating line.  Note, new line content may be same
-as current line.  Also computes line-node-types which is a string containing
-the line node types.
+Optional START-NODE and START-OFFSET are used to compute new pt-offset,
+the point offset in the line used to restore point after updating line.
+Note, new line content may be same as current line.  Also computes
+line-node-types which is a string containing the line node types.
 Returns electric indent info, ei-info,
-  (list NEW-LINE-CONTENT LINE-OFFSET LINE-NODE-TYPES FIRST-NODE-IN-LINE)
+  (list NEW-LINE-CONTENT PT-OFFSET LINE-NODE-TYPES FIRST-NODE-IN-LINE)
 or nil."
   (save-excursion
     (back-to-indentation)
@@ -3175,7 +3237,7 @@ or nil."
       (cl-return-from matlab-ts-mode--ei-get-new-line))
 
     ;; Compute ei-line, the electric indented line content
-    (let* (line-offset ;; used in restoring point
+    (let* (pt-offset ;; used in restoring point
            (ei-line (buffer-substring (line-beginning-position) (point)))
            (pair (matlab-ts-mode--move-to-and-get-node))
            (node (or (car pair)
@@ -3251,7 +3313,7 @@ or nil."
                 (extra-chars (matlab-ts-mode--node-extra-chars node-end 
next-node-start)))
            ;; Update ei-line
            (when (equal start-node node)
-             (setq line-offset (+ (length ei-line) start-offset)))
+             (setq pt-offset (+ (length ei-line) start-offset)))
 
            (setq ei-line (matlab-ts-mode--concat-ei-line ei-line node 
extra-chars n-spaces-between))
 
@@ -3260,33 +3322,353 @@ or nil."
            )))
 
       (when node
+        (when (equal start-node node)
+          (setq pt-offset (+ (length ei-line) start-offset)))
         (when matlab-ts-mode--electric-indent-assert
           (setq line-node-types (matlab-ts-mode--update-line-node-types 
line-node-types
                                                                         node 
node-type)))
         (let ((extra-chars (matlab-ts-mode--node-extra-chars
                             (min (treesit-node-end node) (line-end-position))
                             (line-end-position))))
-          (setq ei-line (matlab-ts-mode--concat-ei-line ei-line node 
extra-chars 0))))
+          (setq ei-line (matlab-ts-mode--concat-ei-line ei-line node 
extra-chars))))
+
+      (list ei-line pt-offset line-node-types first-node))))
+
+(cl-defun matlab-ts-mode--ei-m-matrix-first-col-extra (matrix)
+  "For MATRIX indent alignment, get first-col-extra."
+  ;; For first-col-extra consider the following where 
matlab-ts-mode--array-indent-level is 2.
+  ;; In this case first-col-extra will be 1.
+  ;;   m = [
+  ;;         1 2
+  ;;         3 4
+  ;;       ];
+  (let ((first-col-extra (save-excursion
+                           (goto-char (treesit-node-start matrix))
+                           (forward-char) ;; step over the "["
+                           (let ((found-element nil))
+                             ;; found a matrix element?
+                             (while (and (not found-element)
+                                         (re-search-forward "[^ \t]" 
(line-end-position) t))
+                               (backward-char)
+                               (let ((node (treesit-node-at (point))))
+                                 (when (not (string-match-p (rx bos (or 
"comment"
+                                                                        
"line_continuation")
+                                                                eos)
+                                                            (treesit-node-type 
node)))
+                                   (setq found-element t))
+                                 (goto-char (min (line-end-position)
+                                                 (treesit-node-end node)))))
+                             (if found-element 0 (1- 
matlab-ts-mode--array-indent-level))))))
+    first-col-extra))
+
+(cl-defun matlab-ts-mode--ei-m-matrix-col-widths (matrix first-col-extra 
&optional first-col-only)
+  "Get multi-line MATRIX column widths adding in FIRST-COL-EXTRA to first 
column.
+If optional FIRST-COL-ONLY is non-nil, then return only the width of the
+first column in MATRIX.
+Returns alist where each element in the alist is (COLUMN-NUM . WIDTH)"
+  (let ((column-widths '())) ;; (alist-get column-num column-widths) ==> width
+    (dolist (m-child (treesit-node-children matrix))
+      (when (string= (treesit-node-type m-child) "row")
+        (let ((column-num 0))
+          (cl-loop
+           for entry in (treesit-node-children m-child)
+           do
+           (when (not (string= (treesit-node-type entry) ","))
+             (setq column-num (1+ column-num))
+             (let ((width (or (alist-get column-num column-widths) 0))) ;; 
matrix element width
+               (let ((el-width (- (treesit-node-end entry) (treesit-node-start 
entry))))
+                 (when (> el-width width)
+                   (if (= width 0)
+                       (push `(,column-num . ,el-width) column-widths)
+                     (setf (alist-get column-num column-widths) el-width)))))
+             (when first-col-only
+               (cl-return))
+             )))))
+
+    (when (> first-col-extra 0)
+      (let ((col1-width (+ (alist-get 1 column-widths) first-col-extra)))
+        (setf (alist-get 1 column-widths) col1-width)))
+
+    column-widths))
+
+(defun matlab-ts-mode--get-m-matrix-row-in-line ()
+  "Given point within a matrix assignment statement, return row node.
+Note, nil may be returned when line is only a continuation, e.g.
+   v = [1 2; ...
+        ...
+        3 4];
+when on the 2nd continuation only line, nil is returned."
+  (save-excursion
+    (back-to-indentation)
+    (let (row-node
+          found-ans)
+      (cl-loop
+       while (not found-ans) do
+
+       (let* ((node-at-pt (treesit-node-at (point)))
+              (node node-at-pt))
+         (while (and node
+                     (not (string-match-p (rx bos (or "row" "matrix" 
"assignment") eos)
+                                          (treesit-node-type node))))
+           (setq node (treesit-node-parent node)))
+
+         (if (and node
+                  (string= (treesit-node-type node) "row"))
+             (setq found-ans t
+                   row-node node)
+           (goto-char (min (treesit-node-end node-at-pt) (line-end-position)))
+           (when (not (re-search-forward "[^ \t]" (line-end-position) t))
+             (setq found-ans t)))))
+      row-node)))
+
+;; Internal variable that shouldn't be altered. It's used to avoid infinite 
recursion.
+(defvar matlab-ts-mode--ei-align-enabled t)
+
+;; This is used to cache matrix alignments for indent-region
+;; It will be non-nil when called from indent-region.
+(defvar matlab-ts-mode--ei-align-matrix-alist nil)
+
+(defun matlab-ts-mode--ei-indent-matrix-in-tmp-buf (assign-node)
+  "Insert ASSIGN-NODE in to current tmp-buf and indent.
+Point is left at beginning of line containing the ASSIGN-NODE text."
+  (let (assign-str
+        n-levels
+        start-pt)
+    (with-current-buffer (treesit-node-buffer assign-node)
+      (let* ((assign-start-pos (save-excursion (goto-char (treesit-node-start 
assign-node))
+                                              (line-beginning-position)))
+             (assign-end-pos (save-excursion (goto-char (treesit-node-end 
assign-node))
+                                             (line-end-position)))
+             (indent-spaces (- (treesit-node-start assign-node) 
assign-start-pos)))
+        (setq assign-str (buffer-substring assign-start-pos assign-end-pos)
+              n-levels (if (= (mod indent-spaces matlab-ts-mode--indent-level) 
0)
+                           (/ indent-spaces matlab-ts-mode--indent-level)
+                         ;; else: not at a standard level so no need to add 
conditionals as the
+                         ;; indent level will be corrected later.
+                         0))))
+
+    (cl-loop for level from 1 to n-levels do
+             (insert "if 1\n"))
+
+    (insert assign-str "\n")
+
+    (cl-loop for level from 1 to n-levels do
+             (insert "end\n"))
+
+    (matlab-ts-mode)
+
+    ;; Indent to adjust spacing among operators, but don't do other alignment 
items
+    (let ((matlab-ts-mode--ei-align-enabled nil)
+          ;; t-utils-test-indent captures messages using 
treesit--indent-verbose and we don't
+          ;; want to capture the messages from this temp indent-region.
+          (treesit--indent-verbose nil))
+      (indent-region (point-min) (point-max)))
+
+
+    (goto-char (point-min))
+    (when (> n-levels 0)
+      (forward-line n-levels))))
+
+(cl-defun matlab-ts-mode--ei-align-line-in-m-matrix (assign-node ei-info)
+  "Align current line with EI-INFO in a multi-line matrix of ASSIGN-NODE.
+See `matlab-ts-mode--ei-get-new-line' for EI-INFO contents."
 
-      (list ei-line line-offset line-node-types first-node))))
+  ;; TopTester: 
test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m
+  (when matlab-ts-mode--ei-align-matrix-alist ;; Use cached value?
+    (let ((ei-line (alist-get (line-number-at-pos) 
matlab-ts-mode--ei-align-matrix-alist)))
+      (when ei-line
+        (cl-return-from matlab-ts-mode--ei-align-line-in-m-matrix (cons 
ei-line (cdr ei-info))))))
+
+  (let* ((assign-start-linenum (line-number-at-pos (treesit-node-start 
assign-node)))
+         (tmp-buf-ei-linenum (1+ (- (line-number-at-pos) 
assign-start-linenum)))
+         (tmp-buf-row-linenum (if matlab-ts-mode--ei-align-matrix-alist 1 
tmp-buf-ei-linenum))
+         (matrix-alist matlab-ts-mode--ei-align-matrix-alist))
+    (with-temp-buffer
+      (matlab-ts-mode--ei-indent-matrix-in-tmp-buf assign-node)
+
+      (let* ((matrix-node (treesit-node-parent (treesit-search-subtree
+                                                (treesit-buffer-root-node) (rx 
bos "[" eos) nil t)))
+             (first-col-extra (matlab-ts-mode--ei-m-matrix-first-col-extra 
matrix-node))
+             (column-widths (matlab-ts-mode--ei-m-matrix-col-widths 
matrix-node first-col-extra)))
+
+        ;; Move to the line of interest when we called from 
matlab-ts-mode--treesit-indent,
+        ;; otherwise calculate all matrix rows for indent-region.
+        (when (and (not matrix-alist)
+                   (> tmp-buf-ei-linenum 1))
+          (forward-line (1- tmp-buf-ei-linenum)))
+
+        (while (not (eobp)) ;; Adjust column widths
+          (back-to-indentation)
+          (let* ((row-node (matlab-ts-mode--get-m-matrix-row-in-line))
+                 (indent-start-pt (point))
+                 ;; line content does not have leading indent-level spaces
+                 (content (buffer-substring indent-start-pt 
(line-end-position)))
+                 (ei-line (buffer-substring (line-beginning-position) 
(line-end-position)))
+                 n-spaces)
+            (when row-node
+              (let* ((col-num (length column-widths))
+                     (pt-offset (nth 1 ei-info))
+                     (matrix-offset (save-excursion
+                                      (goto-char (treesit-node-start 
matrix-node))
+                                      (1+ (- (point) 
(line-beginning-position)))))
+                     (indent-offset (or (string-match-p "[^ \t]+" ei-line)
+                                        (error "Assert: no offset"))))
+
+                (when (< matrix-offset indent-offset)
+                  (when pt-offset
+                    (setq pt-offset (- pt-offset (- indent-offset 
matrix-offset))))
+                  (setq indent-offset matrix-offset))
+
+                (dolist (element (reverse (treesit-node-children row-node)))
+                  (when (not (string= (treesit-node-type element) ",")) ;; at 
a column?
+                    (let ((width (or (alist-get col-num column-widths)
+                                     (error "Assert: no col width")))
+                          (curr-width (- (treesit-node-end element) 
(treesit-node-start element))))
+                      (setq n-spaces (- width curr-width))
+                      (when (< curr-width width)
+                        (let ((offset (- (treesit-node-start element) 
indent-start-pt)))
+                          (when (and pt-offset
+                                     (or (= indent-start-pt (point-min))
+                                         (<= (+ offset matrix-offset) 
pt-offset)))
+                            (setq pt-offset (+ pt-offset n-spaces)))
+                          (setq content (concat (substring content 0 offset)
+                                                (make-string n-spaces ? )
+                                                (substring content offset)))))
+                      (setq col-num (1- col-num)))))
+
+                (setq ei-line (concat (substring ei-line 0 indent-offset) 
content))
+                (when (= tmp-buf-row-linenum tmp-buf-ei-linenum)
+                  (setq ei-info (list ei-line pt-offset (nth 2 ei-info) (nth 3 
ei-info))))))
+
+            (when matrix-alist
+              (let* ((buf-linenum (1- (+ assign-start-linenum 
tmp-buf-row-linenum))))
+                (push `(,buf-linenum . ,ei-line) matrix-alist)))
+
+            (if (not matrix-alist)
+                (goto-char (point-max))
+              (forward-line)
+              (setq tmp-buf-row-linenum (1+ tmp-buf-row-linenum)))))))
+
+    (when matrix-alist
+      (setq matlab-ts-mode--ei-align-matrix-alist matrix-alist)))
+  ;; ei-info for current line
+  ei-info)
 
-(defun matlab-ts-mode--ei-is-assign (node)
-  "Is NODE the first node of a single-line assignment?"
-  (let ((parent (treesit-node-parent node)))
+(cl-defun matlab-ts-mode--ei-matrix-ends-on-line (matrix)
+  "Does MATRIX end on a line by itself?"
+  (save-excursion
+    (goto-char (treesit-node-end matrix))
+    (while (re-search-forward "[^ \t]" (line-end-position) t)
+      (backward-char)
+      (let ((node (treesit-node-at (point))))
+        (when (not (string-match-p (rx bos (or "," ";" "comment" 
"line_continuation") eos)
+                                   (treesit-node-type node)))
+          (cl-return-from matlab-ts-mode--ei-matrix-ends-on-line))
+        (goto-char (treesit-node-end node)))))
+  t)
+
+(cl-defun matlab-ts-mode--ei-is-m-matrix (matrix)
+  "Is MATRIX node a multi-line matrix?
+We define a a multi-line matrix has one row per line and more than one
+column."
+  (let ((start-line (line-number-at-pos (treesit-node-start matrix)))
+        (end-line (line-number-at-pos (treesit-node-end matrix)))
+        (n-rows 0)
+        n-cols)
+    (when (and (> end-line start-line) ;; multi-line matrix?
+               (matlab-ts-mode--ei-matrix-ends-on-line matrix)
+               (not (treesit-search-subtree matrix (rx bos "ERROR" eos) nil 
t)))
+      (dolist (child (treesit-node-children matrix))
+        (let ((child-type (treesit-node-type child)))
+          (cond
+           ((string= child-type "row")
+            (let ((row-start-line-num (line-number-at-pos (treesit-node-start 
child))))
+              ;; Return nil if row not on one line
+              (when (not (= row-start-line-num (line-number-at-pos 
(treesit-node-end child))))
+                (cl-return-from matlab-ts-mode--ei-is-m-matrix))
+              ;; Return nil if more than one row one the line
+              (let ((next-node (treesit-node-next-sibling child)))
+                (when (and (string= (treesit-node-type next-node) "row")
+                           (= row-start-line-num (line-number-at-pos
+                                                  (treesit-node-start 
next-node))))
+                  (cl-return-from matlab-ts-mode--ei-is-m-matrix)))
+              ;; Return nil if row contains sub-matrices
+              (when (treesit-search-subtree child (rx bos (or "[" "]") eos) 
nil t)
+                (cl-return-from matlab-ts-mode--ei-is-m-matrix))
+
+              (setq n-rows (1+ n-rows))
+
+              ;; Count the columns
+              (let ((n-cols-in-row 0))
+                (dolist (el (treesit-node-children child))
+                  (when (not (string= (treesit-node-type el) ","))
+                    (setq n-cols-in-row (1+ n-cols-in-row))))
+                (if (not n-cols)
+                    (setq n-cols n-cols-in-row)
+                  (if (not (= n-cols n-cols-in-row))
+                      (cl-return-from matlab-ts-mode--ei-is-m-matrix))))
+              ))
+           ;; Case unexpected matrix child node
+           ((not (string-match-p (rx bos (or "[" "]" "comment" 
"line_continuation") eos)
+                                 child-type))
+            (error "Assert: unexpected matrix child %S" child))))))
+    ;; Matrix with more than one row and more than one column where each row 
is on its own line?
+    (and (> n-rows 1) (>= n-cols 1))))
+
+(defun matlab-ts-mode--ei-is-assign (first-node type)
+  "Is FIRST-NODE of line for an assignment that matches TYPE?
+TYPE can be \\='single-line or \\='multi-line-matrix.  When
+\\='multi-line-matrix, the assignment is to a matrix with two or more
+rows and two or more columns, where each row is on its own line.  The
+assignment node is return or nil."
+  (let ((assign-node (treesit-node-parent first-node)))
+    (while (and assign-node
+                (not (string= (treesit-node-type assign-node) "assignment")))
+      (setq assign-node (treesit-node-parent assign-node)))
+    (when assign-node
+      (save-excursion
+        (beginning-of-line)
+        (when (re-search-forward "=" (line-end-position) t)
+          (backward-char)
+          (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"))
+              (cond
+               ;; Single-line assignment? Example: v1 = [1, 2];
+               ((eq type 'single-line)
+                (when (<= (treesit-node-end assign-node) (line-end-position))
+                  assign-node))
+
+               ;; Multi-line matrix assignment? Example: m1 = [1 2
+               ;;                                              3 4];
+               ((eq type 'multi-line-matrix)
+                (goto-char (treesit-node-end eq-node))
+                (let ((next-node (treesit-node-next-sibling eq-node)))
+                  (while (equal (treesit-node-type next-node) 
"line_continuation")
+                    (setq next-node (treesit-node-next-sibling next-node)))
+                  (when (and (equal (treesit-node-type next-node) "matrix")
+                             (matlab-ts-mode--ei-is-m-matrix next-node))
+                    assign-node)))
+               (t
+                (error "Assert: bad type %S" type))))))))))
+
+(defun matlab-ts-mode--ei-point-in-m-matrix (ei-info)
+  "Are we in a multi-line matrix?
+See `matlab-ts-mode--ei-get-new-line' for EI-INFO contents.  Returns
+mat-info a (list matrix-node n-rows n-cols) if in a multi-line matrix."
+  (let* ((first-node-in-line (nth 3 ei-info))
+         (parent (treesit-node-parent first-node-in-line)))
     (while (and parent
                 (not (string= (treesit-node-type parent) "assignment")))
       (setq parent (treesit-node-parent parent)))
-    (and parent
-         (let ((first-child (treesit-node-child parent 0)))
-           (eq (treesit-node-start node) (treesit-node-start first-child)))
-         (<= (treesit-node-end parent) (line-end-position))
-         (save-excursion
-           (beginning-of-line)
-           (when (re-search-forward "=" (line-end-position) t)
-             (backward-char)
-             (let ((eq-node (treesit-node-at (point))))
-               (and (equal (treesit-node-type eq-node) "=")
-                    (equal (treesit-node-type (treesit-node-parent eq-node)) 
"assignment"))))))))
+    (when parent ;; In an assignment?
+      (save-excursion
+        (goto-char (treesit-node-start parent))
+        (beginning-of-line)
+        (back-to-indentation)
+        (let ((first-node (treesit-node-at (point))))
+          (matlab-ts-mode--ei-is-assign first-node 'multi-line-matrix))))))
 
 (defun matlab-ts-mode--ei-assign-offset (ei-line)
   "Get the assignment offset from the indent-level in EI-LINE."
@@ -3295,13 +3677,14 @@ or nil."
                     first-char-offset)))
     offset))
 
+;; This is used to cache aligned assignments for indent-region
 (defvar-local matlab-ts-mode--ei-align-assign-alist nil)
 
 (defun matlab-ts-mode--ei-align-assignments (ei-info)
   "Update EI-INFO to align assignments.
 See `matlab-ts-mode--ei-get-new-line' for EI-INFO contents."
   (let ((first-node-in-line (nth 3 ei-info)))
-    (when (matlab-ts-mode--ei-is-assign first-node-in-line)
+    (when (matlab-ts-mode--ei-is-assign first-node-in-line 'single-line)
       (let* ((ei-line (nth 0 ei-info))
              (line-assign-offset (matlab-ts-mode--ei-assign-offset ei-line))
              assign-offset
@@ -3327,7 +3710,7 @@ See `matlab-ts-mode--ei-get-new-line' for EI-INFO 
contents."
               (let* ((l-info (matlab-ts-mode--ei-get-new-line))
                      (l-first-node (nth 3 l-info)))
                 (if (and l-first-node
-                         (matlab-ts-mode--ei-is-assign l-first-node))
+                         (matlab-ts-mode--ei-is-assign l-first-node 
'single-line))
                     (let ((l-offset (matlab-ts-mode--ei-assign-offset (nth 0 
l-info))))
                       (push (line-number-at-pos) line-nums)
                       (when (> l-offset assign-offset)
@@ -3340,15 +3723,15 @@ See `matlab-ts-mode--ei-get-new-line' for EI-INFO 
contents."
         (let ((diff (- assign-offset line-assign-offset)))
           (when (> diff 0)
             (let* ((loc (1- (string-match "=" ei-line)))
-                   (new-line-offset (let ((line-offset (nth 1 ei-info)))
-                                      (when line-offset
-                                        (if (<= loc line-offset)
-                                            (+ line-offset diff)
-                                          line-offset)))))
+                   (new-pt-offset (let ((pt-offset (nth 1 ei-info)))
+                                      (when pt-offset
+                                        (if (<= loc pt-offset)
+                                            (+ pt-offset diff)
+                                          pt-offset)))))
               (setq ei-line (concat (substring ei-line 0 loc)
                                     (make-string diff ? )
                                     (substring ei-line loc)))
-              (setq ei-info (list ei-line new-line-offset (nth 2 ei-info) (nth 
3 ei-info)))))))))
+              (setq ei-info (list ei-line new-pt-offset (nth 2 ei-info) (nth 3 
ei-info)))))))))
   ei-info)
 
 (defun matlab-ts-mode--ei-trailing-comment-offset (ei-info)
@@ -3372,6 +3755,7 @@ is identified as a trailing comment."
                 ;; have trailing comment at offset from first char in new-line
                 offset))))))))
 
+;; This is used to cache comment alignments for indent-region
 (defvar-local matlab-ts-mode--ei-align-comment-alist nil)
 
 (defun matlab-ts-mode--ei-align-trailing-comments (ei-info)
@@ -3416,22 +3800,26 @@ See `matlab-ts-mode--ei-get-new-line' for EI-INFO 
contents."
           (when (> diff 0)
             (let* ((ei-line (nth 0 ei-info))
                    (loc (1- (string-match "%" ei-line)))
-                   (new-line-offset (let ((line-offset (nth 1 ei-info)))
-                                      (when line-offset
-                                        (if (<= loc line-offset)
-                                            (+ line-offset diff)
-                                          line-offset)))))
+                   (new-pt-offset (let ((pt-offset (nth 1 ei-info)))
+                                      (when pt-offset
+                                        (if (<= loc pt-offset)
+                                            (+ pt-offset diff)
+                                          pt-offset)))))
               (setq ei-line (concat (substring ei-line 0 loc)
                                     (make-string diff ? )
                                     (substring ei-line loc)))
-              (setq ei-info (list ei-line new-line-offset (nth 2 ei-info) (nth 
3 ei-info))))))))
+              (setq ei-info (list ei-line new-pt-offset (nth 2 ei-info) (nth 3 
ei-info))))))))
     ei-info))
 
 (defun matlab-ts-mode--ei-align (ei-info)
   "Align elements in EI-INFO.
 See `matlab-ts-mode--ei-get-new-line' for EI-INFO contents."
-  (setq ei-info (matlab-ts-mode--ei-align-assignments ei-info))
-  (setq ei-info (matlab-ts-mode--ei-align-trailing-comments ei-info))
+  (let ((matrix-node (matlab-ts-mode--ei-point-in-m-matrix ei-info)))
+    (if matrix-node
+        (setq ei-info (matlab-ts-mode--ei-align-line-in-m-matrix matrix-node 
ei-info)))
+    ;; else do single-line alignments
+    (setq ei-info (matlab-ts-mode--ei-align-assignments ei-info))
+    (setq ei-info (matlab-ts-mode--ei-align-trailing-comments ei-info)))
   ei-info)
 
 (cl-defun matlab-ts-mode--indent-elements-in-line (&optional start-node 
start-offset)
@@ -3442,9 +3830,10 @@ line is updated.  Returns t if line was updated."
   ;; If line was indented (ei-line is not same as current line), then update 
the buffer
   (let ((ei-info (matlab-ts-mode--ei-get-new-line start-node start-offset)))
     (when ei-info
-      (setq ei-info (matlab-ts-mode--ei-align ei-info))
+      (when matlab-ts-mode--ei-align-enabled
+        (setq ei-info (matlab-ts-mode--ei-align ei-info)))
       (let* ((ei-line (nth 0 ei-info))
-             (line-offset (nth 1 ei-info))
+             (pt-offset (nth 1 ei-info))
              (line-node-types (nth 2 ei-info))
              (curr-line (buffer-substring (line-beginning-position) 
(line-end-position)))
              (updated (not (string= curr-line ei-line))))
@@ -3454,8 +3843,8 @@ line is updated.  Returns t if line was updated."
           (insert ei-line)
           (when matlab-ts-mode--electric-indent-assert
             (matlab-ts-mode--electric-indent-assert-match line-node-types))
-          (when line-offset
-            (goto-char (+ (line-beginning-position) line-offset))))
+          (when pt-offset
+            (goto-char (+ (line-beginning-position) pt-offset))))
         ;; result
         updated))))
 
@@ -3473,20 +3862,21 @@ line is updated.  Returns t if line was updated."
           ;; Consider:  A = [B   C];
           ;;                   ^
           ;; node is the invisible "," and moving to start gives us node for C
-          (when (< (point) (treesit-node-start node))
-            (if (re-search-forward "[^ \t]" (line-end-position) t)
-                (progn
-                  (backward-char)
-                  (setq node (treesit-node-at (point))))
-              (if (re-search-backward "[^ \t]" (line-beginning-position) t)
-                  (setq node (treesit-node-at (point)))
-                (setq node nil))))
-
-          (when (and node
-                     (>= (point) (treesit-node-start node))
-                     (<= (point) (treesit-node-end node)))
-            (setq start-node node)
-            (setq start-offset (- (point) (treesit-node-start node))))))
+          (save-excursion
+            (when (< (point) (treesit-node-start node))
+              (if (re-search-forward "[^ \t]" (line-end-position) t)
+                  (progn
+                    (backward-char)
+                    (setq node (treesit-node-at (point))))
+                (if (re-search-backward "[^ \t]" (line-beginning-position) t)
+                    (setq node (treesit-node-at (point)))
+                  (setq node nil))))
+
+            (when (and node
+                       (>= (point) (treesit-node-start node))
+                       (<= (point) (treesit-node-end node)))
+              (setq start-node node)
+              (setq start-offset (- (point) (treesit-node-start node)))))))
 
       (if (matlab-ts-mode--indent-elements-in-line start-node start-offset)
           (when at-eol
@@ -3498,34 +3888,55 @@ line is updated.  Returns t if line was updated."
   "Call `treesit-indent-region' on BEG END, then do electric indent."
   ;; `treesit-indent-region' will not alter the number of lines, but it may 
reduce the buffer size,
   ;; thus grab the start/end lines for 
`matlab-ts-mode--indent-elements-in-line'.
-  (let* ((start-line (line-number-at-pos beg))
-         (curr-line start-line)
-         (end-line (line-number-at-pos end)))
-
-    (when matlab-ts-mode-electric-indent
-      (unwind-protect
+  (if matlab-ts-mode-electric-indent
+      (let* ((start-linenum (line-number-at-pos beg))
+             (curr-linenum start-linenum)
+             (end-linenum (save-excursion
+                            (goto-char end)
+                            (let ((linenum (line-number-at-pos)))
+                              (when (= (point) (line-beginning-position))
+                                (setq linenum (1- linenum)))
+                              linenum))))
+
+        ;; We need to run electric indent before treesit-indent-region. 
Consider
+        ;;    l2 = @(x)((ischar(x) || isstring(x) || isnumeric(x)) && ...
+        ;;                 ~strcmpi(x, 'fubar'));
+        ;; If we indent-region first, we'll get
+        ;;    l2 = @(x)((ischar(x) || isstring(x) || isnumeric(x)) && ...
+        ;;              ~strcmpi(x, 'fubar'));
+        ;; then when we adjust spacing, we'll have the following where the 2nd 
line is not
+        ;; indented correctly.
+        ;;    l2 = @(x) ((ischar(x) || isstring(x) || isnumeric(x)) && ...
+        ;;              ~strcmpi(x, 'fubar'));
+        (unwind-protect
+            (progn
+              ;; Add invalid entry to matlab-ts-mode--ei-align-assign-alist as 
a marker to activate
+              ;; caching of computed offsets for assignment alignment.
+              (setq-local matlab-ts-mode--ei-align-assign-alist '((-1 . 0))
+                          matlab-ts-mode--ei-align-comment-alist '((-1 . 0))
+                          matlab-ts-mode--ei-align-matrix-alist '((-1 . "")))
+              (save-excursion
+                (goto-char beg)
+                (while (<= curr-linenum end-linenum)
+                  (beginning-of-line)
+                  (matlab-ts-mode--indent-elements-in-line)
+                  (forward-line)
+                  (setq curr-linenum (1+ curr-linenum)))
+                ;; Restore point accounting for whitespace adjustments in the 
lines
+                (goto-char (point-min))
+                (forward-line (1- start-linenum))
+                (setq beg (point))
+                (goto-char (point-min))
+                (forward-line end-linenum)
+                (setq end (point)))
+
+              (treesit-indent-region beg end))
           (progn
-            ;; Add invalid entry to matlab-ts-mode--ei-align-assign-alist as a 
marker to activate
-            ;; caching of computed offsets for assignment alignment.
-            (setq-local matlab-ts-mode--ei-align-assign-alist '((-1 . 0))
-                        matlab-ts-mode--ei-align-comment-alist '((-1 . 0)))
-            (save-excursion
-              (goto-char beg)
-              (while (<= curr-line end-line)
-                (beginning-of-line)
-                (matlab-ts-mode--indent-elements-in-line)
-                (forward-line)
-                (setq curr-line (1+ curr-line)))
-              (goto-char (point-min))
-              (forward-line (1- start-line))
-              (setq beg (point))
-              (goto-char (point-min))
-              (forward-line end-line)
-              (setq end (point))))
-
-        (setq-local matlab-ts-mode--ei-align-assign-alist nil
-                    matlab-ts-mode--ei-align-comment-alist nil)))
+            (setq-local matlab-ts-mode--ei-align-assign-alist nil
+                        matlab-ts-mode--ei-align-comment-alist nil
+                        matlab-ts-mode--ei-align-matrix-alist nil))))
 
+    ;; else indent without electric additions
     (treesit-indent-region beg end)))
 
 ;;; Thing settings for movement, etc.
@@ -4009,7 +4420,7 @@ If optional NO-PROMPT is t, fix the name if needed 
without prompting."
                   ;; Case: function_definition or class_definition
                   ((string-match-p (rx bos (or "function_definition" 
"class_definition") eos)
                                    child-type)
-                   (when (treesit-search-subtree child (rx bos "ERROR" eos))
+                   (when (treesit-search-subtree child (rx bos "ERROR" eos) 
nil t)
                      ;; Don't try to fix code if there's an error, e.g. don't 
change in1:
                      ;;   function ...
                      ;;       [    ...
@@ -5280,4 +5691,4 @@ matlab-language-server-lsp-mode.org\n"
 ;; LocalWords:  Keymap keymap netshell gud ebstop mlgud ebclear ebstatus mlg 
mlgud's subjob reindent
 ;; LocalWords:  DWIM dwim parens caar cdar utils fooenum mcode CRLF cmddual 
lang nconc listify kbd
 ;; LocalWords:  matlabls vscode buf dolist sp ppss bobp sexps pragmas curr ei 
isstring eol listp
-;; LocalWords:  nums
+;; LocalWords:  nums setf tmp fubar linenum
diff --git 
a/tests/test-matlab-ts-mode-electric-ends-files/electric_ends_cases_expected.org
 
b/tests/test-matlab-ts-mode-electric-ends-files/electric_ends_cases_expected.org
index 2380a8116a..a4e508493c 100644
--- 
a/tests/test-matlab-ts-mode-electric-ends-files/electric_ends_cases_expected.org
+++ 
b/tests/test-matlab-ts-mode-electric-ends-files/electric_ends_cases_expected.org
@@ -662,9 +662,9 @@
 
 - Invoking      : "C-m" = newline
   Start point   : 1196
-  Moved to point: 1215
-  : 44:18:               case
-  :                          ^
+  Moved to point: 1211
+  : 44:14:               case 
+  :                      ^
   Buffer modified:
   #+begin_src diff
 --- start_contents
@@ -673,7 +673,7 @@
  
              % Case8: (t-utils-xr "C-n" "C-i" (insert "switch a") "C-m" "C-e" 
(insert "1") "C-m" (insert "disp('case 1')"))
              switch a
-+              case
++              case 
 +            end
 +
              % Case9: (t-utils-xr "C-n" "C-i" (insert "while true") "C-m" 
(insert "break"))
@@ -682,15 +682,17 @@
   #+end_src diff
 
 - Invoking      : "C-e" = move-end-of-line
-  Start point   : 1215
-  No point movement
+  Start point   : 1211
+  Moved to point: 1216
+  : 44:19:               case 
+  :                           ^
   No buffer modifications
 
 - Invoking      : (insert "1")
-  Start point   : 1215
-  Moved to point: 1216
-  : 44:19:               case1
-  :                           ^
+  Start point   : 1216
+  Moved to point: 1217
+  : 44:20:               case 1
+  :                            ^
   Buffer modified:
   #+begin_src diff
 --- start_contents
@@ -699,15 +701,15 @@
  
              % Case8: (t-utils-xr "C-n" "C-i" (insert "switch a") "C-m" "C-e" 
(insert "1") "C-m" (insert "disp('case 1')"))
              switch a
--              case
-+              case1
+-              case 
++              case 1
              end
  
              % Case9: (t-utils-xr "C-n" "C-i" (insert "while true") "C-m" 
(insert "break"))
   #+end_src diff
 
 - Invoking      : "C-m" = newline
-  Start point   : 1216
+  Start point   : 1217
   Moved to point: 1234
   : 45:16:                 
   :                        ^
@@ -715,12 +717,10 @@
   #+begin_src diff
 --- start_contents
 +++ end_contents
-@@ -41,7 +41,8 @@
- 
+@@ -42,6 +42,7 @@
              % Case8: (t-utils-xr "C-n" "C-i" (insert "switch a") "C-m" "C-e" 
(insert "1") "C-m" (insert "disp('case 1')"))
              switch a
--              case1
-+              case 1
+               case 1
 +                
              end
  
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic.m
new file mode 100644
index 0000000000..d54bd88647
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic.m
@@ -0,0 +1,18 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+m6 = [1 2
+      3 4];
+
+m7 = [   1111   22
+      1111111, 2222];
+
+m8 = [
+          1111; ...
+       1111111
+     ];
+
+m = [   2, 4000
+     3000,    1]
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected.m
new file mode 100644
index 0000000000..d54bd88647
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected.m
@@ -0,0 +1,18 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+m6 = [1 2
+      3 4];
+
+m7 = [   1111   22
+      1111111, 2222];
+
+m8 = [
+          1111; ...
+       1111111
+     ];
+
+m = [   2, 4000
+     3000,    1]
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected_msgs.m
new file mode 100644
index 0000000000..095ec72a3e
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected_msgs.m
@@ -0,0 +1,18 @@
+% -*- 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 - when typing matrix 
line-by-line, there are %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+% error nodes and thus the matrix alignment doesn't occur %  <{Matched rule: 
(matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
+
+m6 = [1 2 %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+      3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m7 = [   1111   22 %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+      1111111, 2222]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m8 = [ %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+          1111; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       1111111 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+     ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+m = [   2, 4000 %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+     3000,    1] %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m
new file mode 100644
index 0000000000..7611896660
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m
@@ -0,0 +1,97 @@
+% -*- matlab-ts -*-
+
+% Exercise matlab-ts-mode--ei-align-line-in-m-matrix
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+m = [1111,     2, 223
+      ...
+        4, 53333,   6];
+
+                           
+m1 = [           ...
+         1 2222; ...
+                 ...
+       333    4; ...
+     ];
+
+m1b = [
+          1 2222 % comment 1
+        333    4 % comment 2
+      ];
+
+m2 = [ ...
+         1 2222
+       333    4
+     ];
+
+m3a = [  21,     2, 223;
+          4, 53333,   6; ...
+       1222,     4,   5];
+
+m3b = [   1111,        2 ,223;
+             4, 53333,   6; ...
+         1222,     4,   5;];
+
+m3c = [1111,   2, 223;
+       4, 533,   6; ...
+       1222,   4,   5;];
+
+a = 1;
+b = 2;
+c = 3;
+d = 4;
+
+m4a = [a * b + c, d + a;
+               b,     c];
+
+m4b = [a * b + c, d + a;
+               b,     c];
+
+m5a = [a * b + c d + a; ...
+        ...
+        ...
+               b     c];
+
+m6 = [ ...
+       ...
+        1 2222
+       ...
+       ... comment
+      333    4
+     ];
+
+m7 = [   1111; ...
+           22; ...
+          333; ...
+      1111111];
+
+
+
+not1 = [1111, ...
+        22; ...
+        333, ...
+        1111111];
+
+not2 = [1     2 223;
+          [4 53333   6]; ...
+        1222     4   5; ...
+     ];
+
+not3 = [ ...
+       1 2 3; 4 5 6; ...
+       1 4 5; ...
+     ];
+
+not4 = [a * b + c, [d + a];
+        b, c];
+
+not5 = [ ...
+       1 2222; ...
+       333 4; ...
+       ]; if 1, disp('here')
+          end
+
+not6 = [1 2;
+        3 4 5];
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected.m
new file mode 100644
index 0000000000..04b554e148
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected.m
@@ -0,0 +1,97 @@
+% -*- matlab-ts -*-
+
+% Exercise matlab-ts-mode--ei-align-line-in-m-matrix
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+m = [1111,     2, 223
+      ...
+        4, 53333,   6];
+
+
+m1 = [ ...
+         1 2222; ...
+       ...
+       333    4; ...
+     ];
+
+m1b = [
+          1 2222 % comment 1
+        333    4 % comment 2
+      ];
+
+m2 = [ ...
+         1 2222
+       333    4
+     ];
+
+m3a = [  21,     2, 223;
+          4, 53333,   6; ...
+       1222,     4,   5];
+
+m3b = [1111,     2, 223;
+          4, 53333,   6; ...
+       1222,     4,   5;];
+
+m3c = [1111,   2, 223;
+          4, 533,   6; ...
+       1222,   4,   5;];
+
+a = 1;
+b = 2;
+c = 3;
+d = 4;
+
+m4a = [a * b + c, d + a;
+               b,     c];
+
+m4b = [a * b + c, d + a;
+               b,     c];
+
+m5a = [a * b + c d + a; ...
+        ...
+        ...
+               b     c];
+
+m6 = [ ...
+       ...
+         1 2222
+       ...
+       ... comment
+       333    4
+     ];
+
+m7 = [   1111; ...
+           22; ...
+          333; ...
+      1111111];
+
+
+
+not1 = [1111, ...
+        22; ...
+        333, ...
+        1111111];
+
+not2 = [1 2 223;
+        [4 53333 6]; ...
+        1222 4 5; ...
+       ];
+
+not3 = [ ...
+         1 2 3; 4 5 6; ...
+         1 4 5; ...
+       ];
+
+not4 = [a * b + c, [d + a];
+        b, c];
+
+not5 = [ ...
+         1 2222; ...
+         333 4; ...
+       ]; if 1, disp('here')
+          end
+
+not6 = [1 2;
+        3 4 5];
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected_msgs.m
new file mode 100644
index 0000000000..a48af673a8
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected_msgs.m
@@ -0,0 +1,97 @@
+% -*- matlab-ts -*- %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+% Exercise matlab-ts-mode--ei-align-line-in-m-matrix %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+% error nodes and thus the matrix alignment doesn't occur %  <{Matched rule: 
(matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
+
+m = [1111,     2, 223 %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+      ... %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") parent 
2)}>
+        4, 53333,   6]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+
+m1 = [ ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+         1 2222; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       ... %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+       333    4; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+     ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+m1b = [ %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+          1 2222 % comment 1 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+        333    4 % comment 2 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+      ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+m2 = [ ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+         1 2222 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       333    4 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+     ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+m3a = [  21,     2, 223; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+          4, 53333,   6; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       1222,     4,   5]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m3b = [1111,     2, 223; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+          4, 53333,   6; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       1222,     4,   5;]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m3c = [1111,   2, 223; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+          4, 533,   6; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       1222,   4,   5;]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+a = 1; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+b = 2; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+c = 3; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+d = 4; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+m4a = [a * b + c, d + a; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+               b,     c]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m4b = [a * b + c, d + a; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+               b,     c]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m5a = [a * b + c d + a; ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+        ... %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+        ... %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+               b     c]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+m6 = [ ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+       ... %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+         1 2222 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       ... %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+       ... comment %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
+       333    4 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+     ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+m7 = [   1111; ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+           22; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+          333; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+      1111111]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+
+
+not1 = [1111, ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+        22; ... %  <{Matched rule: ((parent-is 
"\\`\\(?:function_output\\|row\\)\\'") parent 0)}>
+        333, ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+        1111111]; %  <{Matched rule: ((parent-is 
"\\`\\(?:function_output\\|row\\)\\'") parent 0)}>
+
+not2 = [1 2 223; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+        [4 53333 6]; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+        1222 4 5; ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+not3 = [ ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+         1 2 3; 4 5 6; ... %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
+         1 4 5; ... %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
+       ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
+
+not4 = [a * b + c, [d + a]; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+        b, c]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+
+not5 = [ ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+         1 2222; ... %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
+         333 4; ... %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
+       ]; if 1, disp('here') %  <{Matched rule: ((node-is "\\`[])}]\\'") 
parent 0)}>
+          end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
+
+not6 = [1 2; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+        3 4 5]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error.m
new file mode 100644
index 0000000000..b06862d0a9
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error.m
@@ -0,0 +1,4 @@
+% -*- matlab-ts -*-
+
+not6 = [1 2;
+        a *** b, d];
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected.m
new file mode 100644
index 0000000000..b06862d0a9
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected.m
@@ -0,0 +1,4 @@
+% -*- matlab-ts -*-
+
+not6 = [1 2;
+        a *** b, d];
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected_msgs.m
new file mode 100644
index 0000000000..0d5ebc651c
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected_msgs.m
@@ -0,0 +1,4 @@
+% -*- matlab-ts -*- %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+not6 = [1 2; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+        a *** b, d]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_expected_msgs.m
index 6c3daf82ac..7747198507 100644
--- 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_expected_msgs.m
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_expected_msgs.m
@@ -23,20 +23,20 @@ f = [e + e -e +e] %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode
 g = -e %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
 
 h = [1 2; ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
-     3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+     3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
 
 c2 = {['freq' '%'] num2str(2)}; %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
 
 c3 = {a b [c '%'] f(1)}; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
 
 x = [ %  <{Matched rule: (matlab-ts-mode--i-top-level matlab-ts-mode--column-0 
0)}>
-      1, 2; %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
-      3, 4; %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+      1, 2; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+      3, 4; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
     ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
 x2 = [ %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
-       1, 2; %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
-       3, 4; %  <{Matched rule: ((parent-is "\\`\\(?:cell\\|matrix\\)\\'") 
parent 2)}>
+       1, 2; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+       3, 4; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
      ]; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
 
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels.m
new file mode 100644
index 0000000000..5db17b9b5d
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels.m
@@ -0,0 +1,20 @@
+% -*- matlab-ts -*-
+
+% Exercise matrix indentation with varying indent levels
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+if foo1
+    m1 = [23, 2
+             233,   4];
+end
+
+
+if foo1
+    if foo2
+        m2 = [ 23,    2
+                 233,    4
+          1   , 3344   ];
+    end
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected.m
new file mode 100644
index 0000000000..8624b8f8f0
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected.m
@@ -0,0 +1,20 @@
+% -*- matlab-ts -*-
+
+% Exercise matrix indentation with varying indent levels
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+if foo1
+    m1 = [ 23, 2
+          233, 4];
+end
+
+
+if foo1
+    if foo2
+        m2 = [ 23,    2
+              233,    4
+                1, 3344];
+    end
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected_msgs.m
new file mode 100644
index 0000000000..c8cb6d362f
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected_msgs.m
@@ -0,0 +1,20 @@
+% -*- matlab-ts -*- %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+
+% Exercise matrix indentation with varying indent levels %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts-mode--column-0 0)}>
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+% error nodes and thus the matrix alignment doesn't occur %  <{Matched rule: 
(matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
+
+if foo1 %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+    m1 = [ 23, 2 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+          233, 4]; %  <{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)}>
+
+
+if foo1 %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
+    if foo2 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+        m2 = [ 23,    2 %  <{Matched rule: ((node-is 
"\\`\\(?:arguments_statement\\|block\\|e\\(?:num\\(?:eration\\)?\\|vents\\)\\|function_definition\\|methods\\|propert\\(?:ies\\|y\\)\\)\\'")
 parent 4)}>
+              233,    4 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+                1, 3344]; %  <{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_with_syntax_errors_expected_msgs.m
 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_with_syntax_errors_expected_msgs.m
index 64b209aba3..14568c67f7 100644
--- 
a/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_with_syntax_errors_expected_msgs.m
+++ 
b/tests/test-matlab-ts-mode-electric-indent-files/electric_indent_with_syntax_errors_expected_msgs.m
@@ -5,14 +5,14 @@ a0 = [1; 1]; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--col
 a1 = [[1]; [7]]; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
 
 h = [1 2; ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
-     3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+     3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
 
 m1 = [a */* b]; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
 
 h2 = 2 * [1 2; ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
-          3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+          3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
 
 m2 = 2 * [a */* b]; %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
 
 h3 = 3 * [1 2; ... %  <{Matched rule: (matlab-ts-mode--i-top-level 
matlab-ts-mode--column-0 0)}>
-          3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+          3 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch.m
 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch.m
new file mode 100644
index 0000000000..3c009d9312
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch.m
@@ -0,0 +1,19 @@
+% -*- matlab-ts -*-
+
+%{
+  Follow validates we do not trim trailing whitespace when electric indent is 
active and point is
+  within an ERROR node.
+
+  case1:
+  (t-utils-xr
+
+  (goto-char (point-max))
+
+  (insert "switch a")   "C-m"
+  "C-e"
+  (insert "1")          "C-m"
+  (insert "disp('1')")
+
+  )
+  
+%}
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch_expected.org
 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch_expected.org
new file mode 100644
index 0000000000..fba32ed8bf
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch_expected.org
@@ -0,0 +1,118 @@
+#+startup: showall
+
+* Executing commands from electric_indent_xr_switch.m:8:2:
+
+  case1: (t-utils-xr
+
+  (goto-char (point-max))
+
+  (insert "switch a")   "C-m"
+  "C-e"
+  (insert "1")          "C-m"
+  (insert "disp('1')")
+
+  )
+
+- Invoking      : (goto-char (point-max))
+  Start point   :  294
+  Moved to point:  301
+  : 20:0: 
+  :       ^
+  No buffer modifications
+
+- Invoking      : (insert "switch a")
+  Start point   :  301
+  Moved to point:  309
+  : 20:8: switch a
+  :               ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -17,3 +17,4 @@
+   )
+   
+ %}
++switch a
+\ No newline at end of file
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  309
+  Moved to point:  312
+  : 21:2:   case 
+  :         ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -17,4 +17,6 @@
+   )
+   
+ %}
+-switch a
+\ No newline at end of file
++switch a
++  case 
++end
+  #+end_src diff
+
+- Invoking      : "C-e" = move-end-of-line
+  Start point   :  312
+  Moved to point:  317
+  : 21:7:   case 
+  :              ^
+  No buffer modifications
+
+- Invoking      : (insert "1")
+  Start point   :  317
+  Moved to point:  318
+  : 21:8:   case 1
+  :               ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -18,5 +18,5 @@
+   
+ %}
+ switch a
+-  case 
++  case 1
+ end
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  318
+  Moved to point:  323
+  : 22:4:     
+  :           ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -19,4 +19,5 @@
+ %}
+ switch a
+   case 1
++    
+ end
+  #+end_src diff
+
+- Invoking      : (insert "disp('1')")
+  Start point   :  323
+  Moved to point:  332
+  : 22:13:     disp('1')
+  :                     ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -19,5 +19,5 @@
+ %}
+ switch a
+   case 1
+-    
++    disp('1')
+ end
+  #+end_src diff
diff --git a/tests/test-matlab-ts-mode-electric-indent-xr.el 
b/tests/test-matlab-ts-mode-electric-indent-xr.el
index 9a2121b846..17e0ca194b 100644
--- a/tests/test-matlab-ts-mode-electric-indent-xr.el
+++ b/tests/test-matlab-ts-mode-electric-indent-xr.el
@@ -49,7 +49,7 @@ and run this function.  The baseline is saved for you as
 after validating it, rename it to
   ./test-matlab-ts-mode-electric-indent-xr-files/NAME_expected.org"
 
-  (let* ((matlab-ts-mode-electric-ends nil)
+  (let* ((matlab-ts-mode-electric-ends t)
          (test-name "test-matlab-ts-mode-electric-indent-xr")
          (m-files (t-utils-get-files test-name
                                      :base-regexp (rx ".m" eos)
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_cell_expected_msgs.m 
b/tests/test-matlab-ts-mode-indent-files/indent_cell_expected_msgs.m
index dc6ba3850a..cf461625e9 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_cell_expected_msgs.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_cell_expected_msgs.m
@@ -13,8 +13,8 @@ function [a1, a2, a3, a4, long_variable_a] = indent_cell %  
<{Matched rule: (mat
 
     a3 = { ... %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
            2,  { 3 %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
-                 4 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-                 5 + ( ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+                 4 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+                 5 + ( ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
                       2 + 0 ... %  <{Matched rule: ((parent-is 
"\\`parenthesis\\'") parent 1)}>
                      ) %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
                } %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
@@ -32,7 +32,7 @@ function [a1, a2, a3, a4, long_variable_a] = indent_cell %  
<{Matched rule: (mat
         }; %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
     c1 = {char([0xD800 0xDC32 0xD800 0xDC00 0xD800 0xDC1C]); %  <{Matched 
rule: ((parent-is "\\`block\\'") parent 0)}>
-          char([0xD800 0xDF3E 0xD800 0xDF48]); %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-          char([0xD802 0xDC11 0xD802 0xDC2C]); %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-          char([0xD83D 0xDE08 0xD83D 0xDE31 0xD83D 0xDE49])}; %  <{Matched 
rule: (matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+          char([0xD800 0xDF3E 0xD800 0xDF48]); %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+          char([0xD802 0xDC11 0xD802 0xDC2C]); %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+          char([0xD83D 0xDE08 0xD83D 0xDE31 0xD83D 0xDE49])}; %  <{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)}>
diff --git 
a/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected_msgs.m 
b/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected_msgs.m
index e41000e576..53aef85656 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected_msgs.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_matrix_expected_msgs.m
@@ -13,13 +13,13 @@ function indent_matrix %  <{Matched rule: 
(matlab-ts-mode--i-top-level matlab-ts
     disp(a); %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
 
     a = [1, 2; %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
-         3, 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+         3, 4]; %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
     disp(a); %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
 
     a = [ ... %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
           2 + [ 3 %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
-                4, %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-                5 + [ ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+                4, %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+                5 + [ ... %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
                       2 %  <{Matched rule: ((parent-is 
"\\`\\(?:cell\\|matrix\\)\\'") parent 2)}>
                     ] %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
               ] %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
diff --git 
a/tests/test-matlab-ts-mode-indent-files/indent_old_indents_expected_msgs.m 
b/tests/test-matlab-ts-mode-indent-files/indent_old_indents_expected_msgs.m
index 5be0c7c3da..f67a9f5409 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_old_indents_expected_msgs.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_old_indents_expected_msgs.m
@@ -133,7 +133,7 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
     A = [ 1 2 3 ]; %!!4 %  <{Matched rule: ((parent-is 
"\\`function_definition\\'") parent 
matlab-ts-mode--set-function-indent-level-for-gp)}>
 
     Blong = [ 1 2; %!!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 
0)}>
-              3 4; %!!14 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+              3 4; %!!14 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
             ]; %!!12 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
     Csep = [ %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
@@ -142,14 +142,14 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
            ]; %!!11 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
     multinest = { [ 1 2               %!!4 %  <{Matched rule: ((parent-is 
"\\`block\\'") parent 0)}>
-                    3 4 ];            %!!20 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-                  { 5 6 7 ...         %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                    3 4 ];            %!!20 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+                  { 5 6 7 ...         %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                     8 9 10 ...        %!!20 %  <{Matched rule: ((parent-is 
"\\`\\(?:function_output\\|row\\)\\'") parent 0)}>
                   };                  %!!18 %  <{Matched rule: ((node-is 
"\\`[])}]\\'") parent 0)}>
-                  fcncall(10, ...     %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                  fcncall(10, ...     %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                           12, ...     %!!26 %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
                           [ 13 14;    %!!26 %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
-                            15 16 ])  %!!28 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                            15 16 ])  %!!28 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                 } ;  %!!16 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 
0)}>
 
     nest = { ... %!!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
@@ -170,9 +170,9 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
     % I don't know why the below indents this way. %  <{Matched rule: 
(matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
     % It should either do all max indent, or all lined up with parens. %  
<{Matched rule: (matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
     thing.thing.long.long.longname({ 'str' %!!4 %  <{Matched rule: ((parent-is 
"\\`block\\'") parent 0)}>
-                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                                    });   %!!35 %  <{Matched rule: ((node-is 
"\\`[])}]\\'") parent 0)}>
 
     thing.thing.long.long.longname('str', ... %!!4 %  <{Matched rule: 
((parent-is "\\`block\\'") parent 0)}>
@@ -209,13 +209,13 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
 
     % !!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
     out = { A     %!!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 
0)}>
-            Blong %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            Csep  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            nest  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            multinest%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            cascade_long_name%!!12 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-            Closures%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            dep %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+            Blong %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            Csep  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            nest  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            multinest%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            cascade_long_name%!!12 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+            Closures%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            dep %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
           };      %!!10 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
 end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
@@ -302,7 +302,7 @@ function B = continuations_and_block_comments %  <{Matched 
rule: (matlab-ts-mode
                  arg2); %!!17 %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
 
     A = [ 1 2  % !!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
-          3 4 ]; % !!10 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+          3 4 ]; % !!10 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
 
     foo(['this is a very long string', ... %!!4 %  <{Matched rule: ((parent-is 
"\\`block\\'") parent 0)}>
          'with a continution to do something very exciting']);%!!9 %  
<{Matched rule: ((parent-is "\\`\\(?:function_output\\|row\\)\\'") parent 0)}>
diff --git 
a/tests/test-matlab-ts-mode-indent-files/indent_old_indents_for_typing_expected_msgs.m
 
b/tests/test-matlab-ts-mode-indent-files/indent_old_indents_for_typing_expected_msgs.m
index 969ae23d85..343f00f6c6 100644
--- 
a/tests/test-matlab-ts-mode-indent-files/indent_old_indents_for_typing_expected_msgs.m
+++ 
b/tests/test-matlab-ts-mode-indent-files/indent_old_indents_for_typing_expected_msgs.m
@@ -135,7 +135,7 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
     A = [ 1 2 3 ]; %!!4 %  <{Matched rule: ((parent-is 
"\\`function_definition\\'") parent 
matlab-ts-mode--set-function-indent-level-for-gp)}>
 
     Blong = [ 1 2; %!!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 
0)}>
-              3 4; %!!14 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+              3 4; %!!14 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
             ]; %!!12 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
     Csep = [ %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
@@ -144,14 +144,14 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
            ]; %!!11 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
     multinest = { [ 1 2               %!!4 %  <{Matched rule: ((parent-is 
"\\`block\\'") parent 0)}>
-                    3 4 ];            %!!20 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-                  { 5 6 7 ...         %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                    3 4 ];            %!!20 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+                  { 5 6 7 ...         %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                     8 9 10 ...        %!!20 %  <{Matched rule: ((parent-is 
"\\`\\(?:function_output\\|row\\)\\'") parent 0)}>
                   };                  %!!18 %  <{Matched rule: ((node-is 
"\\`[])}]\\'") parent 0)}>
-                  fcncall(10, ...     %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                  fcncall(10, ...     %!!18 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                           12, ...     %!!26 %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
                           [ 13 14;    %!!26 %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
-                            15 16 ])  %!!28 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                            15 16 ])  %!!28 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                 } ;  %!!16 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 
0)}>
 
     nest = { ... %!!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
@@ -172,9 +172,9 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
     % I don't know why the below indents this way. %  <{Matched rule: 
(matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
     % It should either do all max indent, or all lined up with parens. %  
<{Matched rule: (matlab-ts-mode--i-block-comment-end-matcher parent 0)}>
     thing.thing.long.long.longname({ 'str' %!!4 %  <{Matched rule: ((parent-is 
"\\`block\\'") parent 0)}>
-                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
+                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+                                     'str' %!!37 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
                                    });   %!!35 %  <{Matched rule: ((node-is 
"\\`[])}]\\'") parent 0)}>
 
     thing.thing.long.long.longname('str', ... %!!4 %  <{Matched rule: 
((parent-is "\\`block\\'") parent 0)}>
@@ -211,13 +211,13 @@ function out = array_constant_decls() %  <{Matched rule: 
(matlab-ts-mode--i-top-
 
     % !!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
     out = { A     %!!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 
0)}>
-            Blong %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            Csep  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            nest  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            multinest%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            cascade_long_name%!!12 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-anchor 0)}>
-            Closures%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
-            dep %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+            Blong %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            Csep  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            nest  %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            multinest%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            cascade_long_name%!!12 %  <{Matched rule: 
(matlab-ts-mode--i-row-matcher matlab-ts-mode--i-row-matcher-anchor 
matlab-ts-mode--i-row-matcher-offset)}>
+            Closures%!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
+            dep %!!12 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
           };      %!!10 %  <{Matched rule: ((node-is "\\`[])}]\\'") parent 0)}>
 
 end %  <{Matched rule: ((node-is 
"\\`\\(?:catch_clause\\|e\\(?:lse\\(?:\\(?:if\\)?_clause\\)\\|nd\\)\\)\\'") 
parent 0)}>
@@ -289,7 +289,7 @@ function B = continuations_and_block_comments %  <{Matched 
rule: (matlab-ts-mode
                  arg2); %!!17 %  <{Matched rule: ((parent-is 
"\\`arguments\\'") parent 0)}>
 
     A = [ 1 2  % !!4 %  <{Matched rule: ((parent-is "\\`block\\'") parent 0)}>
-          3 4 ]; % !!10 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-anchor 0)}>
+          3 4 ]; % !!10 %  <{Matched rule: (matlab-ts-mode--i-row-matcher 
matlab-ts-mode--i-row-matcher-anchor matlab-ts-mode--i-row-matcher-offset)}>
 
     foo(['this is a very long string', ... %!!4 %  <{Matched rule: ((parent-is 
"\\`block\\'") parent 0)}>
          'with a continution to do something very exciting']);%!!9 %  
<{Matched rule: ((parent-is "\\`\\(?:function_output\\|row\\)\\'") parent 0)}>
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic.m
new file mode 100644
index 0000000000..d54bd88647
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic.m
@@ -0,0 +1,18 @@
+% -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+m6 = [1 2
+      3 4];
+
+m7 = [   1111   22
+      1111111, 2222];
+
+m8 = [
+          1111; ...
+       1111111
+     ];
+
+m = [   2, 4000
+     3000,    1]
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected.txt
new file mode 100644
index 0000000000..628468c714
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_basic_expected.txt
@@ -0,0 +1,31 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,321> (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,170]@{% 
t-utils-test-indent: no-line-by-line-indent - wh...}@)
+ (assignment<172,192> left: (identifier[172,174]@{m6}@) =[175,176]
+  right: 
+   (matrix<177,192> [[177,178]
+    (row<178,181> (number[178,179]@{1}@) ,[180,180] (number[180,181]@{2}@))
+    (row<188,191> (number[188,189]@{3}@) ,[190,190] (number[190,191]@{4}@))
+    ][191,192]))
+ ;[192,193]
+ (assignment<195,234> left: (identifier[195,197]@{m7}@) =[198,199]
+  right: 
+   (matrix<200,234> [[200,201]
+    (row<204,213> (number[204,208]@{1111}@) ,[211,211] (number[211,213]@{22}@))
+    (row<220,233> (number[220,227]@{1111111}@) ,[227,228] 
(number[229,233]@{2222}@))
+    ][233,234]))
+ ;[234,235]
+ (assignment<237,285> left: (identifier[237,239]@{m8}@) =[240,241]
+  right: 
+   (matrix<242,285> [[242,243]
+    (row<254,258> (number[254,258]@{1111}@))
+    (line_continuation[260,264]@{...\n}@)
+    (row<271,278> (number[271,278]@{1111111}@))
+    ][284,285]))
+ ;[285,286]
+ (assignment<288,320> left: (identifier[288,289]@{m}@) =[290,291]
+  right: 
+   (matrix<292,320> [[292,293]
+    (row<296,303> (number[296,297]@{2}@) ,[297,298] (number[299,303]@{4000}@))
+    (row<309,319> (number[309,313]@{3000}@) ,[313,314] (number[318,319]@{1}@))
+    ][319,320]))
+ \n[320,321])
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m
new file mode 100644
index 0000000000..7611896660
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols.m
@@ -0,0 +1,97 @@
+% -*- matlab-ts -*-
+
+% Exercise matlab-ts-mode--ei-align-line-in-m-matrix
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+m = [1111,     2, 223
+      ...
+        4, 53333,   6];
+
+                           
+m1 = [           ...
+         1 2222; ...
+                 ...
+       333    4; ...
+     ];
+
+m1b = [
+          1 2222 % comment 1
+        333    4 % comment 2
+      ];
+
+m2 = [ ...
+         1 2222
+       333    4
+     ];
+
+m3a = [  21,     2, 223;
+          4, 53333,   6; ...
+       1222,     4,   5];
+
+m3b = [   1111,        2 ,223;
+             4, 53333,   6; ...
+         1222,     4,   5;];
+
+m3c = [1111,   2, 223;
+       4, 533,   6; ...
+       1222,   4,   5;];
+
+a = 1;
+b = 2;
+c = 3;
+d = 4;
+
+m4a = [a * b + c, d + a;
+               b,     c];
+
+m4b = [a * b + c, d + a;
+               b,     c];
+
+m5a = [a * b + c d + a; ...
+        ...
+        ...
+               b     c];
+
+m6 = [ ...
+       ...
+        1 2222
+       ...
+       ... comment
+      333    4
+     ];
+
+m7 = [   1111; ...
+           22; ...
+          333; ...
+      1111111];
+
+
+
+not1 = [1111, ...
+        22; ...
+        333, ...
+        1111111];
+
+not2 = [1     2 223;
+          [4 53333   6]; ...
+        1222     4   5; ...
+     ];
+
+not3 = [ ...
+       1 2 3; 4 5 6; ...
+       1 4 5; ...
+     ];
+
+not4 = [a * b + c, [d + a];
+        b, c];
+
+not5 = [ ...
+       1 2222; ...
+       333 4; ...
+       ]; if 1, disp('here')
+          end
+
+not6 = [1 2;
+        3 4 5];
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected.txt
new file mode 100644
index 0000000000..da04e9a6a8
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_expected.txt
@@ -0,0 +1,193 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,1546> (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,74]@{% 
Exercise matlab-ts-mode--ei-align-line-in-m-matr...}@) (comment[76,224]@{% 
t-utils-test-indent: no-line-by-line-indent - wh...}@)
+ (assignment<226,280> left: (identifier[226,227]@{m}@) =[228,229]
+  right: 
+   (matrix<230,280> [[230,231]
+    (row<231,247> (number[231,235]@{1111}@) ,[235,236] (number[241,242]@{2}@) 
,[242,243] (number[244,247]@{223}@))
+    (line_continuation[254,258]@{...\n}@)
+    (row<266,279> (number[266,267]@{4}@) ,[267,268] (number[269,274]@{53333}@) 
,[274,275] (number[278,279]@{6}@))
+    ][279,280]))
+ ;[280,281]
+ (assignment<311,401> left: (identifier[311,313]@{m1}@) =[314,315]
+  right: 
+   (matrix<316,401> [[316,317] (line_continuation[328,332]@{...\n}@)
+    (row<341,347> (number[341,342]@{1}@) ,[343,343] (number[343,347]@{2222}@))
+    (line_continuation[349,353]@{...\n}@) (line_continuation[370,374]@{...\n}@)
+    (row<381,389> (number[381,384]@{333}@) ,[388,388] (number[388,389]@{4}@))
+    (line_continuation[391,395]@{...\n}@) ][400,401]))
+ ;[401,402]
+ (assignment<404,477> left: (identifier[404,407]@{m1b}@) =[408,409]
+  right: 
+   (matrix<410,477> [[410,411]
+    (row<422,428> (number[422,423]@{1}@) ,[424,424] (number[424,428]@{2222}@))
+    (comment[429,440]@{% comment 1}@)
+    (row<449,457> (number[449,452]@{333}@) ,[456,456] (number[456,457]@{4}@))
+    (comment[458,469]@{% comment 2}@) ][476,477]))
+ ;[477,478]
+ (assignment<480,529> left: (identifier[480,482]@{m2}@) =[483,484]
+  right: 
+   (matrix<485,529> [[485,486] (line_continuation[487,491]@{...\n}@)
+    (row<500,506> (number[500,501]@{1}@) ,[502,502] (number[502,506]@{2222}@))
+    (row<514,522> (number[514,517]@{333}@) ,[521,521] (number[521,522]@{4}@))
+    ][528,529]))
+ ;[529,530]
+ (assignment<532,610> left: (identifier[532,535]@{m3a}@) =[536,537]
+  right: 
+   (matrix<538,610> [[538,539]
+    (row<541,555> (number[541,543]@{21}@) ,[543,544] (number[549,550]@{2}@) 
,[550,551] (number[552,555]@{223}@))
+    (row<567,580> (number[567,568]@{4}@) ,[568,569] (number[570,575]@{53333}@) 
,[575,576] (number[579,580]@{6}@))
+    (line_continuation[582,586]@{...\n}@)
+    (row<593,609> (number[593,597]@{1222}@) ,[597,598] (number[603,604]@{4}@) 
,[604,605] (number[608,609]@{5}@))
+    ][609,610]))
+ ;[610,611]
+ (assignment<613,703> left: (identifier[613,616]@{m3b}@) =[617,618]
+  right: 
+   (matrix<619,703> [[619,620]
+    (row<623,642> (number[623,627]@{1111}@) ,[627,628] (number[636,637]@{2}@) 
,[638,639] (number[639,642]@{223}@))
+    (row<657,670> (number[657,658]@{4}@) ,[658,659] (number[660,665]@{53333}@) 
,[665,666] (number[669,670]@{6}@))
+    (line_continuation[672,676]@{...\n}@)
+    (row<685,701> (number[685,689]@{1222}@) ,[689,690] (number[695,696]@{4}@) 
,[696,697] (number[700,701]@{5}@))
+    ][702,703]))
+ ;[703,704]
+ (assignment<706,776> left: (identifier[706,709]@{m3c}@) =[710,711]
+  right: 
+   (matrix<712,776> [[712,713]
+    (row<713,727> (number[713,717]@{1111}@) ,[717,718] (number[721,722]@{2}@) 
,[722,723] (number[724,727]@{223}@))
+    (row<736,747> (number[736,737]@{4}@) ,[737,738] (number[739,742]@{533}@) 
,[742,743] (number[746,747]@{6}@))
+    (line_continuation[749,753]@{...\n}@)
+    (row<760,774> (number[760,764]@{1222}@) ,[764,765] (number[768,769]@{4}@) 
,[769,770] (number[773,774]@{5}@))
+    ][775,776]))
+ ;[776,777]
+ (assignment<779,784> left: (identifier[779,780]@{a}@) =[781,782] right: 
(number[783,784]@{1}@))
+ ;[784,785]
+ (assignment<786,791> left: (identifier[786,787]@{b}@) =[788,789] right: 
(number[790,791]@{2}@))
+ ;[791,792]
+ (assignment<793,798> left: (identifier[793,794]@{c}@) =[795,796] right: 
(number[797,798]@{3}@))
+ ;[798,799]
+ (assignment<800,805> left: (identifier[800,801]@{d}@) =[802,803] right: 
(number[804,805]@{4}@))
+ ;[805,806]
+ (assignment<808,857> left: (identifier[808,811]@{m4a}@) =[812,813]
+  right: 
+   (matrix<814,857> [[814,815]
+    (row<815,831>
+     (binary_operator<815,824>
+      left: (binary_operator<815,820> left: (identifier[815,816]@{a}@) 
*[817,818] right: (identifier[819,820]@{b}@))
+      +[821,822] right: (identifier[823,824]@{c}@))
+     ,[824,825]
+     (binary_operator<826,831> left: (identifier[826,827]@{d}@) +[828,829] 
right: (identifier[830,831]@{a}@)))
+    (row<848,856> (identifier[848,849]@{b}@) ,[849,850] 
(identifier[855,856]@{c}@))
+    ][856,857]))
+ ;[857,858]
+ (assignment<860,909> left: (identifier[860,863]@{m4b}@) =[864,865]
+  right: 
+   (matrix<866,909> [[866,867]
+    (row<867,883>
+     (binary_operator<867,876>
+      left: (binary_operator<867,872> left: (identifier[867,868]@{a}@) 
*[869,870] right: (identifier[871,872]@{b}@))
+      +[873,874] right: (identifier[875,876]@{c}@))
+     ,[876,877]
+     (binary_operator<878,883> left: (identifier[878,879]@{d}@) +[880,881] 
right: (identifier[882,883]@{a}@)))
+    (row<900,908> (identifier[900,901]@{b}@) ,[901,902] 
(identifier[907,908]@{c}@))
+    ][908,909]))
+ ;[909,910]
+ (assignment<912,987> left: (identifier[912,915]@{m5a}@) =[916,917]
+  right: 
+   (matrix<918,987> [[918,919]
+    (row<919,934>
+     (binary_operator<919,928>
+      left: (binary_operator<919,924> left: (identifier[919,920]@{a}@) 
*[921,922] right: (identifier[923,924]@{b}@))
+      +[925,926] right: (identifier[927,928]@{c}@))
+     ,[929,929]
+     (binary_operator<929,934> left: (identifier[929,930]@{d}@) +[931,932] 
right: (identifier[933,934]@{a}@)))
+    (line_continuation[936,940]@{...\n}@) 
(line_continuation[948,952]@{...\n}@) (line_continuation[960,964]@{...\n}@)
+    (row<979,986> (identifier[979,980]@{b}@) ,[985,985] 
(identifier[985,986]@{c}@))
+    ][986,987]))
+ ;[987,988]
+ (assignment<990,1078> left: (identifier[990,992]@{m6}@) =[993,994]
+  right: 
+   (matrix<995,1078> [[995,996] (line_continuation[997,1001]@{...\n}@) 
(line_continuation[1008,1012]@{...\n}@)
+    (row<1020,1026> (number[1020,1021]@{1}@) ,[1022,1022] 
(number[1022,1026]@{2222}@))
+    (line_continuation[1034,1038]@{...\n}@) (line_continuation[1045,1057]@{... 
comment\n}@)
+    (row<1063,1071> (number[1063,1066]@{333}@) ,[1070,1070] 
(number[1070,1071]@{4}@))
+    ][1077,1078]))
+ ;[1078,1079]
+ (assignment<1081,1152> left: (identifier[1081,1083]@{m7}@) =[1084,1085]
+  right: 
+   (matrix<1086,1152> [[1086,1087]
+    (row<1090,1094> (number[1090,1094]@{1111}@))
+    (line_continuation[1096,1100]@{...\n}@)
+    (row<1111,1113> (number[1111,1113]@{22}@))
+    (line_continuation[1115,1119]@{...\n}@)
+    (row<1129,1132> (number[1129,1132]@{333}@))
+    (line_continuation[1134,1138]@{...\n}@)
+    (row<1144,1151> (number[1144,1151]@{1111111}@))
+    ][1151,1152]))
+ ;[1152,1153]
+ (assignment<1157,1224> left: (identifier[1157,1161]@{not1}@) =[1162,1163]
+  right: 
+   (matrix<1164,1224> [[1164,1165]
+    (row<1165,1185> (number[1165,1169]@{1111}@) ,[1169,1170] 
(line_continuation[1171,1175]@{...\n}@) (number[1183,1185]@{22}@))
+    (line_continuation[1187,1191]@{...\n}@)
+    (row<1199,1223> (number[1199,1202]@{333}@) ,[1202,1203] 
(line_continuation[1204,1208]@{...\n}@) (number[1216,1223]@{1111111}@))
+    ][1223,1224]))
+ ;[1224,1225]
+ (assignment<1227,1311> left: (identifier[1227,1231]@{not2}@) =[1232,1233]
+  right: 
+   (matrix<1234,1311> [[1234,1235]
+    (row<1235,1246> (number[1235,1236]@{1}@) ,[1241,1241] 
(number[1241,1242]@{2}@) ,[1243,1243] (number[1243,1246]@{223}@))
+    (row<1258,1271>
+     (matrix<1258,1271> [[1258,1259]
+      (row<1259,1270> (number[1259,1260]@{4}@) ,[1261,1261] 
(number[1261,1266]@{53333}@) ,[1269,1269] (number[1269,1270]@{6}@))
+      ][1270,1271]))
+    (line_continuation[1273,1277]@{...\n}@)
+    (row<1285,1299> (number[1285,1289]@{1222}@) ,[1294,1294] 
(number[1294,1295]@{4}@) ,[1298,1298] (number[1298,1299]@{5}@))
+    (line_continuation[1301,1305]@{...\n}@) ][1310,1311]))
+ ;[1311,1312]
+ (assignment<1314,1376> left: (identifier[1314,1318]@{not3}@) =[1319,1320]
+  right: 
+   (matrix<1321,1376> [[1321,1322] (line_continuation[1323,1327]@{...\n}@)
+    (row<1334,1339> (number[1334,1335]@{1}@) ,[1336,1336] 
(number[1336,1337]@{2}@) ,[1338,1338] (number[1338,1339]@{3}@))
+    (row<1341,1346> (number[1341,1342]@{4}@) ,[1343,1343] 
(number[1343,1344]@{5}@) ,[1345,1345] (number[1345,1346]@{6}@))
+    (line_continuation[1348,1352]@{...\n}@)
+    (row<1359,1364> (number[1359,1360]@{1}@) ,[1361,1361] 
(number[1361,1362]@{4}@) ,[1363,1363] (number[1363,1364]@{5}@))
+    (line_continuation[1366,1370]@{...\n}@) ][1375,1376]))
+ ;[1376,1377]
+ (assignment<1379,1420> left: (identifier[1379,1383]@{not4}@) =[1384,1385]
+  right: 
+   (matrix<1386,1420> [[1386,1387]
+    (row<1387,1405>
+     (binary_operator<1387,1396>
+      left: (binary_operator<1387,1392> left: (identifier[1387,1388]@{a}@) 
*[1389,1390] right: (identifier[1391,1392]@{b}@))
+      +[1393,1394] right: (identifier[1395,1396]@{c}@))
+     ,[1396,1397]
+     (matrix<1398,1405> [[1398,1399]
+      (row<1399,1404>
+       (binary_operator<1399,1404> left: (identifier[1399,1400]@{d}@) 
+[1401,1402] right: (identifier[1403,1404]@{a}@)))
+      ][1404,1405]))
+    (row<1415,1419> (identifier[1415,1416]@{b}@) ,[1416,1417] 
(identifier[1418,1419]@{c}@))
+    ][1419,1420]))
+ ;[1420,1421]
+ (assignment<1423,1481> left: (identifier[1423,1427]@{not5}@) =[1428,1429]
+  right: 
+   (matrix<1430,1481> [[1430,1431] (line_continuation[1432,1436]@{...\n}@)
+    (row<1443,1449> (number[1443,1444]@{1}@) ,[1445,1445] 
(number[1445,1449]@{2222}@))
+    (line_continuation[1451,1455]@{...\n}@)
+    (row<1462,1467> (number[1462,1465]@{333}@) ,[1466,1466] 
(number[1466,1467]@{4}@))
+    (line_continuation[1469,1473]@{...\n}@) ][1480,1481]))
+ ;[1481,1482]
+ (if_statement<1483,1515> if[1483,1485] condition: (number[1486,1487]@{1}@) 
,[1487,1488]
+  (block<1489,1502>
+   (function_call<1489,1501> name: (identifier[1489,1493]@{disp}@) ([1493,1494]
+    (arguments<1494,1500>
+     argument: (string<1494,1500> '[1494,1495] 
(string_content[1495,1499]@{here}@) '[1499,1500]))
+    )[1500,1501])
+   \n[1501,1502])
+  end[1512,1515])
+ \n[1515,1517]
+ (assignment<1517,1544> left: (identifier[1517,1521]@{not6}@) =[1522,1523]
+  right: 
+   (matrix<1524,1544> [[1524,1525]
+    (row<1525,1528> (number[1525,1526]@{1}@) ,[1527,1527] 
(number[1527,1528]@{2}@))
+    (row<1538,1543> (number[1538,1539]@{3}@) ,[1540,1540] 
(number[1540,1541]@{4}@) ,[1542,1542] (number[1542,1543]@{5}@))
+    ][1543,1544]))
+ ;[1544,1545] \n[1545,1546])
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error.m
new file mode 100644
index 0000000000..b06862d0a9
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error.m
@@ -0,0 +1,4 @@
+% -*- matlab-ts -*-
+
+not6 = [1 2;
+        a *** b, d];
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected.txt
new file mode 100644
index 0000000000..9d0b5cb418
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_cols_with_error_expected.txt
@@ -0,0 +1,13 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,56> (comment[1,20]@{% -*- matlab-ts -*-}@)
+ (assignment<22,54> left: (identifier[22,26]@{not6}@) =[27,28]
+  right: 
+   (matrix<29,54> [[29,30]
+    (row<30,33> (number[30,31]@{1}@) ,[32,32] (number[32,33]@{2}@))
+    (row<43,53>
+     (binary_operator<43,50> left: (identifier[43,44]@{a}@)
+      (ERROR<45,47> *[45,46] *[46,47])
+      *[47,48] right: (identifier[49,50]@{b}@))
+     ,[50,51] (identifier[52,53]@{d}@))
+    ][53,54]))
+ ;[54,55] \n[55,56])
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels.m
new file mode 100644
index 0000000000..5db17b9b5d
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels.m
@@ -0,0 +1,20 @@
+% -*- matlab-ts -*-
+
+% Exercise matrix indentation with varying indent levels
+
+% t-utils-test-indent: no-line-by-line-indent - when typing matrix 
line-by-line, there are
+% error nodes and thus the matrix alignment doesn't occur
+
+if foo1
+    m1 = [23, 2
+             233,   4];
+end
+
+
+if foo1
+    if foo2
+        m2 = [ 23,    2
+                 233,    4
+          1   , 3344   ];
+    end
+end
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected.txt
new file mode 100644
index 0000000000..ee598d263d
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-files/electric_indent_matrix_n_levels_expected.txt
@@ -0,0 +1,29 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,393> (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,78]@{% 
Exercise matrix indentation with varying indent ...}@) (comment[80,228]@{% 
t-utils-test-indent: no-line-by-line-indent - wh...}@) \n[228,230]
+ (if_statement<230,281> if[230,232] condition: (identifier[233,237]@{foo1}@)
+  (block<242,278>
+   (assignment<242,276> left: (identifier[242,244]@{m1}@) =[245,246]
+    right: 
+     (matrix<247,276> [[247,248]
+      (row<248,253> (number[248,250]@{23}@) ,[250,251] (number[252,253]@{2}@))
+      (row<267,275> (number[267,270]@{233}@) ,[270,271] (number[274,275]@{4}@))
+      ][275,276]))
+   ;[276,277] \n[277,278])
+  end[278,281])
+ \n[281,284]
+ (if_statement<284,392> if[284,286] condition: (identifier[287,291]@{foo1}@) 
\n[291,292]
+  (block<296,389>
+   (if_statement<296,388> if[296,298] condition: (identifier[299,303]@{foo2}@)
+    (block<312,381>
+     (assignment<312,379> left: (identifier[312,314]@{m2}@) =[315,316]
+      right: 
+       (matrix<317,379> [[317,318]
+        (row<319,327> (number[319,321]@{23}@) ,[321,322] 
(number[326,327]@{2}@))
+        (row<345,354> (number[345,348]@{233}@) ,[348,349] 
(number[353,354]@{4}@))
+        (row<365,375> (number[365,366]@{1}@) ,[369,370] 
(number[371,375]@{3344}@))
+        ][378,379]))
+     ;[379,380] \n[380,381])
+    end[385,388])
+   \n[388,389])
+  end[389,392])
+ \n[392,393])
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch.m
new file mode 100644
index 0000000000..3c009d9312
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch.m
@@ -0,0 +1,19 @@
+% -*- matlab-ts -*-
+
+%{
+  Follow validates we do not trim trailing whitespace when electric indent is 
active and point is
+  within an ERROR node.
+
+  case1:
+  (t-utils-xr
+
+  (goto-char (point-max))
+
+  (insert "switch a")   "C-m"
+  "C-e"
+  (insert "1")          "C-m"
+  (insert "disp('1')")
+
+  )
+  
+%}
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch_expected.txt
new file mode 100644
index 0000000000..f4dc23798d
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_switch_expected.txt
@@ -0,0 +1,2 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,301> (comment[1,20]@{% -*- matlab-ts -*-}@) \n[20,22] 
(comment[22,300]@{%\{\n  Follow validates we do not trim trailing wh...}@) 
\n[300,301])

Reply via email to