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

    matlab-ts-mode: improve C-M-f, C-M-b when not at start/end of node
---
 matlab-ts-mode.el                                  | 30 +++++++---
 .../thing_fun_sexp.m                               |  7 +++
 .../thing_fun_sexp_expected.org                    | 65 ++++++++++++++++++++++
 3 files changed, 93 insertions(+), 9 deletions(-)

diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 7f049ec8be..3cdef728a1 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -2933,13 +2933,30 @@ the normal s-expression movement by calling
   (let* ((move-back (and (numberp arg) (< arg 0)))
          (match-paren (if move-back
                           (member (char-before) '(?\] ?\) ?\}))
-                        (member (char-after) '(?\[ ?\( ?\{)))))
+                        (member (char-after) '(?\[ ?\( ?\{))))
+         node)
     (if (or match-paren
-            (let* ((pt-and-node (matlab-ts-mode--real-node-at-point))
-                   (node (cdr pt-and-node)))
-              (equal (treesit-node-type node) "comment")))
+            (equal (setq node (let ((pt-and-node 
(matlab-ts-mode--real-node-at-point)))
+                                (cdr pt-and-node)))
+                   "comment"))
         ;; See 
tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1.m
         (forward-sexp-default-function arg)
+
+      ;; Else ask treesit to do the movement.
+      ;; Note, treesit doesn't behave well when point is not at the end or 
start of the node,
+      ;; so fix that. Consider
+      ;;     function foo
+      ;;         ^          <== point here and C-M-f
+      ;;     end
+      ;;      ^             <== point here and C-M-b
+      ;; See: tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp.m
+      (when (and node
+                 (>= (point) (treesit-node-start node))
+                 (< (point) (treesit-node-end node)))
+        (if move-back
+            (when (string= (treesit-node-type node) "end")
+              (goto-char (treesit-node-end node)))
+          (goto-char (treesit-node-start node))))
       (treesit-forward-sexp arg))))
 
 ;;; Change Log
@@ -3639,11 +3656,6 @@ and this buffer is returned."
   ;; Editing commands
   "C-c C-j" #'matlab-justify-line
 
-  ;; TODO - add these?
-  ;; Navigation commands
-  ;; (define-key km [(meta a)] 'matlab-beginning-of-command)
-  ;; (define-key km [(meta e)] 'matlab-end-of-command)
-
   ;; Integration with `matlab-shell'
   "C-c C-s" 'matlab-shell-save-and-go
   "C-c C-r" 'matlab-shell-run-region
diff --git a/tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp.m 
b/tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp.m
new file mode 100644
index 0000000000..13399e36aa
--- /dev/null
+++ b/tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp.m
@@ -0,0 +1,7 @@
+% -*- matlab-ts -*-
+
+% Case1: (t-utils-xr "C-a" "C-n" "C-f" "C-M-f")
+function a = thing_fun_sexp(b)
+    a = foo2(b) + 1;
+    % Case2: (t-utils-xr "C-a" "C-n" "C-f" "C-M-b")
+end
diff --git 
a/tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp_expected.org 
b/tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp_expected.org
new file mode 100644
index 0000000000..3d705fb7bb
--- /dev/null
+++ b/tests/test-matlab-ts-mode-thing-settings-files/thing_fun_sexp_expected.org
@@ -0,0 +1,65 @@
+#+startup: showall
+
+* Executing commands from thing_fun_sexp.m:3:9:
+
+  Case1: (t-utils-xr "C-a" "C-n" "C-f" "C-M-f")
+
+- Invoking      : "C-a" = move-beginning-of-line
+  Start point   :   69
+  Moved to point:   22
+  : 3:0: % Case1: (t-utils-xr "C-a" "C-n" "C-f" "C-M-f")
+  :      ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :   22
+  Moved to point:   70
+  : 4:0: function a = thing_fun_sexp(b)
+  :      ^
+  No buffer modifications
+
+- Invoking      : "C-f" = forward-char
+  Start point   :   70
+  Moved to point:   71
+  : 4:1: function a = thing_fun_sexp(b)
+  :       ^
+  No buffer modifications
+
+- Invoking      : "C-M-f" = forward-sexp
+  Start point   :   71
+  Moved to point:  177
+  : 7:3: end
+  :         ^
+  No buffer modifications
+
+* Executing commands from thing_fun_sexp.m:6:13:
+
+  Case2: (t-utils-xr "C-a" "C-n" "C-f" "C-M-b")
+
+- Invoking      : "C-a" = move-beginning-of-line
+  Start point   :  173
+  Moved to point:  122
+  : 6:0:     % Case2: (t-utils-xr "C-a" "C-n" "C-f" "C-M-b")
+  :      ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  122
+  Moved to point:  174
+  : 7:0: end
+  :      ^
+  No buffer modifications
+
+- Invoking      : "C-f" = forward-char
+  Start point   :  174
+  Moved to point:  175
+  : 7:1: end
+  :       ^
+  No buffer modifications
+
+- Invoking      : "C-M-b" = backward-sexp
+  Start point   :  175
+  Moved to point:   70
+  : 4:0: function a = thing_fun_sexp(b)
+  :      ^
+  No buffer modifications

Reply via email to