branch: elpa/typst-ts-mode
commit f7790390aa182ae5279eaa77fe94e53e3ed3ee2e
Author: meowking <mr.meowk...@posteo.com>
Commit: meowking <mr.meowk...@posteo.com>

    feat: experimental custom auto-fill-function
---
 typst-ts-editing.el | 32 ++++++++++++++++++++++++++------
 typst-ts-mode.el    |  4 ++++
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/typst-ts-editing.el b/typst-ts-editing.el
index a1badb2e6e..9b4418957d 100644
--- a/typst-ts-editing.el
+++ b/typst-ts-editing.el
@@ -477,13 +477,33 @@ When there is no section it will insert a heading below 
point."
     (unless (eq execute-result 'success)
       (call-interactively (global-key-binding (kbd "TAB"))))))
 
-(defun typst-ts-mode-auto-fill-function ()
-  "Function for `auto-fill-mode'.
-
-Inserts newline and indents according to context."
+(defun typst-ts-editing-calculate-fill-prefix ()
+  "Calculate fill prefix."
+  (let ((fill-prefix nil))
+    (setq
+     fill-prefix
+     (catch 'fill-prefix
+       (let* ((cur-pos (point))
+              (cur-node (treesit-node-at cur-pos))
+              (cur-node-type (treesit-node-type cur-node))
+              (parent-node (treesit-node-parent cur-node))  ; could be nil
+              (parent-node-type (treesit-node-type parent-node))
+              node)
+         (cond
+          ((setq node (typst-ts-core-parent-util-type
+                       (typst-ts-core-get-parent-of-node-at-bol-nonwhite)
+                       "item" t t))
+           (throw 'fill-prefix (fill-context-prefix (line-beginning-position) 
(line-end-position)))))
+         )))
+    fill-prefix))
+
+(defun typst-ts-editing-auto-fill-function ()
+  "Auto Fill Function for `auto-fill-mode'."
   (when (>= (current-column) (current-fill-column))
-    (insert "\n")
-    (typst-ts-mode-indent-line-function)))
+    (let* ((fill-prefix (typst-ts-editing-calculate-fill-prefix))
+                (adaptive-fill-mode (null fill-prefix)))
+           (when fill-prefix (do-auto-fill)))))
+
 
 (provide 'typst-ts-editing)
 
diff --git a/typst-ts-mode.el b/typst-ts-mode.el
index 5843129b78..7551d9dd16 100644
--- a/typst-ts-mode.el
+++ b/typst-ts-mode.el
@@ -626,6 +626,10 @@ typst tree sitter grammar (at least %s)!" 
(current-time-string min-time))
     (setq-local outline-level #'typst-ts-mode-outline-level))
   (setq-local outline-heading-alist typst-ts-mode-outline-heading-alist)
 
+
+  ;; auto fill function
+  (setq-local normal-auto-fill-function 'typst-ts-editing-auto-fill-function)
+
   (treesit-major-mode-setup)
 
   (setq-local indent-line-function #'typst-ts-mode-indent-line-function))

Reply via email to