Skip to content

Update grammars #78

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 1 commit into from
Apr 11, 2025
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 @@ -20,6 +20,7 @@
- [#72](https://github.com/clojure-emacs/clojure-ts-mode/pull/72): Pass fully qualified symbol to `clojure-ts-get-indent-function`.
- [#76](https://github.com/clojure-emacs/clojure-ts-mode/pull/76): Improve performance of semantic indentation by caching rules.
- [#74](https://github.com/clojure-emacs/clojure-ts-mode/issues/74): Add imenu support for keywords definitions.
- [#77](https://github.com/clojure-emacs/clojure-ts-mode/issues/77): Update grammars to the latest versions.

## 0.2.3 (2025-03-04)

Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Once installed, evaluate clojure-ts-mode.el and you should be ready to go.
`clojure-ts-mode` makes use of two TreeSitter grammars to work properly:

- The Clojure grammar, mentioned earlier
- [markdown_inline](https://github.com/MDeiml/tree-sitter-markdown), which
- [markdown-inline](https://github.com/MDeiml/tree-sitter-markdown), which
will be used for docstrings if available and if `clojure-ts-use-markdown-inline` is enabled.

If you have `git` and a C compiler (`cc`) available on your system's `PATH`,
Expand All @@ -137,6 +137,17 @@ option to install it manually, Please, refer to the installation instructions of
each required grammar and make sure you're install the versions expected. (see
`clojure-ts-grammar-recipes` for details)

### Upgrading tree-sitter grammars

To reinstall or upgrade TreeSitter grammars, you can execute:

```emacs-lisp
M-x clojure-ts-reinstall-grammars
```

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

## Configuration

To see a list of available configuration options do `M-x customize-group <RET> clojure-ts`.
Expand Down
44 changes: 27 additions & 17 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,14 @@ if a third argument (the value) is provided.

(defvar clojure-ts--treesit-range-settings
(treesit-range-rules
:embed 'markdown_inline
:embed 'markdown-inline
:host 'clojure
(clojure-ts--docstring-query '@capture)))

(defun clojure-ts--font-lock-settings (markdown-available)
"Return font lock settings suitable for use in `treesit-font-lock-settings'.
When MARKDOWN-AVAILABLE is non-nil, includes rules for highlighting docstrings
with the markdown_inline grammar."
with the markdown-inline grammar."
(append
(treesit-font-lock-rules
:feature 'string
Expand Down Expand Up @@ -512,11 +512,9 @@ with the markdown_inline grammar."
(when markdown-available
(treesit-font-lock-rules
:feature 'doc
:language 'markdown_inline
:language 'markdown-inline
:override t
`((inline
(code_span (code_span_delimiter) :* @font-lock-delimiter-face)
@font-lock-constant-face))))
`((code_span) @font-lock-constant-face)))

(treesit-font-lock-rules
:feature 'quote
Expand Down Expand Up @@ -985,7 +983,7 @@ and (:defn) is converted to (:inner 1)."
(t nil))))

(defun clojure-ts--dynamic-indent-for-symbol (sym &optional ns)
"Returns the dynamic indentation specification for SYM, if found.
"Return the dynamic indentation specification for SYM, if found.

If the function `clojure-ts-get-indent-function' is defined, call it and
produce a valid indentation specification from its return value.
Expand Down Expand Up @@ -1019,7 +1017,7 @@ If NS is defined, then the fully qualified symbol is passed to
(equal (car spec1) :block)))))))))

(defun clojure-ts--find-semantic-rule (node parent current-depth)
"Returns a suitable indentation rule for NODE, considering the CURRENT-DEPTH.
"Return a suitable indentation rule for NODE, considering the CURRENT-DEPTH.

Attempts to find an indentation rule by examining the symbol name of the
PARENT's first child. If a rule is not found, it navigates up the
Expand Down Expand Up @@ -1153,21 +1151,21 @@ according to the rule. If NODE is nil, use next node after BOL."
(clojure-ts--metadata-node-p prev-sibling))))

(defun clojure-ts--anchor-parent-skip-metadata (_node parent _bol)
"Anchor function that returns position of PARENT start for NODE.
"Return position of PARENT start for NODE.

If PARENT has optional metadata we skip it and return starting position
of the first child's opening paren.

NOTE: This anchor is used to fix indentation issue for forms with type
hints."
NOTE: This serves as an anchor function to resolve an indentation issue
for forms with type hints."
(let ((first-child (treesit-node-child parent 0 t)))
(if (clojure-ts--metadata-node-p first-child)
;; We don't need named node here
(treesit-node-start (treesit-node-child parent 1))
(treesit-node-start parent))))

(defun clojure-ts--match-collection-item-with-metadata (node-type)
"Returns a matcher for a collection item with metadata by NODE-TYPE.
"Return a matcher for a collection item with metadata by NODE-TYPE.

The returned matcher accepts NODE, PARENT and BOL and returns true only
if NODE has metadata and its parent has type NODE-TYPE."
Expand Down Expand Up @@ -1296,9 +1294,9 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."

(defconst clojure-ts-grammar-recipes
'((clojure "https://github.com/sogaiu/tree-sitter-clojure.git"
"v0.0.12")
(markdown_inline "https://github.com/MDeiml/tree-sitter-markdown"
"v0.1.6"
"v0.0.13")
(markdown-inline "https://github.com/MDeiml/tree-sitter-markdown"
"v0.4.1"
"tree-sitter-markdown-inline/src"))
"Intended to be used as the value for `treesit-language-source-alist'.")

Expand All @@ -1316,6 +1314,18 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
(let ((treesit-language-source-alist clojure-ts-grammar-recipes))
(treesit-install-language-grammar grammar)))))))

(defun clojure-ts-reinstall-grammars ()
"Install the required versions of language grammars.

If the grammars are already installed, they will be reinstalled. This
function can also be used to upgrade the grammars if they are outdated."
(interactive)
(dolist (recipe clojure-ts-grammar-recipes)
(let ((grammar (car recipe)))
(message "Installing %s tree-sitter grammar" grammar)
(let ((treesit-language-source-alist clojure-ts-grammar-recipes))
(treesit-install-language-grammar grammar)))))

(defun clojure-ts-mode-variables (&optional markdown-available)
"Initialize buffer-local variables for `clojure-ts-mode'.
See `clojure-ts--font-lock-settings' for usage of MARKDOWN-AVAILABLE."
Expand Down Expand Up @@ -1361,9 +1371,9 @@ See `clojure-ts--font-lock-settings' for usage of MARKDOWN-AVAILABLE."
:syntax-table clojure-ts-mode-syntax-table
(clojure-ts--ensure-grammars)
(let ((use-markdown-inline (and clojure-ts-use-markdown-inline
(treesit-ready-p 'markdown_inline t))))
(treesit-ready-p 'markdown-inline t))))
(when use-markdown-inline
(treesit-parser-create 'markdown_inline)
(treesit-parser-create 'markdown-inline)
(setq-local treesit-range-settings clojure-ts--treesit-range-settings))

(when (treesit-ready-p 'clojure)
Expand Down
Loading