Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more nodes to elisp parser, remove s dependency, add vimscript #6

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Eask
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
(source "melpa")

(depends-on "emacs" "29.1")
(depends-on "s")
(depends-on "fringe-helper")

(setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ the tree-sitter syntax tree.
- [🖥 Usage](#🖥-usage)
- [📇 Commands](#📇-commands)
- [🔨 Supported languages](#🔨-supported-languages)
- [🚀 Add support for non-ts modes](#🚀-Add support for non-ts modes)
- [🚀 Add support for non-ts modes](#🚀-Add-support-for-non-ts-modes)
- [📝 Customization](#📝-customization)
- [⚪ Folding on new nodes](#⚪-folding-on-new-nodes)
- [❔ Example](#❔-example)
Expand Down Expand Up @@ -198,8 +198,8 @@ mode and the value being another alist of fold definitions.
So you can select whatever node that you want to fold on it.

To find what node you'll want to fold closed, refer to the
[tree-sitter documentation](https://emacs-tree-sitter.github.io/getting-started/#view-the-syntax-tree)
about viewing nodes. `tree-sitter-debug` and `tree-sitter-query-builder`
[Emacs tree-sitter documentation](https://www.gnu.org/software/emacs/manual/html_node/elisp/Pattern-Matching.html)
about viewing nodes. `treesit-inspect-mode` and `treesit-explore-mode`
are both very useful for this.

For the folding functions, treesit-fold provides some default
Expand Down Expand Up @@ -463,16 +463,16 @@ Please note that turning on `treesit-fold-indicators-mode` automatically turns o
`treesit-fold-mode` as well. Though, turning off `treesit-fold-indicators-mode` does not
turn off `treesit-fold-mode`

- To enable this automatically whenever `tree-sitter-mode` is enabled, use the global indicator mode:
- To enable this automatically whenever a buffer has a tree-sitter parser, use the global indicator mode:

```elisp
(global-treesit-fold-indicators-mode 1)
```

Else, a hook can be added to tree-sitter directly.
Else, you can add a hook directly.

```elisp
(add-hook 'tree-sitter-after-on-hook #'treesit-fold-indicators-mode)
(add-hook 'c-ts-mode-hook #'treesit-fold-indicators-mode)
```

- To switch to left/right fringe: (Default is `left-fringe`)
Expand Down Expand Up @@ -585,9 +585,9 @@ This plugin makes line comment into foldable range.
[![Donate on paypal](https://img.shields.io/badge/paypal-donate-1?logo=paypal&color=blue)](https://www.paypal.me/jcs090218)
[![Become a patron](https://img.shields.io/badge/patreon-become%20a%20patron-orange.svg?logo=patreon)](https://www.patreon.com/jcs090218)

Enable `tree-sitter-mode` first, then `tree-sitter-query-builder` is useful to test
Ensure your buffer has a tree-sitter parser first, then `treesit-explore-mode` is useful to test
out queries that determine what syntax nodes should be foldable and how to fold
them. [emacs-tree-sitter](https://ubolonton.github.io/emacs-tree-sitter/syntax-highlighting/queries/)
them. [Emacs repository](https://git.savannah.gnu.org/cgit/emacs.git/tree/admin/notes/tree-sitter/starter-guide#n130) and [Emacs tree-sitter manual](https://www.gnu.org/software/emacs/manual/html_node/elisp/Pattern-Matching.html)
has an excellent documentation on how to write `tree-sitter` queries.

### 🔬 Development
Expand Down Expand Up @@ -658,13 +658,12 @@ To look for the correct node you have three options:
This will display a buffer with the whole s-expr representing nodes at point from the current file/buffer.
- `M-x treesit-inspect-node-at-point` in your function to display what your
function is seeing.
- Also you can use `M-x treesit-inspect-mode` which diplay in the mode-line the current node at point.
- use `M-x treesit-inspect-mode` which diplay in the mode-line the current node at point.

> ⚠️ Warning
>
> Make sure you look into the correct repository. Repositories are managed
> under [tree-sitter-langs](https://github.com/emacs-tree-sitter/tree-sitter-langs)'s
> using git submodule. Some tree-sitter module aren't using the latest version!
> Make sure you look into the correct repository. Some repositories are managed
> under https://github.com/tree-sitter/[lang].

### ❓ How to create a summary parser?

Expand Down
10 changes: 10 additions & 0 deletions treesit-fold-parsers.el
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
(t
(treesit-fold-range-c-like-comment node offset))))))))

(defun treesit-fold-parsers-awk ()
"Rule set for Awk."
;; TODO: Complete this function
(append (treesit-fold-parsers-c)))

(defun treesit-fold-parsers-bash ()
"Rule set for Bash."
'((compound_statement . treesit-fold-range-seq)
Expand Down Expand Up @@ -664,6 +669,11 @@
(enumeration_type_definition . treesit-fold-range-seq)
(comment . treesit-fold-range-lua-comment)))

(defun treesit-fold-parsers-vimscript ()
"Rule set for Vimscript."
'((function_definition . treesit-fold-range-vimscript-function)
(if_statement . (treesit-fold-range-seq 1 -4))))

(defun treesit-fold-parsers-xml ()
"Rule set for XML."
'((element . treesit-fold-range-html)
Expand Down
4 changes: 1 addition & 3 deletions treesit-fold-summary.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

;;; Code:

(require 's)

(require 'treesit-fold-util)

(defcustom treesit-fold-summary-show t
Expand Down Expand Up @@ -105,7 +103,7 @@ type of content by checking the word boundary's existence."
(defun treesit-fold-summary-csharp-vsdoc (doc-str)
"Extract summary from DOC-STR in C# vsdoc."
(let ((type-triple (string-match-p "///" doc-str)))
(setq doc-str (s-replace-regexp "<[/]*[^>]+." "" doc-str)) ; should use `replace-regexp-in-string'?
(setq doc-str (replace-regexp-in-string "<[/]*[^>]+." "" doc-str))
(treesit-fold-summary--generic doc-str (if type-triple "///" "//"))))

(defun treesit-fold-summary-csharp (doc-str)
Expand Down
32 changes: 26 additions & 6 deletions treesit-fold.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
;; Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacs-tree-sitter/treesit-fold
;; Version: 0.1.0
;; Package-Requires: ((emacs "29.1") (s "1.9.0") (fringe-helper "1.0.1"))
;; Package-Requires: ((emacs "29.1") (fringe-helper "1.0.1"))
;; Keywords: convenience folding tree-sitter

;; This file is NOT part of GNU Emacs.
Expand Down Expand Up @@ -40,7 +40,6 @@
(require 'seq)
(require 'subr-x)

(require 's)
(require 'treesit)

(require 'treesit-fold-util)
Expand All @@ -63,7 +62,8 @@
(agda-mode . ,(treesit-fold-parsers-agda))
(arduino-mode . ,(treesit-fold-parsers-arduino))
(asm-mode . ,(treesit-fold-parsers-asm))
(awk-ts-mode . ,(treesit-fold-parsers-c))
(awk-mode . ,(treesit-fold-parsers-awk))
(awk-ts-mode . ,(treesit-fold-parsers-awk))
(fasm-mode . ,(treesit-fold-parsers-asm))
(masm-mode . ,(treesit-fold-parsers-asm))
(nasm-mode . ,(treesit-fold-parsers-asm))
Expand Down Expand Up @@ -124,6 +124,7 @@
(julia-mode . ,(treesit-fold-parsers-julia))
(julia-ts-mode . ,(treesit-fold-parsers-julia))
(kotlin-mode . ,(treesit-fold-parsers-kotlin))
(kotlin-ts-mode . ,(treesit-fold-parsers-kotlin))
(latex-mode . ,(treesit-fold-parsers-latex))
(latex-ts-mode . ,(treesit-fold-parsers-latex))
(LaTeX-mode . ,(treesit-fold-parsers-latex))
Expand All @@ -141,19 +142,20 @@
(makefile-bsdmake-mode . ,(treesit-fold-parsers-make))
(makefile-imake-mode . ,(treesit-fold-parsers-make))
(markdown-mode . ,(treesit-fold-parsers-markdown))
(markdown-ts-mode . ,(treesit-fold-parsers-markdown))
(matlab-mode . ,(treesit-fold-parsers-matlab))
(mermaid-mode . ,(treesit-fold-parsers-mermaid))
(mermaid-ts-mode . ,(treesit-fold-parsers-mermaid))
(ninja-mode . ,(treesit-fold-parsers-ninja))
(noir-mode . ,(treesit-fold-parsers-noir))
(noir-ts-mode . ,(treesit-fold-parsers-noir))
(nix-mode . ,(treesit-fold-parsers-nix))
(nix-ts-mode . ,(treesit-fold-parsers-nix))
(ocaml-mode . ,(treesit-fold-parsers-ocaml))
(ocaml-ts-mode . ,(treesit-fold-parsers-ocaml))
(org-mode . ,(treesit-fold-parsers-org))
(pascal-mode . ,(treesit-fold-parsers-pascal))
(pascal-ts-mode . ,(treesit-fold-parsers-pascal))
(perl-mode . ,(treesit-fold-parsers-perl))
(perl-ts-mode . ,(treesit-fold-parsers-perl))
(php-mode . ,(treesit-fold-parsers-php))
(php-ts-mode . ,(treesit-fold-parsers-php))
(python-mode . ,(treesit-fold-parsers-python))
Expand All @@ -167,9 +169,9 @@
(rust-ts-mode . ,(treesit-fold-parsers-rust))
(rustic-mode . ,(treesit-fold-parsers-rust))
(scheme-mode . ,(treesit-fold-parsers-scheme))
(scheme-ts-mode . ,(treesit-fold-parsers-scheme))
(sh-mode . ,(treesit-fold-parsers-bash))
(scala-mode . ,(treesit-fold-parsers-scala))
(scala-ts-mode . ,(treesit-fold-parsers-scala))
(sql-mode . ,(treesit-fold-parsers-sql))
(svelte-mode . ,(treesit-fold-parsers-svelte))
(swift-mode . ,(treesit-fold-parsers-swift))
Expand All @@ -179,12 +181,17 @@
(conf-toml-mode . ,(treesit-fold-parsers-toml))
(tuareg-mode . ,(treesit-fold-parsers-ocaml))
(typescript-mode . ,(treesit-fold-parsers-typescript))
(typescript-ts-mode . ,(treesit-fold-parsers-typescript))
(tsx-ts-mode . ,(treesit-fold-parsers-typescript))
(verilog-mode . ,(treesit-fold-parsers-verilog))
(verilog-ts-mode . ,(treesit-fold-parsers-verilog))
(vhdl-mode . ,(treesit-fold-parsers-vhdl))
(vhdl-ts-mode . ,(treesit-fold-parsers-vhdl))
(vimscript-ts-mode . ,(treesit-fold-parsers-vimscript))
(nxml-mode . ,(treesit-fold-parsers-xml))
(xml-ts-mode . ,(treesit-fold-parsers-xml))
(yaml-mode . ,(treesit-fold-parsers-yaml))
(yaml-ts-mode . ,(treesit-fold-parsers-yaml))
(k8s-mode . ,(treesit-fold-parsers-yaml))
(zig-mode . ,(treesit-fold-parsers-zig)))
"An alist of (major-mode . (foldable-node-type . function)).
Expand Down Expand Up @@ -399,6 +406,7 @@ This function is borrowed from `tree-sitter-node-at-point'."
(treesit-fold-summary--get (buffer-substring beg end)))
(propertize treesit-fold-replacement
'mouse-face 'treesit-fold-replacement-mouse-face
'help-echo "mouse-1: unfold this node"
'keymap map)))
(overlay-put ov 'face 'treesit-fold-replacement-face)
(overlay-put ov 'modification-hooks '(treesit-fold--on-change))
Expand Down Expand Up @@ -1458,6 +1466,18 @@ more information."
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))

(defun treesit-fold-range-vimscript-function (node offset)
"Return the fold range for `function!' and `func' NODE
in Vimscript.

For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((param-node (treesit-node-child node 1))
(beg (treesit-node-start param-node))
(end (treesit-node-end node)))
(unless treesit-fold-on-next-line ; display nicely
(setq beg (treesit-fold--last-eol beg)))
(treesit-fold--cons-add (cons beg end) offset)))

(provide 'treesit-fold)
;;; treesit-fold.el ends here
Loading