From 9aa67b489a4ba85381f725050370f728500e33e0 Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Fri, 19 Apr 2024 12:05:04 -0700 Subject: [PATCH] query for text of current toplevel form --- emacs/coalton-mode.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/emacs/coalton-mode.el b/emacs/coalton-mode.el index 271936578..1db9738d5 100644 --- a/emacs/coalton-mode.el +++ b/emacs/coalton-mode.el @@ -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)