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

    matlab-ts-mode--i-next-line-matcher: reorganize
---
 matlab-ts-mode.el | 131 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 72 insertions(+), 59 deletions(-)

diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 8da86b52c4..6fa2a64770 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -1755,6 +1755,68 @@ Sets `matlab-ts-mode--i-next-line-pair' to (ANCHOR-NODE 
. OFFSET)"
                                                                 "row")
                                                         eos)))
 
+(defun matlab-ts-mode--i-next-line-matcher-comment (node)
+  "Is NODE a comment we should indent when in context of an error?"
+
+  (when (and node
+             (string-match-p (rx (seq bos "comment" eos)) (treesit-node-type 
node)))
+    (let ((prev-sibling (treesit-node-prev-sibling node)))
+      (when (equal (treesit-node-type prev-sibling) "ERROR")
+        (let ((last-child (treesit-node-child prev-sibling -1)))
+
+          ;; function foo(a,b,stuff,cmddual1fake,cmddual2fake)
+          ;;     if foo %!!4
+          ;;         C = "this is the end of the line";
+          ;;         % !!8                                      <== TAB to here
+          (when (and last-child
+                     (string= (treesit-node-type last-child) ";"))
+            (setq last-child (treesit-node-prev-sibling last-child)))
+
+          (when last-child
+            ;; classdef foo
+            ;;     properties
+            ;;         foo1;
+            ;;     end
+            ;;     % comment    <== RET on this node, last-child == "end"
+            ;; See: 
tests/test-matlab-ts-mode-indent-xr-files/indent_xr_classdef3.m
+            (setq matlab-ts-mode--i-next-line-pair
+                  (cons (treesit-node-start last-child) 0))
+            (cl-return-from matlab-ts-mode--i-next-line-matcher t)))))))
+
+(defun matlab-ts-mode--not-node-and-blank (node bol)
+  "Is NODE nil with BOL on a blank line?"
+  ;; Consider the one-liner:
+  ;;    classdef indent_xr_classdef1
+  ;;        ^                                 TAB should go here
+  ;;  on entry node is nil and parent is ERROR, so backup to the newline:
+  ;;   (source_file
+  ;;    (ERROR classdef (identifier) \n))
+  ;; See: tests/test-matlab-ts-mode-indent-xr-files/indent_xr_classdef1.m
+  (and (not node)
+       (save-excursion
+         (goto-char bol)
+         (beginning-of-line)
+         (looking-at "^[ \t]*$" t))))
+
+(defun matlab-ts-mode--last-child-of-error (node parent)
+  "Get updated parent if ellipsis NODE with PARENT is not part of an error."
+
+  ;; Handle continuation where the node is not part of the error node. Example:
+  ;;    (source_file
+  ;;     (ERROR classdef (identifier) \n properties \n (identifier) =)
+  ;;     (line_continuation))
+  ;; For
+  ;;    classdef indent_xr_classdef2
+  ;;        properties
+  ;;            p3continued = ...
+  ;;                ^                       << RET on prior line should go here
+  ;; In this case, last-child-of-error-node will be the "=" node.
+  (when (and (not node)
+             (string= (treesit-node-type parent) "line_continuation"))
+    (let ((prev-sibling (treesit-node-prev-sibling parent)))
+      (when (equal (treesit-node-type prev-sibling) "ERROR")
+        (treesit-node-child prev-sibling -1)))))
+
 (cl-defun matlab-ts-mode--i-next-line-matcher (node parent bol &rest _)
   "Matcher for indent on a newline being inserted when in presence of errors.
 If so, set `matlab-ts-mode--i-next-line-pair'.
@@ -1778,68 +1840,19 @@ Prev-siblings:
     > #<treesit-node identifier in 30-33>
       > #<treesit-node \""
 
-  (when (and node
-             (string-match-p (rx (seq bos "comment" eos)) (treesit-node-type 
node)))
-    (let ((prev-sibling (treesit-node-prev-sibling node)))
-      (when (equal (treesit-node-type prev-sibling) "ERROR")
-        (let ((last-child (treesit-node-child prev-sibling -1)))
+  (when (matlab-ts-mode--i-next-line-matcher-comment node)
+    (cl-return-from matlab-ts-mode--i-next-line-matcher t))
 
-          ;; function foo(a,b,stuff,cmddual1fake,cmddual2fake)
-          ;;     if foo %!!4
-          ;;         C = "this is the end of the line";
-          ;;         % !!8                                      <== TAB to here
-          (when (and last-child
-                     (string= (treesit-node-type last-child) ";"))
-            (setq last-child (treesit-node-prev-sibling last-child)))
+  (when (matlab-ts-mode--not-node-and-blank node bol)
+    (setq parent (treesit-node-at (point))))
 
-          (when last-child
-            ;; classdef foo
-            ;;     properties
-            ;;         foo1;
-            ;;     end
-            ;;     % comment    <== RET on this node, last-child == "end"
-            ;; See: 
tests/test-matlab-ts-mode-indent-xr-files/indent_xr_classdef3.m
-            (setq matlab-ts-mode--i-next-line-pair
-                  (cons (treesit-node-start last-child) 0))
-            (cl-return-from matlab-ts-mode--i-next-line-matcher t))))))
-
-  (let (last-child-of-error-node)
-
-    (when (and (not node)
-               (save-excursion
-                 (goto-char bol)
-                 (beginning-of-line)
-                 (looking-at "^[ \t]*$" t)))
-      ;; Consider the one-liner:
-      ;;    classdef indent_xr_classdef1
-      ;;        ^                                 TAB should go here
-      ;;  on entry node is nil and parent is ERROR, so backup to the newline:
-      ;;   (source_file
-      ;;    (ERROR classdef (identifier) \n))
-      ;; See: tests/test-matlab-ts-mode-indent-xr-files/indent_xr_classdef1.m
-      (setq parent (treesit-node-at (point))))
-
-    ;; Handle continuation where it's not part of the error node. Example:
-    ;;    (source_file
-    ;;     (ERROR classdef (identifier) \n properties \n (identifier) =)
-    ;;     (line_continuation))
-    ;; For
-    ;;    classdef indent_xr_classdef2
-    ;;        properties
-    ;;            p3continued = ...
-    ;;                ^                       << RET on prior line should go 
here
-    ;; In this case, last-child-of-error-node will be the "=" node.
-    (when (and (not node)
-               (string= (treesit-node-type parent) "line_continuation"))
-      (let ((prev-sibling (treesit-node-prev-sibling parent)))
-        (when (equal (treesit-node-type prev-sibling) "ERROR")
-          (setq parent (treesit-node-child prev-sibling -1))
-          (setq last-child-of-error-node parent))))
+  (let ((last-child-of-error-node (matlab-ts-mode--last-child-of-error node 
parent)))
+    (when last-child-of-error-node
+      (setq parent last-child-of-error-node))
 
     (when (or last-child-of-error-node
-              (and node
-                   (string= (treesit-node-type node) "ERROR"))
-              (string-match-p (rx (seq bos (or "ERROR" "\n") eos)) 
(treesit-node-type parent) )
+              (and node (string= (treesit-node-type node) "ERROR"))
+              (string-match-p (rx (seq bos (or "ERROR" "\n") eos)) 
(treesit-node-type parent))
               (equal (treesit-node-type (treesit-node-parent parent)) "ERROR"))
 
       (let ((node-to-check (or last-child-of-error-node node parent))
@@ -1945,7 +1958,7 @@ Prev-siblings:
                              (not (equal node error-node)))
                          (> (treesit-node-start error-node) 
(treesit-node-start anchor-node)))
                 (setq anchor-node error-node)))
-            
+
             (matlab-ts-mode--i-next-line-indent-level node bol anchor-node 
last-child-of-error-node)
             ;; t ==> matched
             t))))))

Reply via email to