Skip to content

Commit 82c4b33

Browse files
rrudakovbbatsov
authored andcommitted
[#15] Introduce clojure-ts-extra-def-forms customization
1 parent 0b30585 commit 82c4b33

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
- [#99](https://github.com/clojure-emacs/clojure-ts-mode/pull/99): Fix bug in `clojure-ts-align` when nested form has extra spaces.
1212
- [#99](https://github.com/clojure-emacs/clojure-ts-mode/pull/99): Fix bug in `clojure-ts-unwind` when there is only one expression after
1313
threading symbol.
14-
- Introduce `clojure-ts-jank-use-cpp-parser` customization which allows
14+
- [#103](https://github.com/clojure-emacs/clojure-ts-mode/issues/103): Introduce `clojure-ts-jank-use-cpp-parser` customization which allows
1515
highlighting C++ syntax in Jank `native/raw` forms.
16-
- Introduce `clojure-ts-clojurescript-use-js-parser` customization which allows
17-
highlighting JS syntax in ClojureScript `js*` forms.
18-
16+
- [#103](https://github.com/clojure-emacs/clojure-ts-mode/issues/103): Introduce `clojure-ts-clojurescript-use-js-parser` customization which
17+
allows highlighting JS syntax in ClojureScript `js*` forms.
18+
- Introduce the `clojure-ts-extra-def-forms` customization option to specify
19+
additional `defn`-like forms that should be fontified.
1920

2021
## 0.4.0 (2025-05-15)
2122

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,37 @@ highlighted like regular Clojure code.
318318
> section](https://www.gnu.org/software/emacs/manual/html_node/emacs/Parser_002dbased-Font-Lock.html)
319319
> of the Emacs manual for more details.
320320
321+
#### Extending font-lock rules
322+
323+
In `clojure-ts-mode` it is possible to specify additional defn-like forms that
324+
should be fontified. For example to highlight the following form from Hiccup
325+
library as a function definition:
326+
327+
```clojure
328+
(defelem file-upload
329+
"Creates a file upload input."
330+
[name]
331+
(input-field "file" name nil))
332+
```
333+
334+
You can add `defelem` to `clojure-ts-extra-def-forms` list like this:
335+
336+
```emacs-lisp
337+
(add-to-list 'clojure-ts-extra-def-forms "defelem")
338+
```
339+
340+
or set this variable using `setopt`:
341+
342+
```emacs-lisp
343+
(setopt clojure-ts-extra-def-forms '("defelem"))
344+
```
345+
346+
This setting will highlight `defelem` symbol, function name and the docstring.
347+
348+
**NOTE**: Setting `clojure-ts-extra-def-forms` won't change the indentation rule for
349+
these forms. For indentation rules you should use
350+
`clojure-ts-semantic-indent-rules` variable (see [semantic indentation](#customizing-semantic-indentation) section).
351+
321352
### Highlight markdown syntax in docstrings
322353

323354
By default Markdown syntax is highlighted in the docstrings using

clojure-ts-mode.el

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ values like this:
260260
:safe #'booleanp
261261
:type 'boolean)
262262

263+
(defcustom clojure-ts-extra-def-forms nil
264+
"List of forms that should be fontified the same way as defn."
265+
:package-version '(clojure-ts-mode . "0.5")
266+
:safe #'listp
267+
:type '(repeat string))
268+
263269
(defvar clojure-ts-mode-remappings
264270
'((clojure-mode . clojure-ts-mode)
265271
(clojurescript-mode . clojure-ts-clojurescript-mode)
@@ -468,6 +474,15 @@ if a third argument (the value) is provided.
468474
:anchor (str_lit (str_content) ,capture-symbol) @font-lock-doc-face)
469475
(:match ,clojure-ts-function-docstring-symbols
470476
@_def_symbol))
477+
((list_lit :anchor [(comment) (meta_lit) (old_meta_lit)] :*
478+
:anchor (sym_lit) @_def_symbol
479+
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
480+
;; Function_name
481+
:anchor (sym_lit)
482+
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
483+
:anchor (str_lit (str_content) ,capture-symbol) @font-lock-doc-face)
484+
(:match ,(clojure-ts-symbol-regexp clojure-ts-extra-def-forms)
485+
@_def_symbol))
471486
;; Captures docstrings in defprotcol, definterface
472487
((list_lit :anchor [(comment) (meta_lit) (old_meta_lit)] :*
473488
:anchor (sym_lit) @_def_symbol
@@ -630,6 +645,12 @@ literals with regex grammar."
630645
"defonce")
631646
eol))
632647
@font-lock-keyword-face))
648+
((list_lit :anchor [(comment) (meta_lit) (old_meta_lit)] :*
649+
:anchor (sym_lit (sym_name) @font-lock-keyword-face)
650+
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
651+
:anchor (sym_lit (sym_name) @font-lock-function-name-face))
652+
(:match ,(clojure-ts-symbol-regexp clojure-ts-extra-def-forms)
653+
@font-lock-keyword-face))
633654
((anon_fn_lit
634655
marker: "#" @font-lock-property-face))
635656
;; Methods implementation

test/clojure-ts-mode-font-lock-test.el

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,22 @@ DESCRIPTION is the description of the spec."
230230
(set-parameter [m ^PreparedStatement s i]
231231
(.setObject s i (->pgobject m))))"
232232
(81 93 font-lock-function-name-face))))
233+
234+
;;;; Extra def forms
235+
236+
(describe "clojure-ts-extra-def-forms"
237+
(it "should respect the value of clojure-ts-extra-def-forms"
238+
(with-clojure-ts-buffer "(defelem file-upload
239+
\"Creates a file upload input.\"
240+
[name]
241+
(input-field \"file\" name nil))"
242+
(setopt clojure-ts-extra-def-forms '("defelem"))
243+
(clojure-ts-mode)
244+
(font-lock-ensure)
245+
(goto-char (point-min))
246+
(expect (get-text-property 2 'face)
247+
:to-equal 'font-lock-keyword-face)
248+
(expect (get-text-property 10 'face)
249+
:to-equal 'font-lock-function-name-face)
250+
(expect (get-text-property 25 'face)
251+
:to-equal 'font-lock-doc-face))))

0 commit comments

Comments
 (0)