Skip to content

Commit 09f7da6

Browse files
rrudakovbbatsov
authored andcommitted
[#82] Support outline-minor-mode comments headings
1 parent e960a90 commit 09f7da6

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [#16](https://github.com/clojure-emacs/clojure-ts-mode/issues/16): Introduce `clojure-ts-align`.
66
- [#11](https://github.com/clojure-emacs/clojure-ts-mode/issues/11): Enable regex syntax highlighting.
77
- [#16](https://github.com/clojure-emacs/clojure-ts-mode/issues/16): Add support for automatic aligning forms.
8+
- [#82](https://github.com/clojure-emacs/clojure-ts-mode/issues/82): Introduce `clojure-ts-outline-variant`.
89

910
## 0.3.0 (2025-04-15)
1011

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Every new line in the docstrings is indented by
340340
`clojure-ts-docstring-fill-prefix-width` number of spaces (set to 2 by default
341341
which matches the `clojure-mode` settings).
342342

343-
#### imenu
343+
### imenu
344344

345345
`clojure-ts-mode` supports various types of definition that can be navigated
346346
using `imenu`, such as:
@@ -353,6 +353,19 @@ using `imenu`, such as:
353353
- class (forms such as `deftype`, `defrecord` and `defstruct`)
354354
- keyword (for example, spec definitions)
355355

356+
### Integration with `outline-minor-mode`
357+
358+
`clojure-ts-mode` supports two integration variants with
359+
`outline-minor-mode`. The default variant uses special top-level comments (level
360+
1 heading starts with three semicolons, level 2 heading starts with four,
361+
etc.). The other variant treats def-like forms (the same forms produced by the
362+
`imenu` command) as outline headings. To use the second option, use the
363+
following customization:
364+
365+
```emacs-lisp
366+
(setopt clojure-ts-outline-variant 'imenu)
367+
```
368+
356369
## Migrating to clojure-ts-mode
357370

358371
If you are migrating to `clojure-ts-mode` note that `clojure-mode` is still required for cider and clj-refactor packages to work properly.

clojure-ts-mode.el

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ double quotes on the third column."
133133
:type 'boolean
134134
:package-version '(clojure-ts-mode . "0.3"))
135135

136+
(defcustom clojure-ts-outline-variant 'comments
137+
"Determines how `clojure-ts-mode' integrates with `outline-minor-mode'.
138+
139+
If set to the symbol `comments', then top-level comments starting with
140+
three or more semicolons will be treated as outline headings. If set to
141+
`imenu', then def-like forms are treated as outline headings."
142+
:safe #'symbolp
143+
:type '(choice (const :tag "Use special comments" comments)
144+
(const :tag "Use imenu" imenu))
145+
:package-version '(clojure-ts-mode . "0.4"))
146+
136147
(defcustom clojure-ts-align-reader-conditionals nil
137148
"Whether to align reader conditionals, as if they were maps."
138149
:package-version '(clojure-ts-mode . "0.4")
@@ -913,6 +924,20 @@ Includes a dispatch value when applicable (defmethods)."
913924
By default `treesit-defun-name-function' is used to extract definition names.
914925
See `clojure-ts--standard-definition-node-name' for the implementation used.")
915926

927+
;;; Outline settings
928+
929+
(defun clojure-ts--outline-predicate (node)
930+
"Return TRUE if NODE is an outline heading comment."
931+
(and (string= (treesit-node-type node) "comment")
932+
(string-match-p "^\\(?:;;;;* \\).*" (treesit-node-text node))))
933+
934+
(defun clojure-ts--outline-level ()
935+
"Return the current level of the outline heading at point."
936+
(let* ((node (treesit-outline--at-point))
937+
(node-text (treesit-node-text node)))
938+
(string-match ";;\\(;+\\) " node-text)
939+
(- (match-end 1) (match-beginning 1))))
940+
916941
(defcustom clojure-ts-indent-style 'semantic
917942
"Automatic indentation style to use when mode `clojure-ts-mode' is run.
918943
@@ -1708,6 +1733,13 @@ REGEX-AVAILABLE."
17081733
(setq-local indent-tabs-mode nil)
17091734
(setq-local comment-add 1)
17101735
(setq-local comment-start ";")
1736+
(when (equal clojure-ts-outline-variant 'comments)
1737+
;; NOTE: If `imenu' option is selected for `clojure-ts-outline-variant', all
1738+
;; necessary variables will be set automatically by
1739+
;; `treesit-major-mode-setup'.
1740+
(setq-local treesit-outline-predicate #'clojure-ts--outline-predicate
1741+
outline-search-function #'treesit-outline-search
1742+
outline-level #'clojure-ts--outline-level))
17111743

17121744
(setq-local treesit-font-lock-settings
17131745
(clojure-ts--font-lock-settings markdown-available regex-available))

test/samples/outline.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(ns outline)
2+
3+
4+
;;; First heading level 1
5+
6+
(defn foo
7+
[bar]
8+
(println bar))
9+
10+
;;;; Heading level 2
11+
12+
(def baz
13+
{:hello "World"})
14+
15+
;;; Second heading level 1
16+
17+
(defn hello-world
18+
[]
19+
(println "Hello, world!"))

0 commit comments

Comments
 (0)