branch: elpa/treesit-fold
commit e704add10d377aa28544fe15a05b7d81efbe590b
Author: Jen-Chieh <[email protected]>
Commit: Jen-Chieh <[email protected]>
fix(llvm): Fix folding for LLVM's label
---
ts-fold-parsers.el | 3 ++-
ts-fold.el | 21 +++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el
index b08a7c2b2d..87a57459f9 100644
--- a/ts-fold-parsers.el
+++ b/ts-fold-parsers.el
@@ -58,6 +58,7 @@
(declare-function ts-fold-range-julia "ts-fold.el")
(declare-function ts-fold-range-kotlin-when "ts-fold.el")
(declare-function ts-fold-range-lisp-function "ts-fold.el")
+(declare-function ts-fold-range-llvm-label "ts-fold.el")
(declare-function ts-fold-range-lua-comment "ts-fold.el")
(declare-function ts-fold-range-lua-function "ts-fold.el")
(declare-function ts-fold-range-lua-if "ts-fold.el")
@@ -360,7 +361,7 @@
(defun ts-fold-parsers-llvm ()
"Rule set for LLVM."
'((function_body . ts-fold-range-seq)
- (label . ts-fold-range-asm-label)
+ (label . ts-fold-range-llvm-label)
(comment
. (lambda (node offset)
(ts-fold-range-line-comment node offset ";;")))))
diff --git a/ts-fold.el b/ts-fold.el
index e0f3e35d42..5434a16157 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -804,6 +804,27 @@ more information."
(end (1- (tsc-node-end-position node))))
(ts-fold--cons-add (cons beg end) offset)))
+(defun ts-fold-range-llvm--find-last-instruction (node)
+ "Find the last instruction node by starting NODE."
+ (let* ((iter-node (ts-fold--next-prev-node-skip-newline node t))
+ (last iter-node))
+ (while (and iter-node
+ (not (member (ts-fold-2str (tsc-node-type iter-node))
+ (ts-fold-listify '("label" "}")))))
+ (setq last iter-node
+ iter-node (ts-fold--next-prev-node-skip-newline iter-node t)))
+ last)) ; return last insturction node
+
+(defun ts-fold-range-llvm-label (node offset)
+ "Define fold range for `label' in LLVM.
+
+For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
+more information."
+ (when-let* ((beg (tsc-node-end-position node))
+ (end (ts-fold-range-llvm--find-last-instruction node))
+ (end (tsc-node-end-position end)))
+ (ts-fold--cons-add (cons beg end) offset)))
+
(defun ts-fold-range-lua-comment (node offset)
"Define fold range for Lua comemnt.