Skip to content

Commit e3845dd

Browse files
committed
refactor: update indents
1 parent 81ed41c commit e3845dd

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

netlinx-mode-indent.el

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,67 @@
1212

1313
;;; Code:
1414

15+
(defgroup netlinx nil
16+
"Major mode for NetLinx using tree-sitter."
17+
:group 'languages
18+
:prefix "netlinx-")
19+
20+
;; Indentation configuration
21+
(defcustom netlinx-mode-indent-offset 4
22+
"Number of spaces for each indentation step in `netlinx-mode'."
23+
:type 'integer
24+
:safe 'integerp
25+
:group 'netlinx)
26+
27+
(defcustom netlinx-mode-align-argument-list-to-first-sibling nil
28+
"Align argument lists to the first sibling.
29+
30+
If set to t, the following indentation is used:
31+
32+
send_command device, \"'PROPERTY-Name,Value1,Value2,',
33+
'Value3'\"
34+
35+
Otherwise, the indentation is:
36+
37+
send_command device, \"'PROPERTY-Name,Value1,Value2,',
38+
'Value3'\""
39+
:type 'boolean
40+
:safe 'booleanp
41+
:group 'netlinx)
42+
43+
(defcustom netlinx-mode-align-device-array-to-first-sibling nil
44+
"Align device array elements to the first sibling.
45+
46+
If set to t, the following indentation is used:
47+
48+
dvDevices = { dvPanel1,
49+
dvPanel2,
50+
dvPanel3 }
51+
52+
Otherwise, the indentation is:
53+
54+
dvDevices = { dvPanel1,
55+
dvPanel2,
56+
dvPanel3 }"
57+
:type 'boolean
58+
:safe 'booleanp
59+
:group 'netlinx)
60+
61+
;; Helper function for conditional indentation
62+
(defun netlinx-mode--indent-argument-list (node parent &rest _)
63+
"Indent argument list NODE relative to PARENT.
64+
Uses `netlinx-mode-align-argument-list-to-first-sibling' to determine alignment."
65+
(if netlinx-mode-align-argument-list-to-first-sibling
66+
(treesit-node-start (treesit-node-child parent 0))
67+
`(parent-bol ,netlinx-mode-indent-offset)))
68+
69+
(defun netlinx-mode--indent-initializer-list (node parent &rest _)
70+
"Indent initializer list NODE relative to PARENT.
71+
Uses `netlinx-mode-align-device-array-to-first-sibling' to determine alignment."
72+
(if netlinx-mode-align-device-array-to-first-sibling
73+
(treesit-node-start (treesit-node-child parent 0))
74+
`(parent-bol ,netlinx-mode-indent-offset)))
75+
1576
;; Indentation rules for tree-sitter
1677
(defvar netlinx-mode--indent-rules
1778
`((netlinx
@@ -44,11 +105,11 @@
44105
((parent-is "compound_statement") parent-bol netlinx-mode-indent-offset)
45106

46107
;; Array and struct initializers
47-
((parent-is "initializer_list") parent-bol netlinx-mode-indent-offset)
108+
((parent-is "initializer_list") netlinx-mode--indent-initializer-list)
48109

49110
;; Parameter lists
50111
((parent-is "parameter_list") parent-bol netlinx-mode-indent-offset)
51-
((parent-is "argument_list") parent-bol netlinx-mode-indent-offset)
112+
((parent-is "argument_list") netlinx-mode--indent-argument-list)
52113

53114
;; Default: maintain current indentation for other nodes
54115
(no-node parent-bol 0)))

netlinx-mode.el

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@
6363
:type 'string
6464
:group 'netlinx)
6565

66-
;; Indentation configuration
67-
(defcustom netlinx-mode-indent-offset 4
68-
"Number of spaces for each indentation step in `netlinx-mode'."
69-
:type 'integer
70-
:safe 'integerp
71-
:group 'netlinx)
72-
7366
;; Specify the version/tag of the grammar to use
7467
(defcustom netlinx-mode-grammar-version
7568
"v1.0.4"

test/netlinx-mode-test.el

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,22 @@
2323
(should (custom-variable-p 'netlinx-mode-grammar-version))
2424
(should (custom-variable-p 'netlinx-mode-help-file))
2525
(should (custom-variable-p 'netlinx-mode-indent-offset))
26+
(should (custom-variable-p 'netlinx-mode-align-argument-list-to-first-sibling))
27+
(should (custom-variable-p 'netlinx-mode-align-device-array-to-first-sibling))
2628
(should (stringp netlinx-mode-grammar-location))
2729
(should (stringp netlinx-mode-grammar-version))
2830
(should (integerp netlinx-mode-indent-offset))
29-
(should (= netlinx-mode-indent-offset 4)))
31+
(should (= netlinx-mode-indent-offset 4))
32+
(should (booleanp netlinx-mode-align-argument-list-to-first-sibling))
33+
(should (booleanp netlinx-mode-align-device-array-to-first-sibling)))
34+
35+
(ert-deftest netlinx-mode-test-indent-alignment-options-safe ()
36+
"Test that indent alignment options have safety predicates."
37+
(should (get 'netlinx-mode-align-argument-list-to-first-sibling 'safe-local-variable))
38+
(should (get 'netlinx-mode-align-device-array-to-first-sibling 'safe-local-variable))
39+
(should (funcall (get 'netlinx-mode-align-argument-list-to-first-sibling 'safe-local-variable) t))
40+
(should (funcall (get 'netlinx-mode-align-argument-list-to-first-sibling 'safe-local-variable) nil))
41+
(should-not (funcall (get 'netlinx-mode-align-argument-list-to-first-sibling 'safe-local-variable) "not-a-boolean")))
3042

3143
;;; Face Tests
3244

0 commit comments

Comments
 (0)