branch: elpa/treesit-fold
commit 54d12c982252d17c828811cd06958a492083d7e6
Author: Jen-Chieh Shen <[email protected]>
Commit: GitHub <[email protected]>

    feat: Add TOML support (#68)
    
    * feat: Add TOML support
    
    * changelog
---
 CHANGELOG.md       |  1 +
 README.md          |  2 +-
 ts-fold-parsers.el | 22 ++++++++++++++++++----
 ts-fold-summary.el |  2 ++
 ts-fold.el         | 21 +++++++++++++++++----
 5 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a047e27820..50d287457f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for 
recommendations on how
 * Add Haskell support (#65)
 * Add Perl support (#66)
 * Add Markdown support (#67)
+* Add TOML support (#68)
 
 ## 0.1.0
 > Released Oct 18, 2021
diff --git a/README.md b/README.md
index 7a80c4fd5f..3798c041d4 100644
--- a/README.md
+++ b/README.md
@@ -130,7 +130,7 @@ These languages are fairly complete:
 - Perl / PHP / Python
 - R / Ruby / Rust
 - Scala / Swift
-- TypeScript / TSX
+- TOML / TypeScript / TSX
 - YAML
 
 These languages are in development:
diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el
index 14b361b925..d93ed73035 100644
--- a/ts-fold-parsers.el
+++ b/ts-fold-parsers.el
@@ -44,6 +44,8 @@
 (declare-function ts-fold-range-c-preproc-if "ts-fold.el")
 (declare-function ts-fold-range-c-preproc-elif "ts-fold.el")
 (declare-function ts-fold-range-c-preproc-else "ts-fold.el")
+(declare-function ts-fold-range-elisp-function "ts-fold.el")
+(declare-function ts-fold-range-elixir "ts-fold.el")
 (declare-function ts-fold-range-haskell-function "ts-fold.el")
 (declare-function ts-fold-range-html "ts-fold.el")
 (declare-function ts-fold-range-julia "ts-fold.el")
@@ -61,8 +63,7 @@
 (declare-function ts-fold-range-ruby-class-def "ts-fold.el")
 (declare-function ts-fold-range-ruby-if "ts-fold.el")
 (declare-function ts-fold-range-rust-macro "ts-fold.el")
-(declare-function ts-fold-range-elisp-function "ts-fold.el")
-(declare-function ts-fold-range-elixir "ts-fold.el")
+(declare-function ts-fold-range-toml-table "ts-fold.el")
 
 ;;
 ;; (@* "Parsers" )
@@ -331,14 +332,27 @@
     (extension_declaration . ts-fold-range-seq)
     (comment               . ts-fold-range-c-like-comment)))
 
+(defun ts-fold-parsers-toml ()
+  "Rule set for TOML."
+  '((table . ts-fold-range-toml-table)
+    (array . ts-fold-range-seq)
+    (comment
+     . (lambda (node offset)
+         (ts-fold-range-line-comment node offset "#")))))
+
 (defun ts-fold-parsers-typescript ()
   "Rule set for TypeScript."
   (append (ts-fold-parsers-javascript)))
 
 (defun ts-fold-parsers-yaml ()
   "Rule set for YAML."
-  '((comment . (lambda (node offset) (ts-fold-range-line-comment node offset 
"#")))
-    (block_mapping_pair . ((lambda (node offset) (ts-fold-range-markers node 
offset ":")) 0 1))))
+  '((comment
+     . (lambda (node offset)
+         (ts-fold-range-line-comment node offset "#")))
+    (block_mapping_pair
+     . ((lambda (node offset)
+          (ts-fold-range-markers node offset ":"))
+        0 1))))
 
 (provide 'ts-fold-parsers)
 ;;; ts-fold-parsers.el ends here
diff --git a/ts-fold-summary.el b/ts-fold-summary.el
index 63585f4f56..61bddad8d3 100644
--- a/ts-fold-summary.el
+++ b/ts-fold-summary.el
@@ -235,6 +235,8 @@ type of content by checking the word boundary's existence."
     (scala-mode        . ts-fold-summary-javadoc)
     (sh-mode           . ts-fold-summary-javadoc)
     (swift-mode        . ts-fold-summary-c)
+    (toml-mode         . ts-fold-summary-javadoc)
+    (conf-toml-mode    . ts-fold-summary-javadoc)
     (typescript-mode   . ts-fold-summary-javadoc)
     (nxml-mode         . ts-fold-summary-xml))
   "Alist mapping `major-mode' to doc parser function."
diff --git a/ts-fold.el b/ts-fold.el
index 40338d9ee5..5e2cf91b5c 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -95,6 +95,8 @@
     (sh-mode         . ,(ts-fold-parsers-bash))
     (scala-mode      . ,(ts-fold-parsers-scala))
     (swift-mode      . ,(ts-fold-parsers-swift))
+    (toml-mode       . ,(ts-fold-parsers-toml))
+    (conf-toml-mode  . ,(ts-fold-parsers-toml))
     (tuareg-mode     . ,(ts-fold-parsers-ocaml))
     (typescript-mode . ,(ts-fold-parsers-typescript))
     (yaml-mode       . ,(ts-fold-parsers-yaml)))
@@ -711,10 +713,10 @@ more information."
 
 For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
 more information."
-  (when-let* ((end_child (ts-fold-last-child node))
-              (do_child (tsc-get-nth-child node 1))
-              (beg (tsc-node-start-position do_child))
-              (end (tsc-node-start-position end_child)))
+  (when-let* ((end-child (ts-fold-last-child node))
+              (do-child (tsc-get-nth-child node 1))
+              (beg (tsc-node-start-position do-child))
+              (end (tsc-node-start-position end-child)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-julia (node offset)
@@ -820,5 +822,16 @@ more information."
       (setq end (ts-fold-point-before-line-break end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
+(defun ts-fold-range-toml-table (node offset)
+  "Return the fold range for `table' NODE in TOML.
+
+For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
+more information."
+  (when-let* ((close-bracket (car (ts-fold-find-children node "]")))
+              (beg (tsc-node-end-position close-bracket))
+              (end-child (ts-fold-last-child node))
+              (end (tsc-node-end-position end-child)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
 (provide 'ts-fold)
 ;;; ts-fold.el ends here

Reply via email to