Skip to content

Commit

Permalink
query for text of current toplevel form
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwman committed Apr 23, 2024
1 parent acdcbb4 commit 9aa67b4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions emacs/coalton-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,28 @@
(let ((nodes (treesit-query-capture 'coalton coalton--query-package)))
(treesit-node-text (cdr (assoc 'package-name nodes)) t)))

(defun coalton--find-parent (node pred)
"Find first parent of NODE matching PRED."
(cond ((null node)
nil)
((funcall pred node)
node)
(t
(coalton--find-parent (treesit-node-parent node) pred))))

(defun coalton--toplevel-form-p (node)
"Is NODE a toplevel program element?"
(and (coalton--list-p node)
(string-equal "program" (treesit-node-type
(treesit-node-parent node)))))

(defun coalton--node-at-point ()
(treesit-node-at (point)))

(defun coalton-toplevel-form ()
"Return the text of the toplevel form at point."
(when-let ((node (coalton--find-parent (coalton--node-at-point)
#'coalton--toplevel-form-p)))
(treesit-node-text node t)))

(provide 'coalton-mode)

0 comments on commit 9aa67b4

Please sign in to comment.