aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2024-12-01 16:40:05 -0800
committerYuan Fu2024-12-01 17:53:23 -0800
commit9acf6eff01a82c3fe9ca8897f38b76dbb6079648 (patch)
tree95f4631560a8064801db7258a61e76d9dd75343c
parent994258f55675baa8473e6cff9aab7ecb31338580 (diff)
downloademacs-9acf6eff01a82c3fe9ca8897f38b76dbb6079648.tar.gz
emacs-9acf6eff01a82c3fe9ca8897f38b76dbb6079648.zip
Standardize and promote c-ts-mode's custom matcher and anchor
Specifically, standalone-parent and prev-sibling. The c-ts-mode custom version skips labels and proproc directives. * lisp/progmodes/c-ts-mode.el: (c-ts-mode--standalone-parent-skip-preproc): Rename to c-ts-mode--standalone-parent, and make it skip labels too. (c-ts-mode--preproc-indent-rules): Rename standalone-parent and prev-sibling. (c-ts-mode--label-indent-rules): Use custom standalone-parent and prev-sibling.
-rw-r--r--lisp/progmodes/c-ts-mode.el41
1 files changed, 18 insertions, 23 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 54617e20eba..6699e4ece48 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -281,13 +281,13 @@ is actually the parent of point at the moment of indentation."
281 0 281 0
282 c-ts-mode-indent-offset))) 282 c-ts-mode-indent-offset)))
283 283
284(defun c-ts-mode--anchor-prev-sibling (node parent bol &rest _) 284(defun c-ts-mode--prev-sibling (node parent bol &rest _)
285 "Return the start of the previous named sibling of NODE. 285 "Return the start of the previous named sibling of NODE.
286 286
287This anchor handles the special case where the previous sibling 287This anchor handles the special case where the previous sibling is a
288is a labeled_statement; in that case, return the child of the 288labeled_statement or preproc directive; in that case, return the child
289labeled statement instead. (Actually, recursively go down until 289of the labeled statement instead. (Actually, recursively go down until
290the node isn't a labeled_statement.) E.g., 290the node isn't a labeled_statement or preproc.) E.g.,
291 291
292label: 292label:
293 int x = 1; 293 int x = 1;
@@ -340,8 +340,8 @@ characters of the current line."
340 ;; prev-sibling doesn't have a child. 340 ;; prev-sibling doesn't have a child.
341 (treesit-node-start prev-sibling))) 341 (treesit-node-start prev-sibling)))
342 342
343(defun c-ts-mode--standalone-parent-skip-preproc (_n parent &rest _) 343(defun c-ts-mode--standalone-parent (_n parent &rest _)
344 "Like the standalone-parent anchor but skips preproc nodes. 344 "Like the standalone-parent anchor but skips preproc nodes and labels.
345PARENT is the parent of the current node." 345PARENT is the parent of the current node."
346 (save-excursion 346 (save-excursion
347 (treesit-node-start 347 (treesit-node-start
@@ -350,7 +350,8 @@ PARENT is the parent of the current node."
350 ;; nil. 350 ;; nil.
351 parent (lambda (node) 351 parent (lambda (node)
352 (and node 352 (and node
353 (not (string-search "preproc" (treesit-node-type node))) 353 (not (treesit-node-match-p
354 node (rx (or "preproc" "labeled_statement"))))
354 (progn 355 (progn
355 (goto-char (treesit-node-start node)) 356 (goto-char (treesit-node-start node))
356 (looking-back (rx bol (* whitespace)) 357 (looking-back (rx bol (* whitespace))
@@ -381,24 +382,24 @@ NODE and PARENT as usual."
381 `(((node-is "preproc") column-0 0) 382 `(((node-is "preproc") column-0 0)
382 ((node-is "#endif") column-0 0) 383 ((node-is "#endif") column-0 0)
383 ((match "preproc_call" "compound_statement") column-0 0) 384 ((match "preproc_call" "compound_statement") column-0 0)
384 ((prev-line-is "#endif") c-ts-mode--anchor-prev-sibling 0) 385 ((prev-line-is "#endif") c-ts-mode--prev-sibling 0)
385 ;; Top-level things under a preproc directive. Note that 386 ;; Top-level things under a preproc directive. Note that
386 ;; "preproc" matches more than one type: it matches 387 ;; "preproc" matches more than one type: it matches
387 ;; preproc_if, preproc_elif, etc. 388 ;; preproc_if, preproc_elif, etc.
388 ((n-p-gp nil "preproc" "translation_unit") column-0 0) 389 ((n-p-gp nil "preproc" "translation_unit") column-0 0)
389 ;; Indent rule for an empty line after a preproc directive. 390 ;; Indent rule for an empty line after a preproc directive.
390 ((and no-node (parent-is ,(rx (or "\n" "preproc")))) 391 ((and no-node (parent-is ,(rx (or "\n" "preproc"))))
391 c-ts-mode--standalone-parent-skip-preproc c-ts-mode--preproc-offset) 392 c-ts-mode--standalone-parent c-ts-mode--preproc-offset)
392 ;; Statement under a preproc directive, the first statement 393 ;; Statement under a preproc directive, the first statement
393 ;; indents against parent, the rest statements indent to 394 ;; indents against parent, the rest statements indent to
394 ;; their prev-sibling. 395 ;; their prev-sibling.
395 ((match nil ,(rx "preproc_" (or "if" "elif")) nil 3 3) 396 ((match nil ,(rx "preproc_" (or "if" "elif")) nil 3 3)
396 c-ts-mode--standalone-parent-skip-preproc c-ts-mode-indent-offset) 397 c-ts-mode--standalone-parent c-ts-mode-indent-offset)
397 ((match nil "preproc_ifdef" nil 2 2) 398 ((match nil "preproc_ifdef" nil 2 2)
398 c-ts-mode--standalone-parent-skip-preproc c-ts-mode-indent-offset) 399 c-ts-mode--standalone-parent c-ts-mode-indent-offset)
399 ((match nil "preproc_else" nil 1 1) 400 ((match nil "preproc_else" nil 1 1)
400 c-ts-mode--standalone-parent-skip-preproc c-ts-mode-indent-offset) 401 c-ts-mode--standalone-parent c-ts-mode-indent-offset)
401 ((parent-is "preproc") c-ts-mode--anchor-prev-sibling 0)) 402 ((parent-is "preproc") c-ts-mode--prev-sibling 0))
402 "Indent rules for preprocessors.") 403 "Indent rules for preprocessors.")
403 404
404(defun c-ts-mode--macro-heuristic-rules (node parent &rest _) 405(defun c-ts-mode--macro-heuristic-rules (node parent &rest _)
@@ -554,24 +555,18 @@ NODE, PARENT, BOL, ARGS are as usual."
554 (cons (pos-bol) 1)) 555 (cons (pos-bol) 1))
555 ;; Indent the label itself. 556 ;; Indent the label itself.
556 ((treesit-node-match-p node "labeled_statement") 557 ((treesit-node-match-p node "labeled_statement")
557 (cons (apply (alist-get 'standalone-parent 558 (cons (c-ts-mode--standalone-parent node parent bol args)
558 treesit-simple-indent-presets)
559 node parent bol args)
560 0)) 559 0))
561 ;; Indent the statement below the label. 560 ;; Indent the statement below the label.
562 ((treesit-node-match-p parent "labeled_statement") 561 ((treesit-node-match-p parent "labeled_statement")
563 (cons (apply (alist-get 'standalone-parent 562 (cons (c-ts-mode--standalone-parent node parent bol args)
564 treesit-simple-indent-presets)
565 parent (treesit-node-parent parent) bol args)
566 c-ts-mode-indent-offset)) 563 c-ts-mode-indent-offset))
567 ;; If previous sibling is a labeled_statement, align to it's 564 ;; If previous sibling is a labeled_statement, align to it's
568 ;; children, which is the previous statement. 565 ;; children, which is the previous statement.
569 ((and (not (treesit-node-match-p node "}")) 566 ((and (not (treesit-node-match-p node "}"))
570 (treesit-node-match-p (treesit-node-prev-sibling node) 567 (treesit-node-match-p (treesit-node-prev-sibling node)
571 "labeled_statement")) 568 "labeled_statement"))
572 (cons (treesit-node-start 569 (cons (c-ts-mode--prev-sibling node parent bol args)
573 (treesit-node-child
574 (treesit-node-prev-sibling node) 1 'named))
575 0)) 570 0))
576 (t nil))) 571 (t nil)))
577 572