emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/treesit-fold 2014def24a 289/417: feat: Add Verilog support


From: ELPA Syncer
Subject: [nongnu] elpa/treesit-fold 2014def24a 289/417: feat: Add Verilog support (#75)
Date: Mon, 1 Jul 2024 10:02:36 -0400 (EDT)

branch: elpa/treesit-fold
commit 2014def24aebf05e2c4a327fbc54be20ab6c8438
Author: Jen-Chieh Shen <jcs090218@gmail.com>
Commit: GitHub <noreply@github.com>

    feat: Add Verilog support (#75)
    
    * feat: Add Verilog support
    
    * progress
    
    * match next line
---
 CHANGELOG.md       |  1 +
 README.md          |  1 +
 ts-fold-parsers.el |  9 +++++++
 ts-fold-summary.el |  1 +
 ts-fold-util.el    | 18 ++++++++++++++
 ts-fold.el         | 72 +++++++++++++++++++++++++++++++++++++++++-------------
 6 files changed, 85 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4c6d7e46e5..5177103cc6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,6 +33,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for 
recommendations on how
 * Add GDScript support (#72)
 * Add Scheme support (#73)
 * Add Beancount support (#74)
+* Add Verilog support (#75)
 
 ## 0.1.0
 > Released Oct 18, 2021
diff --git a/README.md b/README.md
index 8def7c4855..276af85532 100644
--- a/README.md
+++ b/README.md
@@ -132,6 +132,7 @@ These languages are fairly complete:
 - R / Ruby / Rust
 - Scala / Scheme / Swift
 - TOML / TypeScript / TSX
+- Verilog
 - YAML
 
 These languages are in development:
diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el
index 9cdf533621..1ffe47acd7 100644
--- a/ts-fold-parsers.el
+++ b/ts-fold-parsers.el
@@ -66,6 +66,8 @@
 (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-toml-table "ts-fold.el")
+(declare-function ts-fold-range-verilog-list "ts-fold.el")
+(declare-function ts-fold-range-verilog-module "ts-fold.el")
 
 ;;
 ;; (@* "Parsers" )
@@ -386,6 +388,13 @@
   "Rule set for TypeScript."
   (append (ts-fold-parsers-javascript)))
 
+(defun ts-fold-parsers-verilog ()
+  "Rule set for Verilog."
+  '((module_declaration       . ts-fold-range-verilog-module)
+    (list_of_port_connections . ts-fold-range-verilog-list)
+    (initial_construct        . ts-fold-range-initial-construct)
+    (comment                  . ts-fold-range-c-like-comment)))
+
 (defun ts-fold-parsers-yaml ()
   "Rule set for YAML."
   '((comment
diff --git a/ts-fold-summary.el b/ts-fold-summary.el
index 52a5731da1..0f3d47768d 100644
--- a/ts-fold-summary.el
+++ b/ts-fold-summary.el
@@ -242,6 +242,7 @@ type of content by checking the word boundary's existence."
     (toml-mode         . ts-fold-summary-javadoc)
     (conf-toml-mode    . ts-fold-summary-javadoc)
     (typescript-mode   . ts-fold-summary-javadoc)
+    (verilog-mode      . ts-fold-summary-javadoc)
     (nxml-mode         . ts-fold-summary-xml))
   "Alist mapping `major-mode' to doc parser function."
   :type '(alist :key-type symbol :value-type function)
diff --git a/ts-fold-util.el b/ts-fold-util.el
index 58face2122..d66850dd37 100644
--- a/ts-fold-util.el
+++ b/ts-fold-util.el
@@ -78,6 +78,24 @@ Optional argument TRIM, see function `ts-fold--get-face'."
 Optional argument TRIM, see function `ts-fold--get-face'."
   (ts-fold--is-face obj ts-fold--doc-faces trim))
 
+;;
+;; (@* "Positions" )
+;;
+
+(defun ts-fold--last-eol (pos)
+  "Go to POS then find previous line break, and return its position."
+  (save-excursion
+    (goto-char pos)
+    (max 1 (1- (line-beginning-position)))))
+
+(defun ts-fold--bol (point)
+  "Return line beginning position at POINT."
+  (save-excursion (goto-char point) (line-beginning-position)))
+
+(defun ts-fold--eol (point)
+  "Return line end position at POINT."
+  (save-excursion (goto-char point) (line-end-position)))
+
 ;;
 ;; (@* "Math" )
 ;;
diff --git a/ts-fold.el b/ts-fold.el
index 2aa2969981..dea42f5f17 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -104,6 +104,7 @@
     (conf-toml-mode  . ,(ts-fold-parsers-toml))
     (tuareg-mode     . ,(ts-fold-parsers-ocaml))
     (typescript-mode . ,(ts-fold-parsers-typescript))
+    (verilog-mode    . ,(ts-fold-parsers-verilog))
     (yaml-mode       . ,(ts-fold-parsers-yaml)))
   "An alist of (major-mode . (foldable-node-type . function)).
 
@@ -485,23 +486,17 @@ more information."
           (ts-fold-range-line-comment node offset "///")
         (ts-fold-range-line-comment node offset "//")))))
 
-(defun ts-fold-point-before-line-break (pos)
-  "Go to POS then find previous line break, and return its position."
-  (save-excursion
-    (goto-char pos)
-    (max 1 (1- (line-beginning-position)))))
-
 ;;
 ;; (@* "Languages" )
 ;;
 
 (defun ts-fold-range-beancount-transaction (node offset)
-  "Define fold range for `transaction' preprocessor.
+  "Define fold range for `transaction' in Beancount.
 
 For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
 more information."
   (when-let* ((beg (tsc-node-start-position node))
-              (beg (save-excursion (goto-char beg) (line-end-position)))
+              (beg (ts-fold--eol beg))
               (end (1- (tsc-node-end-position node))))
     (ts-fold--cons-add (cons beg end) offset)))
 
@@ -565,6 +560,8 @@ more information."
               (next-node (tsc-get-next-sibling param-node))
               (beg (tsc-node-start-position next-node))
               (end (1- (tsc-node-end-position node))))
+    (unless ts-fold-on-next-line  ; display nicely
+      (setq beg (ts-fold--last-eol beg)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-elisp-function (node offset)
@@ -576,6 +573,8 @@ more information."
   (when-let* ((param-node (tsc-get-nth-child node 4))
               (beg (tsc-node-start-position param-node))
               (end (1- (tsc-node-end-position node))))
+    (unless ts-fold-on-next-line  ; display nicely
+      (setq beg (ts-fold--last-eol beg)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-elixir (node offset)
@@ -595,7 +594,7 @@ more information."
 For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
 more information."
   (when-let* ((beg (tsc-node-start-position node))
-              (beg (save-excursion (goto-char beg) (line-end-position)))
+              (beg (ts-fold--eol beg))
               (end-node (ts-fold-last-child node))
               (end (tsc-node-end-position end-node)))
     (ts-fold--cons-add (cons beg end) offset)))
@@ -654,7 +653,7 @@ more information."
          (beg (tsc-node-end-position params))
          (end (- (tsc-node-end-position node) 3)))  ; fit identifier `end'
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-lua-if (node offset)
@@ -670,7 +669,7 @@ more information."
                   (tsc-node-start-position (car next))
                 (- (tsc-node-end-position node) 3))))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-lua-elseif (node offset)
@@ -685,7 +684,7 @@ more information."
                   (tsc-node-start-position next)
                 (tsc-node-end-position node))))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-lua-else (node offset)
@@ -697,7 +696,7 @@ more information."
          (next (tsc-get-next-sibling node))          ; the `end' node
          (end (tsc-node-start-position next)))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-lua-do-loop (node offset)
@@ -709,7 +708,7 @@ more information."
          (beg (tsc-node-end-position do))
          (end (- (tsc-node-end-position node) 3)))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-lua-repeat (node offset)
@@ -721,7 +720,7 @@ more information."
          (until (car (ts-fold-find-children node "until")))
          (end (tsc-node-start-position until)))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 ;;+ OCaml
@@ -824,7 +823,7 @@ more information."
               (end (tsc-node-end-position node))
               (end (- end 3)))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-ruby-if (node offset)
@@ -838,7 +837,7 @@ more information."
                          ((when-let ((parent (ts-fold-find-parent node "if")))
                             (- (tsc-node-end-position parent) 3))))))
     (when ts-fold-on-next-line  ; display nicely
-      (setq end (ts-fold-point-before-line-break end)))
+      (setq end (ts-fold--last-eol end)))
     (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-rust-macro (node offset)
@@ -863,5 +862,44 @@ more information."
               (end (tsc-node-end-position end-child)))
     (ts-fold--cons-add (cons beg end) offset)))
 
+(defun ts-fold-range-initial-construct (node offset)
+  "Return the fold range for `initial' NODE in Verilog.
+
+For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
+more information."
+  (when-let* ((beg (tsc-node-start-position node))
+              (beg (ts-fold--eol beg))
+              (end-child (ts-fold-last-child node))
+              (end (tsc-node-end-position end-child))
+              (end (ts-fold--bol end)))
+    (when ts-fold-on-next-line
+      (setq end (ts-fold--last-eol end)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
+(defun ts-fold-range-verilog-list (node offset)
+  "Return the fold range for `list' NODE in Verilog.
+
+For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
+more information."
+  (when-let* ((prev (tsc-get-prev-sibling node))
+              (next (tsc-get-next-sibling node))
+              (beg (tsc-node-end-position prev))
+              (end (tsc-node-start-position next)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
+(defun ts-fold-range-verilog-module (node offset)
+  "Return the fold range for `module' NODE in Verilog.
+
+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))
+              (end (ts-fold--bol end)))
+    (when ts-fold-on-next-line
+      (setq end (ts-fold--last-eol end)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
 (provide 'ts-fold)
 ;;; ts-fold.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]