branch: externals/matlab-mode commit 89a3fd857d04f28a053e6f52e2c6122ea2f938c8 Author: John Ciolfi <john.ciolfi...@gmail.com> Commit: John Ciolfi <john.ciolfi...@gmail.com>
matlab-ts-mode: improve M-q --- matlab-ts-mode.el | 50 +++- .../fill_paragraph_comments_expected.org | 14 +- .../fill_paragraph_indent_expected.org | 2 +- .../fill_paragraph_strings_and_ellipsis.m | 52 ++++ ...ill_paragraph_strings_and_ellipsis_expected.org | 297 +++++++++++++++++++++ .../fill_paragraph_test1_expected.org | 2 +- 6 files changed, 401 insertions(+), 16 deletions(-) diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el index 3cdef728a1..b0d50c0415 100644 --- a/matlab-ts-mode.el +++ b/matlab-ts-mode.el @@ -3647,6 +3647,48 @@ and this buffer is returned." (pop-to-buffer (current-buffer) 'other-window))) parse-errors-buf)) +;;; Our M-q matlab-ts-mode-prog-fill-reindent-defun + +(defun matlab-ts-mode-prog-fill-reindent-defun (&optional justify) + "MATLAB refill or reindent the paragraph or defun at point. + +If the point is in a string or a comment, fill the paragraph that +contains point or follows point. Optional JUSTIFY is passed +to `fill-paragraph'. + +Otherwise, reindent the function definition that contains point +or follows point. + +This exists because ellipsis line continuations cause +`prog-fill-reindent-defun' to not behave well. Thus, we handle +these locally." + (interactive "P") + (save-excursion + (let ((treesit-text-node + (and (treesit-available-p) + (treesit-parser-list) + (let ((node (treesit-node-at (point)))) + (and (treesit-node-match-p node 'text t) + (not (equal (treesit-node-type node) "line_continuation"))))))) + (if (or treesit-text-node + ;; We can't fill strings because doing so introduces syntax errors + (let ((sp (syntax-ppss))) + (and (nth 8 sp) ;; string or comment + (not (nth 3 sp)))) ;; not string + (let ((comment-start-pt + (save-excursion + (when (and (re-search-forward "\\s-*\\s<" (line-end-position) t) + (not (equal (treesit-node-type (treesit-node-at (point))) + "line_continuation"))) + (point))))) + (when comment-start-pt + (goto-char comment-start-pt)))) + (fill-paragraph justify (region-active-p)) + (beginning-of-defun) + (let ((start (point))) + (end-of-defun) + (indent-region start (point) nil)))))) + ;;; Keymap (defvar-keymap matlab-ts-mode-map @@ -3654,6 +3696,7 @@ and this buffer is returned." :parent prog-mode-map ;; Editing commands + "M-q" #'matlab-ts-mode-prog-fill-reindent-defun "C-c C-j" #'matlab-justify-line ;; Integration with `matlab-shell' @@ -3944,13 +3987,6 @@ so configuration variables of that mode, do not affect this mode. ;; Activate MATLAB script ";; heading" matlab-sections-minor-mode if needed (matlab-sections-auto-enable-on-mfile-type-fcn (matlab-ts-mode--mfile-type)) - ;; TODO M-q on first line - ;; foobar = struct(... - ;; 'field1', 1, ... - ;; 'field1', 2); - ;; result in - ;; foobar = struct(... 'field1', 1, ... 'field1', 2); - ;; ;; TODO [future] Indent - complex for statement ;; function a = foo(inputArgument1) ;; for (idx = (a.b.getStartValue(((inputArgument1 + someOtherFunction(b)) * 2 - ... diff --git a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_comments_expected.org b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_comments_expected.org index 636943e8df..d02ffc28a3 100644 --- a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_comments_expected.org +++ b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_comments_expected.org @@ -27,7 +27,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 216 No point movement Buffer modified: @@ -67,7 +67,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 568 No point movement Buffer modified: @@ -106,7 +106,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 832 No point movement Buffer modified: @@ -144,7 +144,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 1066 No point movement Buffer modified: @@ -183,7 +183,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 1348 No point movement Buffer modified: @@ -213,7 +213,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 1650 No point movement Buffer modified: @@ -260,7 +260,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 2076 No point movement Buffer modified: diff --git a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_indent_expected.org b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_indent_expected.org index d9903985d8..bdf11b9602 100644 --- a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_indent_expected.org +++ b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_indent_expected.org @@ -16,7 +16,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 125 Moved to point: 128 : 5:12: if a > 2 diff --git a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_strings_and_ellipsis.m b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_strings_and_ellipsis.m new file mode 100644 index 0000000000..857624e912 --- /dev/null +++ b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_strings_and_ellipsis.m @@ -0,0 +1,52 @@ +% -*- matlab-ts -*- + +% Filling code with ellipsis and strings causes problems for prog-fill-reindent-defun +% so we created matlab-ts-mode-prog-fill-reindent-defun to test that M-q on these works. + +function [a, b, f2, f3, f4] = fill_paragraph_strings_and_ellipsis(c) + + % c1: (t-utils-xr "C-n" "C-a" "M-q") + a = c + ... + 2; + + % c2: (t-utils-xr "C-n" "C-a" "M-q") + a = a + 3; % foo bar goo + + b = foo1; + + f2 = foo2; + f3 = foo3; + f4 = foo4; +end + +function b = foo1 + + % c3: (t-utils-xr (re-search-forward "a long string") "M-q") + b = "a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string "; +end + +function foobar = foo2 + + % c4: (t-utils-xr "C-n" "C-a" "M-q") + foobar = struct(... + 'field1', 1, ... + 'field1', 2); +end + +function foobar = foo3 + + % c5: (t-utils-xr "C-n" "C-n" "C-a" "M-q") + foobar = struct(... + 'field1', 1, ... + 'field1', 2); +end + +function foobar = foo4 + + % c6: (t-utils-xr "C-n" "C-n" "C-n" "C-a" "M-q") + foobar = struct(... + 'field1', 1, ... + 'field1', 2); +end + +% c7: (t-utils-xr (t-utils-xr-print-code (point-min) (point-max))) diff --git a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_strings_and_ellipsis_expected.org b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_strings_and_ellipsis_expected.org new file mode 100644 index 0000000000..fb24b796f1 --- /dev/null +++ b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_strings_and_ellipsis_expected.org @@ -0,0 +1,297 @@ +#+startup: showall + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:8:10: + + c1: (t-utils-xr "C-n" "C-a" "M-q") + +- Invoking : "C-n" = next-line + Start point : 308 + Moved to point: 324 + : 9:15: a = c + ... + : ^ + No buffer modifications + +- Invoking : "C-a" = move-beginning-of-line + Start point : 324 + Moved to point: 309 + : 9:0: a = c + ... + : ^ + No buffer modifications + +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun + Start point : 309 + No point movement + Buffer modified: + #+begin_src diff +--- start_contents ++++ end_contents +@@ -7,7 +7,7 @@ + + % c1: (t-utils-xr "C-n" "C-a" "M-q") + a = c + ... +- 2; ++ 2; + + % c2: (t-utils-xr "C-n" "C-a" "M-q") + a = a + 3; % foo bar goo + #+end_src diff + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:12:10: + + c2: (t-utils-xr "C-n" "C-a" "M-q") + +- Invoking : "C-n" = next-line + Start point : 377 + Moved to point: 410 + : 13:32: a = a + 3; % foo bar goo + : ^ + No buffer modifications + +- Invoking : "C-a" = move-beginning-of-line + Start point : 410 + Moved to point: 378 + : 13:0: a = a + 3; % foo bar goo + : ^ + No buffer modifications + +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun + Start point : 378 + No point movement + Buffer modified: + #+begin_src diff +--- start_contents ++++ end_contents +@@ -10,7 +10,7 @@ + 2; + + % c2: (t-utils-xr "C-n" "C-a" "M-q") +- a = a + 3; % foo bar goo ++ a = a + 3; % foo bar goo + + b = foo1; + + #+end_src diff + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:24:10: + + c3: (t-utils-xr (re-search-forward "a long string") "M-q") + +- Invoking : (re-search-forward "a long string") + Start point : 557 + Moved to point: 587 + : 25:29: b = "a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string "; + : ^ + No buffer modifications + +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun + Start point : 587 + Moved to point: 580 + : 25:22: b = "a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string "; + : ^ + Buffer modified: + #+begin_src diff +--- start_contents ++++ end_contents +@@ -22,7 +22,7 @@ + function b = foo1 + + % c3: (t-utils-xr (re-search-forward "a long string") "M-q") +- b = "a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string "; ++ b = "a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string "; + end + + function foobar = foo2 + #+end_src diff + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:30:10: + + c4: (t-utils-xr "C-n" "C-a" "M-q") + +- Invoking : "C-n" = next-line + Start point : 835 + Moved to point: 859 + : 31:23: foobar = struct(... + : ^ + No buffer modifications + +- Invoking : "C-a" = move-beginning-of-line + Start point : 859 + Moved to point: 836 + : 31:0: foobar = struct(... + : ^ + No buffer modifications + +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun + Start point : 836 + No point movement + Buffer modified: + #+begin_src diff +--- start_contents ++++ end_contents +@@ -30,7 +30,7 @@ + % c4: (t-utils-xr "C-n" "C-a" "M-q") + foobar = struct(... + 'field1', 1, ... +- 'field1', 2); ++ 'field1', 2); + end + + function foobar = foo3 + #+end_src diff + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:38:10: + + c5: (t-utils-xr "C-n" "C-n" "C-a" "M-q") + +- Invoking : "C-n" = next-line + Start point : 982 + Moved to point: 1006 + : 39:23: foobar = struct(... + : ^ + No buffer modifications + +- Invoking : "C-n" = next-line + Start point : 1006 + Moved to point: 1030 + : 40:23: 'field1', 1, ... + : ^ + No buffer modifications + +- Invoking : "C-a" = move-beginning-of-line + Start point : 1030 + Moved to point: 1007 + : 40:0: 'field1', 1, ... + : ^ + No buffer modifications + +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun + Start point : 1007 + No point movement + Buffer modified: + #+begin_src diff +--- start_contents ++++ end_contents +@@ -38,7 +38,7 @@ + % c5: (t-utils-xr "C-n" "C-n" "C-a" "M-q") + foobar = struct(... + 'field1', 1, ... +- 'field1', 2); ++ 'field1', 2); + end + + function foobar = foo4 + #+end_src diff + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:46:10: + + c6: (t-utils-xr "C-n" "C-n" "C-n" "C-a" "M-q") + +- Invoking : "C-n" = next-line + Start point : 1135 + Moved to point: 1159 + : 47:23: foobar = struct(... + : ^ + No buffer modifications + +- Invoking : "C-n" = next-line + Start point : 1159 + Moved to point: 1183 + : 48:23: 'field1', 1, ... + : ^ + No buffer modifications + +- Invoking : "C-n" = next-line + Start point : 1183 + Moved to point: 1208 + : 49:23: 'field1', 2); + : ^ + No buffer modifications + +- Invoking : "C-a" = move-beginning-of-line + Start point : 1208 + Moved to point: 1185 + : 49:0: 'field1', 2); + : ^ + No buffer modifications + +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun + Start point : 1185 + No point movement + Buffer modified: + #+begin_src diff +--- start_contents ++++ end_contents +@@ -46,7 +46,7 @@ + % c6: (t-utils-xr "C-n" "C-n" "C-n" "C-a" "M-q") + foobar = struct(... + 'field1', 1, ... +- 'field1', 2); ++ 'field1', 2); + end + + % c7: (t-utils-xr (t-utils-xr-print-code (point-min) (point-max))) + #+end_src diff + +* Executing commands from fill_paragraph_strings_and_ellipsis.m:52:6: + + c7: (t-utils-xr (t-utils-xr-print-code (point-min) (point-max))) + +- Invoking : (t-utils-xr-print-code (point-min) (point-max)) + Start point : 1278 + No point movement + standard-output: + #+begin_src matlab-ts +% -*- matlab-ts -*- + +% Filling code with ellipsis and strings causes problems for prog-fill-reindent-defun +% so we created matlab-ts-mode-prog-fill-reindent-defun to test that M-q on these works. + +function [a, b, f2, f3, f4] = fill_paragraph_strings_and_ellipsis(c) + + % c1: (t-utils-xr \"C-n\" \"C-a\" \"M-q\") + a = c + ... + 2; + + % c2: (t-utils-xr \"C-n\" \"C-a\" \"M-q\") + a = a + 3; % foo bar goo + + b = foo1; + + f2 = foo2; + f3 = foo3; + f4 = foo4; +end + +function b = foo1 + + % c3: (t-utils-xr (re-search-forward \"a long string\") \"M-q\") + b = \"a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string a long string \"; +end + +function foobar = foo2 + + % c4: (t-utils-xr \"C-n\" \"C-a\" \"M-q\") + foobar = struct(... + 'field1', 1, ... + 'field1', 2); +end + +function foobar = foo3 + + % c5: (t-utils-xr \"C-n\" \"C-n\" \"C-a\" \"M-q\") + foobar = struct(... + 'field1', 1, ... + 'field1', 2); +end + +function foobar = foo4 + + % c6: (t-utils-xr \"C-n\" \"C-n\" \"C-n\" \"C-a\" \"M-q\") + foobar = struct(... + 'field1', 1, ... + 'field1', 2); +end + +% c7: (t-utils-xr (t-utils-xr-print-code (point-min) (point-max))) + #+end_src + No buffer modifications diff --git a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_test1_expected.org b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_test1_expected.org index df56621ff3..8d0314cf46 100644 --- a/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_test1_expected.org +++ b/tests/test-matlab-ts-mode-fill-paragraph-files/fill_paragraph_test1_expected.org @@ -27,7 +27,7 @@ : ^ No buffer modifications -- Invoking : "M-q" = prog-fill-reindent-defun +- Invoking : "M-q" = matlab-ts-mode-prog-fill-reindent-defun Start point : 240 No point movement Buffer modified: