Skip to content

Highlight extra def forms as keywords at font-lock level 4 - #160

Merged
bbatsov merged 1 commit into
clojure-emacs:mainfrom
jasonjckn:fix-extra-def-forms-level-4
Oct 29, 2026
Merged

bbatsov merged 1 commit into
clojure-emacs:mainfrom
jasonjckn:fix-extra-def-forms-level-4

Conversation

@jasonjckn

Copy link
Copy Markdown
Contributor

With treesit-font-lock-level set to 4, the head of every form in clojure-ts-extra-def-forms (defelem, >defn, …) is colored as a function call (font-lock-function-call-face) instead of a keyword. The name after it is still colored as a function name.

The rules built from clojure-ts-extra-def-forms are appended after the built-in font-lock queries, and without :override a later rule can't replace a face that an earlier rule already set. The level 4 function feature matches the head of every list, so it wins. defn itself isn't affected, because the builtin rule comes before function and already colors it as a keyword.

This PR gives the extra definition rule :override t, like the doc rule that is computed right next to it in clojure-ts--compute-extra-def-queries.

Background

clojure-ts-extra-def-forms came out of #15, which was opened about Guardrails' >defn. The spec added with it runs at the default font-lock level 3, where the function feature is off, which is why this went unnoticed. Level 4 is opt-in, but it's what you get with e.g. (setq treesit-font-lock-level 4) in Doom Emacs.

Examples

Faces at treesit-font-lock-level 4 with (setopt clojure-ts-extra-def-forms '("defelem" ">defn")), printed by a small batch script run against both checkouts.

defelem (the README example)

(defelem file-upload [name] name)
defelem file-upload
clojure-ts-mode main font-lock-function-call-face font-lock-function-name-face
clojure-ts-mode with this PR font-lock-keyword-face font-lock-function-name-face

Guardrails >defn

(>defn f [x] x)
>defn f
clojure-ts-mode main font-lock-function-call-face font-lock-function-name-face
clojure-ts-mode with this PR font-lock-keyword-face font-lock-function-name-face

>defn with metadata before the name

(>defn ^:private f [x] x)
>defn f
clojure-ts-mode main font-lock-function-call-face font-lock-function-name-face
clojure-ts-mode with this PR font-lock-keyword-face font-lock-function-name-face

Tests

This PR adds a new spec, should highlight extra def forms as keywords at font-lock level 4. It fails on main (the head gets font-lock-function-call-face) and passes with this PR.

How it was checked locally, on Emacs 31.1.50:

  • the full buttercup suite (246 specs, 0 failed);
  • byte-compile with warnings as errors, checkdoc, and an indentation check of the changed files.

It wasn't run locally on Emacs 30.1 this time, so that's left to CI.

Related

This is independent of the series #153–#159 and only touches font-lock. It conflicts with them only in CHANGELOG.md, and we're happy to rebase.

About this PR

The code, tests and this description were generated by an AI (Claude, by Anthropic), at @jasonjckn's request. If AI-generated contributions are a problem for this project, please feel free to close this PR right away.


  • The commits are consistent with our contribution guidelines.
  • You've run M-x checkdoc and fixed any warnings in the code you've written.
  • You've updated the changelog (if adding/changing user-visible functionality).
  • You've updated the readme (if adding/changing user-visible functionality).

The rules built from `clojure-ts-extra-def-forms` are appended after
the built-in font-lock queries, and without :override a later rule
can't replace a face an earlier one already set. At level 4 the
`function` feature matches the head of every list, so the head of an
extra def form (`defelem`, `>defn`, ...) ends up in
font-lock-function-call-face instead of font-lock-keyword-face.

`defn` itself is unaffected because the `builtin` rule, which comes
before `function`, already colors it.

Give the extra `definition` rule :override t, like the `doc` rule that
is computed next to it.
@bbatsov
bbatsov force-pushed the fix-extra-def-forms-level-4 branch from afd9d58 to d0989b8 Compare October 29, 2026 06:15
@bbatsov
bbatsov merged commit 229b069 into clojure-emacs:main Oct 29, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants