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

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

[nongnu] elpa/treesit-fold b429fd33dc 087/417: Move face


From: ELPA Syncer
Subject: [nongnu] elpa/treesit-fold b429fd33dc 087/417: Move face
Date: Mon, 1 Jul 2024 10:02:14 -0400 (EDT)

branch: elpa/treesit-fold
commit b429fd33dcc45a1390620c6686085073177e5b39
Author: Jen-Chieh Shen <jcs090218@gmail.com>
Commit: Jen-Chieh Shen <jcs090218@gmail.com>

    Move face
---
 tree-sitter-fold-summary.el | 160 ++++++++++++++++++++++++++++++++++++++++----
 tree-sitter-fold-util.el    |  32 +++++++++
 2 files changed, 178 insertions(+), 14 deletions(-)

diff --git a/tree-sitter-fold-summary.el b/tree-sitter-fold-summary.el
index f3f2acdc96..802159f05f 100644
--- a/tree-sitter-fold-summary.el
+++ b/tree-sitter-fold-summary.el
@@ -50,6 +50,110 @@ This happens only when summary length is larger than 
variable
   :type 'string
   :group 'tree-sitter-fold)
 
+;;
+;; (@* "Externals" )
+;;
+
+(defvar tree-sitter-fold-replacement-face)
+
+;;
+;; (@* "Parsers" )
+;;
+
+(defun tree-sitter-fold-summary--valid-content-p (content)
+  "Return non-nil if CONTENT is a valid document string for extraction.
+Some programmers use some type of characters for splitting the code module
+into sections.  For instance, ===, ---, ///, =-=, etc.  Try to omit these
+type of content by checking the word boundary's existence."
+  (string-match-p "\\w" content))
+
+(defun tree-sitter-fold-summary--apply-sym (line sym)
+  "Remove SYM from LINE."
+  (when (string-prefix-p sym line)
+    (setq line (substring line (length sym) (length line))
+          line (string-trim line)))
+  line)
+
+(defun tree-sitter-fold-summary--extract-summary (doc-str sym)
+  "Extract only document summary from DOC-STR using SYM"
+  (let ((lines (split-string doc-str "\n")) new-lines)
+    (dolist (line lines)
+      (setq line (string-trim line))
+      (cond ((listp sym)
+             (dolist (c sym) (setq line (tree-sitter-fold-summary--apply-sym 
line c))))
+            (t (setq line (tree-sitter-fold-summary--apply-sym line sym))))
+      (when (tree-sitter-fold-summary--valid-content-p line) (push line 
new-lines)))
+    (reverse new-lines)))
+
+(defun tree-sitter-fold-summary--doc-extract (doc-str sym)
+  "Default way to extract the doc summary from DOC-STR."
+  (let* ((lines (tree-sitter-fold-summary--extract-summary doc-str sym)) 
(summary (nth 0 lines)))
+    (when summary (setq summary (string-trim summary)))
+    (if (string-empty-p summary) nil summary)))
+
+(defun tree-sitter-fold-summary--generic (doc-str sym)
+  "Generic DOC-STR extraction using SYM."
+  (when (tree-sitter-fold-util--doc-faces-p doc-str)
+    (tree-sitter-fold-summary--doc-extract doc-str sym)))
+
+(defun tree-sitter-fold-summary-batch (doc-str)
+  "Extract batch summary from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str '("::" "rem" "REM")))
+
+(defun tree-sitter-fold-summary-csharp-vsdoc (doc-str)
+  "Extract C# vsdoc summary from DOC-STR."
+  (let ((type-triple (string-match-p "///" doc-str)))
+    (setq doc-str (s-replace-regexp "<[/]*[^>]+." "" doc-str))
+    (tree-sitter-fold-summary--generic doc-str (if type-triple "///" "//"))))
+
+(defun tree-sitter-fold-summary-javadoc (doc-str)
+  "Extract javadoc summary from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str "*"))
+
+(defun tree-sitter-fold-summary-go (doc-str)
+  "Extract Go document summary from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str "//"))
+
+(defun tree-sitter-fold-summary-lua-doc (doc-str)
+  "Extract Lua document string from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str "--"))
+
+(defun tree-sitter-fold-summary-python-doc (doc-str)
+  "Extract Python document string from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str "\"\"\""))
+
+(defun tree-sitter-fold-summary-ruby-doc (doc-str)
+  "Extract Ruby document string from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str "#"))
+
+(defun tree-sitter-fold-summary-rust-doc (doc-str)
+  "Extract Rust document summary from DOC-STR."
+  (tree-sitter-fold-summary--generic doc-str "///"))
+
+(defun tree-sitter-fold-summary-c-macro (doc-str)
+  "Parse C macro summary from DOC-STR."
+  (when (tree-sitter-fold-util--is-face doc-str
+                                        '(font-lock-preprocessor-face
+                                          
preproc-font-lock-preprocessor-background))
+    (tree-sitter-fold-summary--doc-extract doc-str "")))
+
+(defun tree-sitter-fold-summary-c (doc-str)
+  "Summary parser for C from DOC-STR."
+  (or (tree-sitter-fold-summary-javadoc doc-str)
+      (tree-sitter-fold-summary-c-macro doc-str)))
+
+(defun tree-sitter-fold-summary-markdown (doc-str)
+  "Extract Makrdown block from DOC-STR."
+  (tree-sitter-fold-summary--doc-extract doc-str '()))
+
+(defun tree-sitter-fold-summary-org (doc-str)
+  "Extract Org block from DOC-STR."
+  (tree-sitter-fold-summary--doc-extract doc-str '()))
+
+;;
+;; (@* "Core" )
+;;
+
 (defun tree-sitter-fold-summary--keep-length (summary)
   "Keep the SUMMARY length to `tree-sitter-fold-summary-max-length'."
   (let ((len-sum (length summary))
@@ -63,24 +167,52 @@ This happens only when summary length is larger than 
variable
   "Return the SUMMARY that has added the summary prefix."
   (format tree-sitter-fold-summary-format summary))
 
-(defun tree-sitter-fold-summary--remove-comments (doc-str)
-  "Remove comments from DOC-STR."
-  ;;(s-replace-regexp "^[ \t]*[*]")
-  (s-replace-regexp (regexp-quote comment-start-skip) "" doc-str)
-  )
+(defun tree-sitter-fold-summary--parser ()
+  "Return the summary parser from `tree-sitter-fold-summary-parsers-alist'."
+  (assoc (buffer-local-value 'major-mode (current-buffer)) 
tree-sitter-fold-summary-parsers-alist))
 
 (defun tree-sitter-fold-summary--get (doc-str)
   "Extract summary from DOC-STR in order to display ontop of the overlay."
   (when (nth 4 (syntax-ppss))
-    (let ((summary (tree-sitter-fold-summary--remove-comments doc-str)))
-      (when (integerp tree-sitter-fold-summary-max-length)
-        (setq summary (tree-sitter-fold-summary--keep-length summary)))
-      (when summary
-        (setq summary (tree-sitter-fold-summary--apply-format summary)
-              summary (propertize summary 'face 
'tree-sitter-fold-replacement-face)))
-      summary
-      nil  ; TODO: Remove this later on
-      )))
+    (let ((parser (cdr (tree-sitter-fold-summary--parser))) summary)
+      (when parser
+        (setq summary (funcall parser doc-str))
+        (when (integerp tree-sitter-fold-summary-max-length)
+          (setq summary (tree-sitter-fold-summary--keep-length summary)))
+        (when summary
+          (setq summary (tree-sitter-fold-summary--apply-format summary)
+                summary (propertize summary 'face 
'tree-sitter-fold-replacement-face))))
+      summary)))
+
+(defcustom tree-sitter-fold-summary-parsers-alist
+  `((actionscript-mode . tree-sitter-fold-summary-javadoc)
+    (bat-mode          . tree-sitter-fold-summary-batch)
+    (c-mode            . tree-sitter-fold-summary-c)
+    (c++-mode          . tree-sitter-fold-summary-c)
+    (csharp-mode       . tree-sitter-fold-summary-csharp-vsdoc)
+    (go-mode           . tree-sitter-fold-summary-go)
+    (java-mode         . tree-sitter-fold-summary-javadoc)
+    (javascript-mode   . tree-sitter-fold-summary-javadoc)
+    (js-mode           . tree-sitter-fold-summary-javadoc)
+    (js2-mode          . tree-sitter-fold-summary-javadoc)
+    (js3-mode          . tree-sitter-fold-summary-javadoc)
+    (kotlin-mode       . tree-sitter-fold-summary-javadoc)
+    (lua-mode          . tree-sitter-fold-summary-lua-doc)
+    (markdown-mode     . tree-sitter-fold-summary-markdown)
+    (objc-mode         . tree-sitter-fold-summary-c)
+    (org-mode          . tree-sitter-fold-summary-org)
+    (php-mode          . tree-sitter-fold-summary-javadoc)
+    (python-mode       . tree-sitter-fold-summary-python-doc)
+    (rjsx-mode         . tree-sitter-fold-summary-javadoc)
+    (ruby-mode         . tree-sitter-fold-summary-ruby-doc)
+    (rust-mode         . tree-sitter-fold-summary-rust-doc)
+    (scala-mode        . tree-sitter-fold-summary-javadoc)
+    (sh-mode           . tree-sitter-fold-summary-javadoc)
+    (swift-mode        . tree-sitter-fold-summary-c)
+    (typescript-mode   . tree-sitter-fold-summary-javadoc))
+  "Alist mapping major-mode to doc parser function."
+  :type 'hook
+  :group 'tree-sitter-fold)
 
 (provide 'tree-sitter-fold-summary)
 ;;; tree-sitter-fold-summary.el ends here
diff --git a/tree-sitter-fold-util.el b/tree-sitter-fold-util.el
index 8b00b0f7d6..d0d2cae682 100644
--- a/tree-sitter-fold-util.el
+++ b/tree-sitter-fold-util.el
@@ -48,5 +48,37 @@
         (push ov lst)))
     lst))
 
+;;
+;; (@* "Face" )
+;;
+
+(defvar tree-sitter-fold-util--doc-faces
+  '(font-lock-doc-face
+    font-lock-comment-face
+    font-lock-comment-delimiter-face
+    tree-sitter-hl-face:comment
+    tree-sitter-hl-face:doc
+    hl-todo)
+  "List of face that apply for document string.")
+
+(defun tree-sitter-fold-util--get-face (obj trim)
+  "Return face name from OBJ.
+If argument TRIM is non-nil, trim the OBJ."
+  (get-text-property 0 'face (if trim (string-trim obj) obj)))
+
+(defun tree-sitter-fold-util--is-face (obj lst-face &optional trim)
+  "Return non-nil if OBJ's face is define inside list LST-FACE.
+Optional argument TRIM, see function `tree-sitter-fold-util--get-face'."
+  (unless (listp lst-face) (setq lst-face (list lst-face)))
+  (let ((faces (tree-sitter-fold-util--get-face obj trim)))
+    (cond ((listp faces)
+           (cl-some (lambda (face) (memq face lst-face)) faces))
+          (t (memq faces lst-face)))))
+
+(defun tree-sitter-fold-util--doc-faces-p (obj &optional trim)
+  "Return non-nil if face at OBJ is within `tree-sitter-fold-util--doc-faces' 
list.
+Optional argument TRIM, see function `tree-sitter-fold-util--get-face'."
+  (tree-sitter-fold-util--is-face obj tree-sitter-fold-util--doc-faces trim))
+
 (provide 'tree-sitter-fold-util)
 ;;; tree-sitter-fold-util.el ends here



reply via email to

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