Git Product home page Git Product logo

tree-sitter-fold's Introduction

Introduction

tree-sitter-fold builds on top of emacs-tree-sitter to provide code folding base on the tree-sitter syntax tree.

Installation

  • with straight.el and use-package:
    (use-package tree-sitter-fold
      :straight (host github repo "junyi-hou/tree-sitter-fold"))
        
  • manual:
    git clone https://github.com/junyi-hou/tree-sitter-fold /path/to/lib
        

    then in emacs:

    (add-to-list 'load-path "/path/to/lib")
    (require 'tree-sitter-fold)
        

Usage

tree-sitter-fold-close: fold the current syntax node.

tree-sitter-fold-open: open all folds inside the current syntax node.

tree-sitter-fold-open-recursively: open the outmost fold of the current syntax node. Keep the sub-folds close.

tree-sitter-fold-close-all: close all foldable syntax nodes in the current buffer.

tree-sitter-fold-open-all: open all folded syntax nodes in the current buffer.

Customization

tree-sitter-fold allows user-defined rules of how to fold syntax nodes. Two variables determine which node is foldable and how tree-sitter-fold should fold those nodes. I only implement folding mechanisms for python. Contribution on other types of folds and for other major modes are welcome.

tree-sitter-fold-foldable-node-alist

This is an alist whose keys are major-mode symbols, and the corresponding values are lists of tree-sitter-node-type considered foldable in the major mode. tree-sitter-fold-close will fold only if the node are one of the types defined in this list under the current major mode.

tree-sitter-fold-range-alist

This variable determines how tree-sitter-fold should fold each of the nodes defined in tree-sitter-fold-foldable-node-alist. It is a nested alist, with the first key being the major mode, and second key being the tree-sitter-node-type. The value should be a function that takes a tree-sitter-node as the single argument, and return a cons cell of buffer position as the range should be folded by tree-sitter-fold. See tree-sitter-fold-range-python for an example.

Supported languages

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 an excellent documentation on how to write tree-sitter queries.

Note on installing additional grammars

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
    tree-sitter --version
        

    The new ABI requires 0.19.3 or newer version of tree-sitter. Node.js is also required to generate grammar parsers. See 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
    ;; make sure I do not re-add the submodule multiple times
    (unless (f-exists-p (concat tree-sitter-langs-git-dir "/repos/r"))
      (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")))
        
  3. compile the new grammar parser by running
    ;; compile only if haven't done so
    (unless (--filter (string= (f-base it) "nix") (f-entries (tree-sitter-langs--bin-dir)))
      ;; do not display compile information in a new buffer
      (cl-letf (((symbol-function 'tree-sitter-langs--buffer) (lambda (&rest _) nil)))
        (tree-sitter-langs-compile 'r)))
        
  4. register the respective major mode with the newly compiled grammar
    (unless (assq 'ess-r-mode tree-sitter-major-mode-language-alist)
      (add-to-list 'tree-sitter-major-mode-language-alist '(ess-r-mode . r)))
        
  5. require the new grammar
    (tree-sitter-require 'r)
        

I have those things together in a function gatsby:install-and-load-tree-sitter-r at here and hook it to ess-r-mode-hook.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.