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

    matlab-ts-mode: move electric indent region code to matlab-ts-mode--ei.el
---
 matlab-ts-mode--ei.el | 46 +++++++++++++++++++++++++++++++++++++-
 matlab-ts-mode.el     | 62 ++++++++++++---------------------------------------
 2 files changed, 59 insertions(+), 49 deletions(-)

diff --git a/matlab-ts-mode--ei.el b/matlab-ts-mode--ei.el
index 3dac2629b1..049e807a51 100644
--- a/matlab-ts-mode--ei.el
+++ b/matlab-ts-mode--ei.el
@@ -1201,7 +1201,7 @@ TAB>  x = 123 ./1 + 567
     (goto-char line-pt)))
 
 (cl-defun matlab-ts-mode--ei-indent-elements-in-line (&optional 
is-indent-region)
-  "Indent current line by adjust spacing around elements.
+  "Indent current line by adjusting spacing around elements.
 When IS-INDENT-REGION is nil, we restore the point to it's logical
 location when the line is updated.  Returns t if line was updated."
 
@@ -1232,6 +1232,50 @@ location when the line is updated.  Returns t if line 
was updated."
         ;; result
         updated))))
 
+(defun matlab-ts-mode--ei-indent-region (beg end)
+  "Indent BEG END region by adjusting spacing around elements."
+  (let* ((start-linenum (line-number-at-pos beg))
+         (curr-linenum start-linenum)
+         (end-linenum (save-excursion
+                        (goto-char end)
+                        (let ((linenum (line-number-at-pos)))
+                          (when (= (point) (line-beginning-position))
+                            (setq linenum (1- linenum)))
+                          linenum))))
+
+    (matlab-ts-mode--ei-workaround-143 beg end)
+
+    (unwind-protect
+        (progn
+          ;; Add an invalid entry to each of the following associative lists. 
This entry is used as
+          ;; a marker to activate caching. Each entry in the lists is a cons 
cell `(LINENUM . INFO)
+          ;; where -1 is not a valid line number.
+          (setq-local matlab-ts-mode--ei-align-assign-alist '((-1 . 0))
+                      matlab-ts-mode--ei-align-prop-alist '((-1 . 0))
+                      matlab-ts-mode--ei-align-comment-alist '((-1 . 0))
+                      matlab-ts-mode--ei-align-matrix-alist '((-1 . "")))
+
+          (save-excursion
+            (goto-char beg)
+              (while (<= curr-linenum end-linenum)
+                (beginning-of-line)
+                (matlab-ts-mode--ei-indent-elements-in-line 'indent-region)
+                (forward-line)
+                (setq curr-linenum (1+ curr-linenum)))
+              ;; Restore point accounting for whitespace adjustments in the 
lines
+              (goto-char (point-min))
+              (forward-line (1- start-linenum))
+              (setq beg (point))
+              (goto-char (point-min))
+              (forward-line end-linenum)
+              (setq end (point))))
+        (setq-local matlab-ts-mode--ei-align-assign-alist nil
+                    matlab-ts-mode--ei-align-prop-alist nil
+                    matlab-ts-mode--ei-align-comment-alist nil
+                    matlab-ts-mode--ei-align-matrix-alist nil)))
+  ;; Return updated BEG and END region points
+  (cons beg end))
+
 (provide 'matlab-ts-mode--ei)
 ;;; matlab-ts-mode--ei.el ends here
 
diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 4d66055736..1930afe338 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -2890,55 +2890,21 @@ Example:
   "Call `treesit-indent-region' on BEG END, then do electric indent."
   ;; `treesit-indent-region' will not alter the number of lines, but it may 
reduce the buffer size,
   ;; thus grab the start/end lines for 
`matlab-ts-mode--ei-indent-elements-in-line'.
-  (when matlab-ts-mode-electric-indent
-    (let* ((start-linenum (line-number-at-pos beg))
-           (curr-linenum start-linenum)
-           (end-linenum (save-excursion
-                          (goto-char end)
-                          (let ((linenum (line-number-at-pos)))
-                            (when (= (point) (line-beginning-position))
-                              (setq linenum (1- linenum)))
-                            linenum))))
-
-      (matlab-ts-mode--ei-workaround-143 beg end)
-
-      ;; We need to run electric indent before treesit-indent-region. Consider
-      ;;    l2 = @(x)((ischar(x) || isstring(x) || isnumeric(x)) && ...
-      ;;                 ~strcmpi(x, 'fubar'));
-      ;; If we indent-region first, we'll get
-      ;;    l2 = @(x)((ischar(x) || isstring(x) || isnumeric(x)) && ...
-      ;;              ~strcmpi(x, 'fubar'));
-      ;; then when we adjust spacing, we'll have the following where the 2nd 
line is not
-      ;; indented correctly.
-      ;;    l2 = @(x) ((ischar(x) || isstring(x) || isnumeric(x)) && ...
-      ;;              ~strcmpi(x, 'fubar'));
-      (unwind-protect
-          (progn
-            ;; Add invalid entry to matlab-ts-mode--ei-align-assign-alist as a 
marker to activate
-            ;; caching of computed offsets for assignment alignment.
-            (setq-local matlab-ts-mode--ei-align-assign-alist '((-1 . 0))
-                        matlab-ts-mode--ei-align-prop-alist '((-1 . 0))
-                        matlab-ts-mode--ei-align-comment-alist '((-1 . 0))
-                        matlab-ts-mode--ei-align-matrix-alist '((-1 . "")))
 
-            (save-excursion
-              (goto-char beg)
-              (while (<= curr-linenum end-linenum)
-                (beginning-of-line)
-                (matlab-ts-mode--ei-indent-elements-in-line 'indent-region)
-                (forward-line)
-                (setq curr-linenum (1+ curr-linenum)))
-              ;; Restore point accounting for whitespace adjustments in the 
lines
-              (goto-char (point-min))
-              (forward-line (1- start-linenum))
-              (setq beg (point))
-              (goto-char (point-min))
-              (forward-line end-linenum)
-              (setq end (point))))
-        (setq-local matlab-ts-mode--ei-align-assign-alist nil
-                    matlab-ts-mode--ei-align-prop-alist nil
-                    matlab-ts-mode--ei-align-comment-alist nil
-                    matlab-ts-mode--ei-align-matrix-alist nil))))
+  (when matlab-ts-mode-electric-indent
+    ;; We need to run electric indent before treesit-indent-region. Consider
+    ;;    l2 = @(x)((ischar(x) || isstring(x) || isnumeric(x)) && ...
+    ;;                 ~strcmpi(x, 'fubar'));
+    ;; If we indent-region first, we'll get
+    ;;    l2 = @(x)((ischar(x) || isstring(x) || isnumeric(x)) && ...
+    ;;              ~strcmpi(x, 'fubar'));
+    ;; then when we adjust spacing, we'll have the following where the 2nd 
line is not
+    ;; indented correctly.
+    ;;    l2 = @(x) ((ischar(x) || isstring(x) || isnumeric(x)) && ...
+    ;;              ~strcmpi(x, 'fubar'));
+    (let ((pair (matlab-ts-mode--ei-indent-region beg end)))
+      (setq beg (car pair)
+            end (cdr pair))))
 
   (treesit-indent-region beg end))
 

Reply via email to