[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
- [nongnu] elpa/treesit-fold f49df1ea3d 124/417: Update doc, (continued)
- [nongnu] elpa/treesit-fold f49df1ea3d 124/417: Update doc, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 6f15c1735a 125/417: Support javascript comment, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold eeb9c03bcc 127/417: Fix conitouse line, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold c33e5e7e0c 128/417: Temporily disable csharp, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 6b6f30eb4a 142/417: Add emoji, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 6aabbc6256 147/417: Update doc, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold e6ac78f0e2 154/417: SCALE AND CENTER, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold a806886568 161/417: Update support language list, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 2409596f33 004/417: readme: fix typo, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 362fc74774 011/417: R: fold brace_list instead, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold f0858f1dbc 009/417: add support for R,
ELPA Syncer <=
- [nongnu] elpa/treesit-fold b3faaaa40d 015/417: support folding nix functions, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold de5ad4b2f2 063/417: Split uitl, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold d07b2bae49 065/417: Complete indicators, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold d2c13aac81 069/417: Clean compile, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 4d4526ffb3 073/417: Update core, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 2dfd8c1656 083/417: Add support lang, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 5c9981872e 095/417: Restrict one, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 2ba784bea0 093/417: Fix externals, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold f13f0c371b 102/417: Core, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 80b8e2b6dc 138/417: Fix code block syntax, ELPA Syncer, 2024/07/01