From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 58844@debbugs.gnu.org
Cc: casouri@gmail.com
Subject: bug#58844: 29.0.50; Font lock features for js/ts-mode
Date: Fri, 28 Oct 2022 21:27:02 +0200 [thread overview]
Message-ID: <87ilk3rbp5.fsf@thornhill.no> (raw)
[-- Attachment #1: Type: text/plain, Size: 233 bytes --]
Hi Yuan!
I've added more font lock features to the js and ts modes. I think they
can serve as a suitable starting point for the features, and serve as
inspiration to other modes, until we settle on a standard here.
Thanks,
Theo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-in-new-font-lock-features-in-js-ts-mode.patch --]
[-- Type: text/x-diff, Size: 13150 bytes --]
From 28d148dc4d3b0a7ba26948c4872ee4aa90a8b51a Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Fri, 28 Oct 2022 21:23:19 +0200
Subject: [PATCH] Add in new font lock features in js/ts-mode
We want to support font lock features on the syntactic level, not the
granularity level.
New supported features are:
- comments
- constants
- keywords
- strings
- declarations
- identifiers
- expressions
- properties
- patterns
- jsx
* lisp/progmodes/js.el (js--treesit-font-lock-settings, js-mode): Add
in the new features.
* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Remove faulty '.'
anchor.
(ts-mode--font-lock-settings, ts-mode): Add in the new features.
---
lisp/progmodes/js.el | 102 ++++++++++++++++------------
lisp/progmodes/ts-mode.el | 136 +++++++++++++++++++++-----------------
2 files changed, 138 insertions(+), 100 deletions(-)
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index dae6de1052..705ea8aad5 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3463,28 +3463,32 @@ js--treesit-font-lock-settings
(treesit-font-lock-rules
:language 'javascript
:override t
- :feature 'minimal
- `(
- ((identifier) @font-lock-constant-face
+ :feature 'comments
+ `((comment) @font-lock-comment-face)
+ :language 'javascript
+ :override t
+ :feature 'constants
+ `(((identifier) @font-lock-constant-face
(:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
- [(this) (super)] @font-lock-keyword-face
-
[(true) (false) (null)] @font-lock-constant-face
- (regex pattern: (regex_pattern)) @font-lock-string-face
- (number) @font-lock-constant-face
-
+ (number) @font-lock-constant-face)
+ :language 'javascript
+ :override t
+ :feature 'keywords
+ `([,@js--treesit-keywords] @font-lock-keyword-face
+ [(this) (super)] @font-lock-keyword-face)
+ :language 'javascript
+ :override t
+ :feature 'strings
+ `((regex pattern: (regex_pattern)) @font-lock-string-face
(string) @font-lock-string-face
- (comment) @font-lock-comment-face
- [,@js--treesit-keywords] @font-lock-keyword-face
-
(template_string) @js--fontify-template-string
- (template_substitution ["${" "}"] @font-lock-constant-face))
+ (template_substitution ["${" "}"] @font-lock-builtin-face))
:language 'javascript
:override t
- :feature 'moderate
- `(
- (function
+ :feature 'declarations
+ `((function
name: (identifier) @font-lock-function-name-face)
(class_declaration
@@ -3499,18 +3503,6 @@ js--treesit-font-lock-settings
(variable_declarator
name: (identifier) @font-lock-variable-name-face)
- (new_expression
- constructor: (identifier) @font-lock-type-face)
-
- (for_in_statement
- left: (identifier) @font-lock-variable-name-face)
-
- (arrow_function
- parameter: (identifier) @font-lock-variable-name-face))
- :language 'javascript
- :override t
- :feature 'full
- `(
(variable_declarator
name: (identifier) @font-lock-function-name-face
value: [(function) (arrow_function)])
@@ -3520,9 +3512,22 @@ js--treesit-font-lock-settings
(identifier)
(identifier)
@font-lock-function-name-face)
- value: (array (number) (function)))
+ value: (array (number) (function))))
+ :language 'javascript
+ :override t
+ :feature 'identifiers
+ `((new_expression
+ constructor: (identifier) @font-lock-type-face)
- (assignment_expression
+ (for_in_statement
+ left: (identifier) @font-lock-variable-name-face)
+
+ (arrow_function
+ parameter: (identifier) @font-lock-variable-name-face))
+ :language 'javascript
+ :override t
+ :feature 'expressions
+ `((assignment_expression
left: [(identifier) @font-lock-function-name-face
(member_expression property: (property_identifier)
@font-lock-function-name-face)]
@@ -3537,9 +3542,11 @@ js--treesit-font-lock-settings
(assignment_expression
left: [(identifier) @font-lock-variable-name-face
(member_expression
- property: (property_identifier) @font-lock-variable-name-face)])
-
- (pair key: (property_identifier) @font-lock-variable-name-face)
+ property: (property_identifier) @font-lock-variable-name-face)]))
+ :language 'javascript
+ :override t
+ :feature 'properties
+ `((pair key: (property_identifier) @font-lock-variable-name-face)
(pair value: (identifier) @font-lock-variable-name-face)
@@ -3549,12 +3556,16 @@ js--treesit-font-lock-settings
((shorthand_property_identifier) @font-lock-variable-name-face)
- (pair_pattern key: (property_identifier) @font-lock-variable-name-face)
-
- ((shorthand_property_identifier_pattern) @font-lock-variable-name-face)
-
- (array_pattern (identifier) @font-lock-variable-name-face)
-
+ ((shorthand_property_identifier_pattern) @font-lock-variable-name-face))
+ :language 'javascript
+ :override t
+ :feature 'patterns
+ `((pair_pattern key: (property_identifier) @font-lock-variable-name-face)
+ (array_pattern (identifier) @font-lock-variable-name-face))
+ :language 'javascript
+ :override t
+ :feature 'jsx
+ `(
(jsx_opening_element
[(nested_identifier (identifier)) (identifier)]
@font-lock-function-name-face)
@@ -3780,7 +3791,17 @@ js-mode
"lexical_declaration")))
;; Fontification.
(setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
- (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full)))
+ (setq-local treesit-font-lock-feature-list
+ '((comments)
+ (constants)
+ (keywords)
+ (strings)
+ (declarations)
+ (identifiers)
+ (expressions)
+ (properties)
+ (patterns)
+ (jsx)))
;; Imenu
(setq-local imenu-create-index-function
#'js--treesit-imenu)
@@ -3802,8 +3823,7 @@ js-mode
(add-hook 'syntax-propertize-extend-region-functions
#'syntax-propertize-multiline 'append 'local)
(add-hook 'syntax-propertize-extend-region-functions
- #'js--syntax-propertize-extend-region 'append 'local)
- )))
+ #'js--syntax-propertize-extend-region 'append 'local))))
(defvar js-json--treesit-font-lock-settings
(treesit-font-lock-rules
diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el
index 15b8cbf711..7deb7a0344 100644
--- a/lisp/progmodes/ts-mode.el
+++ b/lisp/progmodes/ts-mode.el
@@ -60,8 +60,6 @@ ts-mode--indent-rules
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((node-is ">") parent-bol 0)
- ((node-is ".")
- parent-bol ,ts-mode-indent-offset)
((parent-is "ternary_expression")
parent-bol ,ts-mode-indent-offset)
((parent-is "member_expression")
@@ -123,39 +121,32 @@ ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'tsx
:override t
- :feature 'minimal
- `(
- ((identifier) @font-lock-constant-face
+ :feature 'comments
+ `((comment) @font-lock-comment-face)
+ :language 'tsx
+ :override t
+ :feature 'constants
+ `(((identifier) @font-lock-constant-face
(:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
- [,@ts-mode--keywords] @font-lock-keyword-face
- [(this) (super)] @font-lock-keyword-face
-
[(true) (false) (null)] @font-lock-constant-face
- (regex pattern: (regex_pattern)) @font-lock-string-face
- (number) @font-lock-constant-face
-
+ (number) @font-lock-constant-face)
+ :language 'tsx
+ :override t
+ :feature 'keywords
+ `([,@ts-mode--keywords] @font-lock-keyword-face
+ [(this) (super)] @font-lock-keyword-face)
+ :language 'tsx
+ :override t
+ :feature 'strings
+ `((regex pattern: (regex_pattern)) @font-lock-string-face
(string) @font-lock-string-face
-
(template_string) @js--fontify-template-string
- (template_substitution ["${" "}"] @font-lock-builtin-face)
-
- (comment) @font-lock-comment-face)
+ (template_substitution ["${" "}"] @font-lock-builtin-face))
:language 'tsx
:override t
- :feature 'moderate
- '(
- (nested_type_identifier
- module: (identifier) @font-lock-type-face)
-
- (type_identifier) @font-lock-type-face
-
- (predefined_type) @font-lock-type-face
-
- (new_expression
- constructor: (identifier) @font-lock-type-face)
-
- (function
+ :feature 'declarations
+ `((function
name: (identifier) @font-lock-function-name-face)
(function_declaration
@@ -169,6 +160,31 @@ ts-mode--font-lock-settings
(enum_declaration (identifier) @font-lock-type-face)
+ (arrow_function
+ parameter: (identifier) @font-lock-variable-name-face)
+
+ (variable_declarator
+ name: (identifier) @font-lock-function-name-face
+ value: [(function) (arrow_function)])
+
+ (variable_declarator
+ name: (array_pattern
+ (identifier)
+ (identifier) @font-lock-function-name-face)
+ value: (array (number) (function))))
+ :language 'tsx
+ :override t
+ :feature 'identifiers
+ `((nested_type_identifier
+ module: (identifier) @font-lock-type-face)
+
+ (type_identifier) @font-lock-type-face
+
+ (predefined_type) @font-lock-type-face
+
+ (new_expression
+ constructor: (identifier) @font-lock-type-face)
+
(enum_body (property_identifier) @font-lock-type-face)
(enum_assignment name: (property_identifier) @font-lock-type-face)
@@ -182,22 +198,14 @@ ts-mode--font-lock-settings
left: (identifier) @font-lock-variable-name-face)
(arrow_function
- parameter: (identifier) @font-lock-variable-name-face))
+ parameters:
+ [(_ (identifier) @font-lock-variable-name-face)
+ (_ (_ (identifier) @font-lock-variable-name-face))
+ (_ (_ (_ (identifier) @font-lock-variable-name-face)))]))
:language 'tsx
:override t
- :feature 'full
- '(
- (variable_declarator
- name: (identifier) @font-lock-function-name-face
- value: [(function) (arrow_function)])
-
- (variable_declarator
- name: (array_pattern
- (identifier)
- (identifier) @font-lock-function-name-face)
- value: (array (number) (function)))
-
- (assignment_expression
+ :feature 'expressions
+ '((assignment_expression
left: [(identifier) @font-lock-function-name-face
(member_expression
property: (property_identifier) @font-lock-function-name-face)]
@@ -207,15 +215,11 @@ ts-mode--font-lock-settings
function:
[(identifier) @font-lock-function-name-face
(member_expression
- property: (property_identifier) @font-lock-function-name-face)])
-
- (arrow_function
- parameters:
- [(_ (identifier) @font-lock-variable-name-face)
- (_ (_ (identifier) @font-lock-variable-name-face))
- (_ (_ (_ (identifier) @font-lock-variable-name-face)))])
-
- (pair key: (property_identifier) @font-lock-variable-name-face)
+ property: (property_identifier) @font-lock-function-name-face)]))
+ :language 'tsx
+ :override t
+ :feature 'properties
+ `((pair key: (property_identifier) @font-lock-variable-name-face)
(pair value: (identifier) @font-lock-variable-name-face)
@@ -228,15 +232,19 @@ ts-mode--font-lock-settings
((shorthand_property_identifier) @font-lock-variable-name-face)
- (pair_pattern
- key: (property_identifier) @font-lock-variable-name-face)
-
((shorthand_property_identifier_pattern)
- @font-lock-variable-name-face)
-
- (array_pattern (identifier) @font-lock-variable-name-face)
+ @font-lock-variable-name-face))
+ :language 'tsx
+ :override t
+ :feature 'patterns
+ `((pair_pattern
+ key: (property_identifier) @font-lock-variable-name-face)
- (jsx_opening_element
+ (array_pattern (identifier) @font-lock-variable-name-face))
+ :language 'tsx
+ :override t
+ :feature 'jsx
+ `((jsx_opening_element
[(nested_identifier (identifier)) (identifier)]
@font-lock-function-name-face)
@@ -283,7 +291,17 @@ ts-mode
"lexical_declaration")))
;; Font-lock.
(setq-local treesit-font-lock-settings ts-mode--font-lock-settings)
- (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full)))
+ (setq-local treesit-font-lock-feature-list
+ '((comments)
+ (constants)
+ (keywords)
+ (strings)
+ (declarations)
+ (identifiers)
+ (expressions)
+ (properties)
+ (patterns)
+ (jsx)))
;; Imenu.
(setq-local imenu-create-index-function #'js--treesit-imenu)
;; Which-func (use imenu).
--
2.34.1
next reply other threads:[~2022-10-28 19:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-28 19:27 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-10-28 19:55 ` bug#58844: 29.0.50; Font lock features for js/ts-mode Yuan Fu
2022-10-28 19:59 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-28 20:25 ` Yuan Fu
2022-11-12 20:45 ` Stefan Kangas
2022-11-12 20:55 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-12 21:11 ` Stefan Kangas
2022-11-14 9:15 ` Yuan Fu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ilk3rbp5.fsf@thornhill.no \
--to=bug-gnu-emacs@gnu.org \
--cc=58844@debbugs.gnu.org \
--cc=casouri@gmail.com \
--cc=theo@thornhill.no \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).