unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] treesit.el: New function to describe font-lock rules
@ 2023-10-03 13:32 Huan Thieu Nguyen
  2023-10-06  7:05 ` Yuan Fu
  0 siblings, 1 reply; 6+ messages in thread
From: Huan Thieu Nguyen @ 2023-10-03 13:32 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 319 bytes --]

Hello emacs maintainers,

I think most major modes don't need to use multiple tree-sitter 
grammars, so I made a function called `treesit-font-lock-simple-rules' 
which saves the user from typing `:language` for every query-spec.

I have not added an announcement in the news file yet.

Best Regards

Nguyen Thieu Huan

[-- Attachment #2: 0001-Add-treesit-font-lock-simple-rules-function.patch --]
[-- Type: text/x-patch, Size: 1316 bytes --]

From 390870496073d3cc9fb6a85413b21338d0f9ecdb Mon Sep 17 00:00:00 2001
From: Huan Nguyen <goafanxx@gmail.com>
Date: Tue, 3 Oct 2023 15:17:55 +0200
Subject: [PATCH] Add `treesit-font-lock-simple-rules' function.

* lisp/treesit.el (treesit-font-lock-simple-rules):
Add a function which saves the user from typing :language for every
query-spec.
---
 lisp/treesit.el | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index a9761db..3bcca9d 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -837,6 +837,17 @@ treesit-font-lock-rules
                        `("Unexpected value" ,token))))))
       (nreverse result))))
 
+(defun treesit-font-lock-simple-rules (language &rest rules)
+  "Wrapper around `treesit-font-lock-rules'.
+It saves the user from typing :language `LANGUAGE' for every `RULES'."
+  (apply #'treesit-font-lock-rules
+         (seq-reduce
+   (lambda (query-specs keyword)
+     (if (eq :feature keyword)
+	 (append query-specs `(:language ,language ,keyword))
+       (append query-specs `(,keyword))))
+   rules '())))
+
 ;; `font-lock-fontify-region-function' has the LOUDLY argument, but
 ;; `jit-lock-functions' doesn't pass that argument.  So even if we set
 ;; `font-lock-verbose' to t, if jit-lock is enabled (and it's almost
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] treesit.el: New function to describe font-lock rules
  2023-10-03 13:32 [PATCH] treesit.el: New function to describe font-lock rules Huan Thieu Nguyen
@ 2023-10-06  7:05 ` Yuan Fu
       [not found]   ` <1e5cd9da-7be2-486d-bbbb-7b50167e2916@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Yuan Fu @ 2023-10-06  7:05 UTC (permalink / raw)
  To: Huan Thieu Nguyen; +Cc: emacs-devel

Hi Huan,

> On Oct 3, 2023, at 6:32 AM, Huan Thieu Nguyen <nguyenthieuhuan@gmail.com> wrote:
> 
> Hello emacs maintainers,
> 
> I think most major modes don't need to use multiple tree-sitter grammars, so I made a function called `treesit-font-lock-simple-rules' which saves the user from typing `:language` for every query-spec.
> 
> I have not added an announcement in the news file yet.


Sorry for the late response. I agree it’s somewhat tedious. I didn’t came up with a clean solution for it at the time and left it be.

Instead of finding :feature keywords and appending :language keywords after them, maybe we could introduce a secret keyword (say, :default-language or :global-language) to treesit-font-lock-rules, and make treesit-font-lock-simple-rules use that.

We could also just add the :default-language or :global-language keyword to treesit-font-lock-rules. If we do that, we can also add :default-override, too. But adding global keywords doesn’t look super clean to me (and it might be a bit confusing), that’s why I didn’t implement it in the first place.

Yuan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] treesit.el: New function to describe font-lock rules
       [not found]               ` <CALZcaFhB=oOFcO4arckFCMVbddkPb9xPQRAfUiDZp2_f=qd9fA@mail.gmail.com>
@ 2023-10-21  4:11                 ` Yuan Fu
  2023-10-21  7:38                   ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Yuan Fu @ 2023-10-21  4:11 UTC (permalink / raw)
  To: Huan Nguyen; +Cc: emacs-devel, Eli Zaretskii

[-- Attachment #1: Type: text/plain, Size: 348 bytes --]

(Adding emacs-devel back to CC)

> On Oct 18, 2023, at 1:32 AM, Huan Nguyen <nguyenthieuhuan@gmail.com> wrote:
> 
> My copyright assignment process is done.

Great! I’ve pushed it to master. Eli, do we need a NEWS entry for this change? We are adding a new keyword for treesit-font-lock-rules. I attached the merged patch below.

Yuan


[-- Attachment #2: 0001-New-keyword-default-language-in-treesit-font-lock-ru.patch --]
[-- Type: application/octet-stream, Size: 3983 bytes --]

From 70c3bfdafa46a5389b25fab322ca2c2bb75f3583 Mon Sep 17 00:00:00 2001
From: Huan Nguyen <goafanxx@gmail.com>
Date: Sat, 7 Oct 2023 12:03:55 +0200
Subject: [PATCH] New keyword :default-language in `treesit-font-lock-rules'
 function.

* lisp/treesit.el (treesit-font-lock-rules):
Keyword :default-language `LANGUAGE' will be chosen for every :feature.
Using :language will override the :default-language for the next :feature.
---
 lisp/treesit.el | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 402417c..31ebd50 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -922,12 +922,21 @@ treesit-font-lock-rules
           ;; that following queries will apply to.
           current-language current-override
           current-feature
+          ;; default-language will be chosen when current-language is not set
+          default-language
           ;; The list this function returns.
           (result nil))
       (while query-specs
         (let ((token (pop query-specs)))
           (pcase token
             ;; (1) Process keywords.
+            (:default-language
+             (let ((lang (pop query-specs)))
+               (when (or (not (symbolp lang)) (null lang))
+                 (signal 'treesit-font-lock-error
+                         `("Value of :default-language should be a symbol"
+                           ,lang)))
+               (setq default-language lang)))
             (:language
              (let ((lang (pop query-specs)))
                (when (or (not (symbolp lang)) (null lang))
@@ -955,23 +964,24 @@ treesit-font-lock-rules
                (setq current-feature var)))
             ;; (2) Process query.
             ((pred treesit-query-p)
-             (when (null current-language)
-               (signal 'treesit-font-lock-error
-                       `("Language unspecified, use :language keyword to specify a language for this query" ,token)))
-             (when (null current-feature)
-               (signal 'treesit-font-lock-error
-                       `("Feature unspecified, use :feature keyword to specify the feature name for this query" ,token)))
-             (if (treesit-compiled-query-p token)
-                 (push `(,current-language token) result)
-               (push `(,(treesit-query-compile current-language token)
-                       t
-                       ,current-feature
-                       ,current-override)
-                     result))
-             ;; Clears any configurations set for this query.
-             (setq current-language nil
-                   current-override nil
-                   current-feature nil))
+             (let ((lang (or default-language current-language)))
+               (when (null lang)
+                 (signal 'treesit-font-lock-error
+                         `("Language unspecified, use :language keyword or :default-language to specify a language for this query" ,token)))
+               (when (null current-feature)
+                 (signal 'treesit-font-lock-error
+                         `("Feature unspecified, use :feature keyword to specify the feature name for this query" ,token)))
+               (if (treesit-compiled-query-p token)
+                   (push `(,lang token) result)
+                 (push `(,(treesit-query-compile lang token)
+                         t
+                         ,current-feature
+                         ,current-override)
+                       result))
+               ;; Clears any configurations set for this query.
+               (setq current-language nil
+                     current-override nil
+                     current-feature nil)))
             (_ (signal 'treesit-font-lock-error
                        `("Unexpected value" ,token))))))
       (nreverse result))))
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] treesit.el: New function to describe font-lock rules
  2023-10-21  4:11                 ` Yuan Fu
@ 2023-10-21  7:38                   ` Eli Zaretskii
  2023-10-21  8:17                     ` Yuan Fu
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2023-10-21  7:38 UTC (permalink / raw)
  To: Yuan Fu; +Cc: nguyenthieuhuan, emacs-devel

> From: Yuan Fu <casouri@gmail.com>
> Date: Fri, 20 Oct 2023 21:11:01 -0700
> Cc: emacs-devel <emacs-devel@gnu.org>,
>  Eli Zaretskii <eliz@gnu.org>
> 
> Great! I’ve pushed it to master. Eli, do we need a NEWS entry for this change? We are adding a new keyword for treesit-font-lock-rules. I attached the merged patch below.

Not only a NEWS entry is in order, we should also document this
keyword in the ELisp manual, where the other keywords of
treesit-font-lock-rules are documented.

Thanks.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] treesit.el: New function to describe font-lock rules
  2023-10-21  7:38                   ` Eli Zaretskii
@ 2023-10-21  8:17                     ` Yuan Fu
  2023-10-21 19:49                       ` Yuan Fu
  0 siblings, 1 reply; 6+ messages in thread
From: Yuan Fu @ 2023-10-21  8:17 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Huan Nguyen, emacs-devel



> On Oct 21, 2023, at 12:38 AM, Eli Zaretskii <eliz@gnu.org> wrote:
> 
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Fri, 20 Oct 2023 21:11:01 -0700
>> Cc: emacs-devel <emacs-devel@gnu.org>,
>> Eli Zaretskii <eliz@gnu.org>
>> 
>> Great! I’ve pushed it to master. Eli, do we need a NEWS entry for this change? We are adding a new keyword for treesit-font-lock-rules. I attached the merged patch below.
> 
> Not only a NEWS entry is in order, we should also document this
> keyword in the ELisp manual, where the other keywords of
> treesit-font-lock-rules are documented.
> 
> Thanks.

Yes, absolutely.

Yuan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] treesit.el: New function to describe font-lock rules
  2023-10-21  8:17                     ` Yuan Fu
@ 2023-10-21 19:49                       ` Yuan Fu
  0 siblings, 0 replies; 6+ messages in thread
From: Yuan Fu @ 2023-10-21 19:49 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Huan Nguyen, emacs-devel



> On Oct 21, 2023, at 1:17 AM, Yuan Fu <casouri@gmail.com> wrote:
> 
> 
> 
>> On Oct 21, 2023, at 12:38 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>> 
>>> From: Yuan Fu <casouri@gmail.com>
>>> Date: Fri, 20 Oct 2023 21:11:01 -0700
>>> Cc: emacs-devel <emacs-devel@gnu.org>,
>>> Eli Zaretskii <eliz@gnu.org>
>>> 
>>> Great! I’ve pushed it to master. Eli, do we need a NEWS entry for this change? We are adding a new keyword for treesit-font-lock-rules. I attached the merged patch below.
>> 
>> Not only a NEWS entry is in order, we should also document this
>> keyword in the ELisp manual, where the other keywords of
>> treesit-font-lock-rules are documented.
>> 
>> Thanks.
> 
> Yes, absolutely.
> 
> Yuan

Done.

Yuan


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-10-21 19:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-03 13:32 [PATCH] treesit.el: New function to describe font-lock rules Huan Thieu Nguyen
2023-10-06  7:05 ` Yuan Fu
     [not found]   ` <1e5cd9da-7be2-486d-bbbb-7b50167e2916@gmail.com>
     [not found]     ` <35F71E15-AEAB-45FD-ACF3-50133A1766D8@gmail.com>
     [not found]       ` <2c196077-a1c7-4bfa-91b6-209ebf21b6fa@gmail.com>
     [not found]         ` <CB77885F-776A-4397-83E6-07835269FE50@gmail.com>
     [not found]           ` <91640cdb-d78a-4bd5-895c-9aacca3d4c97@gmail.com>
     [not found]             ` <E07BF33E-BD63-42F1-8B7E-FF110CE9642E@gmail.com>
     [not found]               ` <CALZcaFhB=oOFcO4arckFCMVbddkPb9xPQRAfUiDZp2_f=qd9fA@mail.gmail.com>
2023-10-21  4:11                 ` Yuan Fu
2023-10-21  7:38                   ` Eli Zaretskii
2023-10-21  8:17                     ` Yuan Fu
2023-10-21 19:49                       ` Yuan Fu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).