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

    matlab-ts-mode: fix "Mismatched parentheses"
    
    If we use the treesit-forward-sexp function, it doesn't match
    paren's correctly. We were seeing a "Mismatched parentheses"
    message in this case:
    
      Start with:
          function a=foo
              mat = [[1, 2]]
      type closing "]" and we get "Mismatched parentheses" which is coming from
      blink-matching-open
      sets start to (forward-sexp -1) which causes
      blink-matching-check-mismatch
      to look at wrong start point.
---
 contributing/treesit-mode-how-to.org               |  40 +++-
 matlab-ts-mode.el                                  |  35 +--
 .../thing_forward_sexp1.m                          |  17 ++
 .../thing_forward_sexp1_expected.org               | 251 +++++++++++++++++++++
 4 files changed, 322 insertions(+), 21 deletions(-)

diff --git a/contributing/treesit-mode-how-to.org 
b/contributing/treesit-mode-how-to.org
index dfcf6e6fa7..46b1c51b51 100644
--- a/contributing/treesit-mode-how-to.org
+++ b/contributing/treesit-mode-how-to.org
@@ -1242,15 +1242,47 @@ s-expressions, but you cannot do that. Similar for 
forward-list, backward-list.
   C-M-p   Move backward to the previous list expression in the same level, 
backward-list
 #+end_example
 
-TODO - explain you'll need to correct forward-sexp when in comments
+** Setup: configure forward-sexp-function after treesit-major-mode-setup
+
+It is likely that the treesit-forward-sexp won't do the right thing in 
comments or with parenthesis,
+brackets, and braces, so for these we should just use 
=forward-sexp-default-function= for those.
+To do this, we define
+
+#+begin_src emacs-lisp
+(defun LANGUAGE-ts-mode--forward-sexp (&optional arg)
+  "Use `treesit-forward-sexp' when matching code only.
+ARG is described in the docstring of `forward-sexp'.  When we are
+matching a parenthesis, bracket, brace, or when point is in a comment do
+the normal s-expression movement by calling
+`forward-sexp-default-function'."
+  (interactive "^p")
+  (let* ((move-back (and (numberp arg) (< arg 0)))
+         (match-paren (if move-back
+                          (member (char-before) '(?\] ?\) ?\}))
+                        (member (char-after) '(?\[ ?\( ?\{)))))
+    (if (or match-paren
+            (let* ((pt-and-node (LANGUAGE-ts-mode--real-node-at-point))
+                   (node (cdr pt-and-node)))
+              (equal (treesit-node-type node) "comment")))
+        ;; See 
tests/test-LANGUAGE-ts-mode-thing-settings-files/thing_forward_sexp1.m
+        (forward-sexp-default-function arg)
+      (treesit-forward-sexp arg))))
+#+end_src
+
+and then re-set =forward-sexp-function= after =(treesit-major-mode-setup)=:
 
 #+begin_src emacs-lisp
+  (when (treesit-ready-p 'matlab)
+    (treesit-parser-create 'matlab)
+
+    ;; <snip>
+
     (treesit-major-mode-setup)
 
-    ;; Correct forward-sexp setup created by `treesit-major-mode' so that in 
comments we do normal
-    ;; s-expression matching using parenthesis. This fix is need for our tests 
were we need
-    ;; to evaluate (t-utils-NAME ....) expressions from within comments using 
C-x C-e.
+    ;; Correct forward-sexp setup created by `treesit-major-mode' so that for 
parenthesis, brackets,
+    ;; braces, and comments we do normal s-expression matching using 
parenthesis.
     (setq-local forward-sexp-function #'LANGUAGE-ts-mode--forward-sexp)
+    ))
 #+end_src
 
 ** Test: treesit-thing-settings
diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 483b70a017..40a4aa3084 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -1591,14 +1591,21 @@ incomplete statements where NODE is nil and PARENT is 
line_continuation."
   "Tree-sitter things for movement.")
 
 (defun matlab-ts-mode--forward-sexp (&optional arg)
-  "Use `treesit-forward-sexp' when not in comments.
-ARG is described in the docstring of `forward-sexp-function'.
-When in comments do the normal parenthesis s-expression movement
-by calling `forward-sexp-default-function'."
+  "Use `treesit-forward-sexp' when matching code only.
+ARG is described in the docstring of `forward-sexp'.  When we are
+matching a parenthesis, bracket, brace, or when point is in a comment do
+the normal s-expression movement by calling
+`forward-sexp-default-function'."
   (interactive "^p")
-  (let* ((pt-and-node (matlab-ts-mode--real-node-at-point))
-         (node (cdr pt-and-node)))
-    (if (equal (treesit-node-type node) "comment")
+  (let* ((move-back (and (numberp arg) (< arg 0)))
+         (match-paren (if move-back
+                          (member (char-before) '(?\] ?\) ?\}))
+                        (member (char-after) '(?\[ ?\( ?\{)))))
+    (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")))
+        ;; See 
tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1.m
         (forward-sexp-default-function arg)
       (treesit-forward-sexp arg))))
 
@@ -2095,21 +2102,15 @@ is t, add the following to an Init File (e.g. 
`user-init-file' or
     ;;      end
     ;;      See: https://github.com/acristoffers/tree-sitter-matlab/issues/48
     ;;
-    ;; TODO Mismatched parentheses
-    ;;      Start with:
-    ;;        Line1:  mat = [ [1, 2]; [3, 4];
-    ;;        Line2:
-    ;;      then type a ']' on the 2nd line, in echo area, see: Mismatched 
parentheses
-    ;;
     ;; TODO create defcustom matlab-ts-mode-electric-ends that inserts end 
statements
     ;;      when a function, switch, while, for, etc. is entered. This should 
handle continuations.
 
     (treesit-major-mode-setup)
 
-    ;; Correct forward-sexp setup created by `treesit-major-mode' so that in 
comments we do normal
-    ;; s-expression matching using parenthesis. This fix is need for our tests 
work. We need
-    ;; to evaluate (t-utils-NAME ....) expressions from within comments using 
C-x C-e and this
-    ;; leverages forward-sexp to match up the parentheses.
+    ;; Correct forward-sexp setup created by `treesit-major-mode' so that for 
parenthesis, brackets,
+    ;; braces, and comments we do normal s-expression matching using 
parenthesis. This fix is need
+    ;; for our tests work. We need to evaluate (t-utils-NAME ....) expressions 
from within comments
+    ;; using C-x C-e and this leverages forward-sexp to match up the 
parentheses.
     (setq-local forward-sexp-function #'matlab-ts-mode--forward-sexp)
     ))
 
diff --git 
a/tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1.m 
b/tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1.m
new file mode 100644
index 0000000000..01dce39252
--- /dev/null
+++ b/tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1.m
@@ -0,0 +1,17 @@
+% -*- matlab-ts -*-
+function mat = thing_forward_sexp1
+    % (t-utils-xr "C-n" "C-n" "C-e" "C-b" (forward-sexp -1) (forward-sexp) 
"C-b" (forward-sexp -1) (forward-sexp))
+    m = [[1, 2];
+           [3, 4]];
+
+    % (t-utils-xr "C-n" "C-n" "C-e" "C-b" (forward-sexp -1) (forward-sexp) 
"C-b" (forward-sexp -1) (forward-sexp))
+    c = {{1, 2};
+         {3, 4}};
+    
+    % (t-utils-xr "C-n" "C-n" "C-e" "C-b" (forward-sexp -1) (forward-sexp) 
"C-b" (forward-sexp -1) (forward-sexp))
+    v = ((1+2) * ...
+         (3+4));
+
+    % (t-utils-xr "C-n" "C-e" (forward-sexp -1) (forward-sexp) "C-b" 
(forward-sexp -1) (forward-sexp))
+    %  Comment with paren's ((1+2)*(3+4))
+end
diff --git 
a/tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1_expected.org
 
b/tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1_expected.org
new file mode 100644
index 0000000000..c77675e7bf
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-thing-settings-files/thing_forward_sexp1_expected.org
@@ -0,0 +1,251 @@
+#+startup: showall
+
+* Executing commands from thing_forward_sexp1.m:3:6:
+
+  (t-utils-xr "C-n" "C-n" "C-e" "C-b" (forward-sexp -1) (forward-sexp) "C-b" 
(forward-sexp -1) (forward-sexp))
+
+- Invoking      : "C-n" = next-line
+  Start point   :  170
+  Moved to point:  187
+  : 4:16:     m = [[1, 2];
+  :                       ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  187
+  Moved to point:  204
+  : 5:16:            [3, 4]];
+  :                       ^
+  No buffer modifications
+
+- Invoking      : "C-e" = move-end-of-line
+  Start point   :  204
+  Moved to point:  207
+  : 5:19:            [3, 4]];
+  :                          ^
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  207
+  Moved to point:  206
+  : 5:18:            [3, 4]];
+  :                         ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  206
+  Moved to point:  179
+  : 4:8:     m = [[1, 2];
+  :              ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  179
+  Moved to point:  206
+  : 5:18:            [3, 4]];
+  :                         ^
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  206
+  Moved to point:  205
+  : 5:17:            [3, 4]];
+  :                        ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  205
+  Moved to point:  199
+  : 5:11:            [3, 4]];
+  :                  ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  199
+  Moved to point:  205
+  : 5:17:            [3, 4]];
+  :                        ^
+  No buffer modifications
+
+* Executing commands from thing_forward_sexp1.m:7:6:
+
+  (t-utils-xr "C-n" "C-n" "C-e" "C-b" (forward-sexp -1) (forward-sexp) "C-b" 
(forward-sexp -1) (forward-sexp))
+
+- Invoking      : "C-n" = next-line
+  Start point   :  323
+  Moved to point:  340
+  : 8:16:     c = {{1, 2};
+  :                       ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  340
+  Moved to point:  357
+  : 9:16:          {3, 4}};
+  :                       ^
+  No buffer modifications
+
+- Invoking      : "C-e" = move-end-of-line
+  Start point   :  357
+  Moved to point:  358
+  : 9:17:          {3, 4}};
+  :                        ^
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  358
+  Moved to point:  357
+  : 9:16:          {3, 4}};
+  :                       ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  357
+  Moved to point:  332
+  : 8:8:     c = {{1, 2};
+  :              ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  332
+  Moved to point:  357
+  : 9:16:          {3, 4}};
+  :                       ^
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  357
+  Moved to point:  356
+  : 9:15:          {3, 4}};
+  :                      ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  356
+  Moved to point:  350
+  : 9:9:          {3, 4}};
+  :               ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  350
+  Moved to point:  356
+  : 9:15:          {3, 4}};
+  :                      ^
+  No buffer modifications
+
+* Executing commands from thing_forward_sexp1.m:11:6:
+
+  (t-utils-xr "C-n" "C-n" "C-e" "C-b" (forward-sexp -1) (forward-sexp) "C-b" 
(forward-sexp -1) (forward-sexp))
+
+- Invoking      : "C-n" = next-line
+  Start point   :  478
+  Moved to point:  499
+  : 12:20:     v = ((1+2) * ...
+  :                            ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  499
+  Moved to point:  516
+  : 13:16:          (3+4));
+  :                        ^
+  No buffer modifications
+
+- Invoking      : "C-e" = move-end-of-line
+  Start point   :  516
+  No point movement
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  516
+  Moved to point:  515
+  : 13:15:          (3+4));
+  :                       ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  515
+  Moved to point:  487
+  : 12:8:     v = ((1+2) * ...
+  :               ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  487
+  Moved to point:  515
+  : 13:15:          (3+4));
+  :                       ^
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  515
+  Moved to point:  514
+  : 13:14:          (3+4));
+  :                      ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  514
+  Moved to point:  509
+  : 13:9:          (3+4));
+  :                ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  509
+  Moved to point:  514
+  : 13:14:          (3+4));
+  :                      ^
+  No buffer modifications
+
+* Executing commands from thing_forward_sexp1.m:15:6:
+
+  (t-utils-xr "C-n" "C-e" (forward-sexp -1) (forward-sexp) "C-b" (forward-sexp 
-1) (forward-sexp))
+
+- Invoking      : "C-n" = next-line
+  Start point   :  620
+  Moved to point:  662
+  : 16:41:     %  Comment with paren's ((1+2)*(3+4))
+  :                                                 ^
+  No buffer modifications
+
+- Invoking      : "C-e" = move-end-of-line
+  Start point   :  662
+  No point movement
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  662
+  Moved to point:  649
+  : 16:28:     %  Comment with paren's ((1+2)*(3+4))
+  :                                    ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  649
+  Moved to point:  662
+  : 16:41:     %  Comment with paren's ((1+2)*(3+4))
+  :                                                 ^
+  No buffer modifications
+
+- Invoking      : "C-b" = backward-char
+  Start point   :  662
+  Moved to point:  661
+  : 16:40:     %  Comment with paren's ((1+2)*(3+4))
+  :                                                ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp -1)
+  Start point   :  661
+  Moved to point:  656
+  : 16:35:     %  Comment with paren's ((1+2)*(3+4))
+  :                                           ^
+  No buffer modifications
+
+- Invoking      : (forward-sexp)
+  Start point   :  656
+  Moved to point:  661
+  : 16:40:     %  Comment with paren's ((1+2)*(3+4))
+  :                                                ^
+  No buffer modifications

Reply via email to