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

    matlab-ts-mode--ei: handle narrowed buffers
---
 matlab-ts-mode--ei.el                              | 133 ++++++++-------
 matlab-ts-mode.el                                  |   6 +-
 tests/t-utils.el                                   |  25 ++-
 .../electric_indent_xr_narrow_to_region.m          |  30 ++++
 ...lectric_indent_xr_narrow_to_region_expected.org | 187 +++++++++++++++++++++
 .../file_encoding_corrupt_expected.txt             |   2 +-
 .../file_encoding_mcr_expected.txt                 |   2 +-
 .../electric_indent_xr_narrow_to_region.m          |  30 ++++
 ...lectric_indent_xr_narrow_to_region_expected.txt |  17 ++
 9 files changed, 352 insertions(+), 80 deletions(-)

diff --git a/matlab-ts-mode--ei.el b/matlab-ts-mode--ei.el
index a93bf21f00..7c92eda8fc 100644
--- a/matlab-ts-mode--ei.el
+++ b/matlab-ts-mode--ei.el
@@ -42,20 +42,21 @@
 
 (defcustom matlab-ts-mode-electric-indent t
   "*If t, indent (format) language elements within code.
+
 - Canonicalize language element spacing
-     Example                        |  Result
+     Example                        |  Indent result
      -----------------------------  |  -----------------------------
      a=b+c   *d   ;                 |  a = b + c * d;
 
 - Align consecutive assignments
-     Example                        |  Result
+     Example                        |  Indent result
      -----------------------------  |  -----------------------------
      rLength      =12354    ;       |  rLength = 12354;
        rWidth=2;                    |  rWidth  = 2;
          rArea=rLength  *rWidth;    |  rArea   = rLength * rWidth;
 
 - Align properties and arguments
-     Example                        |  Result
+     Example                        |  Indent result
      -----------------------------  |  -----------------------------
      classdef c1                    |  classdef c1
          properties                 |      properties
@@ -66,19 +67,19 @@
      end                            |  end
 
 - Align trailing comments
-     Example                        |  Result
+     Example                        |  Indent result
      -----------------------------  |  -----------------------------
      a = myFcn1(1, 2); % comment 1  |  a = myFcn1(1, 2); % comment 1
      a = a + 5; % comment 2         |  a = a + 2;        % comment 2
 
 - Add missing commas in arrays (matrices and cells)
 
-     Example                        |  Result
+     Example                        |  Indent result
      -----------------------------  |  -----------------------------
      mat1 = [1234,234  12234.24];   |  mat1 = [1234, 234, 12234.24];
 
 - Align matrix columns
-     Example                        |  Result
+     Example                        |  Indent result
      -----------------------------  |  -----------------------------
      mat2 = [1234,234  12234.24     |  mat2 = [1234, 234, 12234.24
                         1,2 3.41];  |             1,   2,     3.41];
@@ -1450,11 +1451,7 @@ This expansion of the region is done to simplify 
electric indent."
          (curr-linenum start-linenum)
          (end-linenum (save-excursion
                         (goto-char end)
-                        (let ((inhibit-field-text-motion t)) (end-of-line))
-                        (let ((linenum (line-number-at-pos)))
-                          (when (= (point) (pos-bol))
-                            (setq linenum (1- linenum)))
-                          linenum)))
+                        (- (line-number-at-pos) (if (= (point) (pos-bol)) 1 
0))))
          (start-pt (point))
          (start-pt-linenum (line-number-at-pos start-pt))
          (start-pt-offset (- start-pt (save-excursion
@@ -1481,62 +1478,64 @@ This expansion of the region is done to simplify 
electric indent."
                       matlab-ts-mode--ei-orig-line-node-types-alist '((-1 . 
0)))
 
           (save-excursion
-
-            ;; Move END point to end of line.
-            ;; To do this, we use end-linenum, because workaround-143 could 
have moved END.
-            (goto-char (point-min))
-            (when (> end-linenum 1)
-              (forward-line (1- end-linenum)))
-            (let ((inhibit-field-text-motion t)) (end-of-line))
-            (setq end (point))
-
-            ;; Move BEG to beginning of line and leave point there.
-            (goto-char beg)
-            (forward-line 0)
-            (setq beg (point))
-
-            (setq new-content-buf (get-buffer-create
-                                   (generate-new-buffer-name " 
*temp-matlab-indent-region*")))
-            (let (region-updated)
-
-              (while (<= curr-linenum end-linenum)
-                (forward-line 0)
-
-                (let* ((tuple (matlab-ts-mode--ei-indent-elements-in-line
-                               'indent-region
-                               (when (= curr-linenum start-pt-linenum) 
start-pt-offset)))
-                       (new-line (nth 0 tuple))
-                       (line-updated (nth 1 tuple))
-                       (new-start-pt-offset (nth 2 tuple)))
-
-                  (when new-start-pt-offset
-                    (setq start-pt-offset new-start-pt-offset))
-
-                  (with-current-buffer new-content-buf
-                    (insert new-line)
-                    (when (< curr-linenum end-linenum)
-                      (insert "\n")))
-
-                  (when line-updated
-                    (setq region-updated t)))
-
-                (forward-line)
-                (setq curr-linenum (1+ curr-linenum)))
-
-              (when region-updated
-                (save-excursion
-                  (goto-char beg)
-                  (delete-region beg end)
-                  (insert (with-current-buffer new-content-buf
-                            (buffer-string)))
-
-                  (when matlab-ts-mode--indent-assert
-                    (matlab-ts-mode--ei-assert-line-nodes-match start-linenum 
end-linenum))
-
-                  ;; Restore end point accounting for whitespace adjustments 
in the lines
-                  (goto-char (point-min))
-                  (forward-line end-linenum)
-                  (setq end (point))))))
+            (save-restriction
+              (widen)
+
+              ;; Move END point to end of line.
+              ;; To do this, we use end-linenum, because workaround-143 could 
have moved END.
+              (goto-char (point-min))
+              (when (> end-linenum 1)
+                (forward-line (1- end-linenum)))
+              (let ((inhibit-field-text-motion t)) (end-of-line))
+              (setq end (point))
+
+              ;; Move BEG to beginning of line and leave point there.
+              (goto-char beg)
+              (forward-line 0)
+              (setq beg (point))
+
+              (setq new-content-buf (get-buffer-create
+                                     (generate-new-buffer-name " 
*temp-matlab-indent-region*")))
+              (let (region-updated)
+
+                (while (<= curr-linenum end-linenum)
+                  (forward-line 0)
+
+                  (let* ((tuple (matlab-ts-mode--ei-indent-elements-in-line
+                                 'indent-region
+                                 (when (= curr-linenum start-pt-linenum) 
start-pt-offset)))
+                         (new-line (nth 0 tuple))
+                         (line-updated (nth 1 tuple))
+                         (new-start-pt-offset (nth 2 tuple)))
+
+                    (when new-start-pt-offset
+                      (setq start-pt-offset new-start-pt-offset))
+
+                    (with-current-buffer new-content-buf
+                      (insert new-line)
+                      (when (< curr-linenum end-linenum)
+                        (insert "\n")))
+
+                    (when line-updated
+                      (setq region-updated t)))
+
+                  (forward-line)
+                  (setq curr-linenum (1+ curr-linenum)))
+
+                (when region-updated
+                  (save-excursion
+                    (goto-char beg)
+                    (delete-region beg end)
+                    (insert (with-current-buffer new-content-buf
+                              (buffer-string)))
+
+                    (when matlab-ts-mode--indent-assert
+                      (matlab-ts-mode--ei-assert-line-nodes-match 
start-linenum end-linenum))
+
+                    ;; Restore end point accounting for whitespace adjustments 
in the lines
+                    (goto-char (point-min))
+                    (forward-line end-linenum)
+                    (setq end (point)))))))
 
           ;; Update point to keep it on the starting semantic element
           (goto-char (point-min))
diff --git a/matlab-ts-mode.el b/matlab-ts-mode.el
index 63f2a72795..b96bb3936d 100644
--- a/matlab-ts-mode.el
+++ b/matlab-ts-mode.el
@@ -2911,8 +2911,10 @@ Example:
   "Call `treesit-indent', then do electric indent."
   (treesit-indent) ;; treesit-indent before electric indent to get updated 
point on the line
   (when matlab-ts-mode-electric-indent
-    (matlab-ts-mode--ei-workaround-143 (pos-bol) (pos-eol) (point))
-    (matlab-ts-mode--ei-indent-elements-in-line)))
+    (save-restriction
+      (widen)
+      (matlab-ts-mode--ei-workaround-143 (pos-bol) (pos-eol) (point))
+      (matlab-ts-mode--ei-indent-elements-in-line))))
 
 (defun matlab-ts-mode--treesit-indent-region (beg end)
   "Call `treesit-indent-region' on BEG END, then do electric indent."
diff --git a/tests/t-utils.el b/tests/t-utils.el
index 210c5b487e..e0ad93d673 100644
--- a/tests/t-utils.el
+++ b/tests/t-utils.el
@@ -572,15 +572,22 @@ skipped."
 
   ;; Set major-mode using file-major-mode if specified,
   ;; else use the "-*- property-first-line -*-" to set the mode
-  (if file-major-mode
-      (funcall file-major-mode)
-    ;; Else get it from the first "property line" specifying local variables
-    (let* ((prop-major-mode (hack-local-variables-prop-line t)))
-      (when (or (not prop-major-mode))
-        (user-error
-         "First line of %s must contain \"-*- MODE-NAME -*-\" or \"-*- mode: 
MODE-NAME -*-\""
-         file))
-      (funcall prop-major-mode)))
+  (let (prop-major-mode)
+    (condition-case err
+        (if file-major-mode
+            (funcall file-major-mode)
+          ;; Else get it from the first "property line" specifying local 
variables
+          (setq prop-major-mode (hack-local-variables-prop-line t))
+          (when (or (not prop-major-mode))
+            (error "First line must contain \"-*- MODE-NAME -*-\" or \"-*- 
mode: MODE-NAME -*-\""))
+          (funcall prop-major-mode))
+      (error
+       (let ((err-msg (concat (error-message-string err)
+                              (if file-major-mode
+                                  " (invalid file-major-mode provided)"
+                                (when prop-major-mode
+                                  " (invalid major-mode specified on first 
line)")))))
+         (error "Failed to set major-mode for %s: %s" (file-name-nondirectory 
file) err-msg)))))
 
   (when setup-callback
     (funcall setup-callback))
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region.m
 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region.m
new file mode 100644
index 0000000000..40f16ecaf8
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region.m
@@ -0,0 +1,30 @@
+% -*- matlab-ts -*-
+
+%{
+  Case1:
+  (t-utils-xr
+
+  (re-search-forward "a{") "C-a"
+  "C-a"
+  "C-@"
+  "C-n"
+  "C-n"
+  (narrow-to-region (mark) (point))
+  "C-@" ;; unset mark
+  "C-x h"  ;; select all
+  (indent-region (point-min) (point-max))
+  (widen)
+
+  (re-search-backward "^fun")
+  (t-utils-xr-print-code (point) (point-max))
+      
+  )
+%}
+
+function electric_indent_xr_narrow_to_region(a, longInputVar, c)
+arguments
+    a{                 mustBeNumeric, mustBeReal   }
+        longInputVar { mustBeNumeric, mustBeReal, mustBeFinite}
+                  c{mustBeNumeric}
+    end
+end
diff --git 
a/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region_expected.org
 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region_expected.org
new file mode 100644
index 0000000000..d8aa8e480e
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region_expected.org
@@ -0,0 +1,187 @@
+#+startup: showall
+
+* Executing commands from electric_indent_xr_narrow_to_region.m:5:2:
+
+  Case1: (t-utils-xr
+
+  (re-search-forward "a{") "C-a"
+  "C-a"
+  "C-@"
+  "C-n"
+  "C-n"
+  (narrow-to-region (mark) (point))
+  "C-@" ;; unset mark
+  "C-x h"  ;; select all
+  (indent-region (point-min) (point-max))
+  (widen)
+
+  (re-search-backward "^fun")
+  (t-utils-xr-print-code (point) (point-max))
+      
+  )
+
+- Invoking      : (re-search-forward "a{")
+  Start point   :  336
+  Moved to point:  422
+  : 26:6:     a{                 mustBeNumeric, mustBeReal   }
+  :             ^
+  No buffer modifications
+
+- Invoking      : "C-a" = move-beginning-of-line
+  Start point   :  422
+  Moved to point:  416
+  : 26:0:     a{                 mustBeNumeric, mustBeReal   }
+  :       ^
+  No buffer modifications
+
+- Invoking      : "C-a" = move-beginning-of-line
+  Start point   :  416
+  No point movement
+  No buffer modifications
+
+- Invoking      : "C-@" = set-mark-command
+  Start point   :  416
+  No point movement
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  416
+  Moved to point:  469
+  : 27:0:         longInputVar { mustBeNumeric, mustBeReal, mustBeFinite}
+  :       ^
+  No buffer modifications
+
+- Invoking      : "C-n" = next-line
+  Start point   :  469
+  Moved to point:  533
+  : 28:0:                   c{mustBeNumeric}
+  :       ^
+  No buffer modifications
+
+- Invoking      : (narrow-to-region (mark) (point))
+  Start point   :  533
+  No point movement
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -1,30 +1,2 @@
+-% -*- matlab-ts -*-
+-
+-%{
+-  Case1:
+-  (t-utils-xr
+-
+-  (re-search-forward "a{") "C-a"
+-  "C-a"
+-  "C-@"
+-  "C-n"
+-  "C-n"
+-  (narrow-to-region (mark) (point))
+-  "C-@" ;; unset mark
+-  "C-x h"  ;; select all
+-  (indent-region (point-min) (point-max))
+-  (widen)
+-
+-  (re-search-backward "^fun")
+-  (t-utils-xr-print-code (point) (point-max))
+-      
+-  )
+-%}
+-
+-function electric_indent_xr_narrow_to_region(a, longInputVar, c)
+-arguments
+     a{                 mustBeNumeric, mustBeReal   }
+         longInputVar { mustBeNumeric, mustBeReal, mustBeFinite}
+-                  c{mustBeNumeric}
+-    end
+-end
+  #+end_src diff
+
+- Invoking      : "C-@" = set-mark-command
+  Start point   :  533
+  No point movement
+  No buffer modifications
+
+- Invoking      : "C-x h" = mark-whole-buffer
+  Start point   :  533
+  Moved to point:  416
+  : 1:0:     a{                 mustBeNumeric, mustBeReal   }
+  :      ^
+  No buffer modifications
+
+- Invoking      : (indent-region (point-min) (point-max))
+  Start point   :  416
+  No point movement
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -1,2 +1,2 @@
+-    a{                 mustBeNumeric, mustBeReal   }
+-        longInputVar { mustBeNumeric, mustBeReal, mustBeFinite}
++    a            {mustBeNumeric, mustBeReal}
++    longInputVar {mustBeNumeric, mustBeReal, mustBeFinite}
+  #+end_src diff
+
+- Invoking      : (widen)
+  Start point   :  416
+  No point movement
+  Buffer modified:
+  #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -1,2 +1,30 @@
++% -*- matlab-ts -*-
++
++%{
++  Case1:
++  (t-utils-xr
++
++  (re-search-forward "a{") "C-a"
++  "C-a"
++  "C-@"
++  "C-n"
++  "C-n"
++  (narrow-to-region (mark) (point))
++  "C-@" ;; unset mark
++  "C-x h"  ;; select all
++  (indent-region (point-min) (point-max))
++  (widen)
++
++  (re-search-backward "^fun")
++  (t-utils-xr-print-code (point) (point-max))
++      
++  )
++%}
++
++function electric_indent_xr_narrow_to_region(a, longInputVar, c)
++arguments
+     a            {mustBeNumeric, mustBeReal}
+     longInputVar {mustBeNumeric, mustBeReal, mustBeFinite}
++                  c{mustBeNumeric}
++    end
++end
+  #+end_src diff
+
+- Invoking      : (re-search-backward "^fun")
+  Start point   :  416
+  Moved to point:  341
+  : 24:0: function electric_indent_xr_narrow_to_region(a, longInputVar, c)
+  :       ^
+  No buffer modifications
+
+- Invoking      : (t-utils-xr-print-code (point) (point-max))
+  Start point   :  341
+  No point movement
+  standard-output:
+  #+begin_src matlab-ts
+function electric_indent_xr_narrow_to_region(a, longInputVar, c)
+arguments
+    a            {mustBeNumeric, mustBeReal}
+    longInputVar {mustBeNumeric, mustBeReal, mustBeFinite}
+                  c{mustBeNumeric}
+    end
+end
+  #+end_src
+  No buffer modifications
diff --git 
a/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_corrupt_expected.txt
 
b/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_corrupt_expected.txt
index d9fd7a1b60..feffbf0717 100644
--- 
a/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_corrupt_expected.txt
+++ 
b/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_corrupt_expected.txt
@@ -1,4 +1,4 @@
 Major mode errored with message
-Not entering matlab-ts-mode due to non-printable utf8 character "" at point 45
+Failed to set major-mode for file_encoding_corrupt.m: Not entering 
matlab-ts-mode due to non-printable utf8 character "" at point 45 (invalid 
file-major-mode provided)
 
 Entered major-mode: fundamental-mode
diff --git 
a/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_mcr_expected.txt 
b/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_mcr_expected.txt
index 26bf02a934..8cca44e99d 100644
--- 
a/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_mcr_expected.txt
+++ 
b/tests/test-matlab-ts-mode-file-encoding-files/file_encoding_mcr_expected.txt
@@ -1,4 +1,4 @@
 Major mode errored with message
-Not activating matlab-ts-mode because this is MATLAB Compiler Runtime content
+Failed to set major-mode for file_encoding_mcr.m: Not activating 
matlab-ts-mode because this is MATLAB Compiler Runtime content (invalid 
file-major-mode provided)
 
 Entered major-mode: fundamental-mode
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region.m
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region.m
new file mode 100644
index 0000000000..40f16ecaf8
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region.m
@@ -0,0 +1,30 @@
+% -*- matlab-ts -*-
+
+%{
+  Case1:
+  (t-utils-xr
+
+  (re-search-forward "a{") "C-a"
+  "C-a"
+  "C-@"
+  "C-n"
+  "C-n"
+  (narrow-to-region (mark) (point))
+  "C-@" ;; unset mark
+  "C-x h"  ;; select all
+  (indent-region (point-min) (point-max))
+  (widen)
+
+  (re-search-backward "^fun")
+  (t-utils-xr-print-code (point) (point-max))
+      
+  )
+%}
+
+function electric_indent_xr_narrow_to_region(a, longInputVar, c)
+arguments
+    a{                 mustBeNumeric, mustBeReal   }
+        longInputVar { mustBeNumeric, mustBeReal, mustBeFinite}
+                  c{mustBeNumeric}
+    end
+end
diff --git 
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region_expected.txt
new file mode 100644
index 0000000000..2870dd4065
--- /dev/null
+++ 
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_narrow_to_region_expected.txt
@@ -0,0 +1,17 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,580> (comment[1,20]@{% -*- matlab-ts -*-}@) \n[20,22] 
(comment[22,339]@{%\{\n  Case1:\n  (t-utils-xr\n\n  (re-search-forwa...}@) 
\n[339,341]
+ (function_definition<341,579> function[341,349] name: 
(identifier[350,385]@{electric_indent_xr_narrow_to_region}@)
+  (function_arguments<385,405> ([385,386] arguments: 
(identifier[386,387]@{a}@) ,[387,388] (identifier[389,401]@{longInputVar}@) 
,[401,402] (identifier[403,404]@{c}@) )[404,405])
+  \n[405,406]
+  (arguments_statement<406,576> arguments[406,415] \n[415,416]
+   (property<420,468> name: (identifier[420,421]@{a}@)
+    (validation_functions<421,468> {[421,422] 
(identifier[439,452]@{mustBeNumeric}@) ,[452,453] 
(identifier[454,464]@{mustBeReal}@) }[467,468]))
+   \n[468,469]
+   (property<477,532> name: (identifier[477,489]@{longInputVar}@)
+    (validation_functions<490,532> {[490,491] 
(identifier[492,505]@{mustBeNumeric}@) ,[505,506] 
(identifier[507,517]@{mustBeReal}@) ,[517,518] 
(identifier[519,531]@{mustBeFinite}@) }[531,532]))
+   \n[532,533]
+   (property<551,567> name: (identifier[551,552]@{c}@)
+    (validation_functions<552,567> {[552,553] 
(identifier[553,566]@{mustBeNumeric}@) }[566,567]))
+   \n[567,568] end[572,575] \n[575,576])
+  end[576,579])
+ \n[579,580])

Reply via email to