branch: externals/matlab-mode
commit d428da9db48e6468fd62008382f3a19f1322fbaf
Author: John Ciolfi <[email protected]>
Commit: John Ciolfi <[email protected]>
matlab-ts-mode: fix TAB electric indent on string lines
---
matlab-ts-mode--ei.el | 109 ++++++++++++---------
.../electric_indent_xr_string.m | 9 ++
.../electric_indent_xr_string_expected.org | 74 ++++++++++++++
.../electric_indent_xr_string.m | 9 ++
.../electric_indent_xr_string_expected.txt | 34 +++++++
5 files changed, 189 insertions(+), 46 deletions(-)
diff --git a/matlab-ts-mode--ei.el b/matlab-ts-mode--ei.el
index 183e9f2282..2c17b16244 100644
--- a/matlab-ts-mode--ei.el
+++ b/matlab-ts-mode--ei.el
@@ -240,10 +240,66 @@ there are non-whitespace characters on the line, nil
otherwise."
(let ((inhibit-field-text-motion t)) (end-of-line))
nil))
+(defun matlab-ts-mode--ei-get-node-to-use (node)
+ "Given NODE, adjust it for electric indent.
+
+Returns (NODE . MODIFIED-NODE-TYPE) pair.
+
+For example, when node parent is a string, we return the parent. When
+node is a \"+\" and parent is a unary_operator, we return MODIFIED-NODE-TYPE to
+be unary-op even though the node type is \"+\"."
+
+ (let* ((node-type (treesit-node-type node))
+ (parent (treesit-node-parent node))
+ (parent-type (treesit-node-type parent)))
+
+ (cond
+ ;; Use string and not the elements of the string
+ ((equal parent-type "string")
+ (setq node parent
+ node-type parent-type))
+
+ ;; convert property identifier to property-id node-type
+ ((and (equal node-type "identifier")
+ (or
+ ;; propertyWithOutDot?
+ (and (equal parent-type "property")
+ (equal (treesit-node-child parent 0) node))
+ ;; property.nameWithDot?
+ (and (equal parent-type "property_name")
+ (equal (treesit-node-child (treesit-node-parent parent) 0)
parent))))
+ (setq node-type "property-id"))
+
+ ;; Unary operator sign, + or -, e.g. [0 -e] or g = - e
+ ((and (equal parent-type "unary_operator")
+ (equal (treesit-node-child parent 0) node))
+ (setq node-type "unary-op"))
+
+ ;; Super-class constructor call
+ ;; [email protected];
+ ((and (equal node-type "@")
+ (equal parent-type "function_call"))
+ (setq node-type "@-fcn-call"))
+
+ ;; Property dimensions
+ ;; foo1 (1, :) {mustBeNumeric, mustBeReal} = [0, 0, 0];
+ ((and (or (equal node-type "number") (equal node-type ":"))
+ (or (equal parent-type "dimensions")
+ (and (equal parent-type "spread_operator")
+ (equal (treesit-node-type (treesit-node-parent parent))
+ "dimensions"))))
+ (setq node-type "prop-dim"))
+ )
+
+ (cons node node-type)))
+
(cl-defun matlab-ts-mode--ei-move-to-and-get-node ()
"Move to and return node.
Will return nil if no next node before end-of-line.
-Assumes point is at of current node or beginning of line."
+Assumes point is at of current node or beginning of line.
+
+Returns (NODE . MODIFIED-NODE-TYPE) where MODIFIED-NODE-TYPE
+is used in `matlab-ts-mode--ei-spacing'"
;; Move point to first non-whitespace char
(let ((eol (pos-eol)))
(when (looking-at "[ \t]")
@@ -251,8 +307,7 @@ Assumes point is at of current node or beginning of line."
(cl-return-from matlab-ts-mode--ei-move-to-and-get-node))
(backward-char))
- (let ((node (treesit-node-at (point)))
- node-type)
+ (let ((node (treesit-node-at (point))))
;; Consider [[1,2];[3,4]] when point is on semicolon, node will be the
prior "]" because the
;; semicolon is an ignored node, so move forward to get to the "[" after
the semicolon.
@@ -274,47 +329,7 @@ Assumes point is at of current node or beginning of line."
(setq node nil))
(when node
- (setq node-type (treesit-node-type node))
- (let* ((parent (treesit-node-parent node))
- (parent-type (treesit-node-type parent)))
- (cond
- ;; Use string and not the elements of the string
- ((equal parent-type "string")
- (setq node parent
- node-type parent-type))
-
- ;; convert property identifier to property-id node-type
- ((and (equal node-type "identifier")
- (or
- ;; propertyWithOutDot?
- (and (equal parent-type "property")
- (equal (treesit-node-child parent 0) node))
- ;; property.nameWithDot?
- (and (equal parent-type "property_name")
- (equal (treesit-node-child (treesit-node-parent parent)
0) parent))))
- (setq node-type "property-id"))
-
- ;; Unary operator sign, + or -, e.g. [0 -e] or g = - e
- ((and (equal parent-type "unary_operator")
- (equal (treesit-node-child parent 0) node))
- (setq node-type "unary-op"))
-
- ;; Super-class constructor call
- ;; [email protected];
- ((and (equal node-type "@")
- (equal parent-type "function_call"))
- (setq node-type "@-fcn-call"))
-
- ;; Property dimensions
- ;; foo1 (1, :) {mustBeNumeric, mustBeReal} = [0, 0, 0];
- ((and (or (equal node-type "number") (equal node-type ":"))
- (or (equal parent-type "dimensions")
- (and (equal parent-type "spread_operator")
- (equal (treesit-node-type (treesit-node-parent
parent))
- "dimensions"))))
- (setq node-type "prop-dim"))
- )))
- (cons node node-type))))
+ (matlab-ts-mode--ei-get-node-to-use node)))))
(defun matlab-ts-mode--ei-assert-match (orig-line orig-line-node-types)
"Assert new line has same code meaning as ORIG-LINE.
@@ -1152,7 +1167,8 @@ See `matlab-ts-mode--ei-get-new-line' for EI-INFO
contents."
(defun matlab-ts-mode--ei-get-start-info ()
"Get start node and start offset in line prior to electric indent.
Returns (cons start-node start-offset) where
-- start-node is non-nil if the point is at a node
+- start-node is non-nil if the point is at a node and start-node will
+ be the modified node from `matlab-ts-mode--ei-get-node-to-use'
- start-offset is the offset of the point from the beginning of start-node.
Example where point is on the \"d\" in width:
area = width * length;
@@ -1181,8 +1197,9 @@ start-node is the identifier node for width and
start-offset is 2."
(when (and node
(>= (point) (treesit-node-start node))
(<= (point) (treesit-node-end node)))
- (setq start-node node)
+ (setq start-node (car (matlab-ts-mode--ei-get-node-to-use node)))
(setq start-offset (- (point) (treesit-node-start node)))))))
+
(cons start-node start-offset)))
(defun matlab-ts-mode--ei-workaround-143 (beg end &optional line-pt)
diff --git
a/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string.m
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string.m
new file mode 100644
index 0000000000..8bd0323547
--- /dev/null
+++
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string.m
@@ -0,0 +1,9 @@
+% -*- matlab-ts -*-
+
+% case1: (t-utils-xr "C-n" "C-n" "C-a" "C-I")
+ error(message('foo.bar.goo' , ...
+ 'filed1', num2str(v1 / 2)));
+
+% case1: (t-utils-xr (re-search-forward "2)") "C-I")
+ error(message('foo.bar.goo' , ...
+ 'filed1', num2str(v1 / 2)));
diff --git
a/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string_expected.org
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string_expected.org
new file mode 100644
index 0000000000..4edd1a6cb8
--- /dev/null
+++
b/tests/test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string_expected.org
@@ -0,0 +1,74 @@
+#+startup: showall
+
+* Executing commands from electric_indent_xr_string.m:3:9:
+
+ case1: (t-utils-xr "C-n" "C-n" "C-a" "C-I")
+
+- Invoking : "C-n" = next-line
+ Start point : 67
+ Moved to point: 113
+ : 4:45: error(message('foo.bar.goo' , ...
+ : ^
+ No buffer modifications
+
+- Invoking : "C-n" = next-line
+ Start point : 113
+ Moved to point: 164
+ : 5:45: 'filed1', num2str(v1 / 2)));
+ : ^
+ No buffer modifications
+
+- Invoking : "C-a" = move-beginning-of-line
+ Start point : 164
+ Moved to point: 119
+ : 5:0: 'filed1', num2str(v1 / 2)));
+ : ^
+ No buffer modifications
+
+- Invoking : "C-I" = indent-for-tab-command
+ Start point : 119
+ Moved to point: 149
+ : 5:30: 'filed1', num2str(v1 / 2)));
+ : ^
+ Buffer modified:
+ #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -2,7 +2,7 @@
+
+ % case1: (t-utils-xr "C-n" "C-n" "C-a" "C-I")
+ error(message('foo.bar.goo' , ...
+- 'filed1', num2str(v1 / 2)));
++ 'filed1', num2str(v1 / 2)));
+
+ % case1: (t-utils-xr (re-search-forward "2)") "C-I")
+ error(message('foo.bar.goo' , ...
+ #+end_src diff
+
+* Executing commands from electric_indent_xr_string.m:7:9:
+
+ case1: (t-utils-xr (re-search-forward "2)") "C-I")
+
+- Invoking : (re-search-forward "2)")
+ Start point : 231
+ Moved to point: 345
+ : 9:62: 'filed1', num2str(v1 / 2)));
+ : ^
+ No buffer modifications
+
+- Invoking : "C-I" = indent-for-tab-command
+ Start point : 345
+ Moved to point: 338
+ : 9:55: 'filed1', num2str(v1 / 2)));
+ : ^
+ Buffer modified:
+ #+begin_src diff
+--- start_contents
++++ end_contents
+@@ -6,4 +6,4 @@
+
+ % case1: (t-utils-xr (re-search-forward "2)") "C-I")
+ error(message('foo.bar.goo' , ...
+- 'filed1', num2str(v1 / 2)));
++ 'filed1', num2str(v1 / 2)));
+ #+end_src diff
diff --git
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string.m
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string.m
new file mode 100644
index 0000000000..8bd0323547
--- /dev/null
+++
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string.m
@@ -0,0 +1,9 @@
+% -*- matlab-ts -*-
+
+% case1: (t-utils-xr "C-n" "C-n" "C-a" "C-I")
+ error(message('foo.bar.goo' , ...
+ 'filed1', num2str(v1 / 2)));
+
+% case1: (t-utils-xr (re-search-forward "2)") "C-I")
+ error(message('foo.bar.goo' , ...
+ 'filed1', num2str(v1 / 2)));
diff --git
a/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string_expected.txt
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string_expected.txt
new file mode 100644
index 0000000000..61bc193be8
--- /dev/null
+++
b/tests/test-matlab-ts-mode-parser-files/copy-of-test-matlab-ts-mode-electric-indent-xr-files/electric_indent_xr_string_expected.txt
@@ -0,0 +1,34 @@
+# -*- t-utils-ts-parse-tree -*-
+(source_file<1,349> (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,67]@{%
case1: (t-utils-xr "C-n" "C-n" "C-a" "C-I")}@)
+ (function_call<84,176> name: (identifier[84,89]@{error}@) ([89,90]
+ (arguments<90,175>
+ argument:
+ (function_call<90,175> name: (identifier[90,97]@{message}@) ([97,98]
+ (arguments<98,174>
+ argument: (string<98,111> '[98,99]
(string_content[99,110]@{foo.bar.goo}@) '[110,111])
+ ,[113,114] (line_continuation[115,119]@{...\n}@)
+ (string<144,152> '[144,145] (string_content[145,151]@{filed1}@)
'[151,152])
+ ,[152,153]
+ (function_call<154,174> name: (identifier[154,161]@{num2str}@) ([161,162]
+ (arguments<162,173>
+ argument: (binary_operator<162,173> left: (identifier[162,164]@{v1}@)
/[167,168] right: (number[172,173]@{2}@)))
+ )[173,174]))
+ )[174,175]))
+ )[175,176])
+ ;[176,177] (comment[179,231]@{% case1: (t-utils-xr (re-search-forward "2)")
"C-I...}@)
+ (function_call<248,347> name: (identifier[248,253]@{error}@) ([253,254]
+ (arguments<254,346>
+ argument:
+ (function_call<254,346> name: (identifier[254,261]@{message}@) ([261,262]
+ (arguments<262,345>
+ argument: (string<262,275> '[262,263]
(string_content[263,274]@{foo.bar.goo}@) '[274,275])
+ ,[277,278] (line_continuation[279,283]@{...\n}@)
+ (string<315,323> '[315,316] (string_content[316,322]@{filed1}@)
'[322,323])
+ ,[323,324]
+ (function_call<325,345> name: (identifier[325,332]@{num2str}@) ([332,333]
+ (arguments<333,344>
+ argument: (binary_operator<333,344> left: (identifier[333,335]@{v1}@)
/[338,339] right: (number[343,344]@{2}@)))
+ )[344,345]))
+ )[345,346]))
+ )[346,347])
+ ;[347,348] \n[348,349])