Skip to content

Commit 5208a89

Browse files
committed
feat: add indent settings
1 parent 270a510 commit 5208a89

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

netlinx-mode-indent.el

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
;;; netlinx-mode-indent.el --- Indent settings for NetLinx mode -*- lexical-binding: t; no-byte-compile: t; -*-
2+
3+
;; Copyright (C) 2024 Norgate AV
4+
5+
;; Author: Norgate AV
6+
;; Maintainer: Norgate AV
7+
8+
;;; Commentary:
9+
10+
;; This file provides indent settings for NetLinx mode.
11+
;; This is an internal library for netlinx-mode.el and is not a standalone package.
12+
13+
;;; Code:
14+
15+
;; Indentation rules for tree-sitter
16+
(defvar netlinx-mode--indent-rules
17+
`((netlinx
18+
;; Closing delimiters align with their opening construct
19+
((node-is "}") parent-bol 0)
20+
((node-is ")") parent-bol 0)
21+
((node-is "]") parent-bol 0)
22+
23+
;; Control flow keywords align with their parent
24+
((node-is "else_clause") parent-bol 0)
25+
((node-is "case_statement") parent-bol netlinx-mode-indent-offset)
26+
((node-is "default_label") parent-bol netlinx-mode-indent-offset)
27+
28+
;; Statement blocks are indented
29+
((parent-is "if_statement") parent-bol netlinx-mode-indent-offset)
30+
((parent-is "for_statement") parent-bol netlinx-mode-indent-offset)
31+
((parent-is "while_statement") parent-bol netlinx-mode-indent-offset)
32+
((parent-is "switch_statement") parent-bol netlinx-mode-indent-offset)
33+
((parent-is "case_statement") parent-bol netlinx-mode-indent-offset)
34+
((parent-is "else_clause") parent-bol netlinx-mode-indent-offset)
35+
((parent-is "select_statement") parent-bol netlinx-mode-indent-offset)
36+
((parent-is "active_statement") parent-bol netlinx-mode-indent-offset)
37+
38+
;; Function and event definitions
39+
((parent-is "define_function") parent-bol netlinx-mode-indent-offset)
40+
((parent-is "function_definition") parent-bol netlinx-mode-indent-offset)
41+
((parent-is "event_handler") parent-bol netlinx-mode-indent-offset)
42+
43+
;; Compound statements
44+
((parent-is "compound_statement") parent-bol netlinx-mode-indent-offset)
45+
46+
;; Array and struct initializers
47+
((parent-is "initializer_list") parent-bol netlinx-mode-indent-offset)
48+
49+
;; Parameter lists
50+
((parent-is "parameter_list") parent-bol netlinx-mode-indent-offset)
51+
((parent-is "argument_list") parent-bol netlinx-mode-indent-offset)
52+
53+
;; Default: maintain current indentation for other nodes
54+
(no-node parent-bol 0)))
55+
"Tree-sitter indentation rules for `netlinx-mode'.")
56+
57+
(provide 'netlinx-mode-indent)
58+
59+
;;; netlinx-mode-indent.el ends here

netlinx-mode.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@
3939
;; Load font-lock (syntax highlighting) settings
4040
(require 'netlinx-mode-font-lock)
4141

42+
;; Load indentation settings
43+
(require 'netlinx-mode-indent)
44+
4245
;; Configuration for tree-sitter grammar
4346
(defcustom netlinx-mode-grammar-location
4447
"https://github.com/Norgate-AV/tree-sitter-netlinx"
4548
"Repository URL for the tree-sitter NetLinx grammar."
4649
:type 'string
4750
:group 'netlinx)
4851

52+
;; Indentation configuration
53+
(defcustom netlinx-mode-indent-offset 4
54+
"Number of spaces for each indentation step in `netlinx-mode'."
55+
:type 'integer
56+
:safe 'integerp
57+
:group 'netlinx)
58+
4959
;; Specify the version/tag of the grammar to use
5060
(defcustom netlinx-mode-grammar-version
5161
"v1.0.4"
@@ -106,6 +116,18 @@ The file path is configured via `netlinx-mode-help-file'."
106116
;; Create parser
107117
(treesit-parser-create 'netlinx)
108118

119+
;; Comments
120+
(setq-local comment-start "// ")
121+
(setq-local comment-end "")
122+
(setq-local comment-start-skip "//+\\s-*")
123+
(setq-local comment-multi-line t)
124+
125+
;; Indentation
126+
(setq-local treesit-simple-indent-rules netlinx-mode--indent-rules)
127+
(setq-local indent-line-function #'treesit-indent)
128+
(setq-local electric-indent-chars
129+
(append "{}():;," electric-indent-chars))
130+
109131
;; Setup font-lock
110132
(setq-local treesit-font-lock-settings netlinx-mode--font-lock-settings)
111133
(setq-local treesit-font-lock-level 4)

test/netlinx-mode-test.el

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
(should (custom-variable-p 'netlinx-mode-grammar-location))
2323
(should (custom-variable-p 'netlinx-mode-grammar-version))
2424
(should (custom-variable-p 'netlinx-mode-help-file))
25+
(should (custom-variable-p 'netlinx-mode-indent-offset))
2526
(should (stringp netlinx-mode-grammar-location))
26-
(should (stringp netlinx-mode-grammar-version)))
27+
(should (stringp netlinx-mode-grammar-version))
28+
(should (integerp netlinx-mode-indent-offset))
29+
(should (= netlinx-mode-indent-offset 4)))
2730

2831
;;; Face Tests
2932

@@ -122,6 +125,67 @@
122125
"Test that the package provides the netlinx-mode feature."
123126
(should (featurep 'netlinx-mode)))
124127

128+
;;; Indentation Tests
129+
130+
(ert-deftest netlinx-mode-test-indent-rules-defined ()
131+
"Test that indentation rules are defined."
132+
(should (boundp 'netlinx-mode--indent-rules))
133+
(should (listp netlinx-mode--indent-rules))
134+
(should (assq 'netlinx netlinx-mode--indent-rules)))
135+
136+
(ert-deftest netlinx-mode-test-indent-offset-safe ()
137+
"Test that indent-offset has a safety predicate."
138+
(should (get 'netlinx-mode-indent-offset 'safe-local-variable))
139+
(should (funcall (get 'netlinx-mode-indent-offset 'safe-local-variable) 4))
140+
(should (funcall (get 'netlinx-mode-indent-offset 'safe-local-variable) 2))
141+
(should-not (funcall (get 'netlinx-mode-indent-offset 'safe-local-variable) "not-a-number")))
142+
143+
(ert-deftest netlinx-mode-test-indent-configuration ()
144+
"Test that indentation is configured when mode is activated."
145+
(with-temp-buffer
146+
(cl-letf (((symbol-function 'netlinx-mode--ensure-grammar) #'ignore)
147+
((symbol-function 'treesit-ready-p) (lambda (&rest _) t))
148+
((symbol-function 'treesit-parser-create) #'ignore)
149+
((symbol-function 'treesit-major-mode-setup) #'ignore))
150+
(netlinx-mode)
151+
(should (local-variable-p 'treesit-simple-indent-rules))
152+
(should (eq treesit-simple-indent-rules netlinx-mode--indent-rules)))))
153+
154+
;;; Comment Configuration Tests
155+
156+
(ert-deftest netlinx-mode-test-comment-start ()
157+
"Test that comment-start is configured correctly."
158+
(with-temp-buffer
159+
(cl-letf (((symbol-function 'netlinx-mode--ensure-grammar) #'ignore)
160+
((symbol-function 'treesit-ready-p) (lambda (&rest _) t))
161+
((symbol-function 'treesit-parser-create) #'ignore)
162+
((symbol-function 'treesit-major-mode-setup) #'ignore))
163+
(netlinx-mode)
164+
(should (local-variable-p 'comment-start))
165+
(should (string= comment-start "// ")))))
166+
167+
(ert-deftest netlinx-mode-test-comment-end ()
168+
"Test that comment-end is configured correctly."
169+
(with-temp-buffer
170+
(cl-letf (((symbol-function 'netlinx-mode--ensure-grammar) #'ignore)
171+
((symbol-function 'treesit-ready-p) (lambda (&rest _) t))
172+
((symbol-function 'treesit-parser-create) #'ignore)
173+
((symbol-function 'treesit-major-mode-setup) #'ignore))
174+
(netlinx-mode)
175+
(should (local-variable-p 'comment-end))
176+
(should (string= comment-end "")))))
177+
178+
(ert-deftest netlinx-mode-test-comment-start-skip ()
179+
"Test that comment-start-skip is configured correctly."
180+
(with-temp-buffer
181+
(cl-letf (((symbol-function 'netlinx-mode--ensure-grammar) #'ignore)
182+
((symbol-function 'treesit-ready-p) (lambda (&rest _) t))
183+
((symbol-function 'treesit-parser-create) #'ignore)
184+
((symbol-function 'treesit-major-mode-setup) #'ignore))
185+
(netlinx-mode)
186+
(should (local-variable-p 'comment-start-skip))
187+
(should (stringp comment-start-skip)))))
188+
125189
(provide 'netlinx-mode-test)
126190

127191
;;; netlinx-mode-test.el ends here

0 commit comments

Comments
 (0)