branch: externals/matlab-mode commit 1de43de2ebad5444efc9dd34e6be7896be65645f Author: John Ciolfi <john.ciolfi...@gmail.com> Commit: John Ciolfi <john.ciolfi...@gmail.com>
matlab-ts-mode: fix indent of spmd --- matlab-ts-mode.el | 146 ++++++++++++--------- .../test-matlab-ts-mode-indent-files/indent_spmd.m | 19 +++ .../indent_spmd_expected.m | 19 +++ .../indent_xr_statement_body_expected.org | 119 ++++++++--------- 4 files changed, 175 insertions(+), 128 deletions(-) diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el index 03dab72a2a..948bee9c90 100644 --- a/matlab-ts-mode.el +++ b/matlab-ts-mode.el @@ -1760,7 +1760,8 @@ Sets `matlab-ts-mode--i-next-line-pair' to (ANCHOR-NODE . OFFSET)" "(" "[" "{" - "row") + "row" + "spmd") eos))) (defun matlab-ts-mode--i-next-line-matcher-comment (node) @@ -1825,6 +1826,80 @@ Sets `matlab-ts-mode--i-next-line-pair' to (ANCHOR-NODE . OFFSET)" (when (equal (treesit-node-type prev-sibling) "ERROR") (treesit-node-child prev-sibling -1))))) +(defun matlab-ts-mode--i-next-line-prev-sibling-to-check (last-child-of-error-node node-to-check) + "Locate the prev-sibling-to-check for `matlab-ts-mode--i-next-line-matcher'. +For LAST-CHILD-OF-ERROR-NODE and NODE-TO-CHECK see +`matlab-ts-mode--i-next-line-matcher'" + + (let (prev-sibling-to-check + prev-sibling-error-node + saw-close-paren + (prev-sibling (if last-child-of-error-node + last-child-of-error-node ;; is our "prev-sibling" + (treesit-node-prev-sibling node-to-check)))) + (while (and prev-sibling + (not prev-sibling-to-check) + (not prev-sibling-error-node)) + (let ((prev-sibling-type (treesit-node-type prev-sibling))) + + ;; Backup over "..." continuations because 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") + (let ((prev-sibling2 (treesit-node-prev-sibling prev-sibling))) + (or (not prev-sibling2) + (string= (treesit-node-type prev-sibling2) "ERROR")))) + (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-error-node prev-sibling)) + + ((string= prev-sibling-type ")") + (setq saw-close-paren t)) + + ((and (string= prev-sibling-type "(") + saw-close-paren) + (setq saw-close-paren nil)) + + ((and (string-match-p matlab-ts-mode--i-next-line-anchors-rx prev-sibling-type) + (or (not (string= prev-sibling-type "(")) + ;; See: test/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 + + ;; no function or similar item before paren: e.g. no longFunction( + (not (let* ((pp-s (treesit-node-prev-sibling prev-sibling)) + (pp-s-type (treesit-node-type pp-s))) + (and pp-s-type + (string-match-p (rx bos (or "identifier" "spmd") eos) pp-s-type)))) + + ;; OR we have something after the paren: e.g. (in1, ... + (not (equal (treesit-node-type + (treesit-node-next-sibling prev-sibling)) + "line_continuation")) + )) + (setq prev-sibling-to-check prev-sibling))) + + (setq prev-sibling (if prev-sibling-error-node + nil + (treesit-node-prev-sibling prev-sibling)))))) + + (cons prev-sibling-to-check prev-sibling-error-node))) + (cl-defun matlab-ts-mode--i-next-line-matcher (node parent bol &rest _) "Matcher for indent on a newline being inserted when in presence of errors. If so, set `matlab-ts-mode--i-next-line-pair'. @@ -1889,61 +1964,11 @@ Prev-siblings: (setq ancestor-to-check nil))) ;; prev-sibling-to-check - (let ((prev-sibling (if last-child-of-error-node - last-child-of-error-node ;; is our "prev-sibling" - (treesit-node-prev-sibling node-to-check)))) - (while (and prev-sibling - (not prev-sibling-to-check) - (not prev-sibling-error-node)) - (let ((prev-sibling-type (treesit-node-type prev-sibling))) - - ;; Backup over "..." continuations because 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") - (let ((prev-sibling2 (treesit-node-prev-sibling prev-sibling))) - (or (not prev-sibling2) - (string= (treesit-node-type prev-sibling2) "ERROR")))) - (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-error-node prev-sibling)) - - ((and (string-match-p matlab-ts-mode--i-next-line-anchors-rx prev-sibling-type) - (or (not (string= prev-sibling-type "(")) - ;; See: test/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 something after the paren: e.g. (in1, ... - (not (equal (treesit-node-type - (treesit-node-next-sibling prev-sibling)) - "line_continuation")) - )) - (setq prev-sibling-to-check prev-sibling))) - - (setq prev-sibling (if prev-sibling-error-node - nil - (treesit-node-prev-sibling prev-sibling))))))) + (let ((pair (matlab-ts-mode--i-next-line-prev-sibling-to-check + last-child-of-error-node + node-to-check))) + (setq prev-sibling-to-check (car pair) + prev-sibling-error-node (cdr pair))) ;; We use regular matching rules when there is NO error. (when (and (not ancestor-error-node) @@ -2333,6 +2358,10 @@ Example: ;; I-Rule: comments in classdef's ((parent-is ,(rx bos "class_definition" eos)) parent ,matlab-ts-mode--indent-level) + ;; I-Rule: comment within spmd + ;; See: tests/test-matlab-ts-mode-indent-files/indent_spmd.m + ((parent-is ,(rx bos "spmd")) parent ,matlab-ts-mode--indent-level) + ;; I-Rule: comment after property: ;; arguments ;; a @@ -3520,11 +3549,6 @@ so configuration variables of that mode, do not affect this mode. ;; Perhaps do this in matlab-ts-mode? ;; Add newline for sweep test if need? ;; - ;; TODO indent - ;; spmd - ;; %foo - ;; end - ;; ;; TODO [future] Improve semantic movement ;; thing-settings doesn't work well. Directly implement C-M-f, M-e, etc. ;; diff --git a/tests/test-matlab-ts-mode-indent-files/indent_spmd.m b/tests/test-matlab-ts-mode-indent-files/indent_spmd.m new file mode 100644 index 0000000000..1397675d6e --- /dev/null +++ b/tests/test-matlab-ts-mode-indent-files/indent_spmd.m @@ -0,0 +1,19 @@ +% -*- matlab-ts -*- + +spmd + % foo + q = magic(spmdIndex + 2); +end + +myClusterPool = parpool("myMJSCluster",15); + +spmdWorkers = myClusterPool.Workers(1:6); +[spmdPool,otherPool] = partition(myClusterPool,"Workers",spmdWorkers); + +fun = @(x) 4./(1 + x.^2); +spmd(spmdPool) + a = (spmdIndex - 1)/spmdSize; + b = spmdIndex/spmdSize; + myIntegral = integral(fun,a,b); + piApprox = spmdPlus(myIntegral); +end diff --git a/tests/test-matlab-ts-mode-indent-files/indent_spmd_expected.m b/tests/test-matlab-ts-mode-indent-files/indent_spmd_expected.m new file mode 100644 index 0000000000..1397675d6e --- /dev/null +++ b/tests/test-matlab-ts-mode-indent-files/indent_spmd_expected.m @@ -0,0 +1,19 @@ +% -*- matlab-ts -*- + +spmd + % foo + q = magic(spmdIndex + 2); +end + +myClusterPool = parpool("myMJSCluster",15); + +spmdWorkers = myClusterPool.Workers(1:6); +[spmdPool,otherPool] = partition(myClusterPool,"Workers",spmdWorkers); + +fun = @(x) 4./(1 + x.^2); +spmd(spmdPool) + a = (spmdIndex - 1)/spmdSize; + b = spmdIndex/spmdSize; + myIntegral = integral(fun,a,b); + piApprox = spmdPlus(myIntegral); +end diff --git a/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_statement_body_expected.org b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_statement_body_expected.org index 4fc1f580d1..1127ca5c22 100644 --- a/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_statement_body_expected.org +++ b/tests/test-matlab-ts-mode-indent-xr-files/indent_xr_statement_body_expected.org @@ -432,43 +432,28 @@ - Invoking : "C-i" = indent-for-tab-command Start point : 837 - Moved to point: 833 - : 30:18: %c - : ^ - Buffer modified: - #+begin_src diff ---- start_contents -+++ end_contents -@@ -27,7 +27,7 @@ - - % Case7: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - spmd -- %c -+ %c - end - - % Case8: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - #+end_src diff + No point movement + No buffer modifications * Executing commands from indent_xr_statement_body.m:33:25: Case8: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - Invoking : "C-n" = next-line - Start point : 930 - Moved to point: 956 + Start point : 934 + Moved to point: 960 : 34:25: elseif a == 2 : ^ No buffer modifications - Invoking : "C-e" = move-end-of-line - Start point : 956 + Start point : 960 No point movement No buffer modifications - Invoking : "C-m" = newline - Start point : 956 - Moved to point: 973 + Start point : 960 + Moved to point: 977 : 35:16: : ^ Buffer modified: @@ -486,8 +471,8 @@ #+end_src diff - Invoking : (insert "%c") - Start point : 973 - Moved to point: 975 + Start point : 977 + Moved to point: 979 : 35:18: %c : ^ Buffer modified: @@ -506,7 +491,7 @@ #+end_src diff - Invoking : "C-i" = indent-for-tab-command - Start point : 975 + Start point : 979 No point movement No buffer modifications @@ -515,20 +500,20 @@ Case9: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - Invoking : "C-n" = next-line - Start point : 1052 - Moved to point: 1069 + Start point : 1056 + Moved to point: 1073 : 38:16: else : ^ No buffer modifications - Invoking : "C-e" = move-end-of-line - Start point : 1069 + Start point : 1073 No point movement No buffer modifications - Invoking : "C-m" = newline - Start point : 1069 - Moved to point: 1086 + Start point : 1073 + Moved to point: 1090 : 39:16: : ^ Buffer modified: @@ -546,8 +531,8 @@ #+end_src diff - Invoking : (insert "%c") - Start point : 1086 - Moved to point: 1088 + Start point : 1090 + Moved to point: 1092 : 39:18: %c : ^ Buffer modified: @@ -566,7 +551,7 @@ #+end_src diff - Invoking : "C-i" = indent-for-tab-command - Start point : 1088 + Start point : 1092 No point movement No buffer modifications @@ -575,20 +560,20 @@ Case10: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - Invoking : "C-n" = next-line - Start point : 1195 - Moved to point: 1215 + Start point : 1199 + Moved to point: 1219 : 44:19: while a : ^ No buffer modifications - Invoking : "C-e" = move-end-of-line - Start point : 1215 + Start point : 1219 No point movement No buffer modifications - Invoking : "C-m" = newline - Start point : 1215 - Moved to point: 1232 + Start point : 1219 + Moved to point: 1236 : 45:16: : ^ Buffer modified: @@ -606,8 +591,8 @@ #+end_src diff - Invoking : (insert "%c") - Start point : 1232 - Moved to point: 1234 + Start point : 1236 + Moved to point: 1238 : 45:18: %c : ^ Buffer modified: @@ -626,7 +611,7 @@ #+end_src diff - Invoking : "C-i" = indent-for-tab-command - Start point : 1234 + Start point : 1238 No point movement No buffer modifications @@ -635,20 +620,20 @@ Case11: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - Invoking : "C-n" = next-line - Start point : 1324 - Moved to point: 1352 + Start point : 1328 + Moved to point: 1356 : 49:27: for fIdx = 1:10 : ^ No buffer modifications - Invoking : "C-e" = move-end-of-line - Start point : 1352 + Start point : 1356 No point movement No buffer modifications - Invoking : "C-m" = newline - Start point : 1352 - Moved to point: 1369 + Start point : 1356 + Moved to point: 1373 : 50:16: : ^ Buffer modified: @@ -666,8 +651,8 @@ #+end_src diff - Invoking : (insert "%c") - Start point : 1369 - Moved to point: 1371 + Start point : 1373 + Moved to point: 1375 : 50:18: %c : ^ Buffer modified: @@ -686,7 +671,7 @@ #+end_src diff - Invoking : "C-i" = indent-for-tab-command - Start point : 1371 + Start point : 1375 No point movement No buffer modifications @@ -695,20 +680,20 @@ Case12: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - Invoking : "C-n" = next-line - Start point : 1473 - Moved to point: 1484 + Start point : 1477 + Moved to point: 1488 : 56:10: events : ^ No buffer modifications - Invoking : "C-e" = move-end-of-line - Start point : 1484 + Start point : 1488 No point movement No buffer modifications - Invoking : "C-m" = newline - Start point : 1484 - Moved to point: 1493 + Start point : 1488 + Moved to point: 1497 : 57:8: : ^ Buffer modified: @@ -726,8 +711,8 @@ #+end_src diff - Invoking : (insert "%c") - Start point : 1493 - Moved to point: 1495 + Start point : 1497 + Moved to point: 1499 : 57:10: %c : ^ Buffer modified: @@ -746,7 +731,7 @@ #+end_src diff - Invoking : "C-i" = indent-for-tab-command - Start point : 1495 + Start point : 1499 No point movement No buffer modifications @@ -755,20 +740,20 @@ Case13: (t-utils-xr "C-n" "C-e" "C-m" (insert "%c") "C-i") - Invoking : "C-n" = next-line - Start point : 1569 - Moved to point: 1585 + Start point : 1573 + Moved to point: 1589 : 61:15: enumeration : ^ No buffer modifications - Invoking : "C-e" = move-end-of-line - Start point : 1585 + Start point : 1589 No point movement No buffer modifications - Invoking : "C-m" = newline - Start point : 1585 - Moved to point: 1594 + Start point : 1589 + Moved to point: 1598 : 62:8: : ^ Buffer modified: @@ -786,8 +771,8 @@ #+end_src diff - Invoking : (insert "%c") - Start point : 1594 - Moved to point: 1596 + Start point : 1598 + Moved to point: 1600 : 62:10: %c : ^ Buffer modified: @@ -806,7 +791,7 @@ #+end_src diff - Invoking : "C-i" = indent-for-tab-command - Start point : 1596 + Start point : 1600 No point movement No buffer modifications @@ -815,7 +800,7 @@ Case14: (t-utils-xr (re-search-backward "^classdef") (print (buffer-substring-no-properties (point) (point-max)))) - Invoking : (re-search-backward "^classdef") - Start point : 1738 + Start point : 1742 Moved to point: 82 : 4:0: classdef indent_xr_statement_body < handle : ^ @@ -852,7 +837,7 @@ classdef indent_xr_statement_body < handle % Case7: (t-utils-xr \"C-n\" \"C-e\" \"C-m\" (insert \"%c\") \"C-i\") spmd - %c + %c end % Case8: (t-utils-xr \"C-n\" \"C-e\" \"C-m\" (insert \"%c\") \"C-i\")