[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/treesit-fold ef390d67aa 022/417: Organize code
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/treesit-fold ef390d67aa 022/417: Organize code |
Date: |
Mon, 1 Jul 2024 10:02:07 -0400 (EDT) |
branch: elpa/treesit-fold
commit ef390d67aa535d7d26c49bf14e12c2d7c0976d31
Author: JenChieh <jcs090218@gmail.com>
Commit: JenChieh <jcs090218@gmail.com>
Organize code
---
tree-sitter-fold.el | 102 +++++++++++++++++++++++++++------------------------
tree-sitter-fold.elc | Bin 0 -> 12162 bytes
2 files changed, 55 insertions(+), 47 deletions(-)
diff --git a/tree-sitter-fold.el b/tree-sitter-fold.el
index 567181bd08..c99aebf1a3 100644
--- a/tree-sitter-fold.el
+++ b/tree-sitter-fold.el
@@ -10,7 +10,7 @@
;; Description: Code folding using tree-sitter
;; Keyword: folding tree-sitter
;; Version: 0.0.1
-;; Package-Requires: ((emacs "25.1") (tree-sitter "0.15.1"))
+;; Package-Requires: ((emacs "26.1") (tree-sitter "0.15.1"))
;; URL: https://github.com/jcs090218/tree-sitter-fold
;; This file is NOT part of GNU Emacs.
@@ -31,19 +31,21 @@
;;; Commentary:
;;
;; This package provides a code-folding mechanism based on tree-sitter
-;; package. Turn on the minor-mode `tree-sitter-fold-mode' to enable
-;; this mechanism. Note that all functionalities provided here based on the
+;; package. Turn on the minor-mode `tree-sitter-fold-mode' to enable
+;; this mechanism. Note that all functionalities provided here based on the
;; `tree-sitter-mode', and thus it should be enabled before
;; `tree-sitter-fold-mode' can properly fold codes.
;;; Code:
-(require 'tree-sitter)
(require 'seq)
+(require 'subr-x)
+
+(require 'tree-sitter)
-;; =============
-;; customization
-;; =============
+;;
+;; (@* "Customization" )
+;;
(defgroup tree-sitter-fold nil
"Code folding using tree-sitter."
@@ -82,37 +84,42 @@ the fold in a cons cell. See
`tree-sitter-fold-range-python' for an example."
:type 'hook
:group 'tree-sitter-fold)
-;; ==========
-;; minor mode
-;; ==========
+;;
+;; (@* "Entry" )
+;;
+
+(defun tree-sitter-fold--enable ()
+ "Start folding minor mode."
+ (setq-local line-move-ignore-invisible t)
+ (add-to-invisibility-spec '(tree-sitter-fold . t))
+
+ ;; evil integration
+ (if (bound-and-true-p evil-fold-list)
+ (add-to-list 'evil-fold-list
+ '((tree-sitter-fold-mode)
+ :open tree-sitter-fold-open
+ :close tree-sitter-fold-close
+ :open-rec tree-sitter-fold-open-recursively
+ :open-all tree-sitter-fold-open-all
+ :close-all tree-sitter-fold-close-all)))
+
+ (run-hooks 'tree-sitter-fold-mode-hook))
+
+(defun tree-sitter-fold--disable ()
+ "Stop folding minor mode."
+ (remove-from-invisibility-spec '(tree-sitter-fold . t))
+ (let ((tree-sitter-mode t))
+ (tree-sitter-fold-open-all)))
(define-minor-mode tree-sitter-fold-mode
"Folding code using tree sitter."
:init-value nil
- :lighter nil
- (if tree-sitter-fold-mode
- (progn
- (setq-local line-move-ignore-invisible t)
- (add-to-invisibility-spec '(tree-sitter-fold . t))
-
- ;; evil integration
- (if (bound-and-true-p evil-fold-list)
- (add-to-list 'evil-fold-list
- '((tree-sitter-fold-mode)
- :open tree-sitter-fold-open
- :close tree-sitter-fold-close
- :open-rec tree-sitter-fold-open-recursively
- :open-all tree-sitter-fold-open-all
- :close-all tree-sitter-fold-close-all)))
-
- (run-hooks 'tree-sitter-fold-mode-hook))
- (remove-from-invisibility-spec '(tree-sitter-fold . t))
- (let ((tree-sitter-mode t))
- (tree-sitter-fold-open-all))))
-
-;; ============================================
-;; using `tree-sitter' to determine fold range.
-;; ============================================
+ :lighter "TS-Fold"
+ (if tree-sitter-fold-mode (tree-sitter-fold--enable)
(tree-sitter-fold--disable)))
+
+;;
+;; (@* "Core" )
+;;
(defun tree-sitter-fold--foldable-node-at-pos (&optional pos)
"Return the smallest foldable node at POS. If POS is nil, use `point'.
@@ -140,9 +147,9 @@ This function is borrowed from `tree-sitter-node-at-point'."
(format "Current node is not found in `tree-sitter-fold-range-alist'
in %s"
major-mode)))))
-;; ========
-;; overlays
-;; ========
+;;
+;; (@* "Overlays" )
+;;
(defun tree-sitter-fold--create-overlay (range)
"Create invisible overlay in RANGE."
@@ -167,21 +174,22 @@ This function is borrowed from
`tree-sitter-node-at-point'."
(= (overlay-end ov) (cdr range)))))
car)))
-;; ========
-;; commands
-;; ========
+;;
+;; (@* "Commands" )
+;;
(defmacro tree-sitter-fold--ensure-ts (&rest body)
- "Run BODY only if `tree-sitter-mode' is enabled."
+ "Run BODY only if `tree-sitter-mode` is enabled."
(declare (indent 0))
`(if (bound-and-true-p tree-sitter-mode)
(progn ,@body)
(user-error "Ignored, tree-sitter-mode is not enable in the current
buffer")))
(defun tree-sitter-fold-close (&optional node)
- "Fold the syntax node at `point' if it is foldable.
-Foldable nodes are defined in `tree-sitter-fold-foldable-node-alist' for the
current
-`major-mode'. If no foldable node is found in point, do nothing."
+ "Fold the syntax node at `point` if it is foldable.
+
+Foldable nodes are defined in `tree-sitter-fold-foldable-node-alist' for the
+current `major-mode'. If no foldable NODE is found in point, do nothing."
(interactive)
(tree-sitter-fold--ensure-ts
(let ((node (or node (tree-sitter-fold--foldable-node-at-pos))))
@@ -242,9 +250,9 @@ If the current syntax node is not foldable, do nothing."
(delete-overlay ov)
(tree-sitter-fold-close node)))))
-;; =================
-;; language supports
-;; =================
+;;
+;; (@* "Languages" )
+;;
(defun tree-sitter-fold-range-python (node)
"Return the fold range for `function_definition' and `class_definition' NODE
in Python."
diff --git a/tree-sitter-fold.elc b/tree-sitter-fold.elc
new file mode 100644
index 0000000000..4290d25c5d
Binary files /dev/null and b/tree-sitter-fold.elc differ
- [nongnu] elpa/treesit-fold 6603e0ddbb 057/417: Split parser, (continued)
- [nongnu] elpa/treesit-fold 6603e0ddbb 057/417: Split parser, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 7127f4bbcc 061/417: Add comment, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold c6f58d41d5 068/417: Fix, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 445b209eb0 062/417: Fix compatible, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold f57a136cb8 075/417: Fix externals, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold b493772d32 080/417: Fix autoload, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 8d2599d54d 071/417: Fix warnings, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold ac7f2d0071 084/417: Try ruby, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 570305133e 003/417: version 0.0.1, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold cbe049d1bf 010/417: fix typo, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold ef390d67aa 022/417: Organize code,
ELPA Syncer <=
- [nongnu] elpa/treesit-fold d09b81affc 026/417: Add Cask, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 82224e74b5 031/417: Add badges, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 8953a0f68b 030/417: Add CI, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 399af3b42f 036/417: Add more doc, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold d81b48d22d 037/417: Organize order, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold cadd17a046 038/417: Add emoji, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold cd5ba51961 046/417: Update CASK, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 9f6b47beaf 053/417: Split summary, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 89d353d40f 051/417: Update order, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 5efa12917f 049/417: Update address, ELPA Syncer, 2024/07/01