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

    matlab-ts-mode: fix indent as you type for continued fcn out/in args
---
 matlab-ts-mode.el                                  |  82 ++--
 .../indent_fcn_ellipsis.skip.typing.txt            |   1 -
 .../indent_xr_fun5.m                               |  25 ++
 .../indent_xr_fun5_expected.org                    | 443 +++++++++++++++++++++
 4 files changed, 521 insertions(+), 30 deletions(-)

diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index ec57f436f0..710462d9c0 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -1394,34 +1394,49 @@ Prev-siblings:
                       (not prev-sibling-to-check)
                       (not prev-sibling-has-error))
             (let ((prev-sibling-type (treesit-node-type prev-sibling)))
-              (cond
-               ((string= prev-sibling-type "ERROR")
-                (setq prev-sibling-has-error t))
-
-               ((and (string-match-p anchors-rx prev-sibling-type)
-                     (or (not (string= prev-sibling-type "("))
-                         ;; See: 
test-matlab-ts-mode-indent-xr-files/indent_xr_i_cont_incomplete5.m
-                         ;; result = longFunction( ...
-                         ;;     ^                           <== RET on prior 
line or TAB goes here
-                         ;;
-                         ;; See: 
tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun3.m
-                         ;; function out=indent_xr_fun3(in1, ...
-                         ;;                             ^   <== RET on prior 
line or TAB goes here
-
-                         ;; do not have before paren: e.g. no longFunction(
-                         (not (equal (treesit-node-type
-                                      (treesit-node-prev-sibling prev-sibling))
-                                     "identifier"))
-                         ;; OR we have identifier after the paren: e.g. (in1, 
...
-                         (equal (treesit-node-type
-                                 (treesit-node-next-sibling prev-sibling))
-                                "identifier")
-                          ))
-                (setq prev-sibling-to-check prev-sibling)))
-
-              (setq prev-sibling (if prev-sibling-has-error
-                                     nil
-                                   (treesit-node-prev-sibling 
prev-sibling))))))
+
+              ;; Backup over "..." continuations becasue they may not be 
prev-sibling's.
+              ;; Consider:
+              ;;    function ...
+              ;;        [    ...    <== TAB here
+              ;; (source_file (ERROR function) (line_continuation) (ERROR [) 
(line_continuation))
+              (save-excursion
+                (while (and prev-sibling
+                            (string= prev-sibling-type "line_continuation"))
+                  (goto-char (treesit-node-start prev-sibling))
+                  (when (re-search-backward "[^ \t\n\r]" nil t)
+                    (setq prev-sibling (treesit-node-at (point))
+                          prev-sibling-type (when prev-sibling
+                                              (treesit-node-type 
prev-sibling))))))
+              (when prev-sibling
+                (cond
+                 ((string= prev-sibling-type "ERROR")
+                  (setq prev-sibling-has-error t))
+
+                 ((and (string-match-p anchors-rx prev-sibling-type)
+                       (or (not (string= prev-sibling-type "("))
+                           ;; See: 
test-matlab-ts-mode-indent-xr-files/indent_xr_i_cont_incomplete5.m
+                           ;; result = longFunction( ...
+                           ;;     ^                           <== RET on prior 
line or TAB goes here
+                           ;;
+                           ;; See: 
tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun3.m
+                           ;; function out=indent_xr_fun3(in1, ...
+                           ;;                             ^   <== RET on prior 
line or TAB goes here
+                           
+                           ;; do not have before paren: e.g. no longFunction(
+                           (not (equal (treesit-node-type
+                                        (treesit-node-prev-sibling 
prev-sibling))
+                                       "identifier"))
+                           ;; OR we have identifier after the paren: e.g. 
(in1, ...
+                           (equal (treesit-node-type
+                                   (treesit-node-next-sibling prev-sibling))
+                                  "identifier")
+                           ))
+                  (setq prev-sibling-to-check prev-sibling)))
+
+                (setq prev-sibling (if prev-sibling-has-error
+                                       nil
+                                     (treesit-node-prev-sibling 
prev-sibling)))))))
 
         ;; We use regular matching rules if we don't have an error.
         (when (and (not in-error)
@@ -1459,7 +1474,16 @@ Prev-siblings:
                                        matlab-ts-mode--indent-level))
                                     ("("
                                      1)
-                                    ((rx (seq bos (or "[" "{" eos)))
+                                    ("[" ;; either a matrix or function output
+                                     (if (save-excursion
+                                           (goto-char (treesit-node-start 
anchor-node))
+                                           (while (and (re-search-backward "[^ 
\t\r\n]" nil t)
+                                                       (string= 
(treesit-node-type (treesit-node-at (point))) "line_continuation")))
+                                           (string= (treesit-node-type 
(treesit-node-at (point))) "function"))
+                                         ;; function output
+                                         1
+                                       matlab-ts-mode--array-indent-level))
+                                    ("{"
                                      matlab-ts-mode--array-indent-level)
                                     ((rx (seq bos (or "function" 
"function_definition") eos))
                                      (if (and (or in-error 
prev-sibling-has-error)
diff --git 
a/tests/test-matlab-ts-mode-indent-files/indent_fcn_ellipsis.skip.typing.txt 
b/tests/test-matlab-ts-mode-indent-files/indent_fcn_ellipsis.skip.typing.txt
deleted file mode 100644
index 7d3e18125c..0000000000
--- a/tests/test-matlab-ts-mode-indent-files/indent_fcn_ellipsis.skip.typing.txt
+++ /dev/null
@@ -1 +0,0 @@
-Items not indented correctly.
diff --git a/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun5.m 
b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun5.m
new file mode 100644
index 0000000000..e1cec06d79
--- /dev/null
+++ b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun5.m
@@ -0,0 +1,25 @@
+% -*- matlab-ts -*-
+
+%{
+  (t-utils-xr
+
+  (re-search-forward "%}") "C-n"
+
+  (insert "\n") "C-b"  ;; ensure we have a newline
+
+  (insert "function ...")                  "C-m"
+  (insert     "[ ...")                     "C-m"
+  (insert      "b ... comment for b")      "C-m"
+  (insert     "] = ...")                   "C-m"
+  (insert     "indent_xr_fun5 ...")        "C-m"
+  (insert     "( ...")                     "C-m"
+  (insert      "a ... comment for a")      "C-m"
+  (insert     ")")                         "C-m"
+  "C-m"
+  (insert     "b=2*a;")                    "C-m"
+  (insert "end")                           "C-m"
+
+  (re-search-backward "^fun")
+  (t-utils-xr-print-code (point) (point-max))
+  )
+%}
diff --git 
a/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun5_expected.org 
b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun5_expected.org
new file mode 100644
index 0000000000..3449aa516e
--- /dev/null
+++ b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun5_expected.org
@@ -0,0 +1,443 @@
+#+startup: showall
+
+* Executing commands from indent_xr_fun5.m:4:2:
+
+  (t-utils-xr
+
+  (re-search-forward "%}") "C-n"
+
+  (insert "\n") "C-b"  ;; ensure we have a newline
+
+  (insert "function ...")                  "C-m"
+  (insert     "[ ...")                     "C-m"
+  (insert      "b ... comment for b")      "C-m"
+  (insert     "] = ...")                   "C-m"
+  (insert     "indent_xr_fun5 ...")        "C-m"
+  (insert     "( ...")                     "C-m"
+  (insert      "a ... comment for a")      "C-m"
+  (insert     ")")                         "C-m"
+  "C-m"
+  (insert     "b=2*a;")                    "C-m"
+  (insert "end")                           "C-m"
+
+  (re-search-backward "^fun")
+  (t-utils-xr-print-code (point) (point-max))
+  )
+
+- Invoking      : (re-search-forward "%}")
+  Start point   :  704
+  Moved to point:  707
+  : 25:2: %}
+  :         ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  707
+  Moved to point:  708
+  : 26:0: 
+  :       ^
+  No buffer modifications
+
+- Invoking      : (insert "
+")
+  Start point   :  708
+  Moved to point:  709
+  : 27:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -23,3 +23,4 @@
+   (t-utils-xr-print-code (point) (point-max))
+   )
+ %}
++
+  #+end_src diff
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  709
+  Moved to point:  708
+  : 26:0: 
+  :       ^
+  No buffer modifications
+
+- Invoking      : (insert "function ...")
+  Start point   :  708
+  Moved to point:  720
+  : 26:12: function ...
+  :                    ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -23,4 +23,4 @@
+   (t-utils-xr-print-code (point) (point-max))
+   )
+ %}
+-
++function ...
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  720
+  Moved to point:  725
+  : 27:4:     
+  :           ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -24,3 +24,4 @@
+   )
+ %}
+ function ...
++    
+  #+end_src diff
+
+- Invoking      : (insert "[ ...")
+  Start point   :  725
+  Moved to point:  730
+  : 27:9:     [ ...
+  :                ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -24,4 +24,4 @@
+   )
+ %}
+ function ...
+-    
++    [ ...
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  730
+  Moved to point:  736
+  : 28:5:      
+  :            ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -25,3 +25,4 @@
+ %}
+ function ...
+     [ ...
++     
+  #+end_src diff
+
+- Invoking      : (insert "b ... comment for b")
+  Start point   :  736
+  Moved to point:  755
+  : 28:24:      b ... comment for b
+  :                                ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -25,4 +25,4 @@
+ %}
+ function ...
+     [ ...
+-     
++     b ... comment for b
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  755
+  Moved to point:  761
+  : 29:5:      
+  :            ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -26,3 +26,4 @@
+ function ...
+     [ ...
+      b ... comment for b
++     
+  #+end_src diff
+
+- Invoking      : (insert "] = ...")
+  Start point   :  761
+  Moved to point:  768
+  : 29:12:      ] = ...
+  :                    ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -26,4 +26,4 @@
+ function ...
+     [ ...
+      b ... comment for b
+-     
++     ] = ...
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  768
+  Moved to point:  772
+  : 30:4:     
+  :           ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -26,4 +26,5 @@
+ function ...
+     [ ...
+      b ... comment for b
+-     ] = ...
++    ] = ...
++    
+  #+end_src diff
+
+- Invoking      : (insert "indent_xr_fun5 ...")
+  Start point   :  772
+  Moved to point:  790
+  : 30:22:     indent_xr_fun5 ...
+  :                              ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -27,4 +27,4 @@
+     [ ...
+      b ... comment for b
+     ] = ...
+-    
++    indent_xr_fun5 ...
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  790
+  Moved to point:  791
+  : 31:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -28,3 +28,4 @@
+      b ... comment for b
+     ] = ...
+     indent_xr_fun5 ...
++
+  #+end_src diff
+
+- Invoking      : (insert "( ...")
+  Start point   :  791
+  Moved to point:  796
+  : 31:5: ( ...
+  :            ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -28,4 +28,4 @@
+      b ... comment for b
+     ] = ...
+     indent_xr_fun5 ...
+-
++( ...
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  796
+  Moved to point:  806
+  : 32:5:      
+  :            ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -28,4 +28,5 @@
+      b ... comment for b
+     ] = ...
+     indent_xr_fun5 ...
+-( ...
++    ( ...
++     
+  #+end_src diff
+
+- Invoking      : (insert "a ... comment for a")
+  Start point   :  806
+  Moved to point:  825
+  : 32:24:      a ... comment for a
+  :                                ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -29,4 +29,4 @@
+     ] = ...
+     indent_xr_fun5 ...
+     ( ...
+-     
++     a ... comment for a
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  825
+  Moved to point:  831
+  : 33:5:      
+  :            ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -30,3 +30,4 @@
+     indent_xr_fun5 ...
+     ( ...
+      a ... comment for a
++     
+  #+end_src diff
+
+- Invoking      : (insert ")")
+  Start point   :  831
+  Moved to point:  832
+  : 33:6:      )
+  :             ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -30,4 +30,4 @@
+     indent_xr_fun5 ...
+     ( ...
+      a ... comment for a
+-     
++     )
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  832
+  Moved to point:  836
+  : 34:4:     
+  :           ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -30,4 +30,5 @@
+     indent_xr_fun5 ...
+     ( ...
+      a ... comment for a
+-     )
++    )
++    
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  836
+  Moved to point:  837
+  : 35:4:     
+  :           ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -31,4 +31,5 @@
+     ( ...
+      a ... comment for a
+     )
++
+     
+  #+end_src diff
+
+- Invoking      : (insert "b=2*a;")
+  Start point   :  837
+  Moved to point:  843
+  : 35:10:     b=2*a;
+  :                  ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -32,4 +32,4 @@
+      a ... comment for a
+     )
+ 
+-    
++    b=2*a;
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  843
+  Moved to point:  844
+  : 36:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -33,3 +33,4 @@
+     )
+ 
+     b=2*a;
++
+  #+end_src diff
+
+- Invoking      : (insert "end")
+  Start point   :  844
+  Moved to point:  847
+  : 36:3: end
+  :          ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -33,4 +33,4 @@
+     )
+ 
+     b=2*a;
+-
++end
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  847
+  Moved to point:  848
+  : 37:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -34,3 +34,4 @@
+ 
+     b=2*a;
+ end
++
+  #+end_src diff
+
+- Invoking      : (re-search-backward "^fun")
+  Start point   :  848
+  Moved to point:  708
+  : 26:0: function ...
+  :       ^
+  No buffer modifications
+
+- Invoking      : (t-utils-xr-print-code (point) (point-max))
+  Start point   :  708
+  No point movement
+  standard-output:
+  #+begin_src matlab-ts
+function ...
+    [ ...
+     b ... comment for b
+    ] = ...
+    indent_xr_fun5 ...
+    ( ...
+     a ... comment for a
+    )
+
+    b=2*a;
+end
+
+  #+end_src
+  No buffer modifications

Reply via email to