From 862e7980d89b242784d447ee99a007d10aa6e063 Mon Sep 17 00:00:00 2001 From: Jeetaditya Chatterjee Date: Sat, 21 Aug 2021 21:19:27 +0100 Subject: [PATCH] module: add :tools tree-sitter This module installs 'tree-sitter' and 'tree-sitter-langs' as well as the 'evil-textobj-treesitter' and configures them. This module is based on the prototype module that @hlissner has started and I am just fleshing out that work. It also comes with a README file Fix: #4151 --- modules/tools/tree-sitter/README.org | 55 +++++++++++++++++++++++++++ modules/tools/tree-sitter/config.el | 32 ++++++++++++++++ modules/tools/tree-sitter/packages.el | 13 +++++++ modules/tools/tree-sitter/test.js | 52 +++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 modules/tools/tree-sitter/README.org create mode 100644 modules/tools/tree-sitter/config.el create mode 100644 modules/tools/tree-sitter/packages.el create mode 100644 modules/tools/tree-sitter/test.js diff --git a/modules/tools/tree-sitter/README.org b/modules/tools/tree-sitter/README.org new file mode 100644 index 000000000..b15162f87 --- /dev/null +++ b/modules/tools/tree-sitter/README.org @@ -0,0 +1,55 @@ +#+TITLE: tools/tree-sitter +#+DATE: August 17, 2021 +#+SINCE: 3.0.0 +#+STARTUP: inlineimages nofold + +* Table of Contents :TOC_3:noexport: +- [[#description][Description]] + - [[#maintainers][Maintainers]] + - [[#module-flags][Module Flags]] + - [[#plugins][Plugins]] + - [[#hacks][Hacks]] +- [[#prerequisites][Prerequisites]] +- [[#features][Features]] +- [[#configuration][Configuration]] +- [[#troubleshooting][Troubleshooting]] + +* Description +Add [[https://tree-sitter.github.io/tree-sitter/][tree-sitter]] support to doom: + +#+begin_quote +Tree sitter is a parser generator tool and an incremental parsing library. It +can build a concrete syntax tree for a source file and efficiently update the +syntax tree as the source file is edited. +#+end_quote + + ++ include better syntax highlighting of supported code ++ add structural text objects to manipulate functions statements and other code + structures + +** Maintainers +- @jeetelongname + +** Module Flags +This module provides no flags. + +** Plugins ++ [[https://github.com/emacs-tree-sitter/elisp-tree-sitter][tree-sitter]] ++ [[https://github.com/emacs-tree-sitter/tree-sitter-langs][tree-sitter-langs]] ++ [[https://github.com/meain/evil-textobj-tree-sitter][evil-textobj-tree-sitter]]* (=:editor evil +everywhere=) + +** TODO Hacks +# A list of internal modifications to included packages; omit if unneeded + +* Prerequisites +This module has no prerequisites. + +* TODO Features +# An in-depth list of features, how to use them, and their dependencies. + +* TODO Configuration +# How to configure this module, including common problems and how to address them. + +* TODO Troubleshooting +# Common issues and their solution, or places to look for help. diff --git a/modules/tools/tree-sitter/config.el b/modules/tools/tree-sitter/config.el new file mode 100644 index 000000000..b44dce01c --- /dev/null +++ b/modules/tools/tree-sitter/config.el @@ -0,0 +1,32 @@ +;;; tools/tree-sitter/config.el -*- lexical-binding: t; -*- + +(use-package! tree-sitter + :when (bound-and-true-p module-file-suffix) + :hook (prog-mode . tree-sitter-mode) + :hook (tree-sitter-after-on . tree-sitter-hl-mode) + :config + (require 'tree-sitter-langs) + (setq tree-sitter-debug-jump-buttons t + tree-sitter-debug-highlight-jump-region t) + + (defadvice! doom-tree-sitter-fail-gracefully-a (orig-fn &rest args) + "Don't break with errors when current major mode lacks tree-sitter support." + :around #'tree-sitter-mode + (condition-case e + (apply orig-fn args) + (error + (unless (string-match-p (concat "^Cannot find shared library\\|" + "^No language registered\\|" + "cannot open shared object file") + (error-message-string e)) + (signal (car e) (cadr e))))))) + +(use-package! evil-textobj-tree-sitter + :after tree-sitter + :config + (map! + :textobj "f" (evil-textobj-tree-sitter-get-textobj "function.inner") (evil-textobj-tree-sitter-get-textobj "function.outer") ;; redef + :textobj "C" (evil-textobj-tree-sitter-get-textobj "class.inner") (evil-textobj-tree-sitter-get-textobj "class.outer") + :textobj "c" nil (evil-textobj-tree-sitter-get-textobj "comment.outer") + :textobj "i" (evil-textobj-tree-sitter-get-textobj "conditional.inner") (evil-textobj-tree-sitter-get-textobj "conditional.outer") + :textobj "l" (evil-textobj-tree-sitter-get-textobj "loop.inner") (evil-textobj-tree-sitter-get-textobj "loop.outer"))) diff --git a/modules/tools/tree-sitter/packages.el b/modules/tools/tree-sitter/packages.el new file mode 100644 index 000000000..6b4295419 --- /dev/null +++ b/modules/tools/tree-sitter/packages.el @@ -0,0 +1,13 @@ +;; -*- no-byte-compile: t; -*- +;;; tools/tree-sitter/packages.el + +(package! tree-sitter + ;; :ignore (null (bound-and-true-p module-file-suffix)) + :pin "c7a1c34549cad41a3618c6f17e0e9dabd3e98fe1") +(package! tree-sitter-langs + ;; :ignore (null (bound-and-true-p module-file-suffix)) + :pin "e7b8db7c4006c04a4bc1fc6865ec31f223843192") + +(when (featurep! :editor evil +everywhere) + (package! evil-textobj-tree-sitter + :pin "f20598676f99e6fa33759d9807e94f42271c0dfb")) diff --git a/modules/tools/tree-sitter/test.js b/modules/tools/tree-sitter/test.js new file mode 100644 index 000000000..c2ab5d40a --- /dev/null +++ b/modules/tools/tree-sitter/test.js @@ -0,0 +1,52 @@ +// this is a test file i am useing to test functionallity with treesitter + +// blocks +x = 10 + +// calls +call(); + +// class +class Car { + constructor(name, year) { + this.name = name; + this.year = year; + } + age(x) { + return x - this.year; + } +} + +// comments +/* + * multiline my dude + * a poem + * roses are red + * violets are blue + * there is a man behind you + * and he had an axe with him to + */ + + +// conditional +if (x == 1) { + doStuff(); +} else { + x = new Car.age(2020) +} + + +// function +function name(arg) { + doStuff(arg); +} + +const arrowFunc = arg => { + dosStuff(arg); +} + +// loop +for (let i = 0; i <= 10; i++) { + doStuff(i); +} +// param