clojure-ts-mode
- Description
- Major mode for Clojure code
- Latest
- clojure-ts-mode-0.2.2.tar (.sig), 2024-Mar-31, 110 KiB
- Maintainer
- Danny Freeman <danny@dfreeman.email>
- Atom feed
- clojure-ts-mode.xml
- Website
- http://github.com/clojure-emacs/clojure-ts-mode
- Browse ELPA's repository
- CGit or Gitweb
- Badge
To install this package from Emacs, use package-install
or list-packages
.
Full description
Clojure Tree-Sitter Mode
clojure-ts-mode
is an Emacs major mode that provides font-lock (syntax
highlighting), indentation, and navigation support for the
Clojure(Script) programming language, powered by the
tree-sitter-clojure
tree-sitter grammar.
Configuration
To see a list of available configuration options do M-x customize-group <RET> clojure-ts
.
Most configuration changes will require reverting any active clojure-ts-mode
buffers.
Indentation
clojure-ts-mode
currently supports 2 different indentation strategies:
semantic
, the default, which tries to match the indentation ofclojure-mode
andcljfmt
fixed
, a simple indentation strategy outlined by Tonsky in a blog post
Set the var clojure-ts-indent-style
to change it.
emacs-lisp
(setq clojure-ts-indent-style 'fixed)
Note: You can find this article comparing semantic and fixed indentation useful.
Font Locking
Too highlight entire rich comment
expression with the comment font face, set
emacs-lisp
(setq clojure-ts-comment-macro-font-lock-body t)
By default this is nil
, so that anything within a comment
expression is
highlighted like regular clojure code.
Navigation and Evaluation
To make forms inside of (comment ...)
forms appear as top-level forms for evaluation and navigation, set
emacs-lisp
(setq clojure-ts-toplevel-inside-comment-form t)
Rationale
clojure-mode has served us well
for a very long time, but it suffers from a few long-standing
problems, related to
Emacs limitations baked into its design. The introduction of built-in support
for Tree-sitter in Emacs 29 provides a natural opportunity to address many of
them. Enter clojure-ts-mode
.
Keep in mind that the transition to clojure-ts-mode
won't happen overnight for several reasons:
- getting to feature parity with
clojure-mode
will take some time - tools that depend on
clojure-mode
will need to be updated to work withclojure-ts-mode
- we still need to support users of older Emacs versions that don't support Tree-sitter
That's why clojure-ts-mode
is being developed independently of clojure-mode
and will one day replace it when the time is right. (e.g. 3 major Emacs version down the road, so circa Emacs 32)
You can read more about the vision for clojure-ts-mode
here.
Current Status
This library is still under development. Breaking changes should be expected.
Installation
Emacs 29
This package requires Emacs 29 built with tree-sitter support from the emacs-29 branch.
If you decide to build Emacs from source there's some useful information on this in the Emacs repository: - Emacs tree-sitter starter-guide - Emacs install instructions.
Install clojure-ts-mode
clojure-ts-mode is available on MElPA and NonGNU ELPA. It can be installed with
emacs-lisp
(package-install 'clojure-ts-mode)
package-vc
Emacs 29 also includes package-vc-install
, so you can run
emacs-lisp
(package-vc-install "https://github.com/clojure-emacs/clojure-ts-mode")
to install this package from source.
Manual installation
You can install it by cloning the repository and adding it to your load path.
bash
git clone https://github.com/clojure-emacs/clojure-ts-mode.git
emacs-lisp
(add-to-list 'load-path "~/path/to/clojure-ts-mode/")
Once installed, evaluate clojure-ts-mode.el and you should be ready to go.
Install tree-sitter grammars
The compile tree-sitter clojure shared library must be available to Emacs. Additionally, the tree-sitter markdown_inline shared library will also be used for docstrings if available.
If you have git
and a C compiler (cc
) available on your system's PATH
, then these steps should not be necessary.
clojure-ts-mode will install the grammars when you first open a Clojure file and
clojure-ts-ensure-grammars
is set to t
(the default).
If clojure-ts-mode fails to automatically install the grammar, you have the option to install it manually.
From your OS
Some distributions may package the tree-sitter-clojure grammar in their package repositories. If yours does you may be able to install tree-sitter-clojure with your system package manager.
If the version packaged by your OS is out of date, you may see errors in the *Messages*
buffer or your clojure buffers will not have any syntax highlighting.
If this happens you should install the grammar manually with M-x treesit-install-language-grammar <RET> clojure
and follow the prompts.
Recommended values for these prompts can be seen in clojure-ts-grammar-recipes
.
Compile From Source
If all else fails, you can attempt to download and compile manually.
All you need is git
and a C compiler (GCC works well).
To start, clone tree-sitter-clojure.
Then run the following code (depending on your OS) from the tree-sitter-clojure repository on your machine.
Linux
bash
mkdir -p dist
cc -c -I./src src/parser.c -o "parser.o"
cc -fPIC -shared src/parser.o -o "dist/libtree-sitter-clojure.so"
macOS
bash
mkdir -p dist
cc -c -I./src src/parser.c -o "parser.o"
cc -fPIC -shared src/parser.o -o "dist/libtree-sitter-clojure.dylib"
Windows
I don't know how to do this on Windows. Patches welcome!
Finally, in emacs
Then tell Emacs where to find the shared library by adding something like this to your init file:
emacs-lisp
(setq treesit-extra-load-path '( "~/path/to/tree-sitter-clojure/dist"))
OR you can move the libtree-sitter-clojure.so
/libtree-sitter-clojure.dylib
to a directory named tree-sitter
under your user-emacs-directory
(typically ~/.emacs.d
on Unix systems).
Frequently Asked Questions
Does clojure-ts-mode
work with CIDER?
Not yet out of the box, but that should change soon. Feel free to help out with the remaining work, so we can expedite the process.
For now, when you take care of the keybindings for the cider functions you use and ensure cider-mode
is enabled for clojure-ts-mode
buffers in your config, most functionality should already work:
emacs-lisp
(add-hook 'clojure-ts-mode-hook #'cider-mode)
Does clojure-ts-mode
work with inf-clojure
?
License
Copyright © 2022-2024 Danny Freeman and contributors.
Distributed under the GNU General Public License; type C-h C-c to view it.
Old versions
clojure-ts-mode-0.2.0.tar.lz | 2023-Sep-25 | 24.6 KiB |
clojure-ts-mode-0.1.5.tar.lz | 2023-Aug-30 | 20.4 KiB |
clojure-ts-mode-0.1.4.tar.lz | 2023-Aug-25 | 20.1 KiB |
News
Changelog
main (unreleased)
0.2.2 (2024-02-16)
- [#37]: Fix derived modes broken with [#36].
0.2.1 (2024-02-14)
- [#36]: Rename all derived mode vars to match the package prefix.
clojurescript-ts-mode
->clojure-ts-clojurescript-mode
clojurec-ts-mode
->clojure-ts-clojurec-mode
clojure-dart-ts-mode
->clojure-ts-clojuredart-mode
clojure-jank-ts-mode
->clojure-ts-jank-mode
- [#30]: Add custom option
clojure-ts-toplevel-inside-comment-form
as an equivalent toclojure-toplevel-inside-comment-form
inclojure-mode
. - [#32]: Change behavior of
beginning-of-defun
andend-of-defun
to consider all Clojure sexps as defuns.
0.2.0
- Pin grammar revision in treesit-language-source-alist
- Make font lock feature list more conforming with recommendations
- (See treesit-font-lock-level documentation for more information.)
- 2225190ee57ef667d69f2cd740e0137810bc38e7
- Highlight docstrings in interface, protocol, and variable definitions
- Add support for semantic indentation (now the default)
- Highlight "`quoted-symbols` in docs strings like this."
- This feature uses a nested markdown parser. If the parser is not available this feature should be silently disabled.
- 9af0a6b35c708309acdfeb4c0c79061b0fd4eb44
- Highlight methods for
deftype
,defrecord
,defprotocol
,reify
anddefinterface
forms (#20).- 5231c348e509cff91edd1ec59d7a59645395da15
- Thank you rrudakov for this contribution.
- Add derived
clojure-jank-ts-mode
for the Jank dialect of clojure
0.1.5
- Disable treesit-transpose-sexps on Emacs 30 in favor of the default implementation (#17) 623c98292f9207a95169cdeae6f8595c016c6320
- Implement clojure-ts-find-ns function (mostly as a demonstration). d630cd63af8022d5a1fee0e7aa05450b6e0fd75e
0.1.4
- Fix misplaced defcustom form in hastily release 0.1.3 6cba90c556c7e658b815cdbb9b4243bde3273203
0.1.3
- Add custom option for highlighting comment macro body forms as comments. ae3790adc0fc40ad905b8c30b152122991592a4e
- Defaults to OFF, highlighting comment body forms like any other expressions.
- Additionally, does a better job of better detecting comment macros by reducing false positives from forms like (not.clojure.core/comment) ... ...