bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#65570: [Tree Sitter] [Feature Request] Allow Adding User-Defined Que


From: Jacob Faibussowitsch
Subject: bug#65570: [Tree Sitter] [Feature Request] Allow Adding User-Defined Queries
Date: Sun, 27 Aug 2023 14:37:16 -0400

Hello,

Is it possible for users to define their own custom font-lock queries? If so, 
how?

Per the documentation, font-locking is controlled by the contents of 
`treesit-font-lock-settings`. Perusing `lisp/progmodes/c-ts-mode.el` one finds 
`c-ts-mode--font-lock-settings`, which is used to populate 
`treesit-font-lock-settings`, but there seems to be no way to override or 
append to these settings.

For example, suppose that I — just for laughs — wanted to highlight the 
namespace name in the same face as integer-literals for:
```
// namespace.cpp
int x = 2;

namespace foo
{

}
```
I might do:
```
;; init.el

;; ensure the modes are overridden
(add-to-list 'major-mode-remap-alist '(c-mode . c-ts-mode))
(add-to-list 'major-mode-remap-alist '(c++-mode . c++-ts-mode))
(add-to-list 'major-mode-remap-alist '(c-or-c++-mode . c-or-c++-ts-mode))

;; to make it ~pop~
(set-face-attribute 'font-lock-number-face nil :foreground "red")

(use-package c-ts-mode
  :config
  (setq my-custom-font-lock-rules
    (treesit-font-lock-rules
      :language 'cpp
      :override t
      :feature 'type
      `((namespace_definition
         name: (namespace_identifier) @font-lock-number-face))))
  (setq-local treesit-font-lock-settings my-custom-font-lock-rules)
  (treesit-major-mode-setup))
```
But that does not seem to work.

PNG image

You can see that 2 is obviously red, but foo is not (in fact, `M-x 
describe-face` reports it as “default” face). I want both foo and 2 to have the 
same font face.

You can reproduce this with (assuming you paste the above 2 snippets into some 
directory:
```
$ emacs --init-directory=`pwd` ./namespace.cpp
```

Best regards,

Jacob Faibussowitsch
(Jacob Fai - booss - oh - vitch)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]