Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The next generation Clojure major mode for Emacs, powered by TreeSitter

License

NotificationsYou must be signed in to change notification settings

clojure-emacs/clojure-ts-mode

Repository files navigation

NonGNU ELPAMELPA StableMELPALicense GPL 3Lint Status

Clojure Tree-Sitter Mode

clojure-ts-mode is an Emacs major mode that provides font-lock (syntaxhighlighting), indentation, and navigation support for theClojure(Script) programming language, powered by thetree-sitter-clojuretree-sitter grammar.

Rationale

clojure-mode has served us wellfor a very long time, but it suffers from a fewlong-standingproblems, related toEmacs limitations baked into its design. The introduction of built-in supportfor Tree-sitter in Emacs 29 presents a natural opportunity to address many ofthem. Enterclojure-ts-mode, which makes use of TreeSitter to provide:

  • fast, accurate and more granular font-locking
  • fast indentation
  • common Emacs functionality like structured navigation,imenu (an outline of a source buffer), current form inference (used internally by various Emacs modes and utilities), etc

Working with TreeSitter is significantly easier than the legacy Emacs APIs for font-locking andindentation, which makes it easier to contribute toclojure-ts-mode, and to improve it in general.

Keep in mind that the transition toclojure-ts-mode won't happen overnight for several reasons:

  • getting to feature parity withclojure-mode will take some time
  • tools that depend onclojure-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 whyclojure-ts-mode is being developed independently ofclojure-modeand will one day replace it when the time is right. (e.g. 3 major Emacs versiondown the road, so circa Emacs 32)

You can read more about the vision forclojure-ts-modehere.

Current Status

Warning

This library is still under active development. Breaking changes should be expected.

The currently provided functionality should cover the needs of most Clojure programmers, but youcan expect to encounter some bugs and missing functionality here and there.

Those will be addressed over the time, as more and more people useclojure-ts-mode.

Installation

Requirements

Forclojure-ts-mode to work, you need Emacs 30+ built with TreeSitter support.To check if your Emacs supports TreeSitter run the following (e.g. by usingM-:):

(treesit-available-p)

Additionally, you'll need to have Git and some C compiler (cc) installed and availablein your$PATH (or Emacs'sexec-path), forclojure-ts-mode to be able to install the requiredTreeSitter grammars automatically.

Tip

As the TreeSitter support in Emacs is still fairly new and under active development itself, for optimalresults you should use the latest stable Emacs release or even the development version of Emacs.See the "Caveats" section for more on the subject.

Install clojure-ts-mode

Note

That's the recommended way to installclojure-ts-mode.

If you havegit and a C compiler (cc) available on your system'sPATH,clojure-ts-mode will install thegrammars

clojure-ts-mode is available onMElPA andNonGNU ELPA.It can be installed with:

(package-install'clojure-ts-mode)

package-vc

Emacs also includespackage-vc-install, so you can run:

(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.

git clone https://github.com/clojure-emacs/clojure-ts-mode.git
(add-to-list'load-path"~/path/to/clojure-ts-mode/")

Once installed, evaluateclojure-ts-mode.el and you should be ready to go.

Install tree-sitter grammars

Note

clojure-ts-mode install the required grammars automatically, so for mostpeople no manual actions will be required.

clojure-ts-mode makes use of two TreeSitter grammars to work properly:

  • The Clojure grammar, mentioned earlier
  • markdown-inline, whichwill be used for docstrings if available and ifclojure-ts-use-markdown-inline is enabled.

If you havegit and a C compiler (cc) available on your system'sPATH,clojure-ts-mode will install thegrammars when you first open a Clojure file andclojure-ts-ensure-grammars isset tot (the default).

Ifclojure-ts-mode fails to automatically install the grammar, you have theoption to install it manually, Please, refer to the installation instructions ofeach required grammar and make sure you're install the versions expected. (seeclojure-ts-grammar-recipes for details)

Upgrading tree-sitter grammars

To reinstall or upgrade TreeSitter grammars, you can execute:

M-x clojure-ts-reinstall-grammars

This will install the latest compatible grammars, even if they are alreadyinstalled.

Configuration

To see a list of available configuration options doM-x customize-group <RET> clojure-ts.

Most configuration changes will require reverting any activeclojure-ts-mode buffers.

Remapping ofclojure-mode buffers

By default,clojure-ts-mode assumes command over all buffers and file extensions previously associated withclojure-mode (and derived major modes likeclojurescript-mode). To disable this remapping, set

(setopt clojure-ts-auto-remapnil)

You can also use the commandsclojure-ts-activate /clojure-ts-deactivate to interactively change this behavior.

Indentation

clojure-ts-mode currently supports 2 different indentation strategies:

Set the varclojure-ts-indent-style to change it.

(setq clojure-ts-indent-style'fixed)

Tip

You can findthis article comparing semantic and fixed indentation useful.

Customizing semantic indentation

The indentation of special forms and macros with bodies is controlled viaclojure-ts-semantic-indent-rules. Nearly all special forms and built-in macroswith bodies have special indentation settings in clojure-ts-mode, which arealigned with cljfmt indent rules. You can add/alter the indentation settings inyour personal config. Let's assume you want to indent->> and-> like this:

(->> something  ala  bala  portokala)

You can do so by putting the following in your config:

(setopt clojure-ts-semantic-indent-rules '(("->". ((:block1)))                                           ("->>". ((:block1)))))

This means that the body of the->/->> is after the first argument.

The default set of rules is defined asclojure-ts--semantic-indent-rules-defaults, any rule can be overridden usingcustomization option.

Two types of rules are supported::block and:inner, mirroring those incljfmt. When a rule is defined as:block n,n represents the number ofarguments preceding the body. When a rule is defined as:inner n, each formwithin the expression's body, nestedn levels deep, is indented by twospaces. These rule definitions fully reflect thecljfmt rules.

For example:

  • do has a rule((:block 0)).
  • when has a rule((:block 1)).
  • defn andfn have a rule((:inner 0)).
  • letfn has a rule((:block 1) (:inner 2 0)).

Note thatclojure-ts-semantic-indent-rules should be set using thecustomization interface orsetopt; otherwise, it will not be appliedcorrectly.

Project local indentation

Custom indentation rules can be set for individual projects. To achieve this,you need to create a.dir-locals.el file in the project root. The contentshould look like:

((clojure-ts-mode. ((clojure-ts-semantic-indent-rules. (("with-transaction". ((:block1)))                                                          ("with-retry". ((:block1))))))))

In order to apply directory-local variables to existing buffers, they must bereverted.

Font Locking

To highlight entire richcomment expression with the comment font face, set

(setq clojure-ts-comment-macro-font-lock-bodyt)

By default this isnil, so that anything within acomment expression ishighlighted like regular clojure code.

Tip

You can customize the exact level of font-locking via the variablestreesit-font-lock-level (the default value is 3) andtreesit-font-lock-features-list. Checkthissectionof the Emacs manual for more details.

Highlight markdown syntax in docstrings

By default markdown syntax is highlighted in the docstrings usingmarkdown_inline grammar. To disable this feature set

(setopt clojure-ts-use-markdown-inlinenil)

Navigation and Evaluation

To make forms inside of(comment ...) forms appear as top-level forms for evaluation and navigation, set

(setq clojure-ts-toplevel-inside-comment-formt)

Fill paragraph

To change the maximal line length used byM-x prog-fill-reindent-defun (alsobound toM-q by default) to reformat docstrings and comments it's possible tocustomizeclojure-ts-fill-paragraph variable (by default set to the value ofEmacs'fill-paragraph value).

Every new line in the docstrings is indented byclojure-ts-docstring-fill-prefix-width number of spaces (set to 2 by defaultwhich matches theclojure-mode settings).

imenu

clojure-ts-mode supports various types of definition that can be navigatedusingimenu, such as:

  • namespace
  • function
  • macro
  • var
  • interface (forms such asdefprotocol,definterface anddefmulti)
  • class (forms such asdeftype,defrecord anddefstruct)
  • keyword (for example, spec definitions)

Migrating to clojure-ts-mode

If you are migrating toclojure-ts-mode note thatclojure-mode is still required for cider and clj-refactor packages to work properly.

After installing the package do the following.

  • Check the value ofclojure-mode-hook and copy all relevant hooks toclojure-ts-mode-hook.
(add-hook'clojure-ts-mode-hook#'cider-mode)(add-hook'clojure-ts-mode-hook#'enable-paredit-mode)(add-hook'clojure-ts-mode-hook#'rainbow-delimiters-mode)(add-hook'clojure-ts-mode-hook#'clj-refactor-mode)
  • Update.dir-locals.el in all of your Clojure projects to activate directory local variables inclojure-ts-mode.
((clojure-mode  (cider-clojure-cli-aliases.":test:repl")) (clojure-ts-mode  (cider-clojure-cli-aliases.":test:repl")))

Caveats

As the TreeSitter Emacs APIs are new and keep evolving there are somedifferences in the behavior ofclojure-ts-mode on different Emacs versions.Here are some notable examples:

  • On Emacs 29 the parent mode isprog-mode, but on Emacs 30+ it's bothprog-modeandclojure-mode (this is very helpful when dealing withderived-mode-p checks)
  • Navigation by sexp/lists might work differently on Emacs versions lowerthan 31. Starting with version 31, Emacs uses TreeSitter 'things' settings, ifavailable, to rebind some commands.
  • The indentation of list elements with metadata is inconsistent with othercollections. This inconsistency stems from the grammar's interpretation ofnearly every definition or function call as a list. Therefore, modifying theindentation for list elements would adversely affect the indentation ofnumerous other forms.

Frequently Asked Questions

Whatclojure-mode features are currently missing?

As of version 0.2.x, the most obvious missing feature are the variousrefactoring commands inclojure-mode.

Doesclojure-ts-mode work with CIDER?

Yes! Preliminary support forclojure-ts-mode was released inCIDER1.14. Note thatclojure-mode is still needed for some APIs that haven't yet been ported toclojure-ts-mode.

For now, when you take care of the keybindings for the CIDER commands you useand ensurecider-mode is enabled forclojure-ts-mode buffers in your config,most functionality should already work:

(add-hook'clojure-ts-mode-hook#'cider-mode)

Check outthis article for more details.

Note

The dynamic indentation feature in CIDER requires clojure-ts-mode 0.3+.Dynamic font-locking currently doesn't work with clojure-ts-mode.

Doesclojure-ts-mode work withinf-clojure?

Currently, there is anopen PR adding support for inf-clojure.

License

Copyright © 2022-2025 Danny Freeman, Bozhidar Batsov andcontributors.

Distributed under the GNU General Public License; typeC-h C-c to view it.

About

The next generation Clojure major mode for Emacs, powered by TreeSitter

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors41


[8]ページ先頭

©2009-2025 Movatter.jp