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

    matlab-ts-mode: fix indent of added comments after code in incomplete 
function
---
 matlab-ts-mode.el                                  |  34 ++++
 .../indent_comments.m                              |   4 +
 .../indent_comments.skip.typing.txt                |   1 -
 .../indent_comments_expected.m                     |   4 +
 .../indent_fcn_basic.m                             |   8 +
 .../indent_fcn_basic_expected.m                    |   8 +
 .../indent_xr_fun4.m                               |  18 ++
 .../indent_xr_fun4_expected.org                    | 210 +++++++++++++++++++++
 8 files changed, 286 insertions(+), 1 deletion(-)

diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 161f5cf43c..ea469dc61b 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -1496,6 +1496,32 @@ Prev-siblings:
   "Return the offset computed by `matlab-ts-mode--i-next-line-matcher'."
   (cdr matlab-ts-mode--i-next-line-pair))
 
+(defvar matlab-ts-mode--i-comment-under-fcn-pair)
+
+(defun matlab-ts-mode--i-comment-under-fcn-matcher (node _parent _bol &rest _)
+  "Matcher when NODE is a comment and under a function.
+Example:
+   function a=foo
+       a=1;
+       %comment <== TAB goes here."
+  (when (and node
+             (string= (treesit-node-type node) "comment"))
+    (let ((prev-sibling (treesit-node-prev-sibling node)))
+      (when (and prev-sibling
+                 (string= (treesit-node-type prev-sibling) 
"function_definition"))
+        (when (not (equal (treesit-node-type (treesit-node-child prev-sibling 
-1)) "end"))
+          (setq matlab-ts-mode--i-comment-under-fcn-pair
+                (cons (treesit-node-start prev-sibling) 
matlab-ts-mode--indent-level))
+          t)))))
+
+(defun matlab-ts-mode--i-comment-under-fcn-anchor (&rest _)
+  "Return the anchor computed by 
`matlab-ts-mode--i-comment-under-fcn-matcher'."
+  (car matlab-ts-mode--i-comment-under-fcn-pair))
+
+(defun matlab-ts-mode--i-comment-under-fcn-offset (&rest _)
+  "Return the offset computed by 
`matlab-ts-mode--i-comment-under-fcn-matcher'."
+  (cdr matlab-ts-mode--i-comment-under-fcn-pair))
+
 (defvar matlab-ts-mode--indent-rules
   `((matlab
 
@@ -1506,6 +1532,14 @@ Prev-siblings:
       ,#'matlab-ts-mode--i-next-line-anchor
       ,#'matlab-ts-mode--i-next-line-offset)
 
+     ;; I-Rule: comment under function, e.g. typing the following (no end):
+     ;;         function a=foo
+     ;;             a=1;
+     ;;             %comment    <== TAB goes here
+     (,#'matlab-ts-mode--i-comment-under-fcn-matcher
+      ,#'matlab-ts-mode--i-comment-under-fcn-anchor
+      ,#'matlab-ts-mode--i-comment-under-fcn-offset)
+
      ;; I-Rule: classdef's, function's, or code for a script that is at the 
top-level
      ((lambda (node parent _bol &rest _)
         (and node
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_comments.m 
b/tests/test-matlab-ts-mode-indent-files/indent_comments.m
index ea2c8b88d1..0dbda0357f 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_comments.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_comments.m
@@ -1,4 +1,8 @@
 % -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - not possible to indent 
line-by-line
+% because of the multi-line comments.
+
 function b = indent_comments(a)
 % this the doc help
 % comment
diff --git 
a/tests/test-matlab-ts-mode-indent-files/indent_comments.skip.typing.txt 
b/tests/test-matlab-ts-mode-indent-files/indent_comments.skip.typing.txt
deleted file mode 100644
index 6d9c24a567..0000000000
--- a/tests/test-matlab-ts-mode-indent-files/indent_comments.skip.typing.txt
+++ /dev/null
@@ -1 +0,0 @@
-Some comments not indented correctly.
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_comments_expected.m 
b/tests/test-matlab-ts-mode-indent-files/indent_comments_expected.m
index 60581922ab..2498a48d3c 100644
--- a/tests/test-matlab-ts-mode-indent-files/indent_comments_expected.m
+++ b/tests/test-matlab-ts-mode-indent-files/indent_comments_expected.m
@@ -1,4 +1,8 @@
 % -*- matlab-ts -*-
+
+% t-utils-test-indent: no-line-by-line-indent - not possible to indent 
line-by-line
+% because of the multi-line comments.
+
 function b = indent_comments(a)
 % this the doc help
 % comment
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_fcn_basic.m 
b/tests/test-matlab-ts-mode-indent-files/indent_fcn_basic.m
new file mode 100644
index 0000000000..24f7e5bbcc
--- /dev/null
+++ b/tests/test-matlab-ts-mode-indent-files/indent_fcn_basic.m
@@ -0,0 +1,8 @@
+% -*- matlab-ts -*-
+function a=indent_fcn_basic
+% doc comment
+
+    % comment before a
+    a = 1;
+    % comment after a
+end
diff --git a/tests/test-matlab-ts-mode-indent-files/indent_fcn_basic_expected.m 
b/tests/test-matlab-ts-mode-indent-files/indent_fcn_basic_expected.m
new file mode 100644
index 0000000000..24f7e5bbcc
--- /dev/null
+++ b/tests/test-matlab-ts-mode-indent-files/indent_fcn_basic_expected.m
@@ -0,0 +1,8 @@
+% -*- matlab-ts -*-
+function a=indent_fcn_basic
+% doc comment
+
+    % comment before a
+    a = 1;
+    % comment after a
+end
diff --git a/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun4.m 
b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun4.m
new file mode 100644
index 0000000000..e8a8fe0774
--- /dev/null
+++ b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun4.m
@@ -0,0 +1,18 @@
+% -*- matlab-ts -*-
+
+%{
+  (t-utils-xr
+
+  (re-search-forward "%}") "C-n"
+
+  (insert "\n") "C-b"  ;; ensure we have a newline
+
+  (insert "function a=indent_xr_fun4")    "C-m"
+  (insert "a=1;")                         "C-m"
+  (insert "%comment")                     "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_fun4_expected.org 
b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun4_expected.org
new file mode 100644
index 0000000000..4e388a68f6
--- /dev/null
+++ b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_fun4_expected.org
@@ -0,0 +1,210 @@
+#+startup: showall
+
+* Executing commands from indent_xr_fun4.m:4:2:
+
+  (t-utils-xr
+
+  (re-search-forward "%}") "C-n"
+
+  (insert "\n") "C-b"  ;; ensure we have a newline
+
+  (insert "function a=indent_xr_fun4")    "C-m"
+  (insert "a=1;")                         "C-m"
+  (insert "%comment")                     "C-m"
+  (insert "end")                          "C-m"
+
+  (re-search-backward "^fun")
+  (t-utils-xr-print-code (point) (point-max))
+  )
+
+- Invoking      : (re-search-forward "%}")
+  Start point   :  398
+  Moved to point:  401
+  : 18:2: %}
+  :         ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  401
+  Moved to point:  402
+  : 19:0: 
+  :       ^
+  No buffer modifications
+
+- Invoking      : (insert "
+")
+  Start point   :  402
+  Moved to point:  403
+  : 20:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -16,3 +16,4 @@
+   (t-utils-xr-print-code (point) (point-max))
+   )
+ %}
++
+  #+end_src diff
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  403
+  Moved to point:  402
+  : 19:0: 
+  :       ^
+  No buffer modifications
+
+- Invoking      : (insert "function a=indent_xr_fun4")
+  Start point   :  402
+  Moved to point:  427
+  : 19:25: function a=indent_xr_fun4
+  :                                 ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -16,4 +16,4 @@
+   (t-utils-xr-print-code (point) (point-max))
+   )
+ %}
+-
++function a=indent_xr_fun4
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  427
+  Moved to point:  432
+  : 20:4:     
+  :           ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -17,3 +17,4 @@
+   )
+ %}
+ function a=indent_xr_fun4
++    
+  #+end_src diff
+
+- Invoking      : (insert "a=1;")
+  Start point   :  432
+  Moved to point:  436
+  : 20:8:     a=1;
+  :               ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -17,4 +17,4 @@
+   )
+ %}
+ function a=indent_xr_fun4
+-    
++    a=1;
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  436
+  Moved to point:  437
+  : 21:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -18,3 +18,4 @@
+ %}
+ function a=indent_xr_fun4
+     a=1;
++
+  #+end_src diff
+
+- Invoking      : (insert "%comment")
+  Start point   :  437
+  Moved to point:  445
+  : 21:8: %comment
+  :               ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -18,4 +18,4 @@
+ %}
+ function a=indent_xr_fun4
+     a=1;
+-
++%comment
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  445
+  Moved to point:  450
+  : 22:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -18,4 +18,5 @@
+ %}
+ function a=indent_xr_fun4
+     a=1;
+-%comment
++    %comment
++
+  #+end_src diff
+
+- Invoking      : (insert "end")
+  Start point   :  450
+  Moved to point:  453
+  : 22:3: end
+  :          ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -19,4 +19,4 @@
+ function a=indent_xr_fun4
+     a=1;
+     %comment
+-
++end
+  #+end_src diff
+
+- Invoking      : "C-m" = newline
+  Start point   :  453
+  Moved to point:  454
+  : 23:0: 
+  :       ^
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -20,3 +20,4 @@
+     a=1;
+     %comment
+ end
++
+  #+end_src diff
+
+- Invoking      : (re-search-backward "^fun")
+  Start point   :  454
+  Moved to point:  402
+  : 19:0: function a=indent_xr_fun4
+  :       ^
+  No buffer modifications
+
+- Invoking      : (t-utils-xr-print-code (point) (point-max))
+  Start point   :  402
+  No point movement
+  standard-output:
+  #+begin_src matlab-ts
+function a=indent_xr_fun4
+    a=1;
+    %comment
+end
+
+  #+end_src
+  No buffer modifications

Reply via email to