Skip to content

Support 2 variants of integration with outline-minor-mode #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#16](https://github.com/clojure-emacs/clojure-ts-mode/issues/16): Introduce `clojure-ts-align`.
- [#11](https://github.com/clojure-emacs/clojure-ts-mode/issues/11): Enable regex syntax highlighting.
- [#16](https://github.com/clojure-emacs/clojure-ts-mode/issues/16): Add support for automatic aligning forms.
- [#82](https://github.com/clojure-emacs/clojure-ts-mode/issues/82): Introduce `clojure-ts-outline-variant`.

## 0.3.0 (2025-04-15)

Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Every new line in the docstrings is indented by
`clojure-ts-docstring-fill-prefix-width` number of spaces (set to 2 by default
which matches the `clojure-mode` settings).

#### imenu
### imenu

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

### Integration with `outline-minor-mode`

`clojure-ts-mode` supports two integration variants with
`outline-minor-mode`. The default variant uses special top-level comments (level
1 heading starts with three semicolons, level 2 heading starts with four,
etc.). The other variant treats def-like forms (the same forms produced by the
`imenu` command) as outline headings. To use the second option, use the
following customization:

```emacs-lisp
(setopt clojure-ts-outline-variant 'imenu)
```

## Migrating to clojure-ts-mode

If you are migrating to `clojure-ts-mode` note that `clojure-mode` is still required for cider and clj-refactor packages to work properly.
Expand Down
32 changes: 32 additions & 0 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ double quotes on the third column."
:type 'boolean
:package-version '(clojure-ts-mode . "0.3"))

(defcustom clojure-ts-outline-variant 'comments
"Determines how `clojure-ts-mode' integrates with `outline-minor-mode'.

If set to the symbol `comments', then top-level comments starting with
three or more semicolons will be treated as outline headings. If set to
`imenu', then def-like forms are treated as outline headings."
:safe #'symbolp
:type '(choice (const :tag "Use special comments" comments)
(const :tag "Use imenu" imenu))
:package-version '(clojure-ts-mode . "0.4"))

(defcustom clojure-ts-align-reader-conditionals nil
"Whether to align reader conditionals, as if they were maps."
:package-version '(clojure-ts-mode . "0.4")
Expand Down Expand Up @@ -913,6 +924,20 @@ Includes a dispatch value when applicable (defmethods)."
By default `treesit-defun-name-function' is used to extract definition names.
See `clojure-ts--standard-definition-node-name' for the implementation used.")

;;; Outline settings

(defun clojure-ts--outline-predicate (node)
"Return TRUE if NODE is an outline heading comment."
(and (string= (treesit-node-type node) "comment")
(string-match-p "^\\(?:;;;;* \\).*" (treesit-node-text node))))

(defun clojure-ts--outline-level ()
"Return the current level of the outline heading at point."
(let* ((node (treesit-outline--at-point))
(node-text (treesit-node-text node)))
(string-match ";;\\(;+\\) " node-text)
(- (match-end 1) (match-beginning 1))))

(defcustom clojure-ts-indent-style 'semantic
"Automatic indentation style to use when mode `clojure-ts-mode' is run.

Expand Down Expand Up @@ -1708,6 +1733,13 @@ REGEX-AVAILABLE."
(setq-local indent-tabs-mode nil)
(setq-local comment-add 1)
(setq-local comment-start ";")
(when (equal clojure-ts-outline-variant 'comments)
;; NOTE: If `imenu' option is selected for `clojure-ts-outline-variant', all
;; necessary variables will be set automatically by
;; `treesit-major-mode-setup'.
(setq-local treesit-outline-predicate #'clojure-ts--outline-predicate
outline-search-function #'treesit-outline-search
outline-level #'clojure-ts--outline-level))

(setq-local treesit-font-lock-settings
(clojure-ts--font-lock-settings markdown-available regex-available))
Expand Down
19 changes: 19 additions & 0 deletions test/samples/outline.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns outline)


;;; First heading level 1

(defn foo
[bar]
(println bar))

;;;; Heading level 2

(def baz
{:hello "World"})

;;; Second heading level 1

(defn hello-world
[]
(println "Hello, world!"))