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

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

[nongnu] elpa/treesit-fold f0858f1dbc 009/417: add support for R


From: ELPA Syncer
Subject: [nongnu] elpa/treesit-fold f0858f1dbc 009/417: add support for R
Date: Mon, 1 Jul 2024 10:02:04 -0400 (EDT)

branch: elpa/treesit-fold
commit f0858f1dbc0c4f3857c97dc96a2d45c2ecf9bf0e
Author: Junyi Hou <junyi.yi.hou@gmail.com>
Commit: Junyi Hou <junyi.yi.hou@gmail.com>

    add support for R
---
 readme.org          | 43 +++++++++++++++++++++++++++++++++++++++++++
 tree-sitter-fold.el | 17 +++++++++++++++--
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/readme.org b/readme.org
index 5714118927..e6ffefa03e 100644
--- a/readme.org
+++ b/readme.org
@@ -47,12 +47,55 @@ This variable determines how ~tree-sitter-fold~ should fold 
each of the nodes de
 * Supported languages
 
 - python
+- R (need to compile [[https://github.com/r-lib/tree-sitter-r][tree-sitter-r]] 
grammar, see [[Note on installing additional grammars to ~emacs-tree-sitter~]])
 - contribution of other languages are welcome!
 
 * Contribution
 
 Enable ~tree-sitter-mode~ first, then ~tree-sitter-query-builder~ is useful to 
test out queries that determine what syntax nodes should be foldable and how to 
fold them. emacs-tree-sitter has 
[[https://ubolonton.github.io/emacs-tree-sitter/syntax-highlighting/queries/][an
 excellent documentation]] on how to write ~tree-sitter~ queries.
 
+* Note on installing additional grammars to ~emacs-tree-sitter~
+
+Following the following steps to build additional grammar for 
~emacs-tree-sitter~.
+
+1. make sure ~tree-sitter~ is installed and up-to-date. To check, run
+   #+begin_src shell
+     tree-sitter --version
+   #+end_src
+   The new ABI requires ~0.19.3~ or newer version of ~tree-sitter~. ~Node.js~ 
is also required to generate grammar parsers. See 
[[https://github.com/tree-sitter/tree-sitter/blob/master/cli/README.md][here]] 
for detailed instructions of how to install tree-sitter CLI tools.
+
+2. add new grammar repos to ~tree-sitter-langs-git-dir~. In the case of ~R~, 
run
+   #+begin_src emacs-lisp
+     ;; make sure I do not re-add the submodule multiple times
+     (condition-case _
+         (tree-sitter-langs--repo-status 'r)
+       (error
+        (let ((default-directory tree-sitter-langs-git-dir))
+          (tree-sitter-langs--call
+           "git" "submodule" "add" "https://github.com/r-lib/tree-sitter-r"; 
"repos/r"))))
+   #+end_src
+
+3. compile the new grammar parser by running
+   #+begin_src emacs-lisp
+     ;; compile only if haven't done so
+     (unless (--filter (string= (f-base it) "r") (f-entries 
(tree-sitter-langs--bin-dir)))
+       (tree-sitter-langs-compile 'r))
+   #+end_src
+
+4. register the respective major mode with the newly compiled grammar
+   #+begin_src emacs-lisp
+     (unless (assq 'ess-r-mode tree-sitter-major-mode-language-alist)
+       (add-to-list 'tree-sitter-major-mode-language-alist '(ess-r-mode . r)))
+   #+end_src
+
+5. require the new grammar
+
+   #+begin_src emacs-lisp
+     (tree-sitter-require 'r)
+   #+end_src
+
+I have those things together in a function 
~gatsby:install-and-load-tree-sitter-r~ at 
[[https://github.com/junyi-hou/dotfiles/blob/main/main.org#R][here]] and hook 
it to ~ess-r-mode-hook~.
+
 * License
 
 MIT
diff --git a/tree-sitter-fold.el b/tree-sitter-fold.el
index 69ce486262..40d0ee51d4 100644
--- a/tree-sitter-fold.el
+++ b/tree-sitter-fold.el
@@ -30,14 +30,16 @@
   :prefix "tree-sitter-fold-")
 
 (defcustom tree-sitter-fold-foldable-node-alist
-  '((python-mode . (function_definition class_definition)))
+  '((python-mode . (function_definition class_definition))
+    (ess-r-mode . (function_definition)))
   "An alist of (mode . (list of tree-sitter-nodes considered foldable in this 
mode))."
   :type '(alist :key-type symbol :value-type (repeat symbol))
   :group 'tree-sitter-fold)
 
 (defcustom tree-sitter-fold-range-alist
   '((python-mode . ((function_definition . tree-sitter-fold-range-python)
-                    (class_definition . tree-sitter-fold-range-python))))
+                    (class_definition . tree-sitter-fold-range-python)))
+    (ess-r-mode . ((function_definition . tree-sitter-fold-range-r))))
   "An alist of (major-mode . (foldable-node-type . function)).
 FUNCTION is used to determine where the beginning and end for 
FOLDABLE-NODE-TYPE
 in MAJOR-MODE.  It should take a single argument (the syntax node with type
@@ -229,5 +231,16 @@ If the current syntax node is not foldable, do nothing."
          (end (tsc-node-end-position node)))
     (cons beg end)))
 
+(defun tree-sitter-fold-range-r (node)
+  "Return the fold range for `function_definition' NODE in R."
+  (let ((node (tsc-get-nth-named-child node 0)))
+    (while (not (eq (tsc-node-type node) 'brace_list))
+      (setq node (tsc-get-next-named-sibling node)))
+    ;; now node is a brace_list
+    (let ((beg (tsc-node-end-position (tsc-get-nth-child node 0)))
+          (end (1- (tsc-node-end-position node))))
+      (cons beg end))))
+
+
 (provide 'tree-sitter-fold)
 ;;; tree-sitter-fold.el ends here



reply via email to

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