From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#59691: 29.0.60; typescript-ts-mode: any HTML-like elements causes fontification to become invalid and remaining parse-tree to become jsx-expression Date: Thu, 01 Dec 2022 07:01:55 +0100 Message-ID: <871qpj1x2k.fsf@thornhill.no> References: <831qpklejb.fsf@gnu.org> <83mt88jul4.fsf@gnu.org> <0A18F996-F3BB-4A7D-A0BE-76C8374DE517@thornhill.no> <8335a0jofb.fsf@gnu.org> <7BE15945-C463-448A-8593-E3B5D0E374FB@thornhill.no> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3023"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 59691@debbugs.gnu.org, casouri@gmail.com, jostein@secure.kjonigsen.net, jostein@kjonigsen.net To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 01 07:03:20 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p0cfI-0000bZ-5n for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 01 Dec 2022 07:03:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p0cf1-0000ek-S9; Thu, 01 Dec 2022 01:03:03 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p0cf0-0000eL-D2 for bug-gnu-emacs@gnu.org; Thu, 01 Dec 2022 01:03:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p0cf0-0000Cn-4c for bug-gnu-emacs@gnu.org; Thu, 01 Dec 2022 01:03:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p0cf0-0004lF-0b for bug-gnu-emacs@gnu.org; Thu, 01 Dec 2022 01:03:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 01 Dec 2022 06:03:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59691 X-GNU-PR-Package: emacs Original-Received: via spool by 59691-submit@debbugs.gnu.org id=B59691.166987452518291 (code B ref 59691); Thu, 01 Dec 2022 06:03:01 +0000 Original-Received: (at 59691) by debbugs.gnu.org; 1 Dec 2022 06:02:05 +0000 Original-Received: from localhost ([127.0.0.1]:37510 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p0ce4-0004kx-SW for submit@debbugs.gnu.org; Thu, 01 Dec 2022 01:02:05 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]:19068) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p0ce1-0004ka-5K for 59691@debbugs.gnu.org; Thu, 01 Dec 2022 01:02:03 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1669874517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=z37/1e1Kg8MfuXqPL8D6aJTlKxe0iZubQclEXbVTFtU=; b=Yv+VMhVY0KWJEkNqWyRzdVuu+FVVonFDCMEQtRUu2hxrwyFpYokjmfnIslxdb6PL5Xcri6 HL4wqT+ds7YZHxIJFUB3KEFVpJ2LRNMhxzgvqpCmXIZv3BbJMCPIWbn8jWwvwfg9JSVs29 BzyL+vV8R/ttY/ygou0QfZBacgCjpPVFJw0XBhwxqwS7PGWy9SSN5FL+39ifOr48+BsE1k OuVJRv9TTFi7MlWRKr0uTVlusMbc/tlp6siDAtMvFzDo9eKfSyPnaLSW2yYRl4us7AEx5S OyoudTG+leK3lLOfaI5eBN7Dve6hvv/msGoRJ9OFnnQ3tc4JredK719AHX8weA== In-Reply-To: <7BE15945-C463-448A-8593-E3B5D0E374FB@thornhill.no> X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:249594 Archived-At: --=-=-= Content-Type: text/plain Theodor Thornhill writes: > On 30 November 2022 19:19:04 CET, Eli Zaretskii wrote: >>> Date: Wed, 30 Nov 2022 19:10:04 +0100 >>> From: Theodor Thornhill >>> CC: casouri@gmail.com, jostein@secure.kjonigsen.net, 59691@debbugs.gnu.org, >>> jostein@kjonigsen.net >>> >>> >Our automatic turning-on of major-modes looks at the file-name extension and >>> >little else (magic-mode-alist is not useful here). So if a file whose >>> >extension is XYZ can have more than one applicable major-mode, we should try >>> >to do something to turn on the correct mode automatically. What can we do? >>> >>> We are lucky because the extensions in question are ts, tsx and js. So we don't need any smartness :) >> >>And the extensions always say unequivocally what mode to use? Then yes, we >>are lucky, and need just one more major mode (and one more grammar library). > > I have already made that commit, but it is waiting on the indent style patch :) Ok, because it seems we didn't want the indent style patch let's fix this bug instead :) Attached is the new major mode. What do you think? Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-new-TypeScript-mode-tsx-ts-mode.patch >From 5cdae535295d92dfe7feb5ad595e836f31bd3e85 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Tue, 29 Nov 2022 21:39:38 +0100 Subject: [PATCH] Add new TypeScript mode tsx-ts-mode There are in fact two languages supporting TypeScript for tree-sitter. Because TSX causes some ambiguities with types there are two grammars, one called typescript and one called tsx. To account for this and to be as correct as possible we enable using both. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules): Change to a function to accomodate the two languages. (typescript-ts-mode--font-lock-settings): Change to a function to accomodate the two languages. (typescript-ts-mode--base-mode): Parent mode for typescript-ts-mode and tsx-ts-mode. (typescript-ts-mode): Derive from typescript-ts-mode--base-mode and extend with language specific settings (tsx-ts-mode): New major mode that derives from typescript-ts-mode--base-mode and extend it with language specific settings Add autoload cookies for the respective file type extensions: .ts and .tsx. * etc/NEWS: Mention the new mode. --- etc/NEWS | 7 +- lisp/progmodes/typescript-ts-mode.el | 155 +++++++++++++++++---------- 2 files changed, 106 insertions(+), 56 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 4e091a5fed..439d20960b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2972,7 +2972,12 @@ A major mode based on the tree-sitter library for editing programs in the TypeScript language. It includes support for font-locking, indentation, and navigation. -** New major mode 'c-ts-mode'. +** New mode 'tsx-ts-mode'. +A major mode based on the tree-sitter library for editing programs +in the TypeScript language, with support for TSX. It includes +support for font-locking, indentation, and navigation. + +** New mode 'c-ts-mode'. A major mode based on the tree-sitter library for editing programs in the C language. It includes support for font-locking, indentation, Imenu, which-func, and navigation. diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 6c926a4e3e..c1beaf3134 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -22,6 +22,10 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . + +;;; Commentary: +;; + ;;; Code: (require 'treesit) @@ -56,8 +60,10 @@ typescript-ts-mode--syntax-table table) "Syntax table for `typescript-ts-mode'.") -(defvar typescript-ts-mode--indent-rules - `((tsx +(defun typescript-ts-mode--indent-rules (language) + "Rules used for indentation. +Argument LANGUAGE is either 'typescript' or 'tsx'." + `((,language ((parent-is "program") parent-bol 0) ((node-is "}") parent-bol 0) ((node-is ")") parent-bol 0) @@ -82,14 +88,13 @@ typescript-ts-mode--indent-rules ((parent-is "arrow_function") parent-bol typescript-ts-mode-indent-offset) ((parent-is "parenthesized_expression") parent-bol typescript-ts-mode-indent-offset) - ;; TSX - ((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset) - ((node-is "jsx_closing_element") parent 0) - ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset) - ((node-is "/") parent 0) - ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset) - (no-node parent-bol 0))) - "Tree-sitter indent rules.") + ,@(when (eq language 'tsx) + `(((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset) + ((node-is "jsx_closing_element") parent 0) + ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset) + ((node-is "/") parent 0) + ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset))) + (no-node parent-bol 0)))) (defvar typescript-ts-mode--keywords '("!" "abstract" "as" "async" "await" "break" @@ -110,14 +115,16 @@ typescript-ts-mode--operators "&&" "||" "!" "?.") "TypeScript operators for tree-sitter font-locking.") -(defvar typescript-ts-mode--font-lock-settings +(defun typescript-ts-mode--font-lock-settings (language) + "Tree-sitter font-lock settings. +Argument LANGUAGE is either 'typescript' or 'tsx'." (treesit-font-lock-rules - :language 'tsx + :language language :override t :feature 'comment `((comment) @font-lock-comment-face) - :language 'tsx + :language language :override t :feature 'constant `(((identifier) @font-lock-constant-face @@ -125,13 +132,13 @@ typescript-ts-mode--font-lock-settings [(true) (false) (null)] @font-lock-constant-face) - :language 'tsx + :language language :override t :feature 'keyword `([,@typescript-ts-mode--keywords] @font-lock-keyword-face [(this) (super)] @font-lock-keyword-face) - :language 'tsx + :language language :override t :feature 'string `((regex pattern: (regex_pattern)) @font-lock-string-face @@ -139,7 +146,7 @@ typescript-ts-mode--font-lock-settings (template_string) @js--fontify-template-string (template_substitution ["${" "}"] @font-lock-builtin-face)) - :language 'tsx + :language language :override t :feature 'declaration `((function @@ -177,7 +184,7 @@ typescript-ts-mode--font-lock-settings (identifier) @font-lock-function-name-face) value: (array (number) (function)))) - :language 'tsx + :language language :override t :feature 'identifier `((nested_type_identifier @@ -208,7 +215,7 @@ typescript-ts-mode--font-lock-settings (_ (_ (identifier) @font-lock-variable-name-face)) (_ (_ (_ (identifier) @font-lock-variable-name-face)))])) - :language 'tsx + :language language :override t :feature 'expression '((assignment_expression @@ -223,7 +230,7 @@ typescript-ts-mode--font-lock-settings (member_expression property: (property_identifier) @font-lock-function-name-face)])) - :language 'tsx + :language language :override t :feature 'pattern `((pair_pattern @@ -231,7 +238,7 @@ typescript-ts-mode--font-lock-settings (array_pattern (identifier) @font-lock-variable-name-face)) - :language 'tsx + :language language :override t :feature 'jsx `((jsx_opening_element @@ -248,31 +255,31 @@ typescript-ts-mode--font-lock-settings (jsx_attribute (property_identifier) @font-lock-constant-face)) - :language 'tsx + :language language :feature 'number `((number) @font-lock-number-face ((identifier) @font-lock-number-face (:match "^\\(:?NaN\\|Infinity\\)$" @font-lock-number-face))) - :language 'tsx + :language language :feature 'operator `([,@typescript-ts-mode--operators] @font-lock-operator-face (ternary_expression ["?" ":"] @font-lock-operator-face)) - :language 'tsx + :language language :feature 'bracket '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face) - :language 'tsx + :language language :feature 'delimiter '((["," "." ";" ":"]) @font-lock-delimiter-face) - :language 'tsx + :language language :feature 'escape-sequence :override t '((escape_sequence) @font-lock-escape-face) - :language 'tsx + :language language :override t :feature 'property `((pair value: (identifier) @font-lock-variable-name-face) @@ -280,17 +287,71 @@ typescript-ts-mode--font-lock-settings ((shorthand_property_identifier) @font-lock-property-face) ((shorthand_property_identifier_pattern) - @font-lock-property-face))) - "Tree-sitter font-lock settings.") + @font-lock-property-face)))) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)) ;;;###autoload -(add-to-list 'auto-mode-alist '("\\.tsx\\'" . typescript-ts-mode)) +(add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode)) ;;;###autoload -(define-derived-mode typescript-ts-mode prog-mode "TypeScript" +(define-derived-mode typescript-ts-mode--base-mode prog-mode "TypeScript" + "Major mode for editing TypeScript." + :group 'typescript + :syntax-table typescript-ts-mode--syntax-table + + ;; Comments. + (setq-local comment-start "// ") + (setq-local comment-end "") + (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") + (setq-local comment-end-skip + (rx (* (syntax whitespace)) + (group (or (syntax comment-end) + (seq (+ "*") "/"))))) + + ;; Electric + (setq-local electric-indent-chars + (append "{}():;," electric-indent-chars)) + + ;; Navigation. + (setq-local treesit-defun-type-regexp + (rx (or "class_declaration" + "method_definition" + "function_declaration" + "lexical_declaration"))) + ;; Imenu. + (setq-local imenu-create-index-function #'js--treesit-imenu) + + ;; Which-func (use imenu). + (setq-local which-func-functions nil)) + +;;;###autoload +(define-derived-mode typescript-ts-mode typescript-ts-mode--base-mode "TypeScript" + "Major mode for editing TypeScript." + :group 'typescript + :syntax-table typescript-ts-mode--syntax-table + + (when (treesit-ready-p 'typescript) + (treesit-parser-create 'typescript) + + ;; Indent. + (setq-local treesit-simple-indent-rules + (typescript-ts-mode--indent-rules 'typescript)) + + ;; Font-lock. + (setq-local treesit-font-lock-settings + (typescript-ts-mode--font-lock-settings 'typescript)) + (setq-local treesit-font-lock-feature-list + '((comment declaration) + (keyword string) + (constant expression identifier number pattern property) + (bracket delimiter))) + + (treesit-major-mode-setup))) + +;;;###autoload +(define-derived-mode tsx-ts-mode typescript-ts-mode--base-mode "TypeScript[TSX]" "Major mode for editing TypeScript." :group 'typescript :syntax-table typescript-ts-mode--syntax-table @@ -301,43 +362,27 @@ typescript-ts-mode ;; Comments. (setq-local comment-start "// ") (setq-local comment-end "") - (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*"))) - (* (syntax whitespace)))) + (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") (setq-local comment-end-skip (rx (* (syntax whitespace)) (group (or (syntax comment-end) (seq (+ "*") "/"))))) - ;; Electric - (setq-local electric-indent-chars - (append "{}():;," electric-indent-chars)) - ;; Indent. - (setq-local treesit-simple-indent-rules typescript-ts-mode--indent-rules) - - ;; Navigation. - (setq-local treesit-defun-type-regexp - (rx (or "class_declaration" - "method_definition" - "function_declaration" - "lexical_declaration"))) + (setq-local treesit-simple-indent-rules + (typescript-ts-mode--indent-rules 'tsx)) ;; Font-lock. - (setq-local treesit-font-lock-settings typescript-ts-mode--font-lock-settings) + (setq-local treesit-font-lock-settings + (typescript-ts-mode--font-lock-settings 'tsx)) (setq-local treesit-font-lock-feature-list - '(( comment declaration) - ( keyword string) - ( constant expression identifier jsx number pattern property) - ( bracket delimiter))) - ;; Imenu. - (setq-local imenu-create-index-function #'js--treesit-imenu) - - ;; Which-func (use imenu). - (setq-local which-func-functions nil) + '((comment declaration) + (keyword string) + (constant expression identifier jsx number pattern property) + (bracket delimiter))) (treesit-major-mode-setup))) - (provide 'typescript-ts-mode) ;;; typescript-ts-mode.el ends here -- 2.34.1 --=-=-=--